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
6 changes: 4 additions & 2 deletions src/libs/actions/Report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ import {
getNextApproverAccountID,
getOptimisticDataForParentReportAction,
getOriginalReportID,
getOutstandingChildRequest,
getParsedComment,
getPendingChatMembers,
getPolicyExpenseChat,
Expand Down Expand Up @@ -2710,6 +2711,7 @@ function buildNewReportOptimisticData(policy: OnyxEntry<Policy>, reportID: strin
};

const optimisticNextStep = buildNextStep(optimisticReportData, CONST.REPORT.STATUS_NUM.OPEN);
const outstandingChildRequest = getOutstandingChildRequest(optimisticReportData);

const optimisticData: OnyxUpdate[] = [
{
Expand Down Expand Up @@ -2737,7 +2739,7 @@ function buildNewReportOptimisticData(policy: OnyxEntry<Policy>, reportID: strin
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${parentReport?.reportID}`,
value: {lastVisibleActionCreated: optimisticReportPreview.created},
value: {lastVisibleActionCreated: optimisticReportPreview.created, ...outstandingChildRequest},
},
{
onyxMethod: Onyx.METHOD.SET,
Expand Down Expand Up @@ -2775,7 +2777,7 @@ function buildNewReportOptimisticData(policy: OnyxEntry<Policy>, reportID: strin
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${parentReport?.reportID}`,
value: {lastVisibleActionCreated: parentReport?.lastVisibleActionCreated},
value: {lastVisibleActionCreated: parentReport?.lastVisibleActionCreated, hasOutstandingChildRequest: parentReport?.hasOutstandingChildRequest},
},
];

Expand Down
76 changes: 75 additions & 1 deletion tests/actions/ReportTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import * as ReportUtils from '@src/libs/ReportUtils';
import ONYXKEYS from '@src/ONYXKEYS';
import type * as OnyxTypes from '@src/types/onyx';
import createCollection from '../utils/collections/createCollection';
import createRandomPolicy from '../utils/collections/policies';
import createRandomReportAction from '../utils/collections/reportActions';
import {createRandomReport} from '../utils/collections/reports';
import getIsUsingFakeTimers from '../utils/getIsUsingFakeTimers';
Expand All @@ -39,7 +40,7 @@ jest.mock('@libs/ReportUtils', () => {
const originalModule = jest.requireActual<Report>('@libs/ReportUtils');
return {
...originalModule,
getPolicyExpenseChat: jest.fn().mockReturnValue({reportID: '1234'}),
getPolicyExpenseChat: jest.fn().mockReturnValue({reportID: '1234', hasOutstandingChildRequest: false}),
};
});

Expand Down Expand Up @@ -81,6 +82,7 @@ describe('actions/Report', () => {
// Onyx.clear() promise is resolved in batch which happens after the current microtasks cycle
setImmediate(jest.runOnlyPendingTimers);
}
global.fetch = TestHelper.getGlobalFetchMock();

// Clear the queue before each test to avoid test pollution
SequentialQueue.resetQueue();
Expand Down Expand Up @@ -1534,8 +1536,22 @@ describe('actions/Report', () => {
it('should create new report and "create report" quick action, when createNewReport gets called', async () => {
const accountID = 1234;
const policyID = '5678';
const mockFetchData = fetch as MockFetch;
// Given a policy with harvesting is disabled
const policy = {
...createRandomPolicy(Number(policyID)),
isPolicyExpenseChatEnabled: true,
type: CONST.POLICY.TYPE.TEAM,
harvesting: {
enabled: false,
},
Comment on lines +1542 to +1547

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coming from #76263 checklist createRandomPolicy cause flaky test with autoReportingFrequency the isn't IMMEDIATE so we have add fixed autoReportingFrequency: CONST.POLICY.AUTO_REPORTING_FREQUENCIES.IMMEDIATE,

};
await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`, policy);

mockFetchData.pause();
const reportID = Report.createNewReport({accountID}, policyID);
const parentReport = ReportUtils.getPolicyExpenseChat(accountID, policyID);

const reportPreviewAction = await new Promise<OnyxEntry<OnyxTypes.ReportAction<typeof CONST.REPORT.ACTIONS.TYPE.REPORT_PREVIEW>>>((resolve) => {
const connection = Onyx.connect({
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${parentReport?.reportID}`,
Expand All @@ -1560,6 +1576,7 @@ describe('actions/Report', () => {
// assert correctness of crucial onyx data
expect(createdReport?.reportID).toBe(reportID);
expect(parentPolicyExpenseChat?.lastVisibleActionCreated).toBe(reportPreviewAction?.created);
expect(parentPolicyExpenseChat?.hasOutstandingChildRequest).toBe(true);
expect(createdReport?.total).toBe(0);
expect(createdReport?.parentReportActionID).toBe(reportPreviewAction?.reportActionID);

Expand All @@ -1581,6 +1598,63 @@ describe('actions/Report', () => {
},
});
});

// When the request fails
mockFetchData.fail();
await mockFetchData.resume();
await waitForBatchedUpdates();

// Then the onyx data should be reverted to the state before the request
await new Promise<void>((resolve) => {
const connection = Onyx.connect({
key: ONYXKEYS.COLLECTION.REPORT,
waitForCollectionCallback: true,
callback: (reports) => {
Onyx.disconnect(connection);
const parentPolicyExpenseChat = reports?.[`${ONYXKEYS.COLLECTION.REPORT}${parentReport?.reportID}`];
expect(parentPolicyExpenseChat?.hasOutstandingChildRequest).toBe(parentReport?.hasOutstandingChildRequest);

resolve();
},
});
});
});

it('should not optimistic outstandingChildRequest when create report with harvesting is enabled', async () => {
const accountID = 1234;
const policyID = '5678';
// Given a policy with harvesting is enabled
const policy = {
...createRandomPolicy(Number(policyID)),
isPolicyExpenseChatEnabled: true,
type: CONST.POLICY.TYPE.TEAM,
harvesting: {
enabled: true,
},
};
const parentReport = ReportUtils.getPolicyExpenseChat(accountID, policyID);
await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`, policy);
if (parentReport?.reportID) {
await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${parentReport?.reportID}`, parentReport);
}

// When create new report
Report.createNewReport({accountID}, policyID);

// Then the parent report's hasOutstandingChildRequest property should remain unchanged
await new Promise<void>((resolve) => {
const connection = Onyx.connect({
key: ONYXKEYS.COLLECTION.REPORT,
waitForCollectionCallback: true,
callback: (reports) => {
Onyx.disconnect(connection);
const parentPolicyExpenseChat = reports?.[`${ONYXKEYS.COLLECTION.REPORT}${parentReport?.reportID}`];
expect(parentPolicyExpenseChat?.hasOutstandingChildRequest).toBe(parentReport?.hasOutstandingChildRequest);

resolve();
},
});
});
});

describe('completeOnboarding', () => {
Expand Down