From 89d115b1c06efa378ccd7de6cf4b7ce24b2457f8 Mon Sep 17 00:00:00 2001 From: Yauheni Date: Thu, 16 May 2024 11:42:13 +0200 Subject: [PATCH 1/7] Fix ts issues related with TestHelper --- tests/actions/PolicyCategoryTest.ts | 34 +++++++++++------------------ tests/actions/PolicyMemberTest.ts | 28 ++++++++++-------------- tests/actions/PolicyProfileTest.ts | 10 ++++----- 3 files changed, 29 insertions(+), 43 deletions(-) diff --git a/tests/actions/PolicyCategoryTest.ts b/tests/actions/PolicyCategoryTest.ts index 2817a1661db4..7c060b40467e 100644 --- a/tests/actions/PolicyCategoryTest.ts +++ b/tests/actions/PolicyCategoryTest.ts @@ -6,6 +6,7 @@ import ONYXKEYS from '@src/ONYXKEYS'; import createRandomPolicy from '../utils/collections/policies'; import createRandomPolicyCategories from '../utils/collections/policyCategory'; import * as TestHelper from '../utils/TestHelper'; +import type {MockFetch} from '../utils/TestHelper'; import waitForBatchedUpdates from '../utils/waitForBatchedUpdates'; OnyxUpdateManager(); @@ -16,9 +17,10 @@ describe('actions/PolicyCategory', () => { }); }); + let mockFetch: MockFetch; beforeEach(() => { - // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. global.fetch = TestHelper.getGlobalFetchMock(); + mockFetch = fetch as MockFetch; return Onyx.clear().then(waitForBatchedUpdates); }); @@ -27,8 +29,7 @@ describe('actions/PolicyCategory', () => { 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(); + mockFetch?.pause?.(); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy); Policy.setWorkspaceRequiresCategory(fakePolicy.id, true); await waitForBatchedUpdates(); @@ -46,8 +47,7 @@ describe('actions/PolicyCategory', () => { }, }); }); - // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. - await fetch.resume(); + await mockFetch?.resume?.(); await waitForBatchedUpdates(); await new Promise((resolve) => { const connectionID = Onyx.connect({ @@ -68,8 +68,7 @@ describe('actions/PolicyCategory', () => { 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(); + mockFetch?.pause?.(); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${fakePolicy.id}`, fakeCategories); Policy.createPolicyCategory(fakePolicy.id, newCategoryName); @@ -89,8 +88,7 @@ describe('actions/PolicyCategory', () => { }, }); }); - // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. - await fetch.resume(); + await mockFetch?.resume?.(); await waitForBatchedUpdates(); await new Promise((resolve) => { const connectionID = Onyx.connect({ @@ -115,8 +113,7 @@ describe('actions/PolicyCategory', () => { 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(); + mockFetch?.pause?.(); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${fakePolicy.id}`, fakeCategories); Policy.renamePolicyCategory(fakePolicy.id, { @@ -140,8 +137,7 @@ describe('actions/PolicyCategory', () => { }, }); }); - // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. - await fetch.resume(); + await mockFetch?.resume?.(); await waitForBatchedUpdates(); await new Promise((resolve) => { const connectionID = Onyx.connect({ @@ -170,8 +166,7 @@ describe('actions/PolicyCategory', () => { enabled: true, }, }; - // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. - fetch.pause(); + mockFetch?.pause?.(); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${fakePolicy.id}`, fakeCategories); Policy.setWorkspaceCategoryEnabled(fakePolicy.id, categoriesToUpdate); @@ -191,8 +186,7 @@ describe('actions/PolicyCategory', () => { }, }); }); - // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. - await fetch.resume(); + await mockFetch?.resume?.(); await waitForBatchedUpdates(); await new Promise((resolve) => { const connectionID = Onyx.connect({ @@ -217,8 +211,7 @@ describe('actions/PolicyCategory', () => { 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(); + mockFetch?.pause?.(); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${fakePolicy.id}`, fakeCategories); Policy.deleteWorkspaceCategories(fakePolicy.id, categoriesToDelete); @@ -235,8 +228,7 @@ describe('actions/PolicyCategory', () => { }, }); }); - // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. - await fetch.resume(); + await mockFetch?.resume?.(); await waitForBatchedUpdates(); await new Promise((resolve) => { const connectionID = Onyx.connect({ diff --git a/tests/actions/PolicyMemberTest.ts b/tests/actions/PolicyMemberTest.ts index 8d982d4a1892..4f3afc51ec66 100644 --- a/tests/actions/PolicyMemberTest.ts +++ b/tests/actions/PolicyMemberTest.ts @@ -11,6 +11,7 @@ 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 type {MockFetch} from '../utils/TestHelper'; import waitForBatchedUpdates from '../utils/waitForBatchedUpdates'; OnyxUpdateManager(); @@ -21,9 +22,10 @@ describe('actions/PolicyMember', () => { }); }); + let mockFetch: MockFetch; beforeEach(() => { - // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. global.fetch = TestHelper.getGlobalFetchMock(); + mockFetch = fetch as MockFetch; return Onyx.clear().then(waitForBatchedUpdates); }); @@ -39,8 +41,7 @@ describe('actions/PolicyMember', () => { 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(); + mockFetch?.pause?.(); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy); Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${fakeReport.reportID}`, fakeReport); Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${fakeReport.reportID}`, { @@ -67,8 +68,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 mockFetch?.resume?.(); await waitForBatchedUpdates(); await new Promise((resolve) => { const connectionID = Onyx.connect({ @@ -101,8 +101,7 @@ describe('actions/PolicyMember', () => { }, }; - // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. - fetch.pause(); + mockFetch?.pause?.(); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy); Onyx.set(`${ONYXKEYS.PERSONAL_DETAILS_LIST}`, {[fakeUser2.accountID]: fakeUser2}); await waitForBatchedUpdates(); @@ -121,8 +120,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 mockFetch?.resume?.(); await waitForBatchedUpdates(); await new Promise((resolve) => { const connectionID = Onyx.connect({ @@ -144,8 +142,7 @@ describe('actions/PolicyMember', () => { 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(); + mockFetch?.pause?.(); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy); Onyx.merge(ONYXKEYS.SESSION, {email: fakeEmail, accountID: fakeAccountID}); Policy.requestWorkspaceOwnerChange(fakePolicy.id); @@ -164,8 +161,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 mockFetch?.resume?.(); await waitForBatchedUpdates(); await new Promise((resolve) => { const connectionID = Onyx.connect({ @@ -197,8 +193,7 @@ describe('actions/PolicyMember', () => { }; const fakeAccountID = 1; - // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. - fetch.pause(); + mockFetch?.pause?.(); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy); Onyx.merge(ONYXKEYS.SESSION, {email: fakeEmail, accountID: fakeAccountID}); Policy.addBillingCardAndRequestPolicyOwnerChange(fakePolicy.id, fakeCard); @@ -217,8 +212,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 mockFetch?.resume?.(); await waitForBatchedUpdates(); await new Promise((resolve) => { const connectionID = Onyx.connect({ diff --git a/tests/actions/PolicyProfileTest.ts b/tests/actions/PolicyProfileTest.ts index 21ee34568100..a6bc0ea541bc 100644 --- a/tests/actions/PolicyProfileTest.ts +++ b/tests/actions/PolicyProfileTest.ts @@ -6,6 +6,7 @@ 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 type {MockFetch} from '../utils/TestHelper'; import waitForBatchedUpdates from '../utils/waitForBatchedUpdates'; OnyxUpdateManager(); @@ -16,9 +17,10 @@ describe('actions/PolicyProfile', () => { }); }); + let mockFetch: MockFetch; beforeEach(() => { - // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. global.fetch = TestHelper.getGlobalFetchMock(); + mockFetch = fetch as MockFetch; return Onyx.clear().then(waitForBatchedUpdates); }); @@ -29,8 +31,7 @@ describe('actions/PolicyProfile', () => { 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(); + mockFetch?.pause?.(); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy); Policy.updateWorkspaceDescription(fakePolicy.id, newDescription, oldDescription); await waitForBatchedUpdates(); @@ -48,8 +49,7 @@ describe('actions/PolicyProfile', () => { }, }); }); - // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. - await fetch.resume(); + await mockFetch?.resume?.(); await waitForBatchedUpdates(); await new Promise((resolve) => { const connectionID = Onyx.connect({ From 6272d95951ed4b95af6d346ceb2947c0b0b62b66 Mon Sep 17 00:00:00 2001 From: Yauheni Date: Thu, 16 May 2024 12:54:03 +0200 Subject: [PATCH 2/7] Fix comments --- tests/actions/IOUTest.ts | 79 +++++++++++++------------- tests/actions/PolicyCategoryTest.ts | 23 ++++---- tests/actions/PolicyMemberTest.ts | 19 +++---- tests/actions/PolicyProfileTest.ts | 7 +-- tests/actions/PolicyTagTest.ts | 67 +++++++++++----------- tests/actions/PolicyTaxTest.ts | 87 ++++++++++++++--------------- tests/actions/PolicyTest.ts | 7 ++- tests/utils/TestHelper.ts | 12 ++-- 8 files changed, 148 insertions(+), 153 deletions(-) diff --git a/tests/actions/IOUTest.ts b/tests/actions/IOUTest.ts index ac320729b2b7..0f9d888b549a 100644 --- a/tests/actions/IOUTest.ts +++ b/tests/actions/IOUTest.ts @@ -63,8 +63,7 @@ describe('actions/IOU', () => { let mockFetch: MockFetch; beforeEach(() => { - global.fetch = TestHelper.getGlobalFetchMock(); - mockFetch = fetch as MockFetch; + mockFetch = TestHelper.getGlobalFetchMock() as MockFetch; return Onyx.clear().then(waitForBatchedUpdates); }); @@ -79,7 +78,7 @@ describe('actions/IOU', () => { let transactionID: string | undefined; let transactionThread: OnyxEntry; let transactionThreadCreatedAction: OnyxEntry; - mockFetch?.pause?.(); + mockFetch.pause(); IOU.requestMoney({reportID: ''}, amount, CONST.CURRENCY.USD, '', merchant, RORY_EMAIL, RORY_ACCOUNT_ID, {login: CARLOS_EMAIL, accountID: CARLOS_ACCOUNT_ID}, comment, {}); return waitForBatchedUpdates() .then( @@ -218,7 +217,7 @@ describe('actions/IOU', () => { }); }), ) - .then(mockFetch?.resume) + .then(mockFetch.resume) .then( () => new Promise((resolve) => { @@ -267,7 +266,7 @@ describe('actions/IOU', () => { let iouAction: OnyxEntry; let iouCreatedAction: OnyxEntry; let transactionID: string | undefined; - mockFetch?.pause?.(); + mockFetch.pause(); return Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${chatReport.reportID}`, chatReport) .then(() => Onyx.set(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReport.reportID}`, { @@ -378,7 +377,7 @@ describe('actions/IOU', () => { }); }), ) - .then(mockFetch?.resume) + .then(mockFetch.resume) .then(waitForBatchedUpdates) .then( () => @@ -462,7 +461,7 @@ describe('actions/IOU', () => { }; let newIOUAction: OnyxEntry; let newTransaction: OnyxEntry; - mockFetch?.pause?.(); + mockFetch.pause?.(); return Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${chatReportID}`, chatReport) .then(() => Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${iouReportID}`, iouReport)) .then(() => @@ -567,7 +566,7 @@ describe('actions/IOU', () => { }); }), ) - .then(mockFetch?.resume) + .then(mockFetch.resume) .then(waitForNetworkPromises) .then( () => @@ -610,7 +609,7 @@ describe('actions/IOU', () => { let transactionID: string; let transactionThreadReport: OnyxEntry; let transactionThreadAction: OnyxEntry; - mockFetch?.pause?.(); + mockFetch.pause(); IOU.requestMoney({reportID: ''}, amount, CONST.CURRENCY.USD, '', '', RORY_EMAIL, RORY_ACCOUNT_ID, {login: CARLOS_EMAIL, accountID: CARLOS_ACCOUNT_ID}, comment, {}); return ( waitForBatchedUpdates() @@ -721,8 +720,8 @@ describe('actions/IOU', () => { }), ) .then((): Promise => { - mockFetch?.fail?.(); - return mockFetch?.resume?.() as Promise; + mockFetch.fail(); + return mockFetch.resume?.() as Promise; }) .then( () => @@ -910,7 +909,7 @@ describe('actions/IOU', () => { ) // Cleanup - .then(mockFetch?.succeed) + .then(mockFetch.succeed) ); }); }); @@ -1061,7 +1060,7 @@ describe('actions/IOU', () => { .then(() => Onyx.set(`${ONYXKEYS.COLLECTION.TRANSACTION}${julesExistingTransaction?.transactionID}`, julesExistingTransaction)) .then(() => { // When we split a bill offline - mockFetch?.pause?.(); + mockFetch.pause(); IOU.splitBill( // TODO: Migrate after the backend accepts accountIDs { @@ -1343,7 +1342,7 @@ describe('actions/IOU', () => { }); }), ) - .then(mockFetch?.resume) + .then(mockFetch.resume) .then(waitForNetworkPromises) .then( () => @@ -1480,7 +1479,7 @@ describe('actions/IOU', () => { }), ) .then(() => { - mockFetch?.pause?.(); + mockFetch.pause(); if (chatReport && iouReport) { IOU.payMoneyRequest(CONST.IOU.PAYMENT_TYPE.ELSEWHERE, chatReport, iouReport); } @@ -1535,7 +1534,7 @@ describe('actions/IOU', () => { }); }), ) - .then(mockFetch?.resume) + .then(mockFetch.resume) .then( () => new Promise((resolve) => { @@ -1593,7 +1592,7 @@ describe('actions/IOU', () => { const merchant = 'NASDAQ'; afterEach(() => { - mockFetch?.resume?.(); + mockFetch.resume?.(); }); it('updates the IOU request and IOU report when offline', () => { @@ -1602,7 +1601,7 @@ describe('actions/IOU', () => { let iouAction: OnyxEntry = null; let transaction: OnyxEntry = null; - mockFetch?.pause?.(); + mockFetch.pause(); IOU.requestMoney({reportID: ''}, amount, CONST.CURRENCY.USD, '', merchant, RORY_EMAIL, RORY_ACCOUNT_ID, {login: CARLOS_EMAIL, accountID: CARLOS_ACCOUNT_ID}, comment, {}); return waitForBatchedUpdates() .then(() => { @@ -1745,7 +1744,7 @@ describe('actions/IOU', () => { }), ) .then(() => { - mockFetch?.resume?.(); + mockFetch.resume?.(); }); }); @@ -1811,7 +1810,7 @@ describe('actions/IOU', () => { return waitForBatchedUpdates(); }) .then(() => { - mockFetch?.fail?.(); + mockFetch.fail(); if (transaction) { IOU.editMoneyRequest( @@ -1904,14 +1903,14 @@ describe('actions/IOU', () => { const merchant = 'NASDAQ'; afterEach(() => { - mockFetch?.resume?.(); + mockFetch.resume?.(); }); it('updates the expense request and expense report when paid while offline', () => { let expenseReport: OnyxEntry; let chatReport: OnyxEntry; - mockFetch?.pause?.(); + mockFetch.pause(); Onyx.set(ONYXKEYS.SESSION, {email: CARLOS_EMAIL, accountID: CARLOS_ACCOUNT_ID}); return waitForBatchedUpdates() .then(() => { @@ -2068,7 +2067,7 @@ describe('actions/IOU', () => { }), ) .then(() => { - mockFetch?.fail?.(); + mockFetch.fail(); if (chatReport && expenseReport) { IOU.payMoneyRequest('ACH', chatReport, expenseReport); } @@ -2218,7 +2217,7 @@ describe('actions/IOU', () => { it('delete an expense (IOU Action and transaction) successfully', async () => { // Given the fetch operations are paused and an expense is initiated - mockFetch?.pause?.(); + mockFetch.pause(); if (transaction && createIOUAction) { // When the expense is deleted @@ -2257,7 +2256,7 @@ describe('actions/IOU', () => { expect(t).toBeFalsy(); // Given fetch operations are resumed - mockFetch?.resume?.(); + mockFetch.resume?.(); await waitForBatchedUpdates(); // Then we recheck the IOU report action from the report actions collection @@ -2292,7 +2291,7 @@ describe('actions/IOU', () => { it('delete the IOU report when there are no visible comments left in the IOU report', async () => { // Given an IOU report and a paused fetch state - mockFetch?.pause?.(); + mockFetch.pause(); if (transaction && createIOUAction) { // When the IOU expense is deleted @@ -2315,7 +2314,7 @@ describe('actions/IOU', () => { expect(report).toBeTruthy(); // Given the resumed fetch state - mockFetch?.resume?.(); + mockFetch.resume?.(); await waitForBatchedUpdates(); report = await new Promise>((resolve) => { @@ -2367,7 +2366,7 @@ describe('actions/IOU', () => { expect(resultActionAfterUpdate?.pendingAction).toBeUndefined(); // When we attempt to delete an expense from the IOU report - mockFetch?.pause?.(); + mockFetch.pause(); if (transaction && createIOUAction) { IOU.deleteMoneyRequest(transaction?.transactionID, createIOUAction, false); } @@ -2393,7 +2392,7 @@ describe('actions/IOU', () => { expect(iouReport).toHaveProperty('chatReportID'); // Given the resumed fetch state - mockFetch?.resume?.(); + mockFetch.resume?.(); allReports = await new Promise>((resolve) => { const connectionID = Onyx.connect({ @@ -2457,7 +2456,7 @@ describe('actions/IOU', () => { await waitForBatchedUpdates(); // Given Fetch is paused and timers have advanced - mockFetch?.pause?.(); + mockFetch.pause(); jest.advanceTimersByTime(10); if (transaction && createIOUAction) { @@ -2479,7 +2478,7 @@ describe('actions/IOU', () => { }); expect(report).toBeFalsy(); - mockFetch?.resume?.(); + mockFetch.resume?.(); // Then After resuming fetch, the report for the given thread ID still does not exist report = await new Promise>((resolve) => { @@ -2647,7 +2646,7 @@ describe('actions/IOU', () => { const resultActionAfter = reportActions?.[reportActionID]; expect(resultActionAfter?.pendingAction).toBeUndefined(); - mockFetch?.pause?.(); + mockFetch.pause(); if (transaction && createIOUAction) { // When deleting expense @@ -2670,7 +2669,7 @@ describe('actions/IOU', () => { // When fetch resumes // Then the transaction thread report should still exist - mockFetch?.resume?.(); + mockFetch.resume?.(); await new Promise((resolve) => { const connectionID = Onyx.connect({ key: `${ONYXKEYS.COLLECTION.REPORT}${thread.reportID}`, @@ -2793,7 +2792,7 @@ describe('actions/IOU', () => { // Verify that our action is no longer in the loading state expect(resultActionAfterUpdate?.pendingAction).toBeUndefined(); - mockFetch?.pause?.(); + mockFetch.pause(); if (transaction && createIOUAction) { // When we delete the expense IOU.deleteMoneyRequest(transaction.transactionID, createIOUAction, false); @@ -2816,7 +2815,7 @@ describe('actions/IOU', () => { }); // When we resume fetch - mockFetch?.resume?.(); + mockFetch.resume?.(); // Then we expect the moneyRequestPreview to show [Deleted expense] @@ -2865,7 +2864,7 @@ describe('actions/IOU', () => { expect(ioupreview?.message?.[0]?.text).toBe('rory@expensifail.com owes $300.00'); // When we delete the first expense - mockFetch?.pause?.(); + mockFetch.pause(); jest.advanceTimersByTime(10); if (transaction && createIOUAction) { IOU.deleteMoneyRequest(transaction.transactionID, createIOUAction, false); @@ -2880,7 +2879,7 @@ describe('actions/IOU', () => { expect(iouReport?.total).toBe(20000); // When we resume - mockFetch?.resume?.(); + mockFetch.resume?.(); // Then we expect the IOU report and reportPreview to update with new totals expect(iouReport).toBeTruthy(); @@ -2956,7 +2955,7 @@ describe('actions/IOU', () => { // When we delete the expense in SingleTransactionView and we should not delete the IOU report - mockFetch?.pause?.(); + mockFetch.pause(); if (transaction && createIOUAction) { IOU.deleteMoneyRequest(transaction.transactionID, createIOUAction, true); @@ -2981,7 +2980,7 @@ describe('actions/IOU', () => { expect(iouReport).toHaveProperty('reportID'); expect(iouReport).toHaveProperty('chatReportID'); - mockFetch?.resume?.(); + mockFetch.resume?.(); allReports = await new Promise>((resolve) => { const connectionID = Onyx.connect({ @@ -3307,7 +3306,7 @@ describe('actions/IOU', () => { }), ) .then(() => { - mockFetch?.fail?.(); + mockFetch.fail(); if (expenseReport) { IOU.submitReport(expenseReport); } diff --git a/tests/actions/PolicyCategoryTest.ts b/tests/actions/PolicyCategoryTest.ts index 7c060b40467e..1fdd7f43400f 100644 --- a/tests/actions/PolicyCategoryTest.ts +++ b/tests/actions/PolicyCategoryTest.ts @@ -19,8 +19,7 @@ describe('actions/PolicyCategory', () => { let mockFetch: MockFetch; beforeEach(() => { - global.fetch = TestHelper.getGlobalFetchMock(); - mockFetch = fetch as MockFetch; + mockFetch = TestHelper.getGlobalFetchMock() as MockFetch; return Onyx.clear().then(waitForBatchedUpdates); }); @@ -29,7 +28,7 @@ describe('actions/PolicyCategory', () => { const fakePolicy = createRandomPolicy(0); fakePolicy.requiresCategory = false; - mockFetch?.pause?.(); + mockFetch.pause(); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy); Policy.setWorkspaceRequiresCategory(fakePolicy.id, true); await waitForBatchedUpdates(); @@ -47,7 +46,7 @@ describe('actions/PolicyCategory', () => { }, }); }); - await mockFetch?.resume?.(); + await mockFetch.resume?.(); await waitForBatchedUpdates(); await new Promise((resolve) => { const connectionID = Onyx.connect({ @@ -68,7 +67,7 @@ describe('actions/PolicyCategory', () => { const fakePolicy = createRandomPolicy(0); const fakeCategories = createRandomPolicyCategories(3); const newCategoryName = 'New category'; - mockFetch?.pause?.(); + mockFetch.pause(); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${fakePolicy.id}`, fakeCategories); Policy.createPolicyCategory(fakePolicy.id, newCategoryName); @@ -88,7 +87,7 @@ describe('actions/PolicyCategory', () => { }, }); }); - await mockFetch?.resume?.(); + await mockFetch.resume?.(); await waitForBatchedUpdates(); await new Promise((resolve) => { const connectionID = Onyx.connect({ @@ -113,7 +112,7 @@ describe('actions/PolicyCategory', () => { const fakeCategories = createRandomPolicyCategories(3); const oldCategoryName = Object.keys(fakeCategories)[0]; const newCategoryName = 'Updated category'; - mockFetch?.pause?.(); + mockFetch.pause(); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${fakePolicy.id}`, fakeCategories); Policy.renamePolicyCategory(fakePolicy.id, { @@ -137,7 +136,7 @@ describe('actions/PolicyCategory', () => { }, }); }); - await mockFetch?.resume?.(); + await mockFetch.resume?.(); await waitForBatchedUpdates(); await new Promise((resolve) => { const connectionID = Onyx.connect({ @@ -166,7 +165,7 @@ describe('actions/PolicyCategory', () => { enabled: true, }, }; - mockFetch?.pause?.(); + mockFetch.pause(); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${fakePolicy.id}`, fakeCategories); Policy.setWorkspaceCategoryEnabled(fakePolicy.id, categoriesToUpdate); @@ -186,7 +185,7 @@ describe('actions/PolicyCategory', () => { }, }); }); - await mockFetch?.resume?.(); + await mockFetch.resume?.(); await waitForBatchedUpdates(); await new Promise((resolve) => { const connectionID = Onyx.connect({ @@ -211,7 +210,7 @@ describe('actions/PolicyCategory', () => { const fakeCategories = createRandomPolicyCategories(3); const categoryNameToDelete = Object.keys(fakeCategories)[0]; const categoriesToDelete = [categoryNameToDelete]; - mockFetch?.pause?.(); + mockFetch.pause(); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${fakePolicy.id}`, fakeCategories); Policy.deleteWorkspaceCategories(fakePolicy.id, categoriesToDelete); @@ -228,7 +227,7 @@ describe('actions/PolicyCategory', () => { }, }); }); - await mockFetch?.resume?.(); + await mockFetch.resume?.(); await waitForBatchedUpdates(); await new Promise((resolve) => { const connectionID = Onyx.connect({ diff --git a/tests/actions/PolicyMemberTest.ts b/tests/actions/PolicyMemberTest.ts index 4f3afc51ec66..5a025829b2b4 100644 --- a/tests/actions/PolicyMemberTest.ts +++ b/tests/actions/PolicyMemberTest.ts @@ -24,8 +24,7 @@ describe('actions/PolicyMember', () => { let mockFetch: MockFetch; beforeEach(() => { - global.fetch = TestHelper.getGlobalFetchMock(); - mockFetch = fetch as MockFetch; + mockFetch = TestHelper.getGlobalFetchMock() as MockFetch; return Onyx.clear().then(waitForBatchedUpdates); }); @@ -41,7 +40,7 @@ describe('actions/PolicyMember', () => { actionName: CONST.REPORT.ACTIONS.TYPE.ACTIONABLE_JOIN_REQUEST, } as ReportAction; - mockFetch?.pause?.(); + mockFetch.pause(); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy); Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${fakeReport.reportID}`, fakeReport); Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${fakeReport.reportID}`, { @@ -68,7 +67,7 @@ describe('actions/PolicyMember', () => { }, }); }); - await mockFetch?.resume?.(); + await mockFetch.resume?.(); await waitForBatchedUpdates(); await new Promise((resolve) => { const connectionID = Onyx.connect({ @@ -101,7 +100,7 @@ describe('actions/PolicyMember', () => { }, }; - mockFetch?.pause?.(); + mockFetch.pause(); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy); Onyx.set(`${ONYXKEYS.PERSONAL_DETAILS_LIST}`, {[fakeUser2.accountID]: fakeUser2}); await waitForBatchedUpdates(); @@ -120,7 +119,7 @@ describe('actions/PolicyMember', () => { }, }); }); - await mockFetch?.resume?.(); + await mockFetch.resume?.(); await waitForBatchedUpdates(); await new Promise((resolve) => { const connectionID = Onyx.connect({ @@ -142,7 +141,7 @@ describe('actions/PolicyMember', () => { const fakeEmail = 'fake@gmail.com'; const fakeAccountID = 1; - mockFetch?.pause?.(); + mockFetch.pause(); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy); Onyx.merge(ONYXKEYS.SESSION, {email: fakeEmail, accountID: fakeAccountID}); Policy.requestWorkspaceOwnerChange(fakePolicy.id); @@ -161,7 +160,7 @@ describe('actions/PolicyMember', () => { }, }); }); - await mockFetch?.resume?.(); + await mockFetch.resume?.(); await waitForBatchedUpdates(); await new Promise((resolve) => { const connectionID = Onyx.connect({ @@ -193,7 +192,7 @@ describe('actions/PolicyMember', () => { }; const fakeAccountID = 1; - mockFetch?.pause?.(); + mockFetch.pause(); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy); Onyx.merge(ONYXKEYS.SESSION, {email: fakeEmail, accountID: fakeAccountID}); Policy.addBillingCardAndRequestPolicyOwnerChange(fakePolicy.id, fakeCard); @@ -212,7 +211,7 @@ describe('actions/PolicyMember', () => { }, }); }); - await mockFetch?.resume?.(); + await mockFetch.resume?.(); await waitForBatchedUpdates(); await new Promise((resolve) => { const connectionID = Onyx.connect({ diff --git a/tests/actions/PolicyProfileTest.ts b/tests/actions/PolicyProfileTest.ts index a6bc0ea541bc..568b2af92dfb 100644 --- a/tests/actions/PolicyProfileTest.ts +++ b/tests/actions/PolicyProfileTest.ts @@ -19,8 +19,7 @@ describe('actions/PolicyProfile', () => { let mockFetch: MockFetch; beforeEach(() => { - global.fetch = TestHelper.getGlobalFetchMock(); - mockFetch = fetch as MockFetch; + mockFetch = TestHelper.getGlobalFetchMock() as MockFetch; return Onyx.clear().then(waitForBatchedUpdates); }); @@ -31,7 +30,7 @@ describe('actions/PolicyProfile', () => { const oldDescription = fakePolicy.description ?? ''; const newDescription = 'Updated description'; const parsedDescription = ReportUtils.getParsedComment(newDescription); - mockFetch?.pause?.(); + mockFetch.pause(); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy); Policy.updateWorkspaceDescription(fakePolicy.id, newDescription, oldDescription); await waitForBatchedUpdates(); @@ -49,7 +48,7 @@ describe('actions/PolicyProfile', () => { }, }); }); - await mockFetch?.resume?.(); + await mockFetch.resume?.(); await waitForBatchedUpdates(); await new Promise((resolve) => { const connectionID = Onyx.connect({ diff --git a/tests/actions/PolicyTagTest.ts b/tests/actions/PolicyTagTest.ts index 74ea13f3d139..db7932063b92 100644 --- a/tests/actions/PolicyTagTest.ts +++ b/tests/actions/PolicyTagTest.ts @@ -20,8 +20,7 @@ describe('actions/Policy', () => { let mockFetch: MockFetch; beforeEach(() => { - global.fetch = TestHelper.getGlobalFetchMock(); - mockFetch = fetch as MockFetch; + mockFetch = TestHelper.getGlobalFetchMock() as MockFetch; return Onyx.clear().then(waitForBatchedUpdates); }); @@ -30,7 +29,7 @@ describe('actions/Policy', () => { const fakePolicy = createRandomPolicy(0); fakePolicy.requiresTag = false; - mockFetch?.pause?.(); + mockFetch.pause(); return Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy) .then(() => { @@ -55,7 +54,7 @@ describe('actions/Policy', () => { }); }), ) - .then(mockFetch?.resume) + .then(mockFetch.resume) .then(waitForBatchedUpdates) .then( () => @@ -77,7 +76,7 @@ describe('actions/Policy', () => { const fakePolicy = createRandomPolicy(0); fakePolicy.requiresTag = true; - mockFetch?.pause?.(); + mockFetch.pause(); return Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy) .then(() => { @@ -102,7 +101,7 @@ describe('actions/Policy', () => { }); }), ) - .then(mockFetch?.resume) + .then(mockFetch.resume) .then(waitForBatchedUpdates) .then( () => @@ -124,16 +123,16 @@ describe('actions/Policy', () => { const fakePolicy = createRandomPolicy(0); fakePolicy.requiresTag = true; - mockFetch?.pause?.(); + mockFetch.pause(); return Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy) .then(() => { - mockFetch?.fail?.(); + mockFetch.fail(); Policy.setPolicyRequiresTag(fakePolicy.id, false); return waitForBatchedUpdates(); }) - .then(mockFetch?.resume) + .then(mockFetch.resume) .then(waitForBatchedUpdates) .then( () => @@ -163,7 +162,7 @@ describe('actions/Policy', () => { const newTagListName = 'New tag list name'; const fakePolicyTags = createRandomPolicyTags(oldTagListName); - mockFetch?.pause?.(); + mockFetch.pause(); return Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy) .then(() => { @@ -199,7 +198,7 @@ describe('actions/Policy', () => { }); }), ) - .then(mockFetch?.resume) + .then(mockFetch.resume) .then(waitForBatchedUpdates) .then( () => @@ -228,14 +227,14 @@ describe('actions/Policy', () => { const newTagListName = 'New tag list name'; const fakePolicyTags = createRandomPolicyTags(oldTagListName); - mockFetch?.pause?.(); + mockFetch.pause(); return Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy) .then(() => { Onyx.set(`${ONYXKEYS.COLLECTION.POLICY_TAGS}${fakePolicy.id}`, fakePolicyTags); }) .then(() => { - mockFetch?.fail?.(); + mockFetch.fail(); Policy.renamePolicyTaglist( fakePolicy.id, @@ -247,7 +246,7 @@ describe('actions/Policy', () => { ); return waitForBatchedUpdates(); }) - .then(mockFetch?.resume) + .then(mockFetch.resume) .then(waitForBatchedUpdates) .then( () => @@ -279,7 +278,7 @@ describe('actions/Policy', () => { const newTagName = 'new tag'; const fakePolicyTags = createRandomPolicyTags(tagListName); - mockFetch?.pause?.(); + mockFetch.pause(); return Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy) .then(() => { @@ -309,7 +308,7 @@ describe('actions/Policy', () => { }); }), ) - .then(mockFetch?.resume) + .then(mockFetch.resume) .then(waitForBatchedUpdates) .then( () => @@ -339,19 +338,19 @@ describe('actions/Policy', () => { const newTagName = 'new tag'; const fakePolicyTags = createRandomPolicyTags(tagListName); - mockFetch?.pause?.(); + mockFetch.pause(); return Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy) .then(() => { Onyx.set(`${ONYXKEYS.COLLECTION.POLICY_TAGS}${fakePolicy.id}`, fakePolicyTags); }) .then(() => { - mockFetch?.fail?.(); + mockFetch.fail(); Policy.createPolicyTag(fakePolicy.id, newTagName); return waitForBatchedUpdates(); }) - .then(mockFetch?.resume) + .then(mockFetch.resume) .then(waitForBatchedUpdates) .then( () => @@ -388,7 +387,7 @@ describe('actions/Policy', () => { return acc; }, {}); - mockFetch?.pause?.(); + mockFetch.pause(); return Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy) .then(() => { @@ -420,7 +419,7 @@ describe('actions/Policy', () => { }); }), ) - .then(mockFetch?.resume) + .then(mockFetch.resume) .then(waitForBatchedUpdates) .then( () => @@ -459,19 +458,19 @@ describe('actions/Policy', () => { return acc; }, {}); - mockFetch?.pause?.(); + mockFetch.pause(); return Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy) .then(() => { Onyx.set(`${ONYXKEYS.COLLECTION.POLICY_TAGS}${fakePolicy.id}`, fakePolicyTags); }) .then(() => { - mockFetch?.fail?.(); + mockFetch.fail(); Policy.setWorkspaceTagEnabled(fakePolicy.id, tagsToUpdate); return waitForBatchedUpdates(); }) - .then(mockFetch?.resume) + .then(mockFetch.resume) .then(waitForBatchedUpdates) .then( () => @@ -507,7 +506,7 @@ describe('actions/Policy', () => { const oldTagName = Object.keys(fakePolicyTags?.[tagListName]?.tags)[0]; const newTagName = 'New tag'; - mockFetch?.pause?.(); + mockFetch.pause(); return Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy) .then(() => { @@ -540,7 +539,7 @@ describe('actions/Policy', () => { }); }), ) - .then(mockFetch?.resume) + .then(mockFetch.resume) .then(waitForBatchedUpdates) .then( () => @@ -571,14 +570,14 @@ describe('actions/Policy', () => { const oldTagName = Object.keys(fakePolicyTags?.[tagListName]?.tags)[0]; const newTagName = 'New tag'; - mockFetch?.pause?.(); + mockFetch.pause(); return Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy) .then(() => { Onyx.set(`${ONYXKEYS.COLLECTION.POLICY_TAGS}${fakePolicy.id}`, fakePolicyTags); }) .then(() => { - mockFetch?.fail?.(); + mockFetch.fail(); Policy.renamePolicyTag(fakePolicy.id, { oldName: oldTagName, @@ -586,7 +585,7 @@ describe('actions/Policy', () => { }); return waitForBatchedUpdates(); }) - .then(mockFetch?.resume) + .then(mockFetch.resume) .then(waitForBatchedUpdates) .then( () => @@ -618,7 +617,7 @@ describe('actions/Policy', () => { const fakePolicyTags = createRandomPolicyTags(tagListName, 2); const tagsToDelete = Object.keys(fakePolicyTags?.[tagListName]?.tags ?? {}); - mockFetch?.pause?.(); + mockFetch.pause(); return Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy) .then(() => { @@ -646,7 +645,7 @@ describe('actions/Policy', () => { }); }), ) - .then(mockFetch?.resume) + .then(mockFetch.resume) .then(waitForBatchedUpdates) .then( () => @@ -676,19 +675,19 @@ describe('actions/Policy', () => { const fakePolicyTags = createRandomPolicyTags(tagListName, 2); const tagsToDelete = Object.keys(fakePolicyTags?.[tagListName]?.tags ?? {}); - mockFetch?.pause?.(); + mockFetch.pause(); return Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy) .then(() => { Onyx.set(`${ONYXKEYS.COLLECTION.POLICY_TAGS}${fakePolicy.id}`, fakePolicyTags); }) .then(() => { - mockFetch?.fail?.(); + mockFetch.fail(); Policy.deletePolicyTags(fakePolicy.id, tagsToDelete); return waitForBatchedUpdates(); }) - .then(mockFetch?.resume) + .then(mockFetch.resume) .then(waitForBatchedUpdates) .then( () => diff --git a/tests/actions/PolicyTaxTest.ts b/tests/actions/PolicyTaxTest.ts index b1c190f9e5ac..b6a1c9b68a1a 100644 --- a/tests/actions/PolicyTaxTest.ts +++ b/tests/actions/PolicyTaxTest.ts @@ -21,8 +21,7 @@ describe('actions/PolicyTax', () => { let mockFetch: MockFetch; beforeEach(() => { - global.fetch = TestHelper.getGlobalFetchMock(); - mockFetch = fetch as MockFetch; + mockFetch = TestHelper.getGlobalFetchMock() as MockFetch; return Onyx.clear() .then(() => Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy)) .then(waitForBatchedUpdates); @@ -31,7 +30,7 @@ describe('actions/PolicyTax', () => { describe('SetPolicyCustomTaxName', () => { it('Set policy`s custom tax name', () => { const customTaxName = 'Custom tag name'; - mockFetch?.pause?.(); + mockFetch.pause(); Policy.setPolicyCustomTaxName(fakePolicy.id, customTaxName); return waitForBatchedUpdates() .then( @@ -50,7 +49,7 @@ describe('actions/PolicyTax', () => { }); }), ) - .then(mockFetch?.resume) + .then(mockFetch.resume) .then(waitForBatchedUpdates) .then( () => @@ -72,7 +71,7 @@ describe('actions/PolicyTax', () => { const customTaxName = 'Custom tag name'; const originalCustomTaxName = fakePolicy?.taxRates?.name; - mockFetch?.pause?.(); + mockFetch.pause(); Policy.setPolicyCustomTaxName(fakePolicy.id, customTaxName); return waitForBatchedUpdates() .then( @@ -92,8 +91,8 @@ describe('actions/PolicyTax', () => { }), ) .then(() => { - mockFetch?.fail?.(); - return mockFetch?.resume?.() as Promise; + mockFetch.fail(); + return mockFetch.resume?.() as Promise; }) .then(waitForBatchedUpdates) .then( @@ -119,7 +118,7 @@ describe('actions/PolicyTax', () => { it('Set policy`s currency default tax', () => { const taxCode = 'id_TAX_RATE_1'; - mockFetch?.pause?.(); + mockFetch.pause(); Policy.setWorkspaceCurrencyDefault(fakePolicy.id, taxCode); return waitForBatchedUpdates() .then( @@ -138,7 +137,7 @@ describe('actions/PolicyTax', () => { }); }), ) - .then(mockFetch?.resume) + .then(mockFetch.resume) .then(waitForBatchedUpdates) .then( () => @@ -160,7 +159,7 @@ describe('actions/PolicyTax', () => { const taxCode = 'id_TAX_RATE_1'; const originalDefaultExternalID = fakePolicy?.taxRates?.defaultExternalID; - mockFetch?.pause?.(); + mockFetch.pause(); Policy.setWorkspaceCurrencyDefault(fakePolicy.id, taxCode); return waitForBatchedUpdates() .then( @@ -180,8 +179,8 @@ describe('actions/PolicyTax', () => { }), ) .then(() => { - mockFetch?.fail?.(); - return mockFetch?.resume?.() as Promise; + mockFetch.fail(); + return mockFetch.resume?.() as Promise; }) .then(waitForBatchedUpdates) .then( @@ -206,7 +205,7 @@ describe('actions/PolicyTax', () => { it('Set policy`s foreign currency default', () => { const taxCode = 'id_TAX_RATE_1'; - mockFetch?.pause?.(); + mockFetch.pause(); Policy.setForeignCurrencyDefault(fakePolicy.id, taxCode); return waitForBatchedUpdates() .then( @@ -225,7 +224,7 @@ describe('actions/PolicyTax', () => { }); }), ) - .then(mockFetch?.resume) + .then(mockFetch.resume) .then(waitForBatchedUpdates) .then( () => @@ -248,7 +247,7 @@ describe('actions/PolicyTax', () => { const taxCode = 'id_TAX_RATE_1'; const originalDefaultForeignCurrencyID = fakePolicy?.taxRates?.foreignTaxDefault; - mockFetch?.pause?.(); + mockFetch.pause(); Policy.setForeignCurrencyDefault(fakePolicy.id, taxCode); return waitForBatchedUpdates() .then( @@ -269,8 +268,8 @@ describe('actions/PolicyTax', () => { ) .then(() => { - mockFetch?.fail?.(); - return mockFetch?.resume?.() as Promise; + mockFetch.fail(); + return mockFetch.resume?.() as Promise; }) .then(waitForBatchedUpdates) .then( @@ -300,7 +299,7 @@ describe('actions/PolicyTax', () => { code: 'id_TAX_RATE_2', }; - mockFetch?.pause?.(); + mockFetch.pause(); createPolicyTax(fakePolicy.id, newTaxRate); return waitForBatchedUpdates() .then( @@ -321,7 +320,7 @@ describe('actions/PolicyTax', () => { }); }), ) - .then(mockFetch?.resume) + .then(mockFetch.resume) .then(waitForBatchedUpdates) .then( () => @@ -348,7 +347,7 @@ describe('actions/PolicyTax', () => { code: 'id_TAX_RATE_2', }; - mockFetch?.pause?.(); + mockFetch.pause(); createPolicyTax(fakePolicy.id, newTaxRate); return waitForBatchedUpdates() .then( @@ -370,8 +369,8 @@ describe('actions/PolicyTax', () => { }), ) .then(() => { - mockFetch?.fail?.(); - return mockFetch?.resume?.() as Promise; + mockFetch.fail(); + return mockFetch.resume?.() as Promise; }) .then(waitForBatchedUpdates) .then( @@ -394,7 +393,7 @@ describe('actions/PolicyTax', () => { describe('SetPolicyTaxesEnabled', () => { it('Disable policy`s taxes', () => { const disableTaxID = 'id_TAX_RATE_1'; - mockFetch?.pause?.(); + mockFetch.pause(); setPolicyTaxesEnabled(fakePolicy.id, [disableTaxID], false); return waitForBatchedUpdates() .then( @@ -415,7 +414,7 @@ describe('actions/PolicyTax', () => { }); }), ) - .then(mockFetch?.resume) + .then(mockFetch.resume) .then(waitForBatchedUpdates) .then( () => @@ -437,7 +436,7 @@ describe('actions/PolicyTax', () => { it('Disable policy`s taxes but API returns an error, then enable policy`s taxes again', () => { const disableTaxID = 'id_TAX_RATE_1'; - mockFetch?.pause?.(); + mockFetch.pause(); setPolicyTaxesEnabled(fakePolicy.id, [disableTaxID], false); const originalTaxes = {...fakePolicy?.taxRates?.taxes}; return waitForBatchedUpdates() @@ -460,8 +459,8 @@ describe('actions/PolicyTax', () => { }), ) .then(() => { - mockFetch?.fail?.(); - return mockFetch?.resume?.() as Promise; + mockFetch.fail(); + return mockFetch.resume?.() as Promise; }) .then(waitForBatchedUpdates) .then( @@ -488,7 +487,7 @@ describe('actions/PolicyTax', () => { it('Rename tax', () => { const taxID = 'id_TAX_RATE_1'; const newTaxName = 'Tax rate 1 updated'; - mockFetch?.pause?.(); + mockFetch.pause(); renamePolicyTax(fakePolicy.id, taxID, newTaxName); return waitForBatchedUpdates() .then( @@ -509,7 +508,7 @@ describe('actions/PolicyTax', () => { }); }), ) - .then(mockFetch?.resume) + .then(mockFetch.resume) .then(waitForBatchedUpdates) .then( () => @@ -533,7 +532,7 @@ describe('actions/PolicyTax', () => { const taxID = 'id_TAX_RATE_1'; const newTaxName = 'Tax rate 1 updated'; const originalTaxRate = {...fakePolicy?.taxRates?.taxes[taxID]}; - mockFetch?.pause?.(); + mockFetch.pause(); renamePolicyTax(fakePolicy.id, taxID, newTaxName); return waitForBatchedUpdates() .then( @@ -555,8 +554,8 @@ describe('actions/PolicyTax', () => { }), ) .then(() => { - mockFetch?.fail?.(); - return mockFetch?.resume?.() as Promise; + mockFetch.fail(); + return mockFetch.resume?.() as Promise; }) .then(waitForBatchedUpdates) .then( @@ -583,7 +582,7 @@ describe('actions/PolicyTax', () => { const taxID = 'id_TAX_RATE_1'; const newTaxValue = 10; const stringTaxValue = `${newTaxValue}%`; - mockFetch?.pause?.(); + mockFetch.pause(); updatePolicyTaxValue(fakePolicy.id, taxID, newTaxValue); return waitForBatchedUpdates() .then( @@ -604,7 +603,7 @@ describe('actions/PolicyTax', () => { }); }), ) - .then(mockFetch?.resume) + .then(mockFetch.resume) .then(waitForBatchedUpdates) .then( () => @@ -629,7 +628,7 @@ describe('actions/PolicyTax', () => { const newTaxValue = 10; const originalTaxRate = {...fakePolicy?.taxRates?.taxes[taxID]}; const stringTaxValue = `${newTaxValue}%`; - mockFetch?.pause?.(); + mockFetch.pause(); updatePolicyTaxValue(fakePolicy.id, taxID, newTaxValue); return waitForBatchedUpdates() .then( @@ -651,8 +650,8 @@ describe('actions/PolicyTax', () => { }), ) .then(() => { - mockFetch?.fail?.(); - return mockFetch?.resume?.() as Promise; + mockFetch.fail(); + return mockFetch.resume?.() as Promise; }) .then(waitForBatchedUpdates) .then( @@ -679,7 +678,7 @@ describe('actions/PolicyTax', () => { const foreignTaxDefault = fakePolicy?.taxRates?.foreignTaxDefault; const taxID = 'id_TAX_RATE_1'; - mockFetch?.pause?.(); + mockFetch.pause(); deletePolicyTaxes(fakePolicy.id, [taxID]); return waitForBatchedUpdates() .then( @@ -701,7 +700,7 @@ describe('actions/PolicyTax', () => { }); }), ) - .then(mockFetch?.resume) + .then(mockFetch.resume) .then(waitForBatchedUpdates) .then( () => @@ -726,7 +725,7 @@ describe('actions/PolicyTax', () => { const taxID = 'id_TAX_RATE_1'; const firstTaxID = 'id_TAX_EXEMPT'; - mockFetch?.pause?.(); + mockFetch.pause(); return Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, {taxRates: {foreignTaxDefault: 'id_TAX_RATE_1'}}) .then(() => { deletePolicyTaxes(fakePolicy.id, [taxID]); @@ -751,7 +750,7 @@ describe('actions/PolicyTax', () => { }); }), ) - .then(mockFetch?.resume) + .then(mockFetch.resume) .then(waitForBatchedUpdates) .then( () => @@ -776,7 +775,7 @@ describe('actions/PolicyTax', () => { const foreignTaxDefault = fakePolicy?.taxRates?.foreignTaxDefault; const taxID = 'id_TAX_RATE_1'; - mockFetch?.pause?.(); + mockFetch.pause(); deletePolicyTaxes(fakePolicy.id, [taxID]); return waitForBatchedUpdates() .then( @@ -799,8 +798,8 @@ describe('actions/PolicyTax', () => { }), ) .then(() => { - mockFetch?.fail?.(); - return mockFetch?.resume?.() as Promise; + mockFetch.fail(); + return mockFetch.resume?.() as Promise; }) .then(waitForBatchedUpdates) .then( diff --git a/tests/actions/PolicyTest.ts b/tests/actions/PolicyTest.ts index 6dae053afac7..b6721c1b5f29 100644 --- a/tests/actions/PolicyTest.ts +++ b/tests/actions/PolicyTest.ts @@ -23,14 +23,15 @@ describe('actions/Policy', () => { }); }); + let mockFetch: MockFetch; beforeEach(() => { - global.fetch = TestHelper.getGlobalFetchMock(); + mockFetch = TestHelper.getGlobalFetchMock() as MockFetch; return Onyx.clear().then(waitForBatchedUpdates); }); describe('createWorkspace', () => { it('creates a new workspace', async () => { - (fetch as MockFetch)?.pause?.(); + mockFetch.pause(); Onyx.set(ONYXKEYS.SESSION, {email: ESH_EMAIL, accountID: ESH_ACCOUNT_ID}); await waitForBatchedUpdates(); @@ -123,7 +124,7 @@ describe('actions/Policy', () => { }); // Check for success data - (fetch as MockFetch)?.resume?.(); + mockFetch.resume(); await waitForBatchedUpdates(); policy = await new Promise((resolve) => { diff --git a/tests/utils/TestHelper.ts b/tests/utils/TestHelper.ts index bd107ba6ed56..40ef19469c1b 100644 --- a/tests/utils/TestHelper.ts +++ b/tests/utils/TestHelper.ts @@ -9,10 +9,10 @@ import type {Response as OnyxResponse, PersonalDetails, Report} from '@src/types import waitForBatchedUpdates from './waitForBatchedUpdates'; type MockFetch = ReturnType & { - pause?: () => void; - fail?: () => void; - succeed?: () => void; - resume?: () => Promise; + pause: () => void; + fail: () => void; + succeed: () => void; + resume: () => Promise; }; type QueueItem = (value: Partial | PromiseLike>) => void; @@ -172,14 +172,14 @@ function getGlobalFetchMock() { json: () => Promise.resolve({jsonCode: 200}), }; - const mockFetch: MockFetch = jest.fn().mockImplementation(() => { + const mockFetch = jest.fn().mockImplementation(() => { if (!isPaused) { return Promise.resolve(getResponse()); } return new Promise((resolve) => { queue.push(resolve); }); - }); + }) as MockFetch; mockFetch.pause = () => (isPaused = true); mockFetch.resume = () => { From ce1658eadf3d05f62b49bfecdd1a6181f3cb6eaf Mon Sep 17 00:00:00 2001 From: Yauheni Date: Thu, 16 May 2024 12:57:56 +0200 Subject: [PATCH 3/7] Fix mockFetch methods --- tests/actions/IOUTest.ts | 24 ++++++++++++------------ tests/actions/PolicyCategoryTest.ts | 10 +++++----- tests/actions/PolicyMemberTest.ts | 8 ++++---- tests/actions/PolicyProfileTest.ts | 2 +- 4 files changed, 22 insertions(+), 22 deletions(-) diff --git a/tests/actions/IOUTest.ts b/tests/actions/IOUTest.ts index 0f9d888b549a..abd0e6998043 100644 --- a/tests/actions/IOUTest.ts +++ b/tests/actions/IOUTest.ts @@ -461,7 +461,7 @@ describe('actions/IOU', () => { }; let newIOUAction: OnyxEntry; let newTransaction: OnyxEntry; - mockFetch.pause?.(); + mockFetch.pause(); return Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${chatReportID}`, chatReport) .then(() => Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${iouReportID}`, iouReport)) .then(() => @@ -1592,7 +1592,7 @@ describe('actions/IOU', () => { const merchant = 'NASDAQ'; afterEach(() => { - mockFetch.resume?.(); + mockFetch.resume(); }); it('updates the IOU request and IOU report when offline', () => { @@ -1744,7 +1744,7 @@ describe('actions/IOU', () => { }), ) .then(() => { - mockFetch.resume?.(); + mockFetch.resume(); }); }); @@ -1903,7 +1903,7 @@ describe('actions/IOU', () => { const merchant = 'NASDAQ'; afterEach(() => { - mockFetch.resume?.(); + mockFetch.resume(); }); it('updates the expense request and expense report when paid while offline', () => { @@ -2256,7 +2256,7 @@ describe('actions/IOU', () => { expect(t).toBeFalsy(); // Given fetch operations are resumed - mockFetch.resume?.(); + mockFetch.resume(); await waitForBatchedUpdates(); // Then we recheck the IOU report action from the report actions collection @@ -2314,7 +2314,7 @@ describe('actions/IOU', () => { expect(report).toBeTruthy(); // Given the resumed fetch state - mockFetch.resume?.(); + mockFetch.resume(); await waitForBatchedUpdates(); report = await new Promise>((resolve) => { @@ -2392,7 +2392,7 @@ describe('actions/IOU', () => { expect(iouReport).toHaveProperty('chatReportID'); // Given the resumed fetch state - mockFetch.resume?.(); + mockFetch.resume(); allReports = await new Promise>((resolve) => { const connectionID = Onyx.connect({ @@ -2478,7 +2478,7 @@ describe('actions/IOU', () => { }); expect(report).toBeFalsy(); - mockFetch.resume?.(); + mockFetch.resume(); // Then After resuming fetch, the report for the given thread ID still does not exist report = await new Promise>((resolve) => { @@ -2669,7 +2669,7 @@ describe('actions/IOU', () => { // When fetch resumes // Then the transaction thread report should still exist - mockFetch.resume?.(); + mockFetch.resume(); await new Promise((resolve) => { const connectionID = Onyx.connect({ key: `${ONYXKEYS.COLLECTION.REPORT}${thread.reportID}`, @@ -2815,7 +2815,7 @@ describe('actions/IOU', () => { }); // When we resume fetch - mockFetch.resume?.(); + mockFetch.resume(); // Then we expect the moneyRequestPreview to show [Deleted expense] @@ -2879,7 +2879,7 @@ describe('actions/IOU', () => { expect(iouReport?.total).toBe(20000); // When we resume - mockFetch.resume?.(); + mockFetch.resume(); // Then we expect the IOU report and reportPreview to update with new totals expect(iouReport).toBeTruthy(); @@ -2980,7 +2980,7 @@ describe('actions/IOU', () => { expect(iouReport).toHaveProperty('reportID'); expect(iouReport).toHaveProperty('chatReportID'); - mockFetch.resume?.(); + mockFetch.resume(); allReports = await new Promise>((resolve) => { const connectionID = Onyx.connect({ diff --git a/tests/actions/PolicyCategoryTest.ts b/tests/actions/PolicyCategoryTest.ts index 1fdd7f43400f..9ac4006bfc80 100644 --- a/tests/actions/PolicyCategoryTest.ts +++ b/tests/actions/PolicyCategoryTest.ts @@ -46,7 +46,7 @@ describe('actions/PolicyCategory', () => { }, }); }); - await mockFetch.resume?.(); + await mockFetch.resume(); await waitForBatchedUpdates(); await new Promise((resolve) => { const connectionID = Onyx.connect({ @@ -87,7 +87,7 @@ describe('actions/PolicyCategory', () => { }, }); }); - await mockFetch.resume?.(); + await mockFetch.resume(); await waitForBatchedUpdates(); await new Promise((resolve) => { const connectionID = Onyx.connect({ @@ -136,7 +136,7 @@ describe('actions/PolicyCategory', () => { }, }); }); - await mockFetch.resume?.(); + await mockFetch.resume(); await waitForBatchedUpdates(); await new Promise((resolve) => { const connectionID = Onyx.connect({ @@ -185,7 +185,7 @@ describe('actions/PolicyCategory', () => { }, }); }); - await mockFetch.resume?.(); + await mockFetch.resume(); await waitForBatchedUpdates(); await new Promise((resolve) => { const connectionID = Onyx.connect({ @@ -227,7 +227,7 @@ describe('actions/PolicyCategory', () => { }, }); }); - await mockFetch.resume?.(); + await mockFetch.resume(); await waitForBatchedUpdates(); await new Promise((resolve) => { const connectionID = Onyx.connect({ diff --git a/tests/actions/PolicyMemberTest.ts b/tests/actions/PolicyMemberTest.ts index 5a025829b2b4..40e7fe34c04a 100644 --- a/tests/actions/PolicyMemberTest.ts +++ b/tests/actions/PolicyMemberTest.ts @@ -67,7 +67,7 @@ describe('actions/PolicyMember', () => { }, }); }); - await mockFetch.resume?.(); + await mockFetch.resume(); await waitForBatchedUpdates(); await new Promise((resolve) => { const connectionID = Onyx.connect({ @@ -119,7 +119,7 @@ describe('actions/PolicyMember', () => { }, }); }); - await mockFetch.resume?.(); + await mockFetch.resume(); await waitForBatchedUpdates(); await new Promise((resolve) => { const connectionID = Onyx.connect({ @@ -160,7 +160,7 @@ describe('actions/PolicyMember', () => { }, }); }); - await mockFetch.resume?.(); + await mockFetch.resume(); await waitForBatchedUpdates(); await new Promise((resolve) => { const connectionID = Onyx.connect({ @@ -211,7 +211,7 @@ describe('actions/PolicyMember', () => { }, }); }); - await mockFetch.resume?.(); + await mockFetch.resume(); await waitForBatchedUpdates(); await new Promise((resolve) => { const connectionID = Onyx.connect({ diff --git a/tests/actions/PolicyProfileTest.ts b/tests/actions/PolicyProfileTest.ts index 568b2af92dfb..5698c8300dec 100644 --- a/tests/actions/PolicyProfileTest.ts +++ b/tests/actions/PolicyProfileTest.ts @@ -48,7 +48,7 @@ describe('actions/PolicyProfile', () => { }, }); }); - await mockFetch.resume?.(); + await mockFetch.resume(); await waitForBatchedUpdates(); await new Promise((resolve) => { const connectionID = Onyx.connect({ From 7ada5ba423bd4aab4d56216c94c8cb4067f08d32 Mon Sep 17 00:00:00 2001 From: Yauheni Date: Thu, 16 May 2024 13:13:41 +0200 Subject: [PATCH 4/7] Test new changes --- tests/actions/PolicyTest.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/actions/PolicyTest.ts b/tests/actions/PolicyTest.ts index b6721c1b5f29..8e3e1ac0b862 100644 --- a/tests/actions/PolicyTest.ts +++ b/tests/actions/PolicyTest.ts @@ -109,7 +109,7 @@ describe('actions/Policy', () => { }); }); - // Each of the three reports should have a a `CREATED` action. + // Each of the three reports should have a a `CREATED` action let adminReportActions: ReportAction[] = Object.values(reportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${adminReportID}`] ?? {}); let announceReportActions: ReportAction[] = Object.values(reportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${announceReportID}`] ?? {}); let expenseReportActions: ReportAction[] = Object.values(reportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${expenseReportID}`] ?? {}); From 6f28159b0149820d06276e4b48956599da70e7d5 Mon Sep 17 00:00:00 2001 From: Yauheni Date: Thu, 16 May 2024 13:48:10 +0200 Subject: [PATCH 5/7] Update tests --- tests/actions/IOUTest.ts | 3 ++- tests/actions/PolicyCategoryTest.ts | 3 ++- tests/actions/PolicyMemberTest.ts | 3 ++- tests/actions/PolicyProfileTest.ts | 7 ++++--- tests/actions/PolicyTagTest.ts | 3 ++- tests/actions/PolicyTaxTest.ts | 3 ++- tests/actions/PolicyTest.ts | 7 +++---- 7 files changed, 17 insertions(+), 12 deletions(-) diff --git a/tests/actions/IOUTest.ts b/tests/actions/IOUTest.ts index abd0e6998043..543f04972bb5 100644 --- a/tests/actions/IOUTest.ts +++ b/tests/actions/IOUTest.ts @@ -63,7 +63,8 @@ describe('actions/IOU', () => { let mockFetch: MockFetch; beforeEach(() => { - mockFetch = TestHelper.getGlobalFetchMock() as MockFetch; + global.fetch = TestHelper.getGlobalFetchMock(); + mockFetch = fetch as MockFetch; return Onyx.clear().then(waitForBatchedUpdates); }); diff --git a/tests/actions/PolicyCategoryTest.ts b/tests/actions/PolicyCategoryTest.ts index 9ac4006bfc80..9d4a644d56cd 100644 --- a/tests/actions/PolicyCategoryTest.ts +++ b/tests/actions/PolicyCategoryTest.ts @@ -19,7 +19,8 @@ describe('actions/PolicyCategory', () => { let mockFetch: MockFetch; beforeEach(() => { - mockFetch = TestHelper.getGlobalFetchMock() as MockFetch; + global.fetch = TestHelper.getGlobalFetchMock(); + mockFetch = fetch as MockFetch; return Onyx.clear().then(waitForBatchedUpdates); }); diff --git a/tests/actions/PolicyMemberTest.ts b/tests/actions/PolicyMemberTest.ts index 40e7fe34c04a..6c8e216cd33f 100644 --- a/tests/actions/PolicyMemberTest.ts +++ b/tests/actions/PolicyMemberTest.ts @@ -24,7 +24,8 @@ describe('actions/PolicyMember', () => { let mockFetch: MockFetch; beforeEach(() => { - mockFetch = TestHelper.getGlobalFetchMock() as MockFetch; + global.fetch = TestHelper.getGlobalFetchMock(); + mockFetch = fetch as MockFetch; return Onyx.clear().then(waitForBatchedUpdates); }); diff --git a/tests/actions/PolicyProfileTest.ts b/tests/actions/PolicyProfileTest.ts index 5698c8300dec..a6bc0ea541bc 100644 --- a/tests/actions/PolicyProfileTest.ts +++ b/tests/actions/PolicyProfileTest.ts @@ -19,7 +19,8 @@ describe('actions/PolicyProfile', () => { let mockFetch: MockFetch; beforeEach(() => { - mockFetch = TestHelper.getGlobalFetchMock() as MockFetch; + global.fetch = TestHelper.getGlobalFetchMock(); + mockFetch = fetch as MockFetch; return Onyx.clear().then(waitForBatchedUpdates); }); @@ -30,7 +31,7 @@ describe('actions/PolicyProfile', () => { const oldDescription = fakePolicy.description ?? ''; const newDescription = 'Updated description'; const parsedDescription = ReportUtils.getParsedComment(newDescription); - mockFetch.pause(); + mockFetch?.pause?.(); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy); Policy.updateWorkspaceDescription(fakePolicy.id, newDescription, oldDescription); await waitForBatchedUpdates(); @@ -48,7 +49,7 @@ describe('actions/PolicyProfile', () => { }, }); }); - await mockFetch.resume(); + await mockFetch?.resume?.(); await waitForBatchedUpdates(); await new Promise((resolve) => { const connectionID = Onyx.connect({ diff --git a/tests/actions/PolicyTagTest.ts b/tests/actions/PolicyTagTest.ts index db7932063b92..ac71f9290079 100644 --- a/tests/actions/PolicyTagTest.ts +++ b/tests/actions/PolicyTagTest.ts @@ -20,7 +20,8 @@ describe('actions/Policy', () => { let mockFetch: MockFetch; beforeEach(() => { - mockFetch = TestHelper.getGlobalFetchMock() as MockFetch; + global.fetch = TestHelper.getGlobalFetchMock(); + mockFetch = fetch as MockFetch; return Onyx.clear().then(waitForBatchedUpdates); }); diff --git a/tests/actions/PolicyTaxTest.ts b/tests/actions/PolicyTaxTest.ts index b6a1c9b68a1a..b99901d01439 100644 --- a/tests/actions/PolicyTaxTest.ts +++ b/tests/actions/PolicyTaxTest.ts @@ -21,7 +21,8 @@ describe('actions/PolicyTax', () => { let mockFetch: MockFetch; beforeEach(() => { - mockFetch = TestHelper.getGlobalFetchMock() as MockFetch; + global.fetch = TestHelper.getGlobalFetchMock(); + mockFetch = fetch as MockFetch; return Onyx.clear() .then(() => Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy)) .then(waitForBatchedUpdates); diff --git a/tests/actions/PolicyTest.ts b/tests/actions/PolicyTest.ts index 8e3e1ac0b862..d90defce99f6 100644 --- a/tests/actions/PolicyTest.ts +++ b/tests/actions/PolicyTest.ts @@ -23,15 +23,14 @@ describe('actions/Policy', () => { }); }); - let mockFetch: MockFetch; beforeEach(() => { - mockFetch = TestHelper.getGlobalFetchMock() as MockFetch; + global.fetch = TestHelper.getGlobalFetchMock(); return Onyx.clear().then(waitForBatchedUpdates); }); describe('createWorkspace', () => { it('creates a new workspace', async () => { - mockFetch.pause(); + (fetch as MockFetch).pause(); Onyx.set(ONYXKEYS.SESSION, {email: ESH_EMAIL, accountID: ESH_ACCOUNT_ID}); await waitForBatchedUpdates(); @@ -124,7 +123,7 @@ describe('actions/Policy', () => { }); // Check for success data - mockFetch.resume(); + (fetch as MockFetch).resume(); await waitForBatchedUpdates(); policy = await new Promise((resolve) => { From 39c9d9b458325a8a6f99bbd2d3120a206972c959 Mon Sep 17 00:00:00 2001 From: Yauheni Date: Thu, 16 May 2024 14:26:34 +0200 Subject: [PATCH 6/7] Reverte changes --- tests/actions/IOUTest.ts | 76 +++++++++++++------------- tests/actions/PolicyCategoryTest.ts | 20 +++---- tests/actions/PolicyMemberTest.ts | 16 +++--- tests/actions/PolicyTagTest.ts | 64 +++++++++++----------- tests/actions/PolicyTaxTest.ts | 84 ++++++++++++++--------------- tests/actions/PolicyTest.ts | 9 ++-- tests/utils/TestHelper.ts | 8 +-- 7 files changed, 139 insertions(+), 138 deletions(-) diff --git a/tests/actions/IOUTest.ts b/tests/actions/IOUTest.ts index 543f04972bb5..ac320729b2b7 100644 --- a/tests/actions/IOUTest.ts +++ b/tests/actions/IOUTest.ts @@ -79,7 +79,7 @@ describe('actions/IOU', () => { let transactionID: string | undefined; let transactionThread: OnyxEntry; let transactionThreadCreatedAction: OnyxEntry; - mockFetch.pause(); + mockFetch?.pause?.(); IOU.requestMoney({reportID: ''}, amount, CONST.CURRENCY.USD, '', merchant, RORY_EMAIL, RORY_ACCOUNT_ID, {login: CARLOS_EMAIL, accountID: CARLOS_ACCOUNT_ID}, comment, {}); return waitForBatchedUpdates() .then( @@ -218,7 +218,7 @@ describe('actions/IOU', () => { }); }), ) - .then(mockFetch.resume) + .then(mockFetch?.resume) .then( () => new Promise((resolve) => { @@ -267,7 +267,7 @@ describe('actions/IOU', () => { let iouAction: OnyxEntry; let iouCreatedAction: OnyxEntry; let transactionID: string | undefined; - mockFetch.pause(); + mockFetch?.pause?.(); return Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${chatReport.reportID}`, chatReport) .then(() => Onyx.set(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReport.reportID}`, { @@ -378,7 +378,7 @@ describe('actions/IOU', () => { }); }), ) - .then(mockFetch.resume) + .then(mockFetch?.resume) .then(waitForBatchedUpdates) .then( () => @@ -462,7 +462,7 @@ describe('actions/IOU', () => { }; let newIOUAction: OnyxEntry; let newTransaction: OnyxEntry; - mockFetch.pause(); + mockFetch?.pause?.(); return Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${chatReportID}`, chatReport) .then(() => Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${iouReportID}`, iouReport)) .then(() => @@ -567,7 +567,7 @@ describe('actions/IOU', () => { }); }), ) - .then(mockFetch.resume) + .then(mockFetch?.resume) .then(waitForNetworkPromises) .then( () => @@ -610,7 +610,7 @@ describe('actions/IOU', () => { let transactionID: string; let transactionThreadReport: OnyxEntry; let transactionThreadAction: OnyxEntry; - mockFetch.pause(); + mockFetch?.pause?.(); IOU.requestMoney({reportID: ''}, amount, CONST.CURRENCY.USD, '', '', RORY_EMAIL, RORY_ACCOUNT_ID, {login: CARLOS_EMAIL, accountID: CARLOS_ACCOUNT_ID}, comment, {}); return ( waitForBatchedUpdates() @@ -721,8 +721,8 @@ describe('actions/IOU', () => { }), ) .then((): Promise => { - mockFetch.fail(); - return mockFetch.resume?.() as Promise; + mockFetch?.fail?.(); + return mockFetch?.resume?.() as Promise; }) .then( () => @@ -910,7 +910,7 @@ describe('actions/IOU', () => { ) // Cleanup - .then(mockFetch.succeed) + .then(mockFetch?.succeed) ); }); }); @@ -1061,7 +1061,7 @@ describe('actions/IOU', () => { .then(() => Onyx.set(`${ONYXKEYS.COLLECTION.TRANSACTION}${julesExistingTransaction?.transactionID}`, julesExistingTransaction)) .then(() => { // When we split a bill offline - mockFetch.pause(); + mockFetch?.pause?.(); IOU.splitBill( // TODO: Migrate after the backend accepts accountIDs { @@ -1343,7 +1343,7 @@ describe('actions/IOU', () => { }); }), ) - .then(mockFetch.resume) + .then(mockFetch?.resume) .then(waitForNetworkPromises) .then( () => @@ -1480,7 +1480,7 @@ describe('actions/IOU', () => { }), ) .then(() => { - mockFetch.pause(); + mockFetch?.pause?.(); if (chatReport && iouReport) { IOU.payMoneyRequest(CONST.IOU.PAYMENT_TYPE.ELSEWHERE, chatReport, iouReport); } @@ -1535,7 +1535,7 @@ describe('actions/IOU', () => { }); }), ) - .then(mockFetch.resume) + .then(mockFetch?.resume) .then( () => new Promise((resolve) => { @@ -1593,7 +1593,7 @@ describe('actions/IOU', () => { const merchant = 'NASDAQ'; afterEach(() => { - mockFetch.resume(); + mockFetch?.resume?.(); }); it('updates the IOU request and IOU report when offline', () => { @@ -1602,7 +1602,7 @@ describe('actions/IOU', () => { let iouAction: OnyxEntry = null; let transaction: OnyxEntry = null; - mockFetch.pause(); + mockFetch?.pause?.(); IOU.requestMoney({reportID: ''}, amount, CONST.CURRENCY.USD, '', merchant, RORY_EMAIL, RORY_ACCOUNT_ID, {login: CARLOS_EMAIL, accountID: CARLOS_ACCOUNT_ID}, comment, {}); return waitForBatchedUpdates() .then(() => { @@ -1745,7 +1745,7 @@ describe('actions/IOU', () => { }), ) .then(() => { - mockFetch.resume(); + mockFetch?.resume?.(); }); }); @@ -1811,7 +1811,7 @@ describe('actions/IOU', () => { return waitForBatchedUpdates(); }) .then(() => { - mockFetch.fail(); + mockFetch?.fail?.(); if (transaction) { IOU.editMoneyRequest( @@ -1904,14 +1904,14 @@ describe('actions/IOU', () => { const merchant = 'NASDAQ'; afterEach(() => { - mockFetch.resume(); + mockFetch?.resume?.(); }); it('updates the expense request and expense report when paid while offline', () => { let expenseReport: OnyxEntry; let chatReport: OnyxEntry; - mockFetch.pause(); + mockFetch?.pause?.(); Onyx.set(ONYXKEYS.SESSION, {email: CARLOS_EMAIL, accountID: CARLOS_ACCOUNT_ID}); return waitForBatchedUpdates() .then(() => { @@ -2068,7 +2068,7 @@ describe('actions/IOU', () => { }), ) .then(() => { - mockFetch.fail(); + mockFetch?.fail?.(); if (chatReport && expenseReport) { IOU.payMoneyRequest('ACH', chatReport, expenseReport); } @@ -2218,7 +2218,7 @@ describe('actions/IOU', () => { it('delete an expense (IOU Action and transaction) successfully', async () => { // Given the fetch operations are paused and an expense is initiated - mockFetch.pause(); + mockFetch?.pause?.(); if (transaction && createIOUAction) { // When the expense is deleted @@ -2257,7 +2257,7 @@ describe('actions/IOU', () => { expect(t).toBeFalsy(); // Given fetch operations are resumed - mockFetch.resume(); + mockFetch?.resume?.(); await waitForBatchedUpdates(); // Then we recheck the IOU report action from the report actions collection @@ -2292,7 +2292,7 @@ describe('actions/IOU', () => { it('delete the IOU report when there are no visible comments left in the IOU report', async () => { // Given an IOU report and a paused fetch state - mockFetch.pause(); + mockFetch?.pause?.(); if (transaction && createIOUAction) { // When the IOU expense is deleted @@ -2315,7 +2315,7 @@ describe('actions/IOU', () => { expect(report).toBeTruthy(); // Given the resumed fetch state - mockFetch.resume(); + mockFetch?.resume?.(); await waitForBatchedUpdates(); report = await new Promise>((resolve) => { @@ -2367,7 +2367,7 @@ describe('actions/IOU', () => { expect(resultActionAfterUpdate?.pendingAction).toBeUndefined(); // When we attempt to delete an expense from the IOU report - mockFetch.pause(); + mockFetch?.pause?.(); if (transaction && createIOUAction) { IOU.deleteMoneyRequest(transaction?.transactionID, createIOUAction, false); } @@ -2393,7 +2393,7 @@ describe('actions/IOU', () => { expect(iouReport).toHaveProperty('chatReportID'); // Given the resumed fetch state - mockFetch.resume(); + mockFetch?.resume?.(); allReports = await new Promise>((resolve) => { const connectionID = Onyx.connect({ @@ -2457,7 +2457,7 @@ describe('actions/IOU', () => { await waitForBatchedUpdates(); // Given Fetch is paused and timers have advanced - mockFetch.pause(); + mockFetch?.pause?.(); jest.advanceTimersByTime(10); if (transaction && createIOUAction) { @@ -2479,7 +2479,7 @@ describe('actions/IOU', () => { }); expect(report).toBeFalsy(); - mockFetch.resume(); + mockFetch?.resume?.(); // Then After resuming fetch, the report for the given thread ID still does not exist report = await new Promise>((resolve) => { @@ -2647,7 +2647,7 @@ describe('actions/IOU', () => { const resultActionAfter = reportActions?.[reportActionID]; expect(resultActionAfter?.pendingAction).toBeUndefined(); - mockFetch.pause(); + mockFetch?.pause?.(); if (transaction && createIOUAction) { // When deleting expense @@ -2670,7 +2670,7 @@ describe('actions/IOU', () => { // When fetch resumes // Then the transaction thread report should still exist - mockFetch.resume(); + mockFetch?.resume?.(); await new Promise((resolve) => { const connectionID = Onyx.connect({ key: `${ONYXKEYS.COLLECTION.REPORT}${thread.reportID}`, @@ -2793,7 +2793,7 @@ describe('actions/IOU', () => { // Verify that our action is no longer in the loading state expect(resultActionAfterUpdate?.pendingAction).toBeUndefined(); - mockFetch.pause(); + mockFetch?.pause?.(); if (transaction && createIOUAction) { // When we delete the expense IOU.deleteMoneyRequest(transaction.transactionID, createIOUAction, false); @@ -2816,7 +2816,7 @@ describe('actions/IOU', () => { }); // When we resume fetch - mockFetch.resume(); + mockFetch?.resume?.(); // Then we expect the moneyRequestPreview to show [Deleted expense] @@ -2865,7 +2865,7 @@ describe('actions/IOU', () => { expect(ioupreview?.message?.[0]?.text).toBe('rory@expensifail.com owes $300.00'); // When we delete the first expense - mockFetch.pause(); + mockFetch?.pause?.(); jest.advanceTimersByTime(10); if (transaction && createIOUAction) { IOU.deleteMoneyRequest(transaction.transactionID, createIOUAction, false); @@ -2880,7 +2880,7 @@ describe('actions/IOU', () => { expect(iouReport?.total).toBe(20000); // When we resume - mockFetch.resume(); + mockFetch?.resume?.(); // Then we expect the IOU report and reportPreview to update with new totals expect(iouReport).toBeTruthy(); @@ -2956,7 +2956,7 @@ describe('actions/IOU', () => { // When we delete the expense in SingleTransactionView and we should not delete the IOU report - mockFetch.pause(); + mockFetch?.pause?.(); if (transaction && createIOUAction) { IOU.deleteMoneyRequest(transaction.transactionID, createIOUAction, true); @@ -2981,7 +2981,7 @@ describe('actions/IOU', () => { expect(iouReport).toHaveProperty('reportID'); expect(iouReport).toHaveProperty('chatReportID'); - mockFetch.resume(); + mockFetch?.resume?.(); allReports = await new Promise>((resolve) => { const connectionID = Onyx.connect({ @@ -3307,7 +3307,7 @@ describe('actions/IOU', () => { }), ) .then(() => { - mockFetch.fail(); + mockFetch?.fail?.(); if (expenseReport) { IOU.submitReport(expenseReport); } diff --git a/tests/actions/PolicyCategoryTest.ts b/tests/actions/PolicyCategoryTest.ts index 9d4a644d56cd..7c060b40467e 100644 --- a/tests/actions/PolicyCategoryTest.ts +++ b/tests/actions/PolicyCategoryTest.ts @@ -29,7 +29,7 @@ describe('actions/PolicyCategory', () => { const fakePolicy = createRandomPolicy(0); fakePolicy.requiresCategory = false; - mockFetch.pause(); + mockFetch?.pause?.(); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy); Policy.setWorkspaceRequiresCategory(fakePolicy.id, true); await waitForBatchedUpdates(); @@ -47,7 +47,7 @@ describe('actions/PolicyCategory', () => { }, }); }); - await mockFetch.resume(); + await mockFetch?.resume?.(); await waitForBatchedUpdates(); await new Promise((resolve) => { const connectionID = Onyx.connect({ @@ -68,7 +68,7 @@ describe('actions/PolicyCategory', () => { const fakePolicy = createRandomPolicy(0); const fakeCategories = createRandomPolicyCategories(3); const newCategoryName = 'New category'; - mockFetch.pause(); + mockFetch?.pause?.(); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${fakePolicy.id}`, fakeCategories); Policy.createPolicyCategory(fakePolicy.id, newCategoryName); @@ -88,7 +88,7 @@ describe('actions/PolicyCategory', () => { }, }); }); - await mockFetch.resume(); + await mockFetch?.resume?.(); await waitForBatchedUpdates(); await new Promise((resolve) => { const connectionID = Onyx.connect({ @@ -113,7 +113,7 @@ describe('actions/PolicyCategory', () => { const fakeCategories = createRandomPolicyCategories(3); const oldCategoryName = Object.keys(fakeCategories)[0]; const newCategoryName = 'Updated category'; - mockFetch.pause(); + mockFetch?.pause?.(); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${fakePolicy.id}`, fakeCategories); Policy.renamePolicyCategory(fakePolicy.id, { @@ -137,7 +137,7 @@ describe('actions/PolicyCategory', () => { }, }); }); - await mockFetch.resume(); + await mockFetch?.resume?.(); await waitForBatchedUpdates(); await new Promise((resolve) => { const connectionID = Onyx.connect({ @@ -166,7 +166,7 @@ describe('actions/PolicyCategory', () => { enabled: true, }, }; - mockFetch.pause(); + mockFetch?.pause?.(); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${fakePolicy.id}`, fakeCategories); Policy.setWorkspaceCategoryEnabled(fakePolicy.id, categoriesToUpdate); @@ -186,7 +186,7 @@ describe('actions/PolicyCategory', () => { }, }); }); - await mockFetch.resume(); + await mockFetch?.resume?.(); await waitForBatchedUpdates(); await new Promise((resolve) => { const connectionID = Onyx.connect({ @@ -211,7 +211,7 @@ describe('actions/PolicyCategory', () => { const fakeCategories = createRandomPolicyCategories(3); const categoryNameToDelete = Object.keys(fakeCategories)[0]; const categoriesToDelete = [categoryNameToDelete]; - mockFetch.pause(); + mockFetch?.pause?.(); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${fakePolicy.id}`, fakeCategories); Policy.deleteWorkspaceCategories(fakePolicy.id, categoriesToDelete); @@ -228,7 +228,7 @@ describe('actions/PolicyCategory', () => { }, }); }); - await mockFetch.resume(); + await mockFetch?.resume?.(); await waitForBatchedUpdates(); await new Promise((resolve) => { const connectionID = Onyx.connect({ diff --git a/tests/actions/PolicyMemberTest.ts b/tests/actions/PolicyMemberTest.ts index 6c8e216cd33f..4f3afc51ec66 100644 --- a/tests/actions/PolicyMemberTest.ts +++ b/tests/actions/PolicyMemberTest.ts @@ -41,7 +41,7 @@ describe('actions/PolicyMember', () => { actionName: CONST.REPORT.ACTIONS.TYPE.ACTIONABLE_JOIN_REQUEST, } as ReportAction; - mockFetch.pause(); + mockFetch?.pause?.(); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy); Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${fakeReport.reportID}`, fakeReport); Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${fakeReport.reportID}`, { @@ -68,7 +68,7 @@ describe('actions/PolicyMember', () => { }, }); }); - await mockFetch.resume(); + await mockFetch?.resume?.(); await waitForBatchedUpdates(); await new Promise((resolve) => { const connectionID = Onyx.connect({ @@ -101,7 +101,7 @@ describe('actions/PolicyMember', () => { }, }; - mockFetch.pause(); + mockFetch?.pause?.(); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy); Onyx.set(`${ONYXKEYS.PERSONAL_DETAILS_LIST}`, {[fakeUser2.accountID]: fakeUser2}); await waitForBatchedUpdates(); @@ -120,7 +120,7 @@ describe('actions/PolicyMember', () => { }, }); }); - await mockFetch.resume(); + await mockFetch?.resume?.(); await waitForBatchedUpdates(); await new Promise((resolve) => { const connectionID = Onyx.connect({ @@ -142,7 +142,7 @@ describe('actions/PolicyMember', () => { const fakeEmail = 'fake@gmail.com'; const fakeAccountID = 1; - mockFetch.pause(); + mockFetch?.pause?.(); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy); Onyx.merge(ONYXKEYS.SESSION, {email: fakeEmail, accountID: fakeAccountID}); Policy.requestWorkspaceOwnerChange(fakePolicy.id); @@ -161,7 +161,7 @@ describe('actions/PolicyMember', () => { }, }); }); - await mockFetch.resume(); + await mockFetch?.resume?.(); await waitForBatchedUpdates(); await new Promise((resolve) => { const connectionID = Onyx.connect({ @@ -193,7 +193,7 @@ describe('actions/PolicyMember', () => { }; const fakeAccountID = 1; - mockFetch.pause(); + mockFetch?.pause?.(); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy); Onyx.merge(ONYXKEYS.SESSION, {email: fakeEmail, accountID: fakeAccountID}); Policy.addBillingCardAndRequestPolicyOwnerChange(fakePolicy.id, fakeCard); @@ -212,7 +212,7 @@ describe('actions/PolicyMember', () => { }, }); }); - await mockFetch.resume(); + await mockFetch?.resume?.(); await waitForBatchedUpdates(); await new Promise((resolve) => { const connectionID = Onyx.connect({ diff --git a/tests/actions/PolicyTagTest.ts b/tests/actions/PolicyTagTest.ts index ac71f9290079..74ea13f3d139 100644 --- a/tests/actions/PolicyTagTest.ts +++ b/tests/actions/PolicyTagTest.ts @@ -30,7 +30,7 @@ describe('actions/Policy', () => { const fakePolicy = createRandomPolicy(0); fakePolicy.requiresTag = false; - mockFetch.pause(); + mockFetch?.pause?.(); return Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy) .then(() => { @@ -55,7 +55,7 @@ describe('actions/Policy', () => { }); }), ) - .then(mockFetch.resume) + .then(mockFetch?.resume) .then(waitForBatchedUpdates) .then( () => @@ -77,7 +77,7 @@ describe('actions/Policy', () => { const fakePolicy = createRandomPolicy(0); fakePolicy.requiresTag = true; - mockFetch.pause(); + mockFetch?.pause?.(); return Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy) .then(() => { @@ -102,7 +102,7 @@ describe('actions/Policy', () => { }); }), ) - .then(mockFetch.resume) + .then(mockFetch?.resume) .then(waitForBatchedUpdates) .then( () => @@ -124,16 +124,16 @@ describe('actions/Policy', () => { const fakePolicy = createRandomPolicy(0); fakePolicy.requiresTag = true; - mockFetch.pause(); + mockFetch?.pause?.(); return Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy) .then(() => { - mockFetch.fail(); + mockFetch?.fail?.(); Policy.setPolicyRequiresTag(fakePolicy.id, false); return waitForBatchedUpdates(); }) - .then(mockFetch.resume) + .then(mockFetch?.resume) .then(waitForBatchedUpdates) .then( () => @@ -163,7 +163,7 @@ describe('actions/Policy', () => { const newTagListName = 'New tag list name'; const fakePolicyTags = createRandomPolicyTags(oldTagListName); - mockFetch.pause(); + mockFetch?.pause?.(); return Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy) .then(() => { @@ -199,7 +199,7 @@ describe('actions/Policy', () => { }); }), ) - .then(mockFetch.resume) + .then(mockFetch?.resume) .then(waitForBatchedUpdates) .then( () => @@ -228,14 +228,14 @@ describe('actions/Policy', () => { const newTagListName = 'New tag list name'; const fakePolicyTags = createRandomPolicyTags(oldTagListName); - mockFetch.pause(); + mockFetch?.pause?.(); return Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy) .then(() => { Onyx.set(`${ONYXKEYS.COLLECTION.POLICY_TAGS}${fakePolicy.id}`, fakePolicyTags); }) .then(() => { - mockFetch.fail(); + mockFetch?.fail?.(); Policy.renamePolicyTaglist( fakePolicy.id, @@ -247,7 +247,7 @@ describe('actions/Policy', () => { ); return waitForBatchedUpdates(); }) - .then(mockFetch.resume) + .then(mockFetch?.resume) .then(waitForBatchedUpdates) .then( () => @@ -279,7 +279,7 @@ describe('actions/Policy', () => { const newTagName = 'new tag'; const fakePolicyTags = createRandomPolicyTags(tagListName); - mockFetch.pause(); + mockFetch?.pause?.(); return Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy) .then(() => { @@ -309,7 +309,7 @@ describe('actions/Policy', () => { }); }), ) - .then(mockFetch.resume) + .then(mockFetch?.resume) .then(waitForBatchedUpdates) .then( () => @@ -339,19 +339,19 @@ describe('actions/Policy', () => { const newTagName = 'new tag'; const fakePolicyTags = createRandomPolicyTags(tagListName); - mockFetch.pause(); + mockFetch?.pause?.(); return Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy) .then(() => { Onyx.set(`${ONYXKEYS.COLLECTION.POLICY_TAGS}${fakePolicy.id}`, fakePolicyTags); }) .then(() => { - mockFetch.fail(); + mockFetch?.fail?.(); Policy.createPolicyTag(fakePolicy.id, newTagName); return waitForBatchedUpdates(); }) - .then(mockFetch.resume) + .then(mockFetch?.resume) .then(waitForBatchedUpdates) .then( () => @@ -388,7 +388,7 @@ describe('actions/Policy', () => { return acc; }, {}); - mockFetch.pause(); + mockFetch?.pause?.(); return Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy) .then(() => { @@ -420,7 +420,7 @@ describe('actions/Policy', () => { }); }), ) - .then(mockFetch.resume) + .then(mockFetch?.resume) .then(waitForBatchedUpdates) .then( () => @@ -459,19 +459,19 @@ describe('actions/Policy', () => { return acc; }, {}); - mockFetch.pause(); + mockFetch?.pause?.(); return Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy) .then(() => { Onyx.set(`${ONYXKEYS.COLLECTION.POLICY_TAGS}${fakePolicy.id}`, fakePolicyTags); }) .then(() => { - mockFetch.fail(); + mockFetch?.fail?.(); Policy.setWorkspaceTagEnabled(fakePolicy.id, tagsToUpdate); return waitForBatchedUpdates(); }) - .then(mockFetch.resume) + .then(mockFetch?.resume) .then(waitForBatchedUpdates) .then( () => @@ -507,7 +507,7 @@ describe('actions/Policy', () => { const oldTagName = Object.keys(fakePolicyTags?.[tagListName]?.tags)[0]; const newTagName = 'New tag'; - mockFetch.pause(); + mockFetch?.pause?.(); return Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy) .then(() => { @@ -540,7 +540,7 @@ describe('actions/Policy', () => { }); }), ) - .then(mockFetch.resume) + .then(mockFetch?.resume) .then(waitForBatchedUpdates) .then( () => @@ -571,14 +571,14 @@ describe('actions/Policy', () => { const oldTagName = Object.keys(fakePolicyTags?.[tagListName]?.tags)[0]; const newTagName = 'New tag'; - mockFetch.pause(); + mockFetch?.pause?.(); return Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy) .then(() => { Onyx.set(`${ONYXKEYS.COLLECTION.POLICY_TAGS}${fakePolicy.id}`, fakePolicyTags); }) .then(() => { - mockFetch.fail(); + mockFetch?.fail?.(); Policy.renamePolicyTag(fakePolicy.id, { oldName: oldTagName, @@ -586,7 +586,7 @@ describe('actions/Policy', () => { }); return waitForBatchedUpdates(); }) - .then(mockFetch.resume) + .then(mockFetch?.resume) .then(waitForBatchedUpdates) .then( () => @@ -618,7 +618,7 @@ describe('actions/Policy', () => { const fakePolicyTags = createRandomPolicyTags(tagListName, 2); const tagsToDelete = Object.keys(fakePolicyTags?.[tagListName]?.tags ?? {}); - mockFetch.pause(); + mockFetch?.pause?.(); return Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy) .then(() => { @@ -646,7 +646,7 @@ describe('actions/Policy', () => { }); }), ) - .then(mockFetch.resume) + .then(mockFetch?.resume) .then(waitForBatchedUpdates) .then( () => @@ -676,19 +676,19 @@ describe('actions/Policy', () => { const fakePolicyTags = createRandomPolicyTags(tagListName, 2); const tagsToDelete = Object.keys(fakePolicyTags?.[tagListName]?.tags ?? {}); - mockFetch.pause(); + mockFetch?.pause?.(); return Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy) .then(() => { Onyx.set(`${ONYXKEYS.COLLECTION.POLICY_TAGS}${fakePolicy.id}`, fakePolicyTags); }) .then(() => { - mockFetch.fail(); + mockFetch?.fail?.(); Policy.deletePolicyTags(fakePolicy.id, tagsToDelete); return waitForBatchedUpdates(); }) - .then(mockFetch.resume) + .then(mockFetch?.resume) .then(waitForBatchedUpdates) .then( () => diff --git a/tests/actions/PolicyTaxTest.ts b/tests/actions/PolicyTaxTest.ts index b99901d01439..b1c190f9e5ac 100644 --- a/tests/actions/PolicyTaxTest.ts +++ b/tests/actions/PolicyTaxTest.ts @@ -31,7 +31,7 @@ describe('actions/PolicyTax', () => { describe('SetPolicyCustomTaxName', () => { it('Set policy`s custom tax name', () => { const customTaxName = 'Custom tag name'; - mockFetch.pause(); + mockFetch?.pause?.(); Policy.setPolicyCustomTaxName(fakePolicy.id, customTaxName); return waitForBatchedUpdates() .then( @@ -50,7 +50,7 @@ describe('actions/PolicyTax', () => { }); }), ) - .then(mockFetch.resume) + .then(mockFetch?.resume) .then(waitForBatchedUpdates) .then( () => @@ -72,7 +72,7 @@ describe('actions/PolicyTax', () => { const customTaxName = 'Custom tag name'; const originalCustomTaxName = fakePolicy?.taxRates?.name; - mockFetch.pause(); + mockFetch?.pause?.(); Policy.setPolicyCustomTaxName(fakePolicy.id, customTaxName); return waitForBatchedUpdates() .then( @@ -92,8 +92,8 @@ describe('actions/PolicyTax', () => { }), ) .then(() => { - mockFetch.fail(); - return mockFetch.resume?.() as Promise; + mockFetch?.fail?.(); + return mockFetch?.resume?.() as Promise; }) .then(waitForBatchedUpdates) .then( @@ -119,7 +119,7 @@ describe('actions/PolicyTax', () => { it('Set policy`s currency default tax', () => { const taxCode = 'id_TAX_RATE_1'; - mockFetch.pause(); + mockFetch?.pause?.(); Policy.setWorkspaceCurrencyDefault(fakePolicy.id, taxCode); return waitForBatchedUpdates() .then( @@ -138,7 +138,7 @@ describe('actions/PolicyTax', () => { }); }), ) - .then(mockFetch.resume) + .then(mockFetch?.resume) .then(waitForBatchedUpdates) .then( () => @@ -160,7 +160,7 @@ describe('actions/PolicyTax', () => { const taxCode = 'id_TAX_RATE_1'; const originalDefaultExternalID = fakePolicy?.taxRates?.defaultExternalID; - mockFetch.pause(); + mockFetch?.pause?.(); Policy.setWorkspaceCurrencyDefault(fakePolicy.id, taxCode); return waitForBatchedUpdates() .then( @@ -180,8 +180,8 @@ describe('actions/PolicyTax', () => { }), ) .then(() => { - mockFetch.fail(); - return mockFetch.resume?.() as Promise; + mockFetch?.fail?.(); + return mockFetch?.resume?.() as Promise; }) .then(waitForBatchedUpdates) .then( @@ -206,7 +206,7 @@ describe('actions/PolicyTax', () => { it('Set policy`s foreign currency default', () => { const taxCode = 'id_TAX_RATE_1'; - mockFetch.pause(); + mockFetch?.pause?.(); Policy.setForeignCurrencyDefault(fakePolicy.id, taxCode); return waitForBatchedUpdates() .then( @@ -225,7 +225,7 @@ describe('actions/PolicyTax', () => { }); }), ) - .then(mockFetch.resume) + .then(mockFetch?.resume) .then(waitForBatchedUpdates) .then( () => @@ -248,7 +248,7 @@ describe('actions/PolicyTax', () => { const taxCode = 'id_TAX_RATE_1'; const originalDefaultForeignCurrencyID = fakePolicy?.taxRates?.foreignTaxDefault; - mockFetch.pause(); + mockFetch?.pause?.(); Policy.setForeignCurrencyDefault(fakePolicy.id, taxCode); return waitForBatchedUpdates() .then( @@ -269,8 +269,8 @@ describe('actions/PolicyTax', () => { ) .then(() => { - mockFetch.fail(); - return mockFetch.resume?.() as Promise; + mockFetch?.fail?.(); + return mockFetch?.resume?.() as Promise; }) .then(waitForBatchedUpdates) .then( @@ -300,7 +300,7 @@ describe('actions/PolicyTax', () => { code: 'id_TAX_RATE_2', }; - mockFetch.pause(); + mockFetch?.pause?.(); createPolicyTax(fakePolicy.id, newTaxRate); return waitForBatchedUpdates() .then( @@ -321,7 +321,7 @@ describe('actions/PolicyTax', () => { }); }), ) - .then(mockFetch.resume) + .then(mockFetch?.resume) .then(waitForBatchedUpdates) .then( () => @@ -348,7 +348,7 @@ describe('actions/PolicyTax', () => { code: 'id_TAX_RATE_2', }; - mockFetch.pause(); + mockFetch?.pause?.(); createPolicyTax(fakePolicy.id, newTaxRate); return waitForBatchedUpdates() .then( @@ -370,8 +370,8 @@ describe('actions/PolicyTax', () => { }), ) .then(() => { - mockFetch.fail(); - return mockFetch.resume?.() as Promise; + mockFetch?.fail?.(); + return mockFetch?.resume?.() as Promise; }) .then(waitForBatchedUpdates) .then( @@ -394,7 +394,7 @@ describe('actions/PolicyTax', () => { describe('SetPolicyTaxesEnabled', () => { it('Disable policy`s taxes', () => { const disableTaxID = 'id_TAX_RATE_1'; - mockFetch.pause(); + mockFetch?.pause?.(); setPolicyTaxesEnabled(fakePolicy.id, [disableTaxID], false); return waitForBatchedUpdates() .then( @@ -415,7 +415,7 @@ describe('actions/PolicyTax', () => { }); }), ) - .then(mockFetch.resume) + .then(mockFetch?.resume) .then(waitForBatchedUpdates) .then( () => @@ -437,7 +437,7 @@ describe('actions/PolicyTax', () => { it('Disable policy`s taxes but API returns an error, then enable policy`s taxes again', () => { const disableTaxID = 'id_TAX_RATE_1'; - mockFetch.pause(); + mockFetch?.pause?.(); setPolicyTaxesEnabled(fakePolicy.id, [disableTaxID], false); const originalTaxes = {...fakePolicy?.taxRates?.taxes}; return waitForBatchedUpdates() @@ -460,8 +460,8 @@ describe('actions/PolicyTax', () => { }), ) .then(() => { - mockFetch.fail(); - return mockFetch.resume?.() as Promise; + mockFetch?.fail?.(); + return mockFetch?.resume?.() as Promise; }) .then(waitForBatchedUpdates) .then( @@ -488,7 +488,7 @@ describe('actions/PolicyTax', () => { it('Rename tax', () => { const taxID = 'id_TAX_RATE_1'; const newTaxName = 'Tax rate 1 updated'; - mockFetch.pause(); + mockFetch?.pause?.(); renamePolicyTax(fakePolicy.id, taxID, newTaxName); return waitForBatchedUpdates() .then( @@ -509,7 +509,7 @@ describe('actions/PolicyTax', () => { }); }), ) - .then(mockFetch.resume) + .then(mockFetch?.resume) .then(waitForBatchedUpdates) .then( () => @@ -533,7 +533,7 @@ describe('actions/PolicyTax', () => { const taxID = 'id_TAX_RATE_1'; const newTaxName = 'Tax rate 1 updated'; const originalTaxRate = {...fakePolicy?.taxRates?.taxes[taxID]}; - mockFetch.pause(); + mockFetch?.pause?.(); renamePolicyTax(fakePolicy.id, taxID, newTaxName); return waitForBatchedUpdates() .then( @@ -555,8 +555,8 @@ describe('actions/PolicyTax', () => { }), ) .then(() => { - mockFetch.fail(); - return mockFetch.resume?.() as Promise; + mockFetch?.fail?.(); + return mockFetch?.resume?.() as Promise; }) .then(waitForBatchedUpdates) .then( @@ -583,7 +583,7 @@ describe('actions/PolicyTax', () => { const taxID = 'id_TAX_RATE_1'; const newTaxValue = 10; const stringTaxValue = `${newTaxValue}%`; - mockFetch.pause(); + mockFetch?.pause?.(); updatePolicyTaxValue(fakePolicy.id, taxID, newTaxValue); return waitForBatchedUpdates() .then( @@ -604,7 +604,7 @@ describe('actions/PolicyTax', () => { }); }), ) - .then(mockFetch.resume) + .then(mockFetch?.resume) .then(waitForBatchedUpdates) .then( () => @@ -629,7 +629,7 @@ describe('actions/PolicyTax', () => { const newTaxValue = 10; const originalTaxRate = {...fakePolicy?.taxRates?.taxes[taxID]}; const stringTaxValue = `${newTaxValue}%`; - mockFetch.pause(); + mockFetch?.pause?.(); updatePolicyTaxValue(fakePolicy.id, taxID, newTaxValue); return waitForBatchedUpdates() .then( @@ -651,8 +651,8 @@ describe('actions/PolicyTax', () => { }), ) .then(() => { - mockFetch.fail(); - return mockFetch.resume?.() as Promise; + mockFetch?.fail?.(); + return mockFetch?.resume?.() as Promise; }) .then(waitForBatchedUpdates) .then( @@ -679,7 +679,7 @@ describe('actions/PolicyTax', () => { const foreignTaxDefault = fakePolicy?.taxRates?.foreignTaxDefault; const taxID = 'id_TAX_RATE_1'; - mockFetch.pause(); + mockFetch?.pause?.(); deletePolicyTaxes(fakePolicy.id, [taxID]); return waitForBatchedUpdates() .then( @@ -701,7 +701,7 @@ describe('actions/PolicyTax', () => { }); }), ) - .then(mockFetch.resume) + .then(mockFetch?.resume) .then(waitForBatchedUpdates) .then( () => @@ -726,7 +726,7 @@ describe('actions/PolicyTax', () => { const taxID = 'id_TAX_RATE_1'; const firstTaxID = 'id_TAX_EXEMPT'; - mockFetch.pause(); + mockFetch?.pause?.(); return Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, {taxRates: {foreignTaxDefault: 'id_TAX_RATE_1'}}) .then(() => { deletePolicyTaxes(fakePolicy.id, [taxID]); @@ -751,7 +751,7 @@ describe('actions/PolicyTax', () => { }); }), ) - .then(mockFetch.resume) + .then(mockFetch?.resume) .then(waitForBatchedUpdates) .then( () => @@ -776,7 +776,7 @@ describe('actions/PolicyTax', () => { const foreignTaxDefault = fakePolicy?.taxRates?.foreignTaxDefault; const taxID = 'id_TAX_RATE_1'; - mockFetch.pause(); + mockFetch?.pause?.(); deletePolicyTaxes(fakePolicy.id, [taxID]); return waitForBatchedUpdates() .then( @@ -799,8 +799,8 @@ describe('actions/PolicyTax', () => { }), ) .then(() => { - mockFetch.fail(); - return mockFetch.resume?.() as Promise; + mockFetch?.fail?.(); + return mockFetch?.resume?.() as Promise; }) .then(waitForBatchedUpdates) .then( diff --git a/tests/actions/PolicyTest.ts b/tests/actions/PolicyTest.ts index d90defce99f6..2b8a9bfe1882 100644 --- a/tests/actions/PolicyTest.ts +++ b/tests/actions/PolicyTest.ts @@ -23,14 +23,15 @@ describe('actions/Policy', () => { }); }); + let mockFetch: MockFetch; beforeEach(() => { - global.fetch = TestHelper.getGlobalFetchMock(); + mockFetch = TestHelper.getGlobalFetchMock() as MockFetch; return Onyx.clear().then(waitForBatchedUpdates); }); describe('createWorkspace', () => { it('creates a new workspace', async () => { - (fetch as MockFetch).pause(); + mockFetch?.pause?.(); Onyx.set(ONYXKEYS.SESSION, {email: ESH_EMAIL, accountID: ESH_ACCOUNT_ID}); await waitForBatchedUpdates(); @@ -108,7 +109,7 @@ describe('actions/Policy', () => { }); }); - // Each of the three reports should have a a `CREATED` action + // Each of the three reports should have a a `CREATED` action. let adminReportActions: ReportAction[] = Object.values(reportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${adminReportID}`] ?? {}); let announceReportActions: ReportAction[] = Object.values(reportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${announceReportID}`] ?? {}); let expenseReportActions: ReportAction[] = Object.values(reportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${expenseReportID}`] ?? {}); @@ -123,7 +124,7 @@ describe('actions/Policy', () => { }); // Check for success data - (fetch as MockFetch).resume(); + mockFetch?.resume?.(); await waitForBatchedUpdates(); policy = await new Promise((resolve) => { diff --git a/tests/utils/TestHelper.ts b/tests/utils/TestHelper.ts index 40ef19469c1b..ddf8960b9f02 100644 --- a/tests/utils/TestHelper.ts +++ b/tests/utils/TestHelper.ts @@ -9,10 +9,10 @@ import type {Response as OnyxResponse, PersonalDetails, Report} from '@src/types import waitForBatchedUpdates from './waitForBatchedUpdates'; type MockFetch = ReturnType & { - pause: () => void; - fail: () => void; - succeed: () => void; - resume: () => Promise; + pause?: () => void; + fail?: () => void; + succeed?: () => void; + resume?: () => Promise; }; type QueueItem = (value: Partial | PromiseLike>) => void; From 3a9a1a5ee5f4df9bb153734ae3393bff21cf26c1 Mon Sep 17 00:00:00 2001 From: Yauheni Date: Thu, 16 May 2024 14:28:36 +0200 Subject: [PATCH 7/7] Reverte changes x2 --- tests/actions/PolicyTest.ts | 7 +++---- tests/utils/TestHelper.ts | 4 ++-- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/tests/actions/PolicyTest.ts b/tests/actions/PolicyTest.ts index 2b8a9bfe1882..6dae053afac7 100644 --- a/tests/actions/PolicyTest.ts +++ b/tests/actions/PolicyTest.ts @@ -23,15 +23,14 @@ describe('actions/Policy', () => { }); }); - let mockFetch: MockFetch; beforeEach(() => { - mockFetch = TestHelper.getGlobalFetchMock() as MockFetch; + global.fetch = TestHelper.getGlobalFetchMock(); return Onyx.clear().then(waitForBatchedUpdates); }); describe('createWorkspace', () => { it('creates a new workspace', async () => { - mockFetch?.pause?.(); + (fetch as MockFetch)?.pause?.(); Onyx.set(ONYXKEYS.SESSION, {email: ESH_EMAIL, accountID: ESH_ACCOUNT_ID}); await waitForBatchedUpdates(); @@ -124,7 +123,7 @@ describe('actions/Policy', () => { }); // Check for success data - mockFetch?.resume?.(); + (fetch as MockFetch)?.resume?.(); await waitForBatchedUpdates(); policy = await new Promise((resolve) => { diff --git a/tests/utils/TestHelper.ts b/tests/utils/TestHelper.ts index ddf8960b9f02..bd107ba6ed56 100644 --- a/tests/utils/TestHelper.ts +++ b/tests/utils/TestHelper.ts @@ -172,14 +172,14 @@ function getGlobalFetchMock() { json: () => Promise.resolve({jsonCode: 200}), }; - const mockFetch = jest.fn().mockImplementation(() => { + const mockFetch: MockFetch = jest.fn().mockImplementation(() => { if (!isPaused) { return Promise.resolve(getResponse()); } return new Promise((resolve) => { queue.push(resolve); }); - }) as MockFetch; + }); mockFetch.pause = () => (isPaused = true); mockFetch.resume = () => {