From 5ab422bc42e65b07e6ca2570186ad42ee77321a7 Mon Sep 17 00:00:00 2001 From: yuldashevyusuf Date: Wed, 16 Sep 2026 18:07:47 +0500 Subject: [PATCH 1/2] Keep the current tab when opening an exitTo link from a signed-in validate login --- src/libs/actions/Session/index.ts | 2 +- .../HandleExitToNavigationTests.tsx | 150 ++++++++++++++++++ 2 files changed, 151 insertions(+), 1 deletion(-) create mode 100644 tests/navigation/HandleExitToNavigationTests.tsx diff --git a/src/libs/actions/Session/index.ts b/src/libs/actions/Session/index.ts index 81c76b2a9d06..a48f0dbbe516 100644 --- a/src/libs/actions/Session/index.ts +++ b/src/libs/actions/Session/index.ts @@ -1600,7 +1600,7 @@ function waitForUserSignIn(): Promise { function handleExitToNavigation(exitTo: Route) { waitForUserSignIn().then(() => { Navigation.waitForProtectedRoutes().then(() => { - Navigation.goBack(ROUTES.HOME, {waitForTransition: true}); + Navigation.goBack(navigationRef.canGoBack() ? undefined : ROUTES.HOME, {waitForTransition: true}); Navigation.navigate(exitTo, {waitForTransition: true}); }); }); diff --git a/tests/navigation/HandleExitToNavigationTests.tsx b/tests/navigation/HandleExitToNavigationTests.tsx new file mode 100644 index 000000000000..245f23a85606 --- /dev/null +++ b/tests/navigation/HandleExitToNavigationTests.tsx @@ -0,0 +1,150 @@ +import {act, render} from '@testing-library/react-native'; + +import useResponsiveLayout from '@hooks/useResponsiveLayout'; + +import getIsNarrowLayout from '@libs/getIsNarrowLayout'; +import Navigation from '@libs/Navigation/Navigation'; +import navigationRef from '@libs/Navigation/navigationRef'; + +import {handleExitToNavigation} from '@userActions/Session'; + +import CONST from '@src/CONST'; +import NAVIGATORS from '@src/NAVIGATORS'; +import ONYXKEYS from '@src/ONYXKEYS'; +import ROUTES from '@src/ROUTES'; +import SCREENS from '@src/SCREENS'; + +import type {InitialState} from '@react-navigation/native'; + +import React from 'react'; +import Onyx from 'react-native-onyx'; + +import TestNavigationContainer from '../utils/TestNavigationContainer'; +import waitForBatchedUpdates from '../utils/waitForBatchedUpdates'; + +jest.mock('@hooks/useResponsiveLayout', () => jest.fn()); +jest.mock('@libs/getIsNarrowLayout', () => jest.fn()); + +jest.mock('@pages/inbox/sidebar/NavigationTabBarAvatar'); + +const mockedGetIsNarrowLayout = jest.mocked(getIsNarrowLayout); +const mockedUseResponsiveLayout = jest.mocked(useResponsiveLayout); + +const REPORT_ID = '1'; + +function getRoutesWithTabNavigatorUnderValidateLogin(activeTabIndex: number): InitialState['routes'] { + return [ + { + name: NAVIGATORS.TAB_NAVIGATOR, + state: { + index: activeTabIndex, + routes: [ + {name: SCREENS.HOME}, + { + name: NAVIGATORS.REPORTS_SPLIT_NAVIGATOR, + state: {index: 0, routes: [{name: SCREENS.INBOX}]}, + }, + { + name: NAVIGATORS.SEARCH_FULLSCREEN_NAVIGATOR, + state: {index: 0, routes: [{name: SCREENS.SEARCH.ROOT}]}, + }, + {name: SCREENS.INSIGHTS}, + {name: NAVIGATORS.SETTINGS_SPLIT_NAVIGATOR}, + {name: NAVIGATORS.WORKSPACE_NAVIGATOR}, + ], + }, + }, + {name: SCREENS.VALIDATE_LOGIN}, + ]; +} + +function getTabNavigatorStates() { + return navigationRef.current + ?.getRootState() + .routes.filter((route) => route.name === NAVIGATORS.TAB_NAVIGATOR) + .map((route) => route.state); +} + +function hasValidateLoginRoute() { + return !!navigationRef.current?.getRootState().routes.some((route) => route.name === SCREENS.VALIDATE_LOGIN); +} + +async function runHandleExitToNavigation() { + await act(async () => { + handleExitToNavigation(ROUTES.REPORT_WITH_ID.getRoute(REPORT_ID)); + await waitForBatchedUpdates(); + }); +} + +describe('handleExitToNavigation', () => { + beforeAll(() => { + Onyx.init({keys: ONYXKEYS}); + }); + + beforeEach(async () => { + mockedGetIsNarrowLayout.mockReturnValue(true); + mockedUseResponsiveLayout.mockReturnValue({ + ...CONST.NAVIGATION_TESTS.DEFAULT_USE_RESPONSIVE_LAYOUT_VALUE, + shouldUseNarrowLayout: true, + }); + jest.spyOn(Navigation, 'waitForProtectedRoutes').mockResolvedValue(); + await Onyx.set(ONYXKEYS.SESSION, {authToken: 'authToken', accountID: 1}); + await waitForBatchedUpdates(); + }); + + afterEach(async () => { + jest.restoreAllMocks(); + await Onyx.clear(); + }); + + it('opens the report inside the Inbox tab when the link is opened from Inbox', async () => { + render( + , + ); + + await runHandleExitToNavigation(); + + expect(hasValidateLoginRoute()).toBe(false); + const tabState = getTabNavigatorStates()?.at(-1); + const focusedTab = tabState?.routes.at(tabState.index ?? 0); + expect(focusedTab?.name).toBe(NAVIGATORS.REPORTS_SPLIT_NAVIGATOR); + expect(focusedTab?.state?.routes.at(-1)?.name).toBe(SCREENS.REPORT); + }); + + it('keeps Spend under the report when the link is opened from Spend', async () => { + render( + , + ); + + await runHandleExitToNavigation(); + + expect(hasValidateLoginRoute()).toBe(false); + const tabStates = getTabNavigatorStates(); + const focusedTabState = tabStates?.at(-1); + const focusedTab = focusedTabState?.routes.at(focusedTabState.index ?? 0); + expect(focusedTab?.name).toBe(NAVIGATORS.REPORTS_SPLIT_NAVIGATOR); + expect(focusedTab?.state?.routes.at(-1)?.name).toBe(SCREENS.REPORT); + expect(tabStates?.at(0)?.index).toBe(2); + }); + + it('goes back to Home when ValidateLogin is the only route', async () => { + const goBackSpy = jest.spyOn(Navigation, 'goBack'); + render(); + + await runHandleExitToNavigation(); + + expect(goBackSpy).toHaveBeenCalledWith(ROUTES.HOME, { + waitForTransition: true, + }); + }); +}); From 721ec6117aa05659c3a4e660d33ae5d17567effa Mon Sep 17 00:00:00 2001 From: yuldashevyusuf Date: Wed, 16 Sep 2026 19:16:59 +0500 Subject: [PATCH 2/2] Remove ValidateLogin directly instead of through goBack when opening an exitTo link --- src/libs/actions/Session/index.ts | 6 +++++- .../HandleExitToNavigationTests.tsx | 21 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/libs/actions/Session/index.ts b/src/libs/actions/Session/index.ts index a48f0dbbe516..e18fb59bdc3d 100644 --- a/src/libs/actions/Session/index.ts +++ b/src/libs/actions/Session/index.ts @@ -1600,7 +1600,11 @@ function waitForUserSignIn(): Promise { function handleExitToNavigation(exitTo: Route) { waitForUserSignIn().then(() => { Navigation.waitForProtectedRoutes().then(() => { - Navigation.goBack(navigationRef.canGoBack() ? undefined : ROUTES.HOME, {waitForTransition: true}); + if (navigationRef.canGoBack()) { + Navigation.pop(navigationRef.getRootState().key); + } else { + Navigation.goBack(ROUTES.HOME, {waitForTransition: true}); + } Navigation.navigate(exitTo, {waitForTransition: true}); }); }); diff --git a/tests/navigation/HandleExitToNavigationTests.tsx b/tests/navigation/HandleExitToNavigationTests.tsx index 245f23a85606..8993bf471b4c 100644 --- a/tests/navigation/HandleExitToNavigationTests.tsx +++ b/tests/navigation/HandleExitToNavigationTests.tsx @@ -137,6 +137,27 @@ describe('handleExitToNavigation', () => { expect(tabStates?.at(0)?.index).toBe(2); }); + it('removes ValidateLogin even when a pop to sidebar is pending', async () => { + render( + , + ); + Navigation.setShouldPopToSidebar(true); + + await runHandleExitToNavigation(); + + expect(hasValidateLoginRoute()).toBe(false); + const tabState = getTabNavigatorStates()?.at(-1); + const focusedTab = tabState?.routes.at(tabState.index ?? 0); + expect(focusedTab?.name).toBe(NAVIGATORS.REPORTS_SPLIT_NAVIGATOR); + expect(focusedTab?.state?.routes.at(-1)?.name).toBe(SCREENS.REPORT); + Navigation.setShouldPopToSidebar(false); + }); + it('goes back to Home when ValidateLogin is the only route', async () => { const goBackSpy = jest.spyOn(Navigation, 'goBack'); render();