Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions tests/actions/AppTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import type {MockFetch} from '../utils/TestHelper';

import * as App from '../../src/libs/actions/App';
import * as PersistedRequests from '../../src/libs/actions/PersistedRequests';
import createMock from '../utils/createMock';
import getOnyxValue from '../utils/getOnyxValue';
import * as TestHelper from '../utils/TestHelper';
import waitForBatchedUpdates from '../utils/waitForBatchedUpdates';
Expand Down Expand Up @@ -245,42 +246,42 @@ describe('actions/App', () => {
});

it('should filter out undefined policies', () => {
const policies = {
const policies = createMock<OnyxCollection<Policy>>({
policy1: {id: 'policy1', name: 'Policy 1'},
policy2: undefined,
policy3: {id: 'policy3', name: 'Policy 3'},
} as unknown as OnyxCollection<Policy>;
});
const result = App.getNonOptimisticPolicyIDs(policies);
expect(result).toEqual(['policy1', 'policy3']);
});

it('should filter out policies with pendingAction ADD', () => {
const policies = {
const policies = createMock<OnyxCollection<Policy>>({
policy1: {id: 'policy1', name: 'Policy 1', pendingAction: 'add'},
policy2: {id: 'policy2', name: 'Policy 2'},
policy3: {id: 'policy3', name: 'Policy 3', pendingAction: 'update'},
} as unknown as OnyxCollection<Policy>;
});
const result = App.getNonOptimisticPolicyIDs(policies);
expect(result).toEqual(['policy2', 'policy3']);
});

it('should return IDs for all valid non-optimistic policies', () => {
const policies = {
const policies = createMock<OnyxCollection<Policy>>({
policy1: {id: 'policy1', name: 'Policy 1'},
policy2: {id: 'policy2', name: 'Policy 2'},
policy3: {id: 'policy3', name: 'Policy 3'},
} as unknown as OnyxCollection<Policy>;
});
const result = App.getNonOptimisticPolicyIDs(policies);
expect(result).toEqual(['policy1', 'policy2', 'policy3']);
});

it('should include policies with other pendingAction values', () => {
const policies = {
const policies = createMock<OnyxCollection<Policy>>({
policy1: {id: 'policy1', name: 'Policy 1', pendingAction: 'update'},
policy2: {id: 'policy2', name: 'Policy 2', pendingAction: 'delete'},
policy3: {id: 'policy3', name: 'Policy 3', pendingAction: null},
policy4: {id: 'policy4', name: 'Policy 4', pendingAction: undefined},
} as unknown as OnyxCollection<Policy>;
});
const result = App.getNonOptimisticPolicyIDs(policies);
expect(result).toEqual(['policy1', 'policy2', 'policy3', 'policy4']);
});
Expand Down
17 changes: 9 additions & 8 deletions tests/actions/IOU/SearchUpdateTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import Onyx from 'react-native-onyx';
import currencyList from '../../unit/currencyList.json';
import {createRandomReport} from '../../utils/collections/reports';
import createRandomTransaction from '../../utils/collections/transaction';
import createMock from '../../utils/createMock';
import {getGlobalFetchMock} from '../../utils/TestHelper';
import waitForBatchedUpdates from '../../utils/waitForBatchedUpdates';

Expand Down Expand Up @@ -140,7 +141,7 @@ describe('actions/IOU', () => {
const transaction = {
...createRandomTransaction(1),
};
const currentSearchQueryJSON = {
const currentSearchQueryJSON = createMock<SearchQueryJSON>({
type: CONST.SEARCH.DATA_TYPES.EXPENSE_REPORT,
sortBy: CONST.SEARCH.TABLE_COLUMNS.DATE,
sortOrder: CONST.SEARCH.SORT_ORDER.DESC,
Expand Down Expand Up @@ -181,7 +182,7 @@ describe('actions/IOU', () => {
hash: 939629734,
recentSearchHash: 1023339253,
similarSearchHash: 1855682507,
} as SearchQueryJSON;
});
const iouReport: Report = {
...createRandomReport(2, undefined),
type: CONST.REPORT.TYPE.EXPENSE,
Expand All @@ -202,7 +203,7 @@ describe('actions/IOU', () => {
const transaction = {
...createRandomTransaction(1),
};
const currentSearchQueryJSON = {
const currentSearchQueryJSON = createMock<SearchQueryJSON>({
type: CONST.SEARCH.DATA_TYPES.EXPENSE_REPORT,
sortBy: CONST.SEARCH.TABLE_COLUMNS.DATE,
sortOrder: CONST.SEARCH.SORT_ORDER.DESC,
Expand Down Expand Up @@ -243,7 +244,7 @@ describe('actions/IOU', () => {
inputQuery: 'sortBy:date sortOrder:desc type:expense-report action:approve to:20671314',
recentSearchHash: 244251677,
similarSearchHash: 1539858783,
} as SearchQueryJSON;
});
const iouReport: Report = {
...createRandomReport(2, undefined),
type: CONST.REPORT.TYPE.EXPENSE,
Expand All @@ -265,7 +266,7 @@ describe('actions/IOU', () => {
...createRandomTransaction(1),
reimbursable: true,
};
const currentSearchQueryJSON = {
const currentSearchQueryJSON = createMock<SearchQueryJSON>({
type: CONST.SEARCH.DATA_TYPES.EXPENSE,
sortBy: CONST.SEARCH.TABLE_COLUMNS.DATE,
sortOrder: CONST.SEARCH.SORT_ORDER.DESC,
Expand Down Expand Up @@ -311,7 +312,7 @@ describe('actions/IOU', () => {
inputQuery: 'sortBy:date sortOrder:desc type:expense groupBy:from status:drafts,outstanding reimbursable:yes',
recentSearchHash: 1043581824,
similarSearchHash: 1832274510,
} as SearchQueryJSON;
});

const iouReport: Report = {
...createRandomReport(2, undefined),
Expand All @@ -334,7 +335,7 @@ describe('actions/IOU', () => {
...createRandomTransaction(1),
};
const policyID = '12345';
const currentSearchQueryJSON = {
const currentSearchQueryJSON = createMock<SearchQueryJSON>({
type: 'expense',
sortBy: 'date',
sortOrder: 'desc',
Expand All @@ -357,7 +358,7 @@ describe('actions/IOU', () => {
isDefault: true,
},
],
} as unknown as SearchQueryJSON;
});

// When the IOU report has a matching policyID, it should return true
const matchingIOUReport: Report = {
Expand Down
40 changes: 25 additions & 15 deletions tests/actions/IOUTest/HoldTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ import Onyx from 'react-native-onyx';
import type {MockFetch} from '../../utils/TestHelper';

import createRandomPolicy from '../../utils/collections/policies';
import {getCurrencyDecimalsLocal, getGlobalFetchMock} from '../../utils/TestHelper';
import createMock from '../../utils/createMock';
import {createGlobalFetchMock, getCurrencyDecimalsLocal} from '../../utils/TestHelper';
import {hasDefinedProperty, isObject} from '../../utils/typeGuards';
import waitForBatchedUpdates from '../../utils/waitForBatchedUpdates';

const topMostReportID = '23423423';
Expand Down Expand Up @@ -71,6 +73,7 @@ const RORY_ACCOUNT_ID = 3;
OnyxUpdateManager();

describe('actions/IOU/Hold', () => {
let mockFetch: MockFetch;
beforeAll(() => {
Onyx.init({
keys: ONYXKEYS,
Expand All @@ -84,11 +87,10 @@ describe('actions/IOU/Hold', () => {
return waitForBatchedUpdates();
});

let mockFetch: MockFetch;
beforeEach(() => {
jest.clearAllTimers();
global.fetch = getGlobalFetchMock();
mockFetch = fetch as MockFetch;
mockFetch = createGlobalFetchMock();
global.fetch = mockFetch;
return Onyx.clear().then(waitForBatchedUpdates);
});

Expand Down Expand Up @@ -317,7 +319,7 @@ describe('actions/IOU/Hold', () => {
});
});
})
.then(() => mockFetch?.resume?.());
.then(() => mockFetch.resume());
});

test('should invoke navigation for each transaction when isOffline is true', () => {
Expand Down Expand Up @@ -395,7 +397,7 @@ describe('actions/IOU/Hold', () => {
.then(() => {
// Navigation should be called once for each transaction (putOnHold called for each)
expect(Navigation.setNavigationActionToMicrotaskQueue).toHaveBeenCalledTimes(2);
return mockFetch?.resume?.();
return mockFetch.resume();
});
});
});
Expand Down Expand Up @@ -585,7 +587,7 @@ describe('actions/IOU/Hold', () => {
})
.then(() => {
mockFetch.fail();
mockFetch?.resume?.();
mockFetch.resume();
unholdRequest(
transaction.transactionID,
transactionThread.reportID,
Expand Down Expand Up @@ -649,11 +651,11 @@ describe('actions/IOU/Hold', () => {
reimbursableTotal: overrides.reimbursableTotal ?? overrides.total - overrides.nonReimbursableTotal,
unheldReimbursableTotal: overrides.unheldReimbursableTotal ?? (overrides.unheldTotal ?? 0) - (overrides.unheldNonReimbursableTotal ?? 0),
};
const chatReport: Report = {
const chatReport = createMock<Report>({
reportID: '99',
iouReportID: iouReport.reportID,
lastVisibleActionCreated: '2026-01-01 00:00:00.000',
} as Report;
});

const heldAmount = overrides.heldAmount ?? 0;
const heldTransaction = heldAmount > 0 ? buildHeldTransaction(iouReport.reportID, heldAmount) : undefined;
Expand Down Expand Up @@ -737,8 +739,8 @@ describe('actions/IOU/Hold', () => {
(entry) => entry.onyxMethod === Onyx.METHOD.MERGE && entry.key === `${ONYXKEYS.COLLECTION.REPORT}${iouReport.reportID}`,
);
const totalsRestore = restorationEntries.find((entry) => {
const value = entry.value as Partial<Report> | undefined;
return value?.total !== undefined || value?.nonReimbursableTotal !== undefined;
const value = entry.value;
return isObject(value) && (hasDefinedProperty(value, 'total') || hasDefinedProperty(value, 'nonReimbursableTotal'));
});
expect(totalsRestore?.value).toEqual({total: 300, nonReimbursableTotal: 50, reimbursableTotal: 250});
});
Expand Down Expand Up @@ -766,8 +768,12 @@ describe('actions/IOU/Hold', () => {
getCurrencyDecimals: getCurrencyDecimalsLocal,
});
const totalsUpdates = result.optimisticData.filter((entry) => {
const value = entry.value as Partial<Report> | undefined;
return entry.key === `${ONYXKEYS.COLLECTION.REPORT}${iouReport.reportID}` && (value?.total !== undefined || value?.nonReimbursableTotal !== undefined);
const value = entry.value;
return (
entry.key === `${ONYXKEYS.COLLECTION.REPORT}${iouReport.reportID}` &&
isObject(value) &&
(hasDefinedProperty(value, 'total') || hasDefinedProperty(value, 'nonReimbursableTotal'))
);
});
expect(totalsUpdates).toEqual([]);
});
Expand Down Expand Up @@ -795,8 +801,12 @@ describe('actions/IOU/Hold', () => {
getCurrencyDecimals: getCurrencyDecimalsLocal,
});
const totalsUpdates = result.optimisticData.filter((entry) => {
const value = entry.value as Partial<Report> | undefined;
return entry.key === `${ONYXKEYS.COLLECTION.REPORT}${iouReport.reportID}` && (value?.total !== undefined || value?.nonReimbursableTotal !== undefined);
const value = entry.value;
return (
entry.key === `${ONYXKEYS.COLLECTION.REPORT}${iouReport.reportID}` &&
isObject(value) &&
(hasDefinedProperty(value, 'total') || hasDefinedProperty(value, 'nonReimbursableTotal'))
);
});
expect(totalsUpdates).toEqual([]);
});
Expand Down
Loading
Loading