Repository navigation
Discard changes confirmation closes the whole RHP #106972
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
mountiny
merged 19 commits into
Expensify:main
from
gijoe0295:feat/discard-closes-whole-rhp
Nov 21, 2026
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
dbc139d
Add getNavigationActionForReplay to replay an overlay back at the root
gijoe0295 18489dc
Close the whole RHP when Discard follows a click outside it on web
gijoe0295 7fade8c
Close the whole RHP when Discard follows a click outside it on native
gijoe0295 2967c57
Add MoneyRequestTestNavigator to drive create expense steps through r…
gijoe0295 abea713
Test with real navigators that Discard after a click outside closes t…
gijoe0295 08a8cea
Fix the RHP navigation type and drop an unused export in MoneyRequest…
gijoe0295 fe88018
Type the RHP navigation as a NavigationProp goBack in MoneyRequestTes…
gijoe0295 c818714
Close the whole RHP on Discard only for pages that opt in on web
gijoe0295 bc3dbbb
Close the whole RHP on Discard only for pages that opt in on native
gijoe0295 d6023f6
Test with real navigators that Discard after a click outside only clo…
gijoe0295 e743143
Close the whole RHP on Discard from the Amount step while creating an…
gijoe0295 c6f852c
Close the whole RHP on Discard from the Hours step while creating an …
gijoe0295 3609e24
Close the whole RHP on Discard from the map Distance step while creat…
gijoe0295 8f38253
Close the whole RHP on Discard from the Map distance tab while creati…
gijoe0295 48e7b0a
Close the whole RHP on Discard from the manual Distance step while cr…
gijoe0295 bf1afdc
Close the whole RHP on Discard from the Odometer step while creating …
gijoe0295 fdd6ee6
Simplify comments
gijoe0295 9877c80
Simplify comments
gijoe0295 12eb6b5
Remove shouldCloseRHPOnDiscard from Amount step
gijoe0295 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/libs/Navigation/helpers/getNavigationActionForReplay.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| /** | ||
| * Prepares a navigation that a "Discard changes?" prompt blocked, so sending it again after Discard resumes it. | ||
| */ | ||
| import navigationRef from '@libs/Navigation/navigationRef'; | ||
|
|
||
| import CONST from '@src/CONST'; | ||
|
|
||
| import type {NavigationAction} from '@react-navigation/native'; | ||
|
|
||
| const BACK_ACTION_TYPES = new Set<string>([CONST.NAVIGATION.ACTION_TYPE.GO_BACK, CONST.NAVIGATION.ACTION_TYPE.POP]); | ||
|
|
||
| /** | ||
| * Makes Discard close the whole RHP when the user clicked outside it. | ||
| * | ||
| * That click sends a "go back" with no target from the RHP's route, so the root closes the RHP. Sent again as is, the innermost | ||
| * navigator handles it and only closes the top screen, so it's aimed at the root instead. Other actions are returned unchanged. | ||
| * The copy keeps react-navigation's marker that stops the same screen from blocking it again. | ||
| */ | ||
| function getNavigationActionForReplay(action: NavigationAction): NavigationAction { | ||
| if (action.target || !action.source || !BACK_ACTION_TYPES.has(action.type)) { | ||
| return action; | ||
| } | ||
|
|
||
| const rootState = navigationRef.isReady() ? navigationRef.getRootState() : undefined; | ||
| if (!rootState?.routes.some((route) => route.key === action.source)) { | ||
| return action; | ||
| } | ||
|
|
||
| return {...action, target: rootState.key}; | ||
| } | ||
|
|
||
| export default getNavigationActionForReplay; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| import {act, waitFor} from '@testing-library/react-native'; | ||
|
|
||
| import type {DiscardChangesConfirmation} from '@hooks/useDiscardChangesConfirmation/types'; | ||
| import type UseDiscardChangesConfirmationOptions from '@hooks/useDiscardChangesConfirmation/types'; | ||
| import useResponsiveLayout from '@hooks/useResponsiveLayout'; | ||
|
|
||
| import getIsNarrowLayout from '@libs/getIsNarrowLayout'; | ||
| import Navigation from '@libs/Navigation/Navigation'; | ||
| import navigationRef from '@libs/Navigation/navigationRef'; | ||
|
|
||
| import CONST from '@src/CONST'; | ||
| import NAVIGATORS from '@src/NAVIGATORS'; | ||
| import SCREENS from '@src/SCREENS'; | ||
|
|
||
| import type * as ReactNavigation from '@react-navigation/native'; | ||
|
|
||
| import type * as MockUseConfirmModalUtil from '../utils/mockUseConfirmModal'; | ||
|
|
||
| import {MockModalActions, mockShowConfirmModal, resetMockConfirmModal, resolveShowConfirmModal} from '../utils/mockUseConfirmModal'; | ||
| import {clickOutsideRHP, getMoneyRequestStepNames, getRootRouteNames, renderMoneyRequestNavigation} from '../utils/MoneyRequestTestNavigator'; | ||
|
|
||
| // The shared mock stubs `usePreventRemove`, which the native hook relies on, so this test uses the real module | ||
| jest.mock('@react-navigation/native', () => jest.requireActual<typeof ReactNavigation>('@react-navigation/native')); | ||
| jest.mock('@hooks/useResponsiveLayout', () => jest.fn()); | ||
| jest.mock('@libs/getIsNarrowLayout', () => jest.fn()); | ||
|
|
||
| jest.mock('@hooks/useConfirmModal', () => { | ||
| const {default: mockUseConfirmModal} = jest.requireActual<typeof MockUseConfirmModalUtil>('../utils/mockUseConfirmModal'); | ||
| return mockUseConfirmModal; | ||
| }); | ||
|
|
||
| jest.mock('@components/Modal/Global/ModalContext', () => { | ||
| const {createMockModalContextModule} = jest.requireActual<typeof MockUseConfirmModalUtil>('../utils/mockUseConfirmModal'); | ||
| return createMockModalContextModule(); | ||
| }); | ||
|
|
||
| jest.mock('@hooks/useLocalize', () => ({ | ||
| __esModule: true, | ||
| default: () => ({translate: (key: string) => key}), | ||
| })); | ||
|
|
||
| type DiscardHookModule = {default: (options: UseDiscardChangesConfirmationOptions) => DiscardChangesConfirmation}; | ||
|
|
||
| // Jest resolves platform extensions native-first, so each implementation is loaded explicitly to run the same scenarios on both | ||
| const useWebDiscardChangesConfirmation = jest.requireActual<DiscardHookModule>('@hooks/useDiscardChangesConfirmation/index.ts').default; | ||
| const useNativeDiscardChangesConfirmation = jest.requireActual<DiscardHookModule>('@hooks/useDiscardChangesConfirmation/index.native.ts').default; | ||
|
|
||
| describe.each([ | ||
| ['web', useWebDiscardChangesConfirmation], | ||
| ['native', useNativeDiscardChangesConfirmation], | ||
| ])('Discarding changes on a step stacked in the RHP (%s)', (_, useDiscardChangesConfirmation) => { | ||
| /** A create expense step with unsaved changes, which closes the whole RHP on Discard */ | ||
| function DirtyStep() { | ||
| useDiscardChangesConfirmation({getHasUnsavedChanges: () => true, shouldCloseRHPOnDiscard: true}); | ||
| return null; | ||
| } | ||
|
|
||
| /** A page with unsaved changes outside the create expense flow, such as the agent avatar page */ | ||
| function DirtyPageOutsideCreateFlow() { | ||
| useDiscardChangesConfirmation({getHasUnsavedChanges: () => true}); | ||
| return null; | ||
| } | ||
|
|
||
| function renderDirtyStepOverFirstStep(dirtyStep = DirtyStep) { | ||
| renderMoneyRequestNavigation({ | ||
| openSteps: [SCREENS.MONEY_REQUEST.CREATE, SCREENS.MONEY_REQUEST.DYNAMIC_STEP_DESTINATION], | ||
| stepComponents: {[SCREENS.MONEY_REQUEST.DYNAMIC_STEP_DESTINATION]: dirtyStep}, | ||
| stepParams: {action: CONST.IOU.ACTION.CREATE, iouType: CONST.IOU.TYPE.CREATE, transactionID: '1', reportID: '1'}, | ||
| }); | ||
| } | ||
|
|
||
| beforeEach(() => { | ||
| resetMockConfirmModal(); | ||
| jest.mocked(getIsNarrowLayout).mockReturnValue(true); | ||
| jest.mocked(useResponsiveLayout).mockReturnValue({...CONST.NAVIGATION_TESTS.DEFAULT_USE_RESPONSIVE_LAYOUT_VALUE, shouldUseNarrowLayout: true}); | ||
| }); | ||
|
|
||
| it('closes the whole RHP when the user clicks outside it and picks Discard', async () => { | ||
| // Given a step with unsaved changes stacked on another step in the RHP | ||
| renderDirtyStepOverFirstStep(); | ||
|
|
||
| // When the user clicks outside the RHP | ||
| clickOutsideRHP(); | ||
|
|
||
| // Then the RHP stays open while the user is asked | ||
| expect(mockShowConfirmModal).toHaveBeenCalledTimes(1); | ||
| expect(getRootRouteNames()).toEqual([NAVIGATORS.TAB_NAVIGATOR, NAVIGATORS.RIGHT_MODAL_NAVIGATOR]); | ||
|
|
||
| // When the user picks Discard | ||
| resolveShowConfirmModal({action: MockModalActions.CONFIRM}); | ||
|
|
||
| // Then the whole RHP closes, as the click asked, instead of only the step on top. | ||
| await waitFor(() => expect(getRootRouteNames()).toEqual([NAVIGATORS.TAB_NAVIGATOR])); | ||
| }); | ||
|
|
||
| it('only closes a page outside the create expense flow when the user clicks outside the RHP and picks Discard', async () => { | ||
| // Given a page with unsaved changes outside the create expense flow, stacked on another page in the RHP | ||
| renderDirtyStepOverFirstStep(DirtyPageOutsideCreateFlow); | ||
|
|
||
| // When the user clicks outside the RHP and picks Discard | ||
| clickOutsideRHP(); | ||
| expect(mockShowConfirmModal).toHaveBeenCalledTimes(1); | ||
| resolveShowConfirmModal({action: MockModalActions.CONFIRM}); | ||
|
|
||
| // Then only that page closes and the RHP stays on the page below it, as before | ||
| await waitFor(() => expect(getMoneyRequestStepNames()).toEqual([SCREENS.MONEY_REQUEST.CREATE])); | ||
| expect(getRootRouteNames()).toEqual([NAVIGATORS.TAB_NAVIGATOR, NAVIGATORS.RIGHT_MODAL_NAVIGATOR]); | ||
| }); | ||
|
|
||
| it('keeps the RHP and the step open when the user cancels', async () => { | ||
| // Given a step with unsaved changes stacked on another step in the RHP | ||
| renderDirtyStepOverFirstStep(); | ||
|
|
||
| // When the user clicks outside the RHP and cancels the prompt | ||
| clickOutsideRHP(); | ||
| expect(mockShowConfirmModal).toHaveBeenCalledTimes(1); | ||
| await act(async () => { | ||
| resolveShowConfirmModal({action: MockModalActions.CLOSE}); | ||
| }); | ||
|
|
||
| // Then nothing closes, so the changes are still there | ||
| expect(getRootRouteNames()).toEqual([NAVIGATORS.TAB_NAVIGATOR, NAVIGATORS.RIGHT_MODAL_NAVIGATOR]); | ||
| expect(getMoneyRequestStepNames()).toEqual([SCREENS.MONEY_REQUEST.CREATE, SCREENS.MONEY_REQUEST.DYNAMIC_STEP_DESTINATION]); | ||
| }); | ||
|
|
||
| it('closes the whole RHP when the user presses Esc and picks Discard', async () => { | ||
| // Given a step with unsaved changes stacked on another step in the RHP | ||
| renderDirtyStepOverFirstStep(); | ||
|
|
||
| // When the user presses Esc, which dismisses the modal, and picks Discard | ||
| act(() => { | ||
| Navigation.dismissModal(); | ||
| }); | ||
| expect(mockShowConfirmModal).toHaveBeenCalledTimes(1); | ||
| resolveShowConfirmModal({action: MockModalActions.CONFIRM}); | ||
|
|
||
| // Then the whole RHP closes | ||
| await waitFor(() => expect(getRootRouteNames()).toEqual([NAVIGATORS.TAB_NAVIGATOR])); | ||
| }); | ||
|
|
||
| it('only leaves the step when the user goes back from it and picks Discard', async () => { | ||
| // Given a step with unsaved changes stacked on another step in the RHP | ||
| renderDirtyStepOverFirstStep(); | ||
|
|
||
| // When the user goes back from the step, which the step's own stack handles, and picks Discard | ||
| act(() => { | ||
| navigationRef.current?.goBack(); | ||
| }); | ||
| expect(mockShowConfirmModal).toHaveBeenCalledTimes(1); | ||
| resolveShowConfirmModal({action: MockModalActions.CONFIRM}); | ||
|
|
||
| // Then only the step closes and the RHP stays on the step below it | ||
| await waitFor(() => expect(getMoneyRequestStepNames()).toEqual([SCREENS.MONEY_REQUEST.CREATE])); | ||
| expect(getRootRouteNames()).toEqual([NAVIGATORS.TAB_NAVIGATOR, NAVIGATORS.RIGHT_MODAL_NAVIGATOR]); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| import getNavigationActionForReplay from '@libs/Navigation/helpers/getNavigationActionForReplay'; | ||
|
|
||
| let mockIsNavigationReady = true; | ||
| jest.mock('@libs/Navigation/navigationRef', () => ({ | ||
| __esModule: true, | ||
| default: { | ||
| isReady: () => mockIsNavigationReady, | ||
| getRootState: () => ({key: 'root', routes: [{key: 'tab-navigator'}, {key: 'rhp-route'}]}), | ||
| }, | ||
| })); | ||
|
|
||
| describe('getNavigationActionForReplay', () => { | ||
| beforeEach(() => { | ||
| mockIsNavigationReady = true; | ||
| }); | ||
|
|
||
| it('targets the root navigator for a back without a target, sent from a root-level route', () => { | ||
| // Given the RHP overlay's GO_BACK without a target, sent from the root-level RHP route | ||
| const action = {type: 'GO_BACK', source: 'rhp-route'}; | ||
|
|
||
| // When preparing it for replay through the container | ||
| const actionForReplay = getNavigationActionForReplay(action); | ||
|
|
||
| // Then it targets the root, which closed the whole RHP the first time | ||
| expect(actionForReplay).toEqual({type: 'GO_BACK', source: 'rhp-route', target: 'root'}); | ||
| }); | ||
|
|
||
| it.each([ | ||
| ['a targeted action', {type: 'POP', source: 'rhp-route', target: 'money-request-stack'}], | ||
| ['an action without a source', {type: 'GO_BACK'}], | ||
| ['an action that is not a back', {type: 'NAVIGATE', source: 'rhp-route'}], | ||
| ['an action sent from a nested screen', {type: 'GO_BACK', source: 'confirmation-route'}], | ||
| ])('replays %s unchanged', (_, action) => { | ||
| // Given an action the deepest navigator handles the same way on replay | ||
| // When preparing it for replay | ||
| // Then it's left as is | ||
| expect(getNavigationActionForReplay(action)).toBe(action); | ||
| }); | ||
|
|
||
| it('replays the action unchanged before navigation is ready', () => { | ||
| // Given a navigation container that isn't ready | ||
| mockIsNavigationReady = false; | ||
| const action = {type: 'GO_BACK', source: 'rhp-route'}; | ||
|
|
||
| // When preparing an overlay back for replay | ||
| // Then it's left as is | ||
| expect(getNavigationActionForReplay(action)).toBe(action); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.