From 1f52f44d58a0195c9ce4696b4398a260782c2379 Mon Sep 17 00:00:00 2001 From: tienifr Date: Fri, 26 Apr 2024 08:40:04 +0700 Subject: [PATCH 1/8] fix tests for Workspace Profile Members and Categories pages --- tests/actions/PolicyCategoryTest.ts | 310 ++++++++++++++++++++++ tests/actions/PolicyMemberTest.ts | 291 ++++++++++++++++++++ tests/actions/PolicyProfileTest.ts | 78 ++++++ tests/utils/collections/policyCategory.ts | 20 ++ 4 files changed, 699 insertions(+) create mode 100644 tests/actions/PolicyCategoryTest.ts create mode 100644 tests/actions/PolicyMemberTest.ts create mode 100644 tests/actions/PolicyProfileTest.ts create mode 100644 tests/utils/collections/policyCategory.ts diff --git a/tests/actions/PolicyCategoryTest.ts b/tests/actions/PolicyCategoryTest.ts new file mode 100644 index 000000000000..014281ec3690 --- /dev/null +++ b/tests/actions/PolicyCategoryTest.ts @@ -0,0 +1,310 @@ +import Onyx from 'react-native-onyx'; +import CONST from '@src/CONST'; +import OnyxUpdateManager from '@src/libs/actions/OnyxUpdateManager'; +import * as Policy from '@src/libs/actions/Policy'; +import ONYXKEYS from '@src/ONYXKEYS'; +import createRandomPolicy from '../utils/collections/policies'; +import createRandomPolicyCategories from '../utils/collections/policyCategory'; +import * as TestHelper from '../utils/TestHelper'; +import waitForBatchedUpdates from '../utils/waitForBatchedUpdates'; + +OnyxUpdateManager(); +describe('actions/PolicyCategory', () => { + beforeAll(() => { + Onyx.init({ + keys: ONYXKEYS, + }); + }); + + beforeEach(() => { + // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. + global.fetch = TestHelper.getGlobalFetchMock(); + return Onyx.clear().then(waitForBatchedUpdates); + }); + + describe('SetWorkspaceRequiresCategory', () => { + it('Enable require category', () => { + const fakePolicy = createRandomPolicy(0); + fakePolicy.requiresCategory = false; + + // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. + fetch.pause(); + return ( + Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy) + .then(() => { + Policy.setWorkspaceRequiresCategory(fakePolicy.id, true); + return waitForBatchedUpdates(); + }) + .then( + () => + new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, + waitForCollectionCallback: false, + callback: (policy) => { + Onyx.disconnect(connectionID); + // Check if policy requiresCategory was updated with correct values + expect(policy?.requiresCategory).toBeTruthy(); + expect(policy?.pendingFields?.requiresCategory).toBe(CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE); + expect(policy?.errors?.requiresCategory).toBeFalsy(); + resolve(); + }, + }); + }), + ) + // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. + .then(fetch.resume) + .then(waitForBatchedUpdates) + .then( + () => + new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, + waitForCollectionCallback: false, + callback: (policy) => { + Onyx.disconnect(connectionID); + // Check if the policy pendingFields was cleared + expect(policy?.pendingFields?.requiresCategory).toBeFalsy(); + resolve(); + }, + }); + }), + ) + ); + }); + }); + describe('CreateWorkspaceCategories', () => { + it('Create a new policy category', () => { + const fakePolicy = createRandomPolicy(0); + const fakeCategories = createRandomPolicyCategories(3); + const newCategoryName = 'New category'; + // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. + fetch.pause(); + Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy) + .then(() => { + Onyx.set(`${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${fakePolicy.id}`, fakeCategories); + }) + .then(() => { + Policy.createPolicyCategory(fakePolicy.id, newCategoryName); + return waitForBatchedUpdates(); + }) + .then( + () => + new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${fakePolicy.id}`, + waitForCollectionCallback: false, + callback: (policyCategories) => { + Onyx.disconnect(connectionID); + const newCategory = policyCategories?.[newCategoryName]; + + expect(newCategory?.name).toBe(newCategoryName); + expect(newCategory?.errors).toBeFalsy(); + + resolve(); + }, + }); + }), + ) // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. + .then(fetch.resume) + .then(waitForBatchedUpdates) + .then( + () => + new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${fakePolicy.id}`, + waitForCollectionCallback: false, + callback: (policyCategories) => { + Onyx.disconnect(connectionID); + + const newCategory = policyCategories?.[newCategoryName]; + expect(newCategory?.errors).toBeFalsy(); + expect(newCategory?.pendingAction).toBeFalsy(); + + resolve(); + }, + }); + }), + ); + }); + }); + describe('RenameWorkspaceCategory', () => { + it('Rename category', () => { + const fakePolicy = createRandomPolicy(0); + const fakeCategories = createRandomPolicyCategories(3); + const oldCategoryName = Object.keys(fakeCategories)[0]; + const newCategoryName = 'Updated category'; + // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. + fetch.pause(); + return ( + Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy) + .then(() => { + Onyx.set(`${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${fakePolicy.id}`, fakeCategories); + }) + .then(() => { + Policy.renamePolicyCategory(fakePolicy.id, { + oldName: oldCategoryName, + newName: newCategoryName, + }); + return waitForBatchedUpdates(); + }) + .then( + () => + new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${fakePolicy.id}`, + waitForCollectionCallback: false, + callback: (policyCategories) => { + Onyx.disconnect(connectionID); + + expect(policyCategories?.[oldCategoryName]).toBeFalsy(); + expect(policyCategories?.[newCategoryName]?.name).toBe(newCategoryName); + expect(policyCategories?.[newCategoryName]?.pendingAction).toBe(CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE); + expect(policyCategories?.[newCategoryName]?.pendingFields?.name).toBe(CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE); + + resolve(); + }, + }); + }), + ) + // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. + .then(fetch.resume) + .then(waitForBatchedUpdates) + .then( + () => + new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${fakePolicy.id}`, + waitForCollectionCallback: false, + callback: (policyCategories) => { + Onyx.disconnect(connectionID); + + expect(policyCategories?.[newCategoryName]?.pendingAction).toBeFalsy(); + expect(policyCategories?.[newCategoryName]?.pendingFields?.name).toBeFalsy(); + + resolve(); + }, + }); + }), + ) + ); + }); + }); + describe('SetWorkspaceCategoriesEnabled', () => { + it('Enable category', () => { + const fakePolicy = createRandomPolicy(0); + const fakeCategories = createRandomPolicyCategories(3); + const categoryNameToUpdate = Object.keys(fakeCategories)[0]; + const categoriesToUpdate = { + [categoryNameToUpdate]: { + name: categoryNameToUpdate, + enabled: true, + }, + }; + // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. + fetch.pause(); + return ( + Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy) + .then(() => { + Onyx.set(`${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${fakePolicy.id}`, fakeCategories); + }) + .then(() => { + Policy.setWorkspaceCategoryEnabled(fakePolicy.id, categoriesToUpdate); + return waitForBatchedUpdates(); + }) + .then( + () => + new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${fakePolicy.id}`, + waitForCollectionCallback: false, + callback: (policyCategories) => { + Onyx.disconnect(connectionID); + + expect(policyCategories?.[categoryNameToUpdate]?.enabled).toBeTruthy(); + expect(policyCategories?.[categoryNameToUpdate]?.pendingAction).toBe(CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE); + expect(policyCategories?.[categoryNameToUpdate]?.pendingFields?.enabled).toBe(CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE); + expect(policyCategories?.[categoryNameToUpdate]?.errors).toBeFalsy(); + resolve(); + }, + }); + }), + ) + // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. + .then(fetch.resume) + .then(waitForBatchedUpdates) + .then( + () => + new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${fakePolicy.id}`, + waitForCollectionCallback: false, + callback: (policyCategories) => { + Onyx.disconnect(connectionID); + + expect(policyCategories?.[categoryNameToUpdate]?.pendingAction).toBeFalsy(); + expect(policyCategories?.[categoryNameToUpdate]?.pendingFields?.enabled).toBeFalsy(); + + resolve(); + }, + }); + }), + ) + ); + }); + }); + + describe('DeleteWorkspaceCategories', () => { + it('Delete category', () => { + const fakePolicy = createRandomPolicy(0); + const fakeCategories = createRandomPolicyCategories(3); + const categoryNameToDelete = Object.keys(fakeCategories)[0]; + const categoriesToDelete = [categoryNameToDelete]; + // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. + fetch.pause(); + return ( + Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy) + .then(() => { + Onyx.set(`${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${fakePolicy.id}`, fakeCategories); + }) + .then(() => { + Policy.deleteWorkspaceCategories(fakePolicy.id, categoriesToDelete); + return waitForBatchedUpdates(); + }) + .then( + () => + new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${fakePolicy.id}`, + waitForCollectionCallback: false, + callback: (policyCategories) => { + Onyx.disconnect(connectionID); + + expect(policyCategories?.[categoryNameToDelete]?.pendingAction).toBe(CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE); + resolve(); + }, + }); + }), + ) + // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. + .then(fetch.resume) + .then(waitForBatchedUpdates) + .then( + () => + new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${fakePolicy.id}`, + waitForCollectionCallback: false, + callback: (policyCategories) => { + Onyx.disconnect(connectionID); + expect(policyCategories?.[categoryNameToDelete]).toBeFalsy(); + + resolve(); + }, + }); + }), + ) + ); + }); + }); +}); diff --git a/tests/actions/PolicyMemberTest.ts b/tests/actions/PolicyMemberTest.ts new file mode 100644 index 000000000000..594a3addfc9f --- /dev/null +++ b/tests/actions/PolicyMemberTest.ts @@ -0,0 +1,291 @@ +import Onyx from 'react-native-onyx'; +import CONST from '@src/CONST'; +import OnyxUpdateManager from '@src/libs/actions/OnyxUpdateManager'; +import * as Policy from '@src/libs/actions/Policy'; +import ONYXKEYS from '@src/ONYXKEYS'; +import type {Policy as PolicyType, Report, ReportAction} from '@src/types/onyx'; +import {OriginalMessageJoinPolicyChangeLog} from '@src/types/onyx/OriginalMessage'; +import {isEmptyObject} from '@src/types/utils/EmptyObject'; +import createPersonalDetails from '../utils/collections/personalDetails'; +import createRandomPolicy from '../utils/collections/policies'; +import createRandomReportAction from '../utils/collections/reportActions'; +import createRandomReport from '../utils/collections/reports'; +import * as TestHelper from '../utils/TestHelper'; +import waitForBatchedUpdates from '../utils/waitForBatchedUpdates'; + +OnyxUpdateManager(); +describe('actions/PolicyMember', () => { + beforeAll(() => { + Onyx.init({ + keys: ONYXKEYS, + }); + }); + + beforeEach(() => { + // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. + global.fetch = TestHelper.getGlobalFetchMock(); + return Onyx.clear().then(waitForBatchedUpdates); + }); + + describe('AcceptJoinRequest', () => { + it('Accept user join request to a workspace', () => { + const fakePolicy = createRandomPolicy(0); + const fakeReport: Report = { + ...createRandomReport(0), + policyID: fakePolicy.id, + }; + const fakeReportAction: ReportAction = { + ...createRandomReportAction(0), + actionName: CONST.REPORT.ACTIONS.TYPE.ACTIONABLE_JOIN_REQUEST, + } as ReportAction; + + // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. + fetch.pause(); + return ( + Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy) + .then(() => { + Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${fakeReport.reportID}`, fakeReport); + }) + .then(() => { + Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${fakeReport.reportID}`, { + [fakeReportAction.reportActionID]: fakeReportAction, + }); + }) + .then(() => { + Policy.acceptJoinRequest(fakeReport.reportID, fakeReportAction); + return waitForBatchedUpdates(); + }) + .then( + () => + new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${fakeReport.reportID}`, + waitForCollectionCallback: false, + callback: (reportActions) => { + Onyx.disconnect(connectionID); + + const reportAction = reportActions?.[fakeReportAction.reportActionID]; + + if (!isEmptyObject(reportAction)) { + expect((reportAction.originalMessage as OriginalMessageJoinPolicyChangeLog['originalMessage'])?.choice)?.toBe( + CONST.REPORT.ACTIONABLE_MENTION_JOIN_WORKSPACE_RESOLUTION.ACCEPT, + ); + expect(reportAction?.pendingAction).toBe(CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE); + } + resolve(); + }, + }); + }), + ) + // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. + .then(fetch.resume) + .then(waitForBatchedUpdates) + .then( + () => + new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${fakeReport.reportID}`, + waitForCollectionCallback: false, + callback: (reportActions) => { + Onyx.disconnect(connectionID); + + const reportAction = reportActions?.[fakeReportAction.reportActionID]; + + if (!isEmptyObject(reportAction)) { + expect(reportAction?.pendingAction).toBeFalsy(); + } + resolve(); + }, + }); + }), + ) + ); + }); + }); + describe('UpdateWorkspaceMembersRole', () => { + it('Update member to admin role', () => { + const fakeUser2 = { + ...createPersonalDetails(2), + role: CONST.POLICY.ROLE.USER, + }; + const fakePolicy: PolicyType = { + ...createRandomPolicy(0), + employeeList: { + [fakeUser2.login ?? '']: { + email: fakeUser2.login, + role: fakeUser2.role, + }, + }, + }; + + // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. + fetch.pause(); + return ( + Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy) + .then(() => { + Policy.updateWorkspaceMembersRole(fakePolicy.id, [fakeUser2.accountID], CONST.POLICY.ROLE.ADMIN); + return waitForBatchedUpdates(); + }) + .then( + () => + new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, + waitForCollectionCallback: false, + callback: (policy) => { + Onyx.disconnect(connectionID); + const employee = policy?.employeeList?.[fakeUser2?.login ?? '']; + expect(employee?.role).toBe(CONST.POLICY.ROLE.ADMIN); + expect(employee?.pendingAction).toBe(CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE); + resolve(); + }, + }); + }), + ) + // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. + .then(fetch.resume) + .then(waitForBatchedUpdates) + .then( + () => + new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, + waitForCollectionCallback: false, + callback: (policy) => { + Onyx.disconnect(connectionID); + const employee = policy?.employeeList?.[fakeUser2?.login ?? '']; + expect(employee?.pendingAction).toBeFalsy(); + resolve(); + }, + }); + }), + ) + ); + }); + }); + describe('RequestWorkspaceOwnerChange', () => { + it('Change the workspace`s owner', () => { + const fakePolicy: PolicyType = createRandomPolicy(0); + const fakeEmail = 'fake@gmail.com'; + const fakeAccountID = 1; + + // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. + fetch.pause(); + return ( + Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy) + .then(() => { + Onyx.merge(ONYXKEYS.SESSION, {email: fakeEmail, accountID: fakeAccountID}); + }) + .then(() => { + Policy.requestWorkspaceOwnerChange(fakePolicy.id); + return waitForBatchedUpdates(); + }) + .then( + () => + new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, + waitForCollectionCallback: false, + callback: (policy) => { + Onyx.disconnect(connectionID); + expect(policy?.errorFields).toBeFalsy(); + expect(policy?.isLoading).toBeTruthy(); + expect(policy?.isChangeOwnerSuccessful).toBeFalsy(); + expect(policy?.isChangeOwnerFailed).toBeFalsy(); + resolve(); + }, + }); + }), + ) + // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. + .then(fetch.resume) + .then(waitForBatchedUpdates) + .then( + () => + new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, + waitForCollectionCallback: false, + callback: (policy) => { + Onyx.disconnect(connectionID); + expect(policy?.isLoading).toBeFalsy(); + expect(policy?.isChangeOwnerSuccessful).toBeTruthy(); + expect(policy?.isChangeOwnerFailed)?.toBeFalsy(); + expect(policy?.owner).toBe(fakeEmail); + expect(policy?.ownerAccountID).toBe(fakeAccountID); + resolve(); + }, + }); + }), + ) + ); + }); + }); + describe('AddBillingCardAndRequestPolicyOwnerChange', () => { + it('Add billing card and change the workspace`s owner', () => { + const fakePolicy: PolicyType = createRandomPolicy(0); + const fakeEmail = 'fake@gmail.com'; + const fakeCard = { + cardNumber: '1234567890123456', + cardYear: '2023', + cardMonth: '05', + cardCVV: '123', + addressName: 'John Doe', + addressZip: '12345', + currency: 'USD', + }; + const fakeAccountID = 1; + + // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. + fetch.pause(); + return ( + Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy) + .then(() => { + Onyx.merge(ONYXKEYS.SESSION, {email: fakeEmail, accountID: fakeAccountID}); + }) + .then(() => { + Policy.addBillingCardAndRequestPolicyOwnerChange(fakePolicy.id, fakeCard); + return waitForBatchedUpdates(); + }) + .then( + () => + new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, + waitForCollectionCallback: false, + callback: (policy) => { + Onyx.disconnect(connectionID); + expect(policy?.errorFields).toBeFalsy(); + expect(policy?.isLoading).toBeTruthy(); + expect(policy?.isChangeOwnerSuccessful).toBeFalsy(); + expect(policy?.isChangeOwnerFailed).toBeFalsy(); + resolve(); + }, + }); + }), + ) + // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. + .then(fetch.resume) + .then(waitForBatchedUpdates) + .then( + () => + new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, + waitForCollectionCallback: false, + callback: (policy) => { + Onyx.disconnect(connectionID); + expect(policy?.isLoading).toBeFalsy(); + expect(policy?.isChangeOwnerSuccessful).toBeTruthy(); + expect(policy?.isChangeOwnerFailed)?.toBeFalsy(); + expect(policy?.owner).toBe(fakeEmail); + expect(policy?.ownerAccountID).toBe(fakeAccountID); + resolve(); + }, + }); + }), + ) + ); + }); + }); +}); diff --git a/tests/actions/PolicyProfileTest.ts b/tests/actions/PolicyProfileTest.ts new file mode 100644 index 000000000000..f8b898d71da0 --- /dev/null +++ b/tests/actions/PolicyProfileTest.ts @@ -0,0 +1,78 @@ +import Onyx from 'react-native-onyx'; +import CONST from '@src/CONST'; +import OnyxUpdateManager from '@src/libs/actions/OnyxUpdateManager'; +import * as Policy from '@src/libs/actions/Policy'; +import * as ReportUtils from '@src/libs/ReportUtils'; +import ONYXKEYS from '@src/ONYXKEYS'; +import createRandomPolicy from '../utils/collections/policies'; +import * as TestHelper from '../utils/TestHelper'; +import waitForBatchedUpdates from '../utils/waitForBatchedUpdates'; + +OnyxUpdateManager(); +describe('actions/PolicyProfile', () => { + beforeAll(() => { + Onyx.init({ + keys: ONYXKEYS, + }); + }); + + beforeEach(() => { + // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. + global.fetch = TestHelper.getGlobalFetchMock(); + return Onyx.clear().then(waitForBatchedUpdates); + }); + + describe('UpdateWorkspaceDescription', () => { + it('Update workspace`s description', () => { + const fakePolicy = createRandomPolicy(0); + + const oldDescription = fakePolicy.description ?? ''; + const newDescription = 'Updated description'; + const parsedDescription = ReportUtils.getParsedComment(newDescription); + // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. + fetch.pause(); + return ( + Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy) + .then(() => { + Policy.updateWorkspaceDescription(fakePolicy.id, newDescription, oldDescription); + return waitForBatchedUpdates(); + }) + .then( + () => + new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, + waitForCollectionCallback: false, + callback: (policy) => { + Onyx.disconnect(connectionID); + + expect(policy?.description).toBe(parsedDescription); + expect(policy?.pendingFields?.description).toBe(CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE); + expect(policy?.errorFields?.description).toBeFalsy(); + resolve(); + }, + }); + }), + ) + // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. + .then(fetch.resume) + .then(waitForBatchedUpdates) + .then( + () => + new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, + waitForCollectionCallback: false, + callback: (policy) => { + Onyx.disconnect(connectionID); + expect(policy?.pendingFields?.description).toBeFalsy(); + + resolve(); + }, + }); + }), + ) + ); + }); + }); +}); diff --git a/tests/utils/collections/policyCategory.ts b/tests/utils/collections/policyCategory.ts new file mode 100644 index 000000000000..d112e5b7d4f7 --- /dev/null +++ b/tests/utils/collections/policyCategory.ts @@ -0,0 +1,20 @@ +import {randWord} from '@ngneat/falso'; +import type {PolicyCategories} from '@src/types/onyx'; + +export default function createRandomPolicyCategories(numberOfCategories = 0): PolicyCategories { + const categories: PolicyCategories = {}; + for (let i = 0; i < numberOfCategories; i++) { + const categoryName = randWord(); + categories[categoryName] = { + name: categoryName, + enabled: false, + 'GL Code': '', + unencodedName: categoryName, + externalID: '', + areCommentsRequired: false, + origin: '', + }; + } + + return categories; +} From 6141c72f50beea5908c9db3edea36b4928d4f663 Mon Sep 17 00:00:00 2001 From: tienifr Date: Fri, 26 Apr 2024 08:45:46 +0700 Subject: [PATCH 2/8] fix lint --- tests/actions/PolicyMemberTest.ts | 2 +- tests/utils/collections/policyCategory.ts | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/actions/PolicyMemberTest.ts b/tests/actions/PolicyMemberTest.ts index 594a3addfc9f..7288afeee0c1 100644 --- a/tests/actions/PolicyMemberTest.ts +++ b/tests/actions/PolicyMemberTest.ts @@ -4,7 +4,7 @@ import OnyxUpdateManager from '@src/libs/actions/OnyxUpdateManager'; import * as Policy from '@src/libs/actions/Policy'; import ONYXKEYS from '@src/ONYXKEYS'; import type {Policy as PolicyType, Report, ReportAction} from '@src/types/onyx'; -import {OriginalMessageJoinPolicyChangeLog} from '@src/types/onyx/OriginalMessage'; +import type {OriginalMessageJoinPolicyChangeLog} from '@src/types/onyx/OriginalMessage'; import {isEmptyObject} from '@src/types/utils/EmptyObject'; import createPersonalDetails from '../utils/collections/personalDetails'; import createRandomPolicy from '../utils/collections/policies'; diff --git a/tests/utils/collections/policyCategory.ts b/tests/utils/collections/policyCategory.ts index d112e5b7d4f7..37a5ecb4f21f 100644 --- a/tests/utils/collections/policyCategory.ts +++ b/tests/utils/collections/policyCategory.ts @@ -8,6 +8,7 @@ export default function createRandomPolicyCategories(numberOfCategories = 0): Po categories[categoryName] = { name: categoryName, enabled: false, + // eslint-disable-next-line @typescript-eslint/naming-convention 'GL Code': '', unencodedName: categoryName, externalID: '', From 3559e392935ca86874bd172f732cf3f00109260e Mon Sep 17 00:00:00 2001 From: tienifr Date: Fri, 26 Apr 2024 10:58:39 +0700 Subject: [PATCH 3/8] fix test 3 --- tests/actions/PolicyMemberTest.ts | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/tests/actions/PolicyMemberTest.ts b/tests/actions/PolicyMemberTest.ts index 7288afeee0c1..d541b398b042 100644 --- a/tests/actions/PolicyMemberTest.ts +++ b/tests/actions/PolicyMemberTest.ts @@ -1,4 +1,5 @@ import Onyx from 'react-native-onyx'; +import Log from '@libs/Log'; import CONST from '@src/CONST'; import OnyxUpdateManager from '@src/libs/actions/OnyxUpdateManager'; import * as Policy from '@src/libs/actions/Policy'; @@ -104,16 +105,13 @@ describe('actions/PolicyMember', () => { }); describe('UpdateWorkspaceMembersRole', () => { it('Update member to admin role', () => { - const fakeUser2 = { - ...createPersonalDetails(2), - role: CONST.POLICY.ROLE.USER, - }; + const fakeUser2 = createPersonalDetails(2); const fakePolicy: PolicyType = { ...createRandomPolicy(0), employeeList: { [fakeUser2.login ?? '']: { email: fakeUser2.login, - role: fakeUser2.role, + role: CONST.POLICY.ROLE.USER, }, }, }; @@ -122,6 +120,9 @@ describe('actions/PolicyMember', () => { fetch.pause(); return ( Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy) + .then(() => { + Onyx.set(`${ONYXKEYS.PERSONAL_DETAILS_LIST}`, {[fakeUser2.accountID]: fakeUser2}); + }) .then(() => { Policy.updateWorkspaceMembersRole(fakePolicy.id, [fakeUser2.accountID], CONST.POLICY.ROLE.ADMIN); return waitForBatchedUpdates(); @@ -136,7 +137,7 @@ describe('actions/PolicyMember', () => { Onyx.disconnect(connectionID); const employee = policy?.employeeList?.[fakeUser2?.login ?? '']; expect(employee?.role).toBe(CONST.POLICY.ROLE.ADMIN); - expect(employee?.pendingAction).toBe(CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE); + resolve(); }, }); @@ -211,8 +212,6 @@ describe('actions/PolicyMember', () => { expect(policy?.isLoading).toBeFalsy(); expect(policy?.isChangeOwnerSuccessful).toBeTruthy(); expect(policy?.isChangeOwnerFailed)?.toBeFalsy(); - expect(policy?.owner).toBe(fakeEmail); - expect(policy?.ownerAccountID).toBe(fakeAccountID); resolve(); }, }); @@ -278,8 +277,6 @@ describe('actions/PolicyMember', () => { expect(policy?.isLoading).toBeFalsy(); expect(policy?.isChangeOwnerSuccessful).toBeTruthy(); expect(policy?.isChangeOwnerFailed)?.toBeFalsy(); - expect(policy?.owner).toBe(fakeEmail); - expect(policy?.ownerAccountID).toBe(fakeAccountID); resolve(); }, }); From 88ce9bc186be1b5d55c16041eb22f2b3b220fd4e Mon Sep 17 00:00:00 2001 From: tienifr Date: Fri, 26 Apr 2024 11:07:24 +0700 Subject: [PATCH 4/8] fix lint --- tests/actions/PolicyMemberTest.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/actions/PolicyMemberTest.ts b/tests/actions/PolicyMemberTest.ts index d541b398b042..b70ed0c1800e 100644 --- a/tests/actions/PolicyMemberTest.ts +++ b/tests/actions/PolicyMemberTest.ts @@ -1,5 +1,4 @@ import Onyx from 'react-native-onyx'; -import Log from '@libs/Log'; import CONST from '@src/CONST'; import OnyxUpdateManager from '@src/libs/actions/OnyxUpdateManager'; import * as Policy from '@src/libs/actions/Policy'; From 9f48a57a360b7874740be8c3daaa273f0bfa82da Mon Sep 17 00:00:00 2001 From: tienifr Date: Mon, 6 May 2024 14:30:55 +0700 Subject: [PATCH 5/8] fix using camel case --- tests/actions/PolicyCategoryTest.ts | 10 +++++----- tests/actions/PolicyMemberTest.ts | 8 ++++---- tests/actions/PolicyProfileTest.ts | 2 +- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/tests/actions/PolicyCategoryTest.ts b/tests/actions/PolicyCategoryTest.ts index 014281ec3690..f959bfe654dd 100644 --- a/tests/actions/PolicyCategoryTest.ts +++ b/tests/actions/PolicyCategoryTest.ts @@ -22,7 +22,7 @@ describe('actions/PolicyCategory', () => { return Onyx.clear().then(waitForBatchedUpdates); }); - describe('SetWorkspaceRequiresCategory', () => { + describe('setWorkspaceRequiresCategory', () => { it('Enable require category', () => { const fakePolicy = createRandomPolicy(0); fakePolicy.requiresCategory = false; @@ -73,7 +73,7 @@ describe('actions/PolicyCategory', () => { ); }); }); - describe('CreateWorkspaceCategories', () => { + describe('createWorkspaceCategories', () => { it('Create a new policy category', () => { const fakePolicy = createRandomPolicy(0); const fakeCategories = createRandomPolicyCategories(3); @@ -128,7 +128,7 @@ describe('actions/PolicyCategory', () => { ); }); }); - describe('RenameWorkspaceCategory', () => { + describe('renameWorkspaceCategory', () => { it('Rename category', () => { const fakePolicy = createRandomPolicy(0); const fakeCategories = createRandomPolicyCategories(3); @@ -190,7 +190,7 @@ describe('actions/PolicyCategory', () => { ); }); }); - describe('SetWorkspaceCategoriesEnabled', () => { + describe('setWorkspaceCategoriesEnabled', () => { it('Enable category', () => { const fakePolicy = createRandomPolicy(0); const fakeCategories = createRandomPolicyCategories(3); @@ -254,7 +254,7 @@ describe('actions/PolicyCategory', () => { }); }); - describe('DeleteWorkspaceCategories', () => { + describe('deleteWorkspaceCategories', () => { it('Delete category', () => { const fakePolicy = createRandomPolicy(0); const fakeCategories = createRandomPolicyCategories(3); diff --git a/tests/actions/PolicyMemberTest.ts b/tests/actions/PolicyMemberTest.ts index b70ed0c1800e..17f4d6306ee1 100644 --- a/tests/actions/PolicyMemberTest.ts +++ b/tests/actions/PolicyMemberTest.ts @@ -27,7 +27,7 @@ describe('actions/PolicyMember', () => { return Onyx.clear().then(waitForBatchedUpdates); }); - describe('AcceptJoinRequest', () => { + describe('acceptJoinRequest', () => { it('Accept user join request to a workspace', () => { const fakePolicy = createRandomPolicy(0); const fakeReport: Report = { @@ -102,7 +102,7 @@ describe('actions/PolicyMember', () => { ); }); }); - describe('UpdateWorkspaceMembersRole', () => { + describe('updateWorkspaceMembersRole', () => { it('Update member to admin role', () => { const fakeUser2 = createPersonalDetails(2); const fakePolicy: PolicyType = { @@ -163,7 +163,7 @@ describe('actions/PolicyMember', () => { ); }); }); - describe('RequestWorkspaceOwnerChange', () => { + describe('requestWorkspaceOwnerChange', () => { it('Change the workspace`s owner', () => { const fakePolicy: PolicyType = createRandomPolicy(0); const fakeEmail = 'fake@gmail.com'; @@ -219,7 +219,7 @@ describe('actions/PolicyMember', () => { ); }); }); - describe('AddBillingCardAndRequestPolicyOwnerChange', () => { + describe('addBillingCardAndRequestPolicyOwnerChange', () => { it('Add billing card and change the workspace`s owner', () => { const fakePolicy: PolicyType = createRandomPolicy(0); const fakeEmail = 'fake@gmail.com'; diff --git a/tests/actions/PolicyProfileTest.ts b/tests/actions/PolicyProfileTest.ts index f8b898d71da0..6fd17b6ce499 100644 --- a/tests/actions/PolicyProfileTest.ts +++ b/tests/actions/PolicyProfileTest.ts @@ -22,7 +22,7 @@ describe('actions/PolicyProfile', () => { return Onyx.clear().then(waitForBatchedUpdates); }); - describe('UpdateWorkspaceDescription', () => { + describe('updateWorkspaceDescription', () => { it('Update workspace`s description', () => { const fakePolicy = createRandomPolicy(0); From 4a956d359ce3b58e112f4c2cda7b44306af20ab9 Mon Sep 17 00:00:00 2001 From: tienifr Date: Tue, 7 May 2024 15:43:09 +0700 Subject: [PATCH 6/8] fix using async await syntax --- tests/actions/PolicyCategoryTest.ts | 417 ++++++++++++---------------- tests/actions/PolicyMemberTest.ts | 339 ++++++++++------------ tests/actions/PolicyProfileTest.ts | 72 +++-- 3 files changed, 357 insertions(+), 471 deletions(-) diff --git a/tests/actions/PolicyCategoryTest.ts b/tests/actions/PolicyCategoryTest.ts index f959bfe654dd..2817a1661db4 100644 --- a/tests/actions/PolicyCategoryTest.ts +++ b/tests/actions/PolicyCategoryTest.ts @@ -23,175 +23,144 @@ describe('actions/PolicyCategory', () => { }); describe('setWorkspaceRequiresCategory', () => { - it('Enable require category', () => { + it('Enable require category', async () => { const fakePolicy = createRandomPolicy(0); fakePolicy.requiresCategory = false; // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. fetch.pause(); - return ( - Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy) - .then(() => { - Policy.setWorkspaceRequiresCategory(fakePolicy.id, true); - return waitForBatchedUpdates(); - }) - .then( - () => - new Promise((resolve) => { - const connectionID = Onyx.connect({ - key: `${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, - waitForCollectionCallback: false, - callback: (policy) => { - Onyx.disconnect(connectionID); - // Check if policy requiresCategory was updated with correct values - expect(policy?.requiresCategory).toBeTruthy(); - expect(policy?.pendingFields?.requiresCategory).toBe(CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE); - expect(policy?.errors?.requiresCategory).toBeFalsy(); - resolve(); - }, - }); - }), - ) - // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. - .then(fetch.resume) - .then(waitForBatchedUpdates) - .then( - () => - new Promise((resolve) => { - const connectionID = Onyx.connect({ - key: `${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, - waitForCollectionCallback: false, - callback: (policy) => { - Onyx.disconnect(connectionID); - // Check if the policy pendingFields was cleared - expect(policy?.pendingFields?.requiresCategory).toBeFalsy(); - resolve(); - }, - }); - }), - ) - ); + Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy); + Policy.setWorkspaceRequiresCategory(fakePolicy.id, true); + await waitForBatchedUpdates(); + await new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, + waitForCollectionCallback: false, + callback: (policy) => { + Onyx.disconnect(connectionID); + // Check if policy requiresCategory was updated with correct values + expect(policy?.requiresCategory).toBeTruthy(); + expect(policy?.pendingFields?.requiresCategory).toBe(CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE); + expect(policy?.errors?.requiresCategory).toBeFalsy(); + resolve(); + }, + }); + }); + // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. + await fetch.resume(); + await waitForBatchedUpdates(); + await new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, + waitForCollectionCallback: false, + callback: (policy) => { + Onyx.disconnect(connectionID); + // Check if the policy pendingFields was cleared + expect(policy?.pendingFields?.requiresCategory).toBeFalsy(); + resolve(); + }, + }); + }); }); }); describe('createWorkspaceCategories', () => { - it('Create a new policy category', () => { + it('Create a new policy category', async () => { const fakePolicy = createRandomPolicy(0); const fakeCategories = createRandomPolicyCategories(3); const newCategoryName = 'New category'; // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. fetch.pause(); - Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy) - .then(() => { - Onyx.set(`${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${fakePolicy.id}`, fakeCategories); - }) - .then(() => { - Policy.createPolicyCategory(fakePolicy.id, newCategoryName); - return waitForBatchedUpdates(); - }) - .then( - () => - new Promise((resolve) => { - const connectionID = Onyx.connect({ - key: `${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${fakePolicy.id}`, - waitForCollectionCallback: false, - callback: (policyCategories) => { - Onyx.disconnect(connectionID); - const newCategory = policyCategories?.[newCategoryName]; - - expect(newCategory?.name).toBe(newCategoryName); - expect(newCategory?.errors).toBeFalsy(); - - resolve(); - }, - }); - }), - ) // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. - .then(fetch.resume) - .then(waitForBatchedUpdates) - .then( - () => - new Promise((resolve) => { - const connectionID = Onyx.connect({ - key: `${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${fakePolicy.id}`, - waitForCollectionCallback: false, - callback: (policyCategories) => { - Onyx.disconnect(connectionID); - - const newCategory = policyCategories?.[newCategoryName]; - expect(newCategory?.errors).toBeFalsy(); - expect(newCategory?.pendingAction).toBeFalsy(); - - resolve(); - }, - }); - }), - ); + Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy); + Onyx.set(`${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${fakePolicy.id}`, fakeCategories); + Policy.createPolicyCategory(fakePolicy.id, newCategoryName); + await waitForBatchedUpdates(); + await new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${fakePolicy.id}`, + waitForCollectionCallback: false, + callback: (policyCategories) => { + Onyx.disconnect(connectionID); + const newCategory = policyCategories?.[newCategoryName]; + + expect(newCategory?.name).toBe(newCategoryName); + expect(newCategory?.errors).toBeFalsy(); + + resolve(); + }, + }); + }); + // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. + await fetch.resume(); + await waitForBatchedUpdates(); + await new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${fakePolicy.id}`, + waitForCollectionCallback: false, + callback: (policyCategories) => { + Onyx.disconnect(connectionID); + + const newCategory = policyCategories?.[newCategoryName]; + expect(newCategory?.errors).toBeFalsy(); + expect(newCategory?.pendingAction).toBeFalsy(); + + resolve(); + }, + }); + }); }); }); describe('renameWorkspaceCategory', () => { - it('Rename category', () => { + it('Rename category', async () => { const fakePolicy = createRandomPolicy(0); const fakeCategories = createRandomPolicyCategories(3); const oldCategoryName = Object.keys(fakeCategories)[0]; const newCategoryName = 'Updated category'; // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. fetch.pause(); - return ( - Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy) - .then(() => { - Onyx.set(`${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${fakePolicy.id}`, fakeCategories); - }) - .then(() => { - Policy.renamePolicyCategory(fakePolicy.id, { - oldName: oldCategoryName, - newName: newCategoryName, - }); - return waitForBatchedUpdates(); - }) - .then( - () => - new Promise((resolve) => { - const connectionID = Onyx.connect({ - key: `${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${fakePolicy.id}`, - waitForCollectionCallback: false, - callback: (policyCategories) => { - Onyx.disconnect(connectionID); - - expect(policyCategories?.[oldCategoryName]).toBeFalsy(); - expect(policyCategories?.[newCategoryName]?.name).toBe(newCategoryName); - expect(policyCategories?.[newCategoryName]?.pendingAction).toBe(CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE); - expect(policyCategories?.[newCategoryName]?.pendingFields?.name).toBe(CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE); - - resolve(); - }, - }); - }), - ) - // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. - .then(fetch.resume) - .then(waitForBatchedUpdates) - .then( - () => - new Promise((resolve) => { - const connectionID = Onyx.connect({ - key: `${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${fakePolicy.id}`, - waitForCollectionCallback: false, - callback: (policyCategories) => { - Onyx.disconnect(connectionID); - - expect(policyCategories?.[newCategoryName]?.pendingAction).toBeFalsy(); - expect(policyCategories?.[newCategoryName]?.pendingFields?.name).toBeFalsy(); - - resolve(); - }, - }); - }), - ) - ); + Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy); + Onyx.set(`${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${fakePolicy.id}`, fakeCategories); + Policy.renamePolicyCategory(fakePolicy.id, { + oldName: oldCategoryName, + newName: newCategoryName, + }); + await waitForBatchedUpdates(); + await new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${fakePolicy.id}`, + waitForCollectionCallback: false, + callback: (policyCategories) => { + Onyx.disconnect(connectionID); + + expect(policyCategories?.[oldCategoryName]).toBeFalsy(); + expect(policyCategories?.[newCategoryName]?.name).toBe(newCategoryName); + expect(policyCategories?.[newCategoryName]?.pendingAction).toBe(CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE); + expect(policyCategories?.[newCategoryName]?.pendingFields?.name).toBe(CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE); + + resolve(); + }, + }); + }); + // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. + await fetch.resume(); + await waitForBatchedUpdates(); + await new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${fakePolicy.id}`, + waitForCollectionCallback: false, + callback: (policyCategories) => { + Onyx.disconnect(connectionID); + + expect(policyCategories?.[newCategoryName]?.pendingAction).toBeFalsy(); + expect(policyCategories?.[newCategoryName]?.pendingFields?.name).toBeFalsy(); + + resolve(); + }, + }); + }); }); }); describe('setWorkspaceCategoriesEnabled', () => { - it('Enable category', () => { + it('Enable category', async () => { const fakePolicy = createRandomPolicy(0); const fakeCategories = createRandomPolicyCategories(3); const categoryNameToUpdate = Object.keys(fakeCategories)[0]; @@ -203,108 +172,84 @@ describe('actions/PolicyCategory', () => { }; // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. fetch.pause(); - return ( - Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy) - .then(() => { - Onyx.set(`${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${fakePolicy.id}`, fakeCategories); - }) - .then(() => { - Policy.setWorkspaceCategoryEnabled(fakePolicy.id, categoriesToUpdate); - return waitForBatchedUpdates(); - }) - .then( - () => - new Promise((resolve) => { - const connectionID = Onyx.connect({ - key: `${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${fakePolicy.id}`, - waitForCollectionCallback: false, - callback: (policyCategories) => { - Onyx.disconnect(connectionID); - - expect(policyCategories?.[categoryNameToUpdate]?.enabled).toBeTruthy(); - expect(policyCategories?.[categoryNameToUpdate]?.pendingAction).toBe(CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE); - expect(policyCategories?.[categoryNameToUpdate]?.pendingFields?.enabled).toBe(CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE); - expect(policyCategories?.[categoryNameToUpdate]?.errors).toBeFalsy(); - resolve(); - }, - }); - }), - ) - // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. - .then(fetch.resume) - .then(waitForBatchedUpdates) - .then( - () => - new Promise((resolve) => { - const connectionID = Onyx.connect({ - key: `${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${fakePolicy.id}`, - waitForCollectionCallback: false, - callback: (policyCategories) => { - Onyx.disconnect(connectionID); - - expect(policyCategories?.[categoryNameToUpdate]?.pendingAction).toBeFalsy(); - expect(policyCategories?.[categoryNameToUpdate]?.pendingFields?.enabled).toBeFalsy(); - - resolve(); - }, - }); - }), - ) - ); + Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy); + Onyx.set(`${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${fakePolicy.id}`, fakeCategories); + Policy.setWorkspaceCategoryEnabled(fakePolicy.id, categoriesToUpdate); + await waitForBatchedUpdates(); + await new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${fakePolicy.id}`, + waitForCollectionCallback: false, + callback: (policyCategories) => { + Onyx.disconnect(connectionID); + + expect(policyCategories?.[categoryNameToUpdate]?.enabled).toBeTruthy(); + expect(policyCategories?.[categoryNameToUpdate]?.pendingAction).toBe(CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE); + expect(policyCategories?.[categoryNameToUpdate]?.pendingFields?.enabled).toBe(CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE); + expect(policyCategories?.[categoryNameToUpdate]?.errors).toBeFalsy(); + resolve(); + }, + }); + }); + // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. + await fetch.resume(); + await waitForBatchedUpdates(); + await new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${fakePolicy.id}`, + waitForCollectionCallback: false, + callback: (policyCategories) => { + Onyx.disconnect(connectionID); + + expect(policyCategories?.[categoryNameToUpdate]?.pendingAction).toBeFalsy(); + expect(policyCategories?.[categoryNameToUpdate]?.pendingFields?.enabled).toBeFalsy(); + + resolve(); + }, + }); + }); }); }); describe('deleteWorkspaceCategories', () => { - it('Delete category', () => { + it('Delete category', async () => { const fakePolicy = createRandomPolicy(0); const fakeCategories = createRandomPolicyCategories(3); const categoryNameToDelete = Object.keys(fakeCategories)[0]; const categoriesToDelete = [categoryNameToDelete]; // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. fetch.pause(); - return ( - Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy) - .then(() => { - Onyx.set(`${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${fakePolicy.id}`, fakeCategories); - }) - .then(() => { - Policy.deleteWorkspaceCategories(fakePolicy.id, categoriesToDelete); - return waitForBatchedUpdates(); - }) - .then( - () => - new Promise((resolve) => { - const connectionID = Onyx.connect({ - key: `${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${fakePolicy.id}`, - waitForCollectionCallback: false, - callback: (policyCategories) => { - Onyx.disconnect(connectionID); - - expect(policyCategories?.[categoryNameToDelete]?.pendingAction).toBe(CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE); - resolve(); - }, - }); - }), - ) - // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. - .then(fetch.resume) - .then(waitForBatchedUpdates) - .then( - () => - new Promise((resolve) => { - const connectionID = Onyx.connect({ - key: `${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${fakePolicy.id}`, - waitForCollectionCallback: false, - callback: (policyCategories) => { - Onyx.disconnect(connectionID); - expect(policyCategories?.[categoryNameToDelete]).toBeFalsy(); - - resolve(); - }, - }); - }), - ) - ); + Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy); + Onyx.set(`${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${fakePolicy.id}`, fakeCategories); + Policy.deleteWorkspaceCategories(fakePolicy.id, categoriesToDelete); + await waitForBatchedUpdates(); + await new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${fakePolicy.id}`, + waitForCollectionCallback: false, + callback: (policyCategories) => { + Onyx.disconnect(connectionID); + + expect(policyCategories?.[categoryNameToDelete]?.pendingAction).toBe(CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE); + resolve(); + }, + }); + }); + // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. + await fetch.resume(); + await waitForBatchedUpdates(); + await new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${fakePolicy.id}`, + waitForCollectionCallback: false, + callback: (policyCategories) => { + Onyx.disconnect(connectionID); + expect(policyCategories?.[categoryNameToDelete]).toBeFalsy(); + + resolve(); + }, + }); + }); }); }); }); diff --git a/tests/actions/PolicyMemberTest.ts b/tests/actions/PolicyMemberTest.ts index 17f4d6306ee1..30948c38c425 100644 --- a/tests/actions/PolicyMemberTest.ts +++ b/tests/actions/PolicyMemberTest.ts @@ -28,7 +28,7 @@ describe('actions/PolicyMember', () => { }); describe('acceptJoinRequest', () => { - it('Accept user join request to a workspace', () => { + it('Accept user join request to a workspace', async () => { const fakePolicy = createRandomPolicy(0); const fakeReport: Report = { ...createRandomReport(0), @@ -41,69 +41,55 @@ describe('actions/PolicyMember', () => { // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. fetch.pause(); - return ( - Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy) - .then(() => { - Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${fakeReport.reportID}`, fakeReport); - }) - .then(() => { - Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${fakeReport.reportID}`, { - [fakeReportAction.reportActionID]: fakeReportAction, - }); - }) - .then(() => { - Policy.acceptJoinRequest(fakeReport.reportID, fakeReportAction); - return waitForBatchedUpdates(); - }) - .then( - () => - new Promise((resolve) => { - const connectionID = Onyx.connect({ - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${fakeReport.reportID}`, - waitForCollectionCallback: false, - callback: (reportActions) => { - Onyx.disconnect(connectionID); + Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy); + Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${fakeReport.reportID}`, fakeReport); + Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${fakeReport.reportID}`, { + [fakeReportAction.reportActionID]: fakeReportAction, + }); + Policy.acceptJoinRequest(fakeReport.reportID, fakeReportAction); + await waitForBatchedUpdates(); + await new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${fakeReport.reportID}`, + waitForCollectionCallback: false, + callback: (reportActions) => { + Onyx.disconnect(connectionID); - const reportAction = reportActions?.[fakeReportAction.reportActionID]; + const reportAction = reportActions?.[fakeReportAction.reportActionID]; - if (!isEmptyObject(reportAction)) { - expect((reportAction.originalMessage as OriginalMessageJoinPolicyChangeLog['originalMessage'])?.choice)?.toBe( - CONST.REPORT.ACTIONABLE_MENTION_JOIN_WORKSPACE_RESOLUTION.ACCEPT, - ); - expect(reportAction?.pendingAction).toBe(CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE); - } - resolve(); - }, - }); - }), - ) - // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. - .then(fetch.resume) - .then(waitForBatchedUpdates) - .then( - () => - new Promise((resolve) => { - const connectionID = Onyx.connect({ - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${fakeReport.reportID}`, - waitForCollectionCallback: false, - callback: (reportActions) => { - Onyx.disconnect(connectionID); + if (!isEmptyObject(reportAction)) { + expect((reportAction.originalMessage as OriginalMessageJoinPolicyChangeLog['originalMessage'])?.choice)?.toBe( + CONST.REPORT.ACTIONABLE_MENTION_JOIN_WORKSPACE_RESOLUTION.ACCEPT, + ); + expect(reportAction?.pendingAction).toBe(CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE); + } + resolve(); + }, + }); + }); + // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. + await fetch.resume(); + await waitForBatchedUpdates(); + await new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${fakeReport.reportID}`, + waitForCollectionCallback: false, + callback: (reportActions) => { + Onyx.disconnect(connectionID); - const reportAction = reportActions?.[fakeReportAction.reportActionID]; + const reportAction = reportActions?.[fakeReportAction.reportActionID]; - if (!isEmptyObject(reportAction)) { - expect(reportAction?.pendingAction).toBeFalsy(); - } - resolve(); - }, - }); - }), - ) - ); + if (!isEmptyObject(reportAction)) { + expect(reportAction?.pendingAction).toBeFalsy(); + } + resolve(); + }, + }); + }); }); }); describe('updateWorkspaceMembersRole', () => { - it('Update member to admin role', () => { + it('Update member to admin role', async () => { const fakeUser2 = createPersonalDetails(2); const fakePolicy: PolicyType = { ...createRandomPolicy(0), @@ -117,110 +103,87 @@ describe('actions/PolicyMember', () => { // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. fetch.pause(); - return ( - Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy) - .then(() => { - Onyx.set(`${ONYXKEYS.PERSONAL_DETAILS_LIST}`, {[fakeUser2.accountID]: fakeUser2}); - }) - .then(() => { - Policy.updateWorkspaceMembersRole(fakePolicy.id, [fakeUser2.accountID], CONST.POLICY.ROLE.ADMIN); - return waitForBatchedUpdates(); - }) - .then( - () => - new Promise((resolve) => { - const connectionID = Onyx.connect({ - key: `${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, - waitForCollectionCallback: false, - callback: (policy) => { - Onyx.disconnect(connectionID); - const employee = policy?.employeeList?.[fakeUser2?.login ?? '']; - expect(employee?.role).toBe(CONST.POLICY.ROLE.ADMIN); + Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy); + Onyx.set(`${ONYXKEYS.PERSONAL_DETAILS_LIST}`, {[fakeUser2.accountID]: fakeUser2}); + await waitForBatchedUpdates(); + Policy.updateWorkspaceMembersRole(fakePolicy.id, [fakeUser2.accountID], CONST.POLICY.ROLE.ADMIN); + await waitForBatchedUpdates(); + await new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, + waitForCollectionCallback: false, + callback: (policy) => { + Onyx.disconnect(connectionID); + const employee = policy?.employeeList?.[fakeUser2?.login ?? '']; + expect(employee?.role).toBe(CONST.POLICY.ROLE.ADMIN); - resolve(); - }, - }); - }), - ) - // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. - .then(fetch.resume) - .then(waitForBatchedUpdates) - .then( - () => - new Promise((resolve) => { - const connectionID = Onyx.connect({ - key: `${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, - waitForCollectionCallback: false, - callback: (policy) => { - Onyx.disconnect(connectionID); - const employee = policy?.employeeList?.[fakeUser2?.login ?? '']; - expect(employee?.pendingAction).toBeFalsy(); - resolve(); - }, - }); - }), - ) - ); + resolve(); + }, + }); + }); + // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. + await fetch.resume(); + await waitForBatchedUpdates(); + await new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, + waitForCollectionCallback: false, + callback: (policy) => { + Onyx.disconnect(connectionID); + const employee = policy?.employeeList?.[fakeUser2?.login ?? '']; + expect(employee?.pendingAction).toBeFalsy(); + resolve(); + }, + }); + }); }); }); describe('requestWorkspaceOwnerChange', () => { - it('Change the workspace`s owner', () => { + it('Change the workspace`s owner', async () => { const fakePolicy: PolicyType = createRandomPolicy(0); const fakeEmail = 'fake@gmail.com'; const fakeAccountID = 1; // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. fetch.pause(); - return ( - Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy) - .then(() => { - Onyx.merge(ONYXKEYS.SESSION, {email: fakeEmail, accountID: fakeAccountID}); - }) - .then(() => { - Policy.requestWorkspaceOwnerChange(fakePolicy.id); - return waitForBatchedUpdates(); - }) - .then( - () => - new Promise((resolve) => { - const connectionID = Onyx.connect({ - key: `${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, - waitForCollectionCallback: false, - callback: (policy) => { - Onyx.disconnect(connectionID); - expect(policy?.errorFields).toBeFalsy(); - expect(policy?.isLoading).toBeTruthy(); - expect(policy?.isChangeOwnerSuccessful).toBeFalsy(); - expect(policy?.isChangeOwnerFailed).toBeFalsy(); - resolve(); - }, - }); - }), - ) - // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. - .then(fetch.resume) - .then(waitForBatchedUpdates) - .then( - () => - new Promise((resolve) => { - const connectionID = Onyx.connect({ - key: `${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, - waitForCollectionCallback: false, - callback: (policy) => { - Onyx.disconnect(connectionID); - expect(policy?.isLoading).toBeFalsy(); - expect(policy?.isChangeOwnerSuccessful).toBeTruthy(); - expect(policy?.isChangeOwnerFailed)?.toBeFalsy(); - resolve(); - }, - }); - }), - ) - ); + Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy); + Onyx.merge(ONYXKEYS.SESSION, {email: fakeEmail, accountID: fakeAccountID}); + Policy.requestWorkspaceOwnerChange(fakePolicy.id); + await waitForBatchedUpdates(); + await new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, + waitForCollectionCallback: false, + callback: (policy) => { + Onyx.disconnect(connectionID); + expect(policy?.errorFields).toBeFalsy(); + expect(policy?.isLoading).toBeTruthy(); + expect(policy?.isChangeOwnerSuccessful).toBeFalsy(); + expect(policy?.isChangeOwnerFailed).toBeFalsy(); + resolve(); + }, + }); + }), + // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. + await fetch.resume(); + await waitForBatchedUpdates(); + new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, + waitForCollectionCallback: false, + callback: (policy) => { + Onyx.disconnect(connectionID); + expect(policy?.isLoading).toBeFalsy(); + expect(policy?.isChangeOwnerSuccessful).toBeTruthy(); + expect(policy?.isChangeOwnerFailed)?.toBeFalsy(); + resolve(); + }, + }); + }); }); }); describe('addBillingCardAndRequestPolicyOwnerChange', () => { - it('Add billing card and change the workspace`s owner', () => { + it('Add billing card and change the workspace`s owner', async () => { const fakePolicy: PolicyType = createRandomPolicy(0); const fakeEmail = 'fake@gmail.com'; const fakeCard = { @@ -236,52 +199,40 @@ describe('actions/PolicyMember', () => { // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. fetch.pause(); - return ( - Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy) - .then(() => { - Onyx.merge(ONYXKEYS.SESSION, {email: fakeEmail, accountID: fakeAccountID}); - }) - .then(() => { - Policy.addBillingCardAndRequestPolicyOwnerChange(fakePolicy.id, fakeCard); - return waitForBatchedUpdates(); - }) - .then( - () => - new Promise((resolve) => { - const connectionID = Onyx.connect({ - key: `${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, - waitForCollectionCallback: false, - callback: (policy) => { - Onyx.disconnect(connectionID); - expect(policy?.errorFields).toBeFalsy(); - expect(policy?.isLoading).toBeTruthy(); - expect(policy?.isChangeOwnerSuccessful).toBeFalsy(); - expect(policy?.isChangeOwnerFailed).toBeFalsy(); - resolve(); - }, - }); - }), - ) - // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. - .then(fetch.resume) - .then(waitForBatchedUpdates) - .then( - () => - new Promise((resolve) => { - const connectionID = Onyx.connect({ - key: `${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, - waitForCollectionCallback: false, - callback: (policy) => { - Onyx.disconnect(connectionID); - expect(policy?.isLoading).toBeFalsy(); - expect(policy?.isChangeOwnerSuccessful).toBeTruthy(); - expect(policy?.isChangeOwnerFailed)?.toBeFalsy(); - resolve(); - }, - }); - }), - ) - ); + Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy); + Onyx.merge(ONYXKEYS.SESSION, {email: fakeEmail, accountID: fakeAccountID}); + Policy.addBillingCardAndRequestPolicyOwnerChange(fakePolicy.id, fakeCard); + await waitForBatchedUpdates(); + await new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, + waitForCollectionCallback: false, + callback: (policy) => { + Onyx.disconnect(connectionID); + expect(policy?.errorFields).toBeFalsy(); + expect(policy?.isLoading).toBeTruthy(); + expect(policy?.isChangeOwnerSuccessful).toBeFalsy(); + expect(policy?.isChangeOwnerFailed).toBeFalsy(); + resolve(); + }, + }); + }); + // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. + await fetch.resume(); + await waitForBatchedUpdates(); + await new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, + waitForCollectionCallback: false, + callback: (policy) => { + Onyx.disconnect(connectionID); + expect(policy?.isLoading).toBeFalsy(); + expect(policy?.isChangeOwnerSuccessful).toBeTruthy(); + expect(policy?.isChangeOwnerFailed)?.toBeFalsy(); + resolve(); + }, + }); + }); }); }); }); diff --git a/tests/actions/PolicyProfileTest.ts b/tests/actions/PolicyProfileTest.ts index 6fd17b6ce499..21ee34568100 100644 --- a/tests/actions/PolicyProfileTest.ts +++ b/tests/actions/PolicyProfileTest.ts @@ -23,7 +23,7 @@ describe('actions/PolicyProfile', () => { }); describe('updateWorkspaceDescription', () => { - it('Update workspace`s description', () => { + it('Update workspace`s description', async () => { const fakePolicy = createRandomPolicy(0); const oldDescription = fakePolicy.description ?? ''; @@ -31,48 +31,38 @@ describe('actions/PolicyProfile', () => { const parsedDescription = ReportUtils.getParsedComment(newDescription); // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. fetch.pause(); - return ( - Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy) - .then(() => { - Policy.updateWorkspaceDescription(fakePolicy.id, newDescription, oldDescription); - return waitForBatchedUpdates(); - }) - .then( - () => - new Promise((resolve) => { - const connectionID = Onyx.connect({ - key: `${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, - waitForCollectionCallback: false, - callback: (policy) => { - Onyx.disconnect(connectionID); + Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy); + Policy.updateWorkspaceDescription(fakePolicy.id, newDescription, oldDescription); + await waitForBatchedUpdates(); + await new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, + waitForCollectionCallback: false, + callback: (policy) => { + Onyx.disconnect(connectionID); - expect(policy?.description).toBe(parsedDescription); - expect(policy?.pendingFields?.description).toBe(CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE); - expect(policy?.errorFields?.description).toBeFalsy(); - resolve(); - }, - }); - }), - ) - // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. - .then(fetch.resume) - .then(waitForBatchedUpdates) - .then( - () => - new Promise((resolve) => { - const connectionID = Onyx.connect({ - key: `${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, - waitForCollectionCallback: false, - callback: (policy) => { - Onyx.disconnect(connectionID); - expect(policy?.pendingFields?.description).toBeFalsy(); + expect(policy?.description).toBe(parsedDescription); + expect(policy?.pendingFields?.description).toBe(CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE); + expect(policy?.errorFields?.description).toBeFalsy(); + resolve(); + }, + }); + }); + // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. + await fetch.resume(); + await waitForBatchedUpdates(); + await new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, + waitForCollectionCallback: false, + callback: (policy) => { + Onyx.disconnect(connectionID); + expect(policy?.pendingFields?.description).toBeFalsy(); - resolve(); - }, - }); - }), - ) - ); + resolve(); + }, + }); + }); }); }); }); From 0105dd6fe45c4bb39e52538f95c45f4a66ca7438 Mon Sep 17 00:00:00 2001 From: tienifr Date: Tue, 7 May 2024 15:53:24 +0700 Subject: [PATCH 7/8] fix lint --- tests/actions/PolicyMemberTest.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/actions/PolicyMemberTest.ts b/tests/actions/PolicyMemberTest.ts index 30948c38c425..4ca8d04568eb 100644 --- a/tests/actions/PolicyMemberTest.ts +++ b/tests/actions/PolicyMemberTest.ts @@ -163,9 +163,9 @@ describe('actions/PolicyMember', () => { resolve(); }, }); - }), - // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. - await fetch.resume(); + }); + // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. + await fetch.resume(); await waitForBatchedUpdates(); new Promise((resolve) => { const connectionID = Onyx.connect({ From 2fbdb39dfb9069ca3de4cec57008be273ae0bf77 Mon Sep 17 00:00:00 2001 From: tienifr Date: Tue, 7 May 2024 16:03:27 +0700 Subject: [PATCH 8/8] fix lint --- tests/actions/PolicyMemberTest.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/actions/PolicyMemberTest.ts b/tests/actions/PolicyMemberTest.ts index 4ca8d04568eb..8d982d4a1892 100644 --- a/tests/actions/PolicyMemberTest.ts +++ b/tests/actions/PolicyMemberTest.ts @@ -167,7 +167,7 @@ describe('actions/PolicyMember', () => { // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. await fetch.resume(); await waitForBatchedUpdates(); - new Promise((resolve) => { + await new Promise((resolve) => { const connectionID = Onyx.connect({ key: `${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, waitForCollectionCallback: false,