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: 5 additions & 1 deletion src/libs/actions/Session/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1600,7 +1600,11 @@ function waitForUserSignIn(): Promise<boolean> {
function handleExitToNavigation(exitTo: Route) {
waitForUserSignIn().then(() => {
Navigation.waitForProtectedRoutes().then(() => {
Navigation.goBack(ROUTES.HOME, {waitForTransition: true});
if (navigationRef.canGoBack()) {
Navigation.pop(navigationRef.getRootState().key);

@QichenZhu QichenZhu Sep 21, 2026 •

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.

I'm not questioning it, just curious why you went with this approach instead of your original proposal. In what scenario would these two approaches behave differently, so I can test it?

@yusufdeveloper2903 yusufdeveloper2903 Sep 22, 2026 •

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.

Short answer on why I moved off my proposal's line.

goBack(undefined) doesn't always pop ValidateLogin. When shouldPopToSidebar is set, goBack takes the popToSidebar() branch first, and popToSidebar() clears the flag and returns without popping anything, because the top root route is ValidateLogin and not a split navigator. NavigationRoot sets that flag on any wide → narrow change while a split navigator is on top (e.g. rotating landscape → portrait with a report open), and it stays set until the next parameterless goBack().

I ran both versions through the real navigators in Jest with the flag set. The report opens on the right tab either way, so the fix for this issue holds in both, but the resulting root stack differs:

  • this PR: [TabNavigator → Inbox split: Inbox, Report]
  • my original line: [TabNavigator(Inbox), ValidateLogin, TabNavigator(Inbox split: Report)]

So with the proposal's line the consumed ValidateLogin stays in the stack and the report is pushed above it in a second tab navigator: Back from the report shows the /v/ loading screen, and only the second Back returns to the report list.

Navigation.pop(navigationRef.getRootState().key) is dispatched at the root stack directly, so the flag can't redirect it and the only route it can remove is ValidateLogin. The lone-route case is unchanged: still goBack(ROUTES.HOME, {waitForTransition: true}). This is covered by removes ValidateLogin even when a pop to sidebar is pending — it fails with my original line (Expected: false Received: true) and passes with this one.

} else {
Navigation.goBack(ROUTES.HOME, {waitForTransition: true});
}
Navigation.navigate(exitTo, {waitForTransition: true});
});
});
Expand Down
171 changes: 171 additions & 0 deletions tests/navigation/HandleExitToNavigationTests.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
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(
<TestNavigationContainer
initialState={{
index: 1,
routes: getRoutesWithTabNavigatorUnderValidateLogin(1),
}}
/>,
);

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(
<TestNavigationContainer
initialState={{
index: 1,
routes: getRoutesWithTabNavigatorUnderValidateLogin(2),
}}
/>,
);

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('removes ValidateLogin even when a pop to sidebar is pending', async () => {
render(
<TestNavigationContainer
initialState={{
index: 1,
routes: getRoutesWithTabNavigatorUnderValidateLogin(1),
}}
/>,
);
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(<TestNavigationContainer initialState={{index: 0, routes: [{name: SCREENS.VALIDATE_LOGIN}]}} />);

await runHandleExitToNavigation();

expect(goBackSpy).toHaveBeenCalledWith(ROUTES.HOME, {
waitForTransition: true,
});
});
});
Loading