Skip to content

Commit f7c9652

Browse files
committed
simplify tests
1 parent 779a361 commit f7c9652

2 files changed

Lines changed: 37 additions & 39 deletions

File tree

tests/actions/IOUTest.ts

Lines changed: 35 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1627,27 +1627,7 @@ describe('actions/IOU', () => {
16271627
it('correctly implements RedBrickRoad error handling for ShareTrackedExpense when inviting new user to workspace', async () => {
16281628
const amount = 5000;
16291629
const comment = 'Shared tracked expense test';
1630-
let selfDMReportID: string | undefined;
1631-
let policyExpenseChatReportID: string | undefined;
1632-
let transactionID: string | undefined;
1633-
let linkedTrackedExpenseReportAction: OnyxEntry<ReportAction>;
1634-
let linkedTrackedExpenseReportID: string | undefined;
1635-
let actionableWhisperReportActionID: string | undefined;
1636-
let moneyRequestReportID: string | undefined;
16371630

1638-
// Helper function to promisify Onyx.connect
1639-
const waitForOnyxData = <T>(key: string, waitForCollection = false): Promise<T> => {
1640-
return new Promise<T>((resolve) => {
1641-
const connection = Onyx.connect({
1642-
key,
1643-
waitForCollectionCallback: waitForCollection,
1644-
callback: (data) => {
1645-
Onyx.disconnect(connection);
1646-
resolve(data as T);
1647-
},
1648-
});
1649-
});
1650-
};
16511631

16521632
// Setup test data - create a self DM report and policy expense chat
16531633
const selfDMReport: Report = {
@@ -1724,21 +1704,38 @@ describe('actions/IOU', () => {
17241704
mockFetch?.resume?.();
17251705
await waitForBatchedUpdates();
17261706

1707+
// Helper for collection data that requires waitForCollectionCallback
1708+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
1709+
const waitForCollectionData = <T>(key: string): Promise<T> => {
1710+
return new Promise<T>((resolve) => {
1711+
const connection = Onyx.connect({
1712+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
1713+
key: key as any,
1714+
waitForCollectionCallback: true,
1715+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
1716+
callback: (data: any) => {
1717+
Onyx.disconnect(connection);
1718+
resolve(data as T);
1719+
},
1720+
});
1721+
});
1722+
};
1723+
17271724
// Capture the created tracked expense data
1728-
const allReports = await waitForOnyxData<OnyxCollection<Report>>(ONYXKEYS.COLLECTION.REPORT, true);
1725+
const allReports = await waitForCollectionData<OnyxCollection<Report>>(ONYXKEYS.COLLECTION.REPORT);
17291726
const reports = Object.values(allReports ?? {});
17301727
const selfDMReportOnyx = reports.find((report) => report?.reportID === selfDMReport.reportID);
1731-
selfDMReportID = selfDMReportOnyx?.reportID;
1728+
const selfDMReportID = selfDMReportOnyx?.reportID;
17321729

1733-
const reportActions = await waitForOnyxData<OnyxCollection<ReportAction>>(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${selfDMReportID}`);
1730+
const reportActions = await getOnyxValue(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${selfDMReportID}`);
17341731
const actions = Object.values(reportActions ?? {});
1735-
linkedTrackedExpenseReportAction = actions.find((action) => isMoneyRequestAction(action));
1736-
actionableWhisperReportActionID = actions.find((action) => isActionableTrackExpense(action))?.reportActionID;
1732+
const linkedTrackedExpenseReportAction = actions.find((action) => action && isMoneyRequestAction(action));
1733+
const actionableWhisperReportActionID = actions.find((action) => action && isActionableTrackExpense(action))?.reportActionID;
17371734

1738-
const allTransactions = await waitForOnyxData<OnyxCollection<Transaction>>(ONYXKEYS.COLLECTION.TRANSACTION, true);
1735+
const allTransactions = await waitForCollectionData<OnyxCollection<Transaction>>(ONYXKEYS.COLLECTION.TRANSACTION);
17391736
const transaction = Object.values(allTransactions ?? {}).find((t) => !isEmptyObject(t));
1740-
transactionID = transaction?.transactionID;
1741-
linkedTrackedExpenseReportID = transaction?.reportID;
1737+
const transactionID = transaction?.transactionID;
1738+
const linkedTrackedExpenseReportID = transaction?.reportID;
17421739

17431740
// Now pause fetch and share the tracked expense with accountant
17441741
mockFetch?.pause?.();
@@ -1772,15 +1769,15 @@ describe('actions/IOU', () => {
17721769
await waitForBatchedUpdates();
17731770

17741771
// Verify optimistic data is created with pending status
1775-
const allReportsAfterShare = await waitForOnyxData<OnyxCollection<Report>>(ONYXKEYS.COLLECTION.REPORT, true);
1772+
const allReportsAfterShare = await waitForCollectionData<OnyxCollection<Report>>(ONYXKEYS.COLLECTION.REPORT);
17761773
const reportsAfterShare = Object.values(allReportsAfterShare ?? {});
17771774

17781775
// Find the policy expense chat and money request report
17791776
const policyExpenseChatOnyx = reportsAfterShare.find((report) => report?.reportID === policyExpenseChat.reportID);
17801777
const moneyRequestReport = reportsAfterShare.find((report) => report?.type === CONST.REPORT.TYPE.IOU);
17811778

1782-
policyExpenseChatReportID = policyExpenseChatOnyx?.reportID;
1783-
moneyRequestReportID = moneyRequestReport?.reportID;
1779+
const policyExpenseChatReportID = policyExpenseChatOnyx?.reportID;
1780+
const moneyRequestReportID = moneyRequestReport?.reportID;
17841781

17851782
// Verify accountant was added to the expense chat
17861783
expect(policyExpenseChatOnyx?.participants?.[accountant.accountID]).toBeTruthy();
@@ -1790,25 +1787,25 @@ describe('actions/IOU', () => {
17901787
expect(moneyRequestReport?.type).toBe(CONST.REPORT.TYPE.IOU);
17911788

17921789
// Verify money request report actions are pending
1793-
const moneyRequestReportActions = await waitForOnyxData<OnyxCollection<ReportAction>>(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${moneyRequestReportID}`);
1790+
const moneyRequestReportActions = await getOnyxValue(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${moneyRequestReportID}`);
17941791
const moneyRequestActions = Object.values(moneyRequestReportActions ?? {});
1795-
const createdAction = moneyRequestActions.find((action) => action?.actionName === CONST.REPORT.ACTIONS.TYPE.CREATED);
1796-
const iouAction = moneyRequestActions.find((action) => isMoneyRequestAction(action));
1792+
const createdAction = moneyRequestActions.find((action) => action && action.actionName === CONST.REPORT.ACTIONS.TYPE.CREATED);
1793+
const iouAction = moneyRequestActions.find((action) => action && isMoneyRequestAction(action));
17971794

17981795
// Both actions should be pending
17991796
expect(createdAction?.pendingAction).toBe(CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD);
18001797
expect(iouAction?.pendingAction).toBe(CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD);
18011798

18021799
// Verify policy expense chat report preview is pending
1803-
const policyExpenseChatActions = await waitForOnyxData<OnyxCollection<ReportAction>>(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${policyExpenseChatReportID}`);
1800+
const policyExpenseChatActions = await getOnyxValue(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${policyExpenseChatReportID}`);
18041801
const policyExpenseActions = Object.values(policyExpenseChatActions ?? {});
1805-
const reportPreviewAction = policyExpenseActions.find((action) => action?.actionName === CONST.REPORT.ACTIONS.TYPE.REPORT_PREVIEW);
1802+
const reportPreviewAction = policyExpenseActions.find((action) => action && action.actionName === CONST.REPORT.ACTIONS.TYPE.REPORT_PREVIEW);
18061803

18071804
// Report preview should be pending
18081805
expect(reportPreviewAction?.pendingAction).toBe(CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD);
18091806

18101807
// Verify transaction is pending
1811-
const transactionData = await waitForOnyxData<Transaction>(`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`);
1808+
const transactionData = await getOnyxValue(`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`);
18121809

18131810
// Transaction should be pending
18141811
expect(transactionData?.pendingAction).toBe(CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD);
@@ -1818,7 +1815,7 @@ describe('actions/IOU', () => {
18181815
await (mockFetch?.resume?.() as Promise<unknown>);
18191816

18201817
// Verify error handling after failure - focus on workspace invitation error
1821-
const policyData = await waitForOnyxData<Policy>(`${ONYXKEYS.COLLECTION.POLICY}${policy.id}`);
1818+
const policyData = await getOnyxValue(`${ONYXKEYS.COLLECTION.POLICY}${policy.id}`);
18221819

18231820
// The new accountant should have been added to the employee list with error
18241821
const accountantEmployee = policyData?.employeeList?.[accountant.email];

tests/utils/getOnyxValue.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import Onyx from 'react-native-onyx';
22
import type {KeyValueMapping, OnyxEntry, OnyxKey} from 'react-native-onyx';
33

4-
export default function getOnyxValue<TKey extends OnyxKey>(key: TKey): Promise<OnyxEntry<KeyValueMapping[TKey]>> {
4+
export default function getOnyxValue<TKey extends OnyxKey>(key: TKey, waitForCollectionCallback = false): Promise<OnyxEntry<KeyValueMapping[TKey]>> {
55
return new Promise((resolve) => {
66
Onyx.connect({
77
key,
88
callback: (value) => resolve(value),
9+
waitForCollectionCallback,
910
});
1011
});
1112
}

0 commit comments

Comments
 (0)