From 4fcc185ab71a79309bfdc98418a6487187efa09b Mon Sep 17 00:00:00 2001 From: Georgia Monahan <38015950+grgia@users.noreply.github.com> Date: Wed, 25 Mar 2026 08:45:33 +0000 Subject: [PATCH] Revert "Preserve deep-linked report route during onboarding guard redirect" --- .../RootStackRouter.ts | 24 +-- .../handleNavigationGuardRedirect.test.ts | 189 ------------------ 2 files changed, 4 insertions(+), 209 deletions(-) delete mode 100644 tests/unit/Navigation/guards/handleNavigationGuardRedirect.test.ts diff --git a/src/libs/Navigation/AppNavigator/createRootStackNavigator/RootStackRouter.ts b/src/libs/Navigation/AppNavigator/createRootStackNavigator/RootStackRouter.ts index e41ed17ed863..fc70a1464801 100644 --- a/src/libs/Navigation/AppNavigator/createRootStackNavigator/RootStackRouter.ts +++ b/src/libs/Navigation/AppNavigator/createRootStackNavigator/RootStackRouter.ts @@ -1,5 +1,5 @@ import {CommonActions, StackRouter} from '@react-navigation/native'; -import type {NavigationState, PartialState, RouterConfigOptions, StackActionType, StackNavigationState} from '@react-navigation/native'; +import type {RouterConfigOptions, StackActionType, StackNavigationState} from '@react-navigation/native'; import type {ParamListBase} from '@react-navigation/routers'; import {createGuardContext, evaluateGuards} from '@libs/Navigation/guards'; import getAdaptedStateFromPath from '@libs/Navigation/helpers/getAdaptedStateFromPath'; @@ -94,26 +94,10 @@ function handleNavigationGuards( return null; } - const redirectRoute = redirectState.routes.at(-1); - - // If the focused route is already the redirect target (e.g., multiple actions triggered - // on fresh app open all fire the same guard), don't add it again. - if (redirectRoute && state.routes[state.index]?.name === redirectRoute.name) { - return state; - } - - const hasExistingFullScreenRoute = state.routes.some((route) => isFullScreenName(route.name)); - - // When the current stack already has a fullscreen route (e.g., a deep-linked report), - // append only the redirect target on top of the existing routes so the user returns - // to them after the redirect screen is dismissed. Otherwise (fresh app with no stack), - // use the full redirect state which includes the base route (e.g., Home). - const routes = hasExistingFullScreenRoute && redirectRoute ? [...state.routes, redirectRoute] : redirectState.routes; - const resetAction = CommonActions.reset({ - index: routes.length - 1, - routes, - } as PartialState); + index: redirectState.index ?? redirectState.routes.length - 1, + routes: redirectState.routes, + }); return stackRouter.getStateForAction(state, resetAction, configOptions); } diff --git a/tests/unit/Navigation/guards/handleNavigationGuardRedirect.test.ts b/tests/unit/Navigation/guards/handleNavigationGuardRedirect.test.ts deleted file mode 100644 index 17edec9a1fec..000000000000 --- a/tests/unit/Navigation/guards/handleNavigationGuardRedirect.test.ts +++ /dev/null @@ -1,189 +0,0 @@ -import type {StackNavigationState} from '@react-navigation/native'; -import type {ParamListBase} from '@react-navigation/routers'; -import RootStackRouter from '@libs/Navigation/AppNavigator/createRootStackNavigator/RootStackRouter'; -import {evaluateGuards} from '@libs/Navigation/guards'; -import getAdaptedStateFromPath from '@libs/Navigation/helpers/getAdaptedStateFromPath'; -import NAVIGATORS from '@src/NAVIGATORS'; -import SCREENS from '@src/SCREENS'; - -jest.mock('@libs/Navigation/guards', () => ({ - // eslint-disable-next-line @typescript-eslint/naming-convention - __esModule: true, - createGuardContext: jest.fn(() => ({ - isAuthenticated: true, - isLoading: false, - currentUrl: '', - })), - evaluateGuards: jest.fn(() => ({type: 'ALLOW'})), - registerGuard: jest.fn(), - clearGuards: jest.fn(), - getRegisteredGuards: jest.fn(() => []), -})); - -jest.mock('@libs/Navigation/helpers/getAdaptedStateFromPath', () => ({ - // eslint-disable-next-line @typescript-eslint/naming-convention - __esModule: true, - default: jest.fn(), -})); - -const mockedEvaluateGuards = evaluateGuards as jest.Mock; -const mockedGetAdaptedStateFromPath = getAdaptedStateFromPath as jest.Mock; - -const routeNames = [SCREENS.HOME, NAVIGATORS.REPORTS_SPLIT_NAVIGATOR, NAVIGATORS.ONBOARDING_MODAL_NAVIGATOR, NAVIGATORS.SETTINGS_SPLIT_NAVIGATOR]; - -describe('handleNavigationGuards - REDIRECT stack preservation', () => { - const router = RootStackRouter({}); - - const configOptions = { - routeNames, - routeParamList: {} as ParamListBase, - routeGetIdList: {} as Record) => string) | undefined>, - }; - - const mockAction = { - type: 'NAVIGATE' as const, - payload: {name: SCREENS.HOME}, - }; - - beforeEach(() => { - jest.clearAllMocks(); - mockedEvaluateGuards.mockReturnValue({type: 'ALLOW'}); - }); - - it('should preserve existing fullscreen routes and append redirect target on top', () => { - // Given the current stack has a deep-linked report (a fullscreen route) - const state: StackNavigationState = { - key: 'root', - index: 0, - routeNames, - routes: [{key: 'report-1', name: NAVIGATORS.REPORTS_SPLIT_NAVIGATOR, params: undefined}], - stale: false, - type: 'stack', - preloadedRoutes: [], - }; - - // When the guard returns REDIRECT to onboarding and getAdaptedStateFromPath returns a state with Home + OnboardingModalNavigator - mockedEvaluateGuards.mockReturnValue({type: 'REDIRECT', route: 'onboarding/purpose'}); - mockedGetAdaptedStateFromPath.mockReturnValue({ - routes: [ - {name: SCREENS.HOME, key: 'home-1'}, - {name: NAVIGATORS.ONBOARDING_MODAL_NAVIGATOR, key: 'onboarding-1'}, - ], - }); - - const result = router.getStateForAction(state, mockAction, configOptions); - - // Then the deep-linked report should be preserved and onboarding should be appended on top - expect(result).not.toBeNull(); - expect(result?.routes).toHaveLength(2); - expect(result?.routes[0].name).toBe(NAVIGATORS.REPORTS_SPLIT_NAVIGATOR); - expect(result?.routes[1].name).toBe(NAVIGATORS.ONBOARDING_MODAL_NAVIGATOR); - expect(result?.index).toBe(1); - }); - - it('should use the full redirect state when no existing fullscreen route is present', () => { - // Given a fresh app state with no fullscreen routes (e.g., only a non-fullscreen route) - const state: StackNavigationState = { - key: 'root', - index: 0, - routeNames: [...routeNames, 'SomeNonFullScreenRoute'], - routes: [{key: 'other-1', name: 'SomeNonFullScreenRoute', params: undefined}], - stale: false, - type: 'stack', - preloadedRoutes: [], - }; - - // When the guard returns REDIRECT to onboarding - mockedEvaluateGuards.mockReturnValue({type: 'REDIRECT', route: 'onboarding/purpose'}); - mockedGetAdaptedStateFromPath.mockReturnValue({ - routes: [ - {name: SCREENS.HOME, key: 'home-1'}, - {name: NAVIGATORS.ONBOARDING_MODAL_NAVIGATOR, key: 'onboarding-1'}, - ], - }); - - const result = router.getStateForAction(state, mockAction, configOptions); - - // Then the full redirect state (Home + Onboarding) should be used since there's no fullscreen route to preserve - expect(result).not.toBeNull(); - expect(result?.routes).toHaveLength(2); - expect(result?.routes[0].name).toBe(SCREENS.HOME); - expect(result?.routes[1].name).toBe(NAVIGATORS.ONBOARDING_MODAL_NAVIGATOR); - expect(result?.index).toBe(1); - }); - - it('should not duplicate the redirect target when it is already the focused route', () => { - // Given the current stack already has the onboarding route as the focused route - const state: StackNavigationState = { - key: 'root', - index: 1, - routeNames, - routes: [ - {key: 'home-1', name: SCREENS.HOME, params: undefined}, - {key: 'onboarding-1', name: NAVIGATORS.ONBOARDING_MODAL_NAVIGATOR, params: undefined}, - ], - stale: false, - type: 'stack', - preloadedRoutes: [], - }; - - // When the guard fires the same REDIRECT again - mockedEvaluateGuards.mockReturnValue({type: 'REDIRECT', route: 'onboarding/purpose'}); - mockedGetAdaptedStateFromPath.mockReturnValue({ - routes: [ - {name: SCREENS.HOME, key: 'home-2'}, - {name: NAVIGATORS.ONBOARDING_MODAL_NAVIGATOR, key: 'onboarding-2'}, - ], - }); - - const result = router.getStateForAction(state, mockAction, configOptions); - - // Then the state should be unchanged (no duplicate onboarding route added) - expect(result).toEqual(state); - }); - - it('should not process redirect when guard allows navigation', () => { - // Given the guard allows navigation - const state: StackNavigationState = { - key: 'root', - index: 0, - routeNames, - routes: [{key: 'home-1', name: SCREENS.HOME, params: undefined}], - stale: false, - type: 'stack', - preloadedRoutes: [], - }; - - mockedEvaluateGuards.mockReturnValue({type: 'ALLOW'}); - - // Normal routing may error with minimal config — we only care that redirect logic was not triggered - try { - router.getStateForAction(state, mockAction, configOptions); - } catch { - // Expected: StackRouter needs full config for normal routing - } - - // Then getAdaptedStateFromPath should NOT have been called (no redirect processing) - expect(mockedGetAdaptedStateFromPath).not.toHaveBeenCalled(); - }); - - it('should return unchanged state when guard blocks navigation', () => { - // Given the guard blocks navigation - const state: StackNavigationState = { - key: 'root', - index: 0, - routeNames, - routes: [{key: 'home-1', name: SCREENS.HOME, params: undefined}], - stale: false, - type: 'stack', - preloadedRoutes: [], - }; - - mockedEvaluateGuards.mockReturnValue({type: 'BLOCK', reason: 'Test block'}); - - const result = router.getStateForAction(state, mockAction, configOptions); - - // Then the state should be returned unchanged - expect(result).toEqual(state); - }); -});