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
41 changes: 21 additions & 20 deletions tests/actions/IOU/PerDiemTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import createRandomPolicy from '../../utils/collections/policies';
import createRandomPolicyCategories from '../../utils/collections/policyCategory';
import createRandomPolicyTags from '../../utils/collections/policyTags';
import createRandomTransaction from '../../utils/collections/transaction';
import createMock from '../../utils/createMock';
import getOnyxValue from '../../utils/getOnyxValue';
import {formatPhoneNumber, getCurrencyDecimalsLocal, getGlobalFetchMock} from '../../utils/TestHelper';
import waitForBatchedUpdates from '../../utils/waitForBatchedUpdates';
Expand Down Expand Up @@ -321,10 +322,10 @@ describe('PerDiem', () => {
accountID: 123,
login: 'payee@example.com',
},
};
} satisfies RequestMoneyParticipantParams;

const parentChatReport = {} as OnyxEntry<Report>;
const participantParams = mockParticipantParams as unknown as RequestMoneyParticipantParams;
const parentChatReport = createMock<OnyxEntry<Report>>({});
const participantParams = mockParticipantParams;

const earlyPolicyID = getPerDiemExpensePolicyID({
report: parentChatReport,
Expand Down Expand Up @@ -430,7 +431,7 @@ describe('PerDiem', () => {
};

// When: Call getPerDiemExpenseInformation
const parentChatReport = {} as OnyxEntry<Report>;
const parentChatReport = createMock<OnyxEntry<Report>>({});

const earlyPolicyID = getPerDiemExpensePolicyID({
report: parentChatReport,
Expand Down Expand Up @@ -635,7 +636,7 @@ describe('PerDiem', () => {
role: CONST.REPORT.ROLE.MEMBER,
};

const mockTransactionParams = {
const mockTransactionParams = createMock<PerDiemExpenseTransactionParams>({
comment: 'Conference per diem',
currency: 'USD',
created: '2024-01-20',
Expand All @@ -645,17 +646,17 @@ describe('PerDiem', () => {
billable: false,
attendees: [],
reimbursable: true,
};
});

const mockParticipantParams = {
const mockParticipantParams = createMock<RequestMoneyParticipantParams>({
payeeAccountID: 456,
payeeEmail: 'payee@example.com',
participant: mockParticipant,
};
});

// When: Call getPerDiemExpenseInformation with existing chat report
const parentChatReport = existingChatReport as OnyxEntry<Report>;
const participantParams = mockParticipantParams as RequestMoneyParticipantParams;
const parentChatReport = createMock<OnyxEntry<Report>>(existingChatReport);
const participantParams = mockParticipantParams;

const earlyPolicyID = getPerDiemExpensePolicyID({
report: parentChatReport,
Expand All @@ -671,7 +672,7 @@ describe('PerDiem', () => {
dateFnsLocale: undefined,
parentChatReport,
getCurrencyDecimals: getCurrencyDecimalsLocal,
transactionParams: mockTransactionParams as PerDiemExpenseTransactionParams,
transactionParams: mockTransactionParams,
participantParams,
recentlyUsedParams: {},
policyTags,
Expand Down Expand Up @@ -737,7 +738,7 @@ describe('PerDiem', () => {
quantity: 1,
};

const mockTransactionParams = {
const mockTransactionParams = createMock<PerDiemExpenseTransactionParams>({
comment: 'Policy per diem',
currency: 'USD',
created: '2024-01-25',
Expand All @@ -747,21 +748,21 @@ describe('PerDiem', () => {
billable: true,
attendees: [],
reimbursable: true,
};
});

const mockParticipantParams = {
const mockParticipantParams = createMock<RequestMoneyParticipantParams>({
payeeAccountID: 456,
payeeEmail: 'payee@example.com',
participant: mockParticipant,
};
});

const mockPolicyParams = {
policy: createRandomPolicy(2),
};

// When: Call getPerDiemExpenseInformation for policy expense chat
const parentChatReport = {} as OnyxEntry<Report>;
const participantParams = mockParticipantParams as RequestMoneyParticipantParams;
const parentChatReport = createMock<OnyxEntry<Report>>({});
const participantParams = mockParticipantParams;

const earlyPolicyID = getPerDiemExpensePolicyID({
report: parentChatReport,
Expand All @@ -777,7 +778,7 @@ describe('PerDiem', () => {
dateFnsLocale: undefined,
parentChatReport,
getCurrencyDecimals: getCurrencyDecimalsLocal,
transactionParams: mockTransactionParams as PerDiemExpenseTransactionParams,
transactionParams: mockTransactionParams,
participantParams,
policyParams: mockPolicyParams,
recentlyUsedParams: {},
Expand Down Expand Up @@ -1029,15 +1030,15 @@ describe('PerDiem', () => {
};

// When calling getPerDiemExpenseInformation with personalDetails
const parentChatReport = {} as OnyxEntry<Report>;
const parentChatReport = createMock<OnyxEntry<Report>>({});
const participantParams = {
payeeAccountID: RORY_ACCOUNT_ID,
payeeEmail: RORY_EMAIL,
participant: {
accountID: RORY_ACCOUNT_ID,
login: RORY_EMAIL,
},
} as unknown as RequestMoneyParticipantParams;
} satisfies RequestMoneyParticipantParams;

const earlyPolicyID = getPerDiemExpensePolicyID({
report: parentChatReport,
Expand Down
17 changes: 9 additions & 8 deletions tests/actions/IOUTest/PayMoneyRequestTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ import createRandomPolicy from '../../utils/collections/policies';
import createRandomReportAction from '../../utils/collections/reportActions';
import {createRandomReport} from '../../utils/collections/reports';
import createRandomTransaction from '../../utils/collections/transaction';
import createMock from '../../utils/createMock';
import getOnyxValue from '../../utils/getOnyxValue';
import {formatPhoneNumber, getCurrencyDecimalsLocal, getGlobalFetchMock, getOnyxData, translateLocal} from '../../utils/TestHelper';
import {createGlobalFetchMock, formatPhoneNumber, getCurrencyDecimalsLocal, getOnyxData, translateLocal} from '../../utils/TestHelper';
import waitForBatchedUpdates from '../../utils/waitForBatchedUpdates';

const topMostReportID = '23423423';
Expand Down Expand Up @@ -118,8 +119,8 @@ describe('actions/IOU/PayMoneyRequest', () => {
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 @@ -1229,7 +1230,7 @@ describe('actions/IOU/PayMoneyRequest', () => {
await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${chatReport.reportID}`, chatReport);
await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${iouReport.reportID}`, iouReport);

(Navigation.navigate as jest.Mock).mockClear();
jest.mocked(Navigation.navigate).mockClear();

payMoneyRequest({
conciergeChat: undefined,
Expand Down Expand Up @@ -1299,7 +1300,7 @@ describe('actions/IOU/PayMoneyRequest', () => {
await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${chatReport.reportID}`, chatReport);
await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${iouReport.reportID}`, iouReport);

(Navigation.navigate as jest.Mock).mockClear();
jest.mocked(Navigation.navigate).mockClear();

payMoneyRequest({
conciergeChat: undefined,
Expand Down Expand Up @@ -1359,7 +1360,7 @@ describe('actions/IOU/PayMoneyRequest', () => {
await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${chatReport.reportID}`, chatReport);
await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${iouReport.reportID}`, iouReport);

(Navigation.navigate as jest.Mock).mockClear();
jest.mocked(Navigation.navigate).mockClear();

payMoneyRequest({
conciergeChat: undefined,
Expand Down Expand Up @@ -1558,7 +1559,7 @@ describe('actions/IOU/PayMoneyRequest', () => {
.then(() => {
if (chatReport && expenseReport) {
// And when the payment is cancelled
cancelPayment(expenseReport, chatReport, {} as Policy, true, CARLOS_ACCOUNT_ID, CARLOS_EMAIL, true, false);
cancelPayment(expenseReport, chatReport, createMock<Policy>({}), true, CARLOS_ACCOUNT_ID, CARLOS_EMAIL, true, false);
}
return waitForBatchedUpdates();
})
Expand Down Expand Up @@ -1664,7 +1665,7 @@ describe('actions/IOU/PayMoneyRequest', () => {
if (chatReport && expenseReport) {
mockFetch?.pause?.();
// And when the payment is cancelled
cancelPayment(expenseReport, chatReport, {} as Policy, true, CARLOS_ACCOUNT_ID, CARLOS_EMAIL, true, false);
cancelPayment(expenseReport, chatReport, createMock<Policy>({}), true, CARLOS_ACCOUNT_ID, CARLOS_EMAIL, true, false);
}
await waitForBatchedUpdates();

Expand Down
49 changes: 16 additions & 33 deletions tests/actions/MergeTransactionTest.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import {getReportPreviewReportAction} from '@libs/actions/IOU/MoneyRequestBuilder';
import {areTransactionsEligibleForMerge, getTransactionsForMerging, mergeTransactionRequest, setMergeTransactionKey, setupMergeTransactionData} from '@libs/actions/MergeTransaction';
import {addComment, openReport} from '@libs/actions/Report';
import * as API from '@libs/API';
import {WRITE_COMMANDS} from '@libs/API/types';
import {getLoginsByAccountIDs} from '@libs/PersonalDetailsUtils';
import {getOriginalMessage, getReportAction} from '@libs/ReportActionsUtils';
import {getOriginalMessage, getReportAction, isActionOfType} from '@libs/ReportActionsUtils';
import {buildTransactionThread} from '@libs/ReportUtils';

import CONST from '@src/CONST';
Expand Down Expand Up @@ -231,8 +232,8 @@ describe('mergeTransactionRequest', () => {
});

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

Expand Down Expand Up @@ -528,9 +529,7 @@ describe('mergeTransactionRequest', () => {
await Onyx.set(`${ONYXKEYS.COLLECTION.TRANSACTION}${sourceTransaction.transactionID}`, sourceTransaction);
await Onyx.set(`${ONYXKEYS.COLLECTION.MERGE_TRANSACTION}${mergeTransactionID}`, mergeTransaction);

type ApiModule = {write: (...args: unknown[]) => unknown};
const getApiModule = (): ApiModule => jest.requireActual('@libs/API');
const writeSpy = jest.spyOn(getApiModule(), 'write');
const writeSpy = jest.spyOn(API, 'write');

mockFetch?.pause?.();

Expand Down Expand Up @@ -567,21 +566,7 @@ describe('mergeTransactionRequest', () => {
expect(writeSpy).toHaveBeenCalled();
const [firstCall] = writeSpy.mock.calls;
const [calledCommand, rawParams] = firstCall;
const calledParams = rawParams as {
transactionID: string;
transactionIDList: string[];
created: string;
merchant: string;
amount: number;
currency: string;
category: string;
billable?: boolean;
reimbursable?: boolean;
tag?: string;
receiptID?: string;
reportID: string;
comment: string;
};
const calledParams = rawParams;

expect(calledCommand).toBe(WRITE_COMMANDS.MERGE_TRANSACTION);
expect(calledParams).toEqual(
Expand Down Expand Up @@ -900,19 +885,17 @@ describe('mergeTransactionRequest', () => {
// The source IOU action is no longer removed — only the target report's IOU action is handled in the cross-report branch
expect(sourceReportActions?.[sourceIOUActionID]).toBeDefined();

const newIOUAction = Object.values(sourceReportActions ?? {}).find((action) => {
const reportAction = action as OnyxEntry<ReportAction>;
const originalMessage = getOriginalMessage(reportAction) as OriginalMessageIOU | undefined;
return (
reportAction?.actionName === CONST.REPORT.ACTIONS.TYPE.IOU &&
originalMessage?.type === CONST.IOU.REPORT_ACTION_TYPE.CREATE &&
originalMessage?.IOUTransactionID === targetTransaction.transactionID
);
}) as OnyxEntry<ReportAction>;
const newIOUAction = Object.values(sourceReportActions ?? {}).find((action): action is ReportAction<typeof CONST.REPORT.ACTIONS.TYPE.IOU> => {
if (!isActionOfType(action, CONST.REPORT.ACTIONS.TYPE.IOU)) {
return false;
}
const originalMessage = getOriginalMessage(action);
return originalMessage?.type === CONST.IOU.REPORT_ACTION_TYPE.CREATE && originalMessage?.IOUTransactionID === targetTransaction.transactionID;
});

// Verify the new IOU action is created and points to the merged transaction
expect(newIOUAction).toBeTruthy();
expect((getOriginalMessage(newIOUAction) as OriginalMessageIOU | undefined)?.IOUTransactionID).toBe(targetTransaction.transactionID);
expect(getOriginalMessage(newIOUAction)?.IOUTransactionID).toBe(targetTransaction.transactionID);

const updatedTargetTransactionThread = await new Promise<Report | null>((resolve) => {
const connection = Onyx.connect({
Expand Down Expand Up @@ -1280,7 +1263,7 @@ describe('mergeTransactionRequest', () => {
await waitForBatchedUpdates();

// Then we expect the reportPreview to update with new childVisibleActionCount
previewAction = getReportPreviewReportAction(chatReport.reportID, sourceReport.reportID) as OnyxEntry<ReportAction>;
previewAction = getReportPreviewReportAction(chatReport.reportID, sourceReport.reportID) ?? undefined;
expect(previewAction).toBeTruthy();
expect(previewAction?.childVisibleActionCount).toEqual(0);
expect(previewAction?.childCommenterCount).toEqual(0);
Expand All @@ -1302,7 +1285,7 @@ describe('mergeTransactionRequest', () => {
await waitForBatchedUpdates();

// Then we expect the reportPreview to update with new childVisibleActionCount
previewAction = getReportPreviewReportAction(chatReport.reportID, sourceReport.reportID) as OnyxEntry<ReportAction>;
previewAction = getReportPreviewReportAction(chatReport.reportID, sourceReport.reportID) ?? undefined;
expect(previewAction).toBeTruthy();
expect(previewAction?.childVisibleActionCount).toEqual(0);
expect(previewAction?.childCommenterCount).toEqual(0);
Expand Down
Loading
Loading