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: 11 additions & 6 deletions src/libs/actions/IOU/StartExpenseFlows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,17 +183,22 @@ function createDraftWorkspaceAndNavigateToConfirmationScreen(
},
]);
setMoneyRequestReportID(transactionID, expenseChatReportID);
if (isCategorizing) {
Navigation.navigate(
createDynamicRoute(DYNAMIC_ROUTES.MONEY_REQUEST_STEP_CATEGORY.getRoute({action: actionName, iouType: CONST.IOU.TYPE.SUBMIT, transactionID, reportID: expenseChatReportID})),
);
} else {
// The participants and reportID above are written with Onyx.merge, which lands a microtask after this press handler.
// Navigating synchronously would render the confirmation screen before the merge, so it would see no participants and
// auto-open the Choose recipient picker. Defer navigation by a microtask plus an animation frame so the merge is applied first.
Navigation.setNavigationActionToMicrotaskQueue(() => {

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.

❌ CONSISTENCY-18 (docs)

This change wraps the navigation in Navigation.setNavigationActionToMicrotaskQueue(...), but there is no comment explaining why the deferral is needed. Deferral calls like this are exactly the kind of non-obvious workaround the next contributor cannot recover from the code alone: it reads as an arbitrary indirection, and someone could easily "simplify" it back to a direct Navigation.navigate(...) and silently reintroduce the recipient-picker flash. The crucial reasoning lives only in the PR description.

Add a comment directly above the call capturing the why, for example:

// The recipient participant is written with Onyx.merge, which lands a microtask after this press
// handler. Navigating synchronously would render the confirmation screen before the merge, so it
// would see no participants and auto-open the Choose recipient picker. Defer navigation by a
// microtask plus an animation frame so the merge is applied first.
Navigation.setNavigationActionToMicrotaskQueue(() => {

Reviewed at: 6733889 | Please rate this suggestion with 👍 or 👎 to help us improve! Reactions are used to monitor reviewer efficiency.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

added

if (isCategorizing) {
Navigation.navigate(
createDynamicRoute(DYNAMIC_ROUTES.MONEY_REQUEST_STEP_CATEGORY.getRoute({action: actionName, iouType: CONST.IOU.TYPE.SUBMIT, transactionID, reportID: expenseChatReportID})),
);
return;
}
// The confirmation route needs a `:reportID`, so it points at the (draft) workspace expense chat to resolve the
// draft policy/destination. That report only lives in REPORT_DRAFT. Send back to the origin report (e.g. the
// self-DM the track expense came from) so navigateBack returns there instead of the draft report's start step.
const backTo = backToReportID ? ROUTES.REPORT_WITH_ID.getRoute(backToReportID) : undefined;
Navigation.navigate(ROUTES.MONEY_REQUEST_STEP_CONFIRMATION.getRoute(actionName, CONST.IOU.TYPE.SUBMIT, transactionID, expenseChatReportID, undefined, true, backTo));
}
});
}

type CreateDraftTransactionParams = {
Expand Down
40 changes: 39 additions & 1 deletion tests/actions/IOU/CreateDraftTransactionTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jest.mock('@src/libs/Navigation/Navigation', () => ({
dismissModalWithReport: jest.fn(),
goBack: jest.fn(),
getTopmostReportId: jest.fn(() => topMostReportID),
setNavigationActionToMicrotaskQueue: jest.fn(),
setNavigationActionToMicrotaskQueue: jest.fn((callback: () => void) => callback()),
removeScreenByKey: jest.fn(),
isNavigationReady: jest.fn(() => Promise.resolve()),
getReportRouteByID: jest.fn(),
Expand Down Expand Up @@ -714,6 +714,44 @@ describe('actions/IOU', () => {
expect(getConfirmationRouteBackTo()).toBe(ROUTES.REPORT_WITH_ID.getRoute(selfDMReport.reportID));
});

it('should defer navigating to the confirmation page until the draft workspace is merged in as the recipient', async () => {
// Given the real navigation deferral rather than the synchronous mock used by the other tests
const {selfDMReport, trackedExpense} = await setUpSelfDMTrackedExpense();
mockedIsReportTopmostSplitNavigator.mockReturnValue(false);
jest.mocked(Navigation.setNavigationActionToMicrotaskQueue).mockImplementationOnce(
jest.requireActual<{default: typeof Navigation.setNavigationActionToMicrotaskQueue}>('@libs/Navigation/helpers/setNavigationActionToMicrotaskQueue').default,
);

// When the expense is submitted to the employer and there is no workspace to submit to
createDraftTransactionAndNavigateToParticipantSelector({
reportID: selfDMReport.reportID,
reportActions: undefined,
actionName: CONST.IOU.ACTION.SUBMIT,
reportActionID: '1',
introSelected: {choice: CONST.ONBOARDING_CHOICES.MANAGE_TEAM},
draftTransactionIDs: [],
activePolicy: undefined,
userBillingGracePeriodEnds: undefined,
amountOwed: 0,
transaction: trackedExpense,
currentUserAccountID: RORY_ACCOUNT_ID,
currentUserEmail: RORY_EMAIL,
currentUserLocalCurrency: '',
submitDestination: CONST.IOU.SUBMIT_DESTINATION.EMPLOYER,
defaultWorkspaceName: "Rory's Workspace",
filteredPoliciesCount: 0,
firstPolicy: undefined,
});

// Then navigation does not happen synchronously, because the participants merge has not landed yet and the
// confirmation page would render without a recipient and auto-open the Choose recipient picker
expect(Navigation.navigate).not.toHaveBeenCalled();

// And it happens once the merge has been applied
await waitForBatchedUpdates();
expect(getConfirmationRouteBackTo()).toBe(ROUTES.REPORT_WITH_ID.getRoute(selfDMReport.reportID));
});

it('should leave the draft transaction unreported when the destination picker is shown', async () => {
// Given a tracked self DM expense and more than one workspace the user can submit to
const {selfDMReport, trackedExpense} = await setUpSelfDMTrackedExpense();
Expand Down
Loading