From de7a08694a20854ba40ab4810e0147e47ea4605c Mon Sep 17 00:00:00 2001 From: yuldashevyusuf Date: Tue, 8 Sep 2026 20:13:25 +0500 Subject: [PATCH] Fix back button loop on Profile after copiloting into an agent --- .../AppNavigator/withAgentAccessDenied.tsx | 7 +- .../navigation/AgentCopilotRedirectTests.tsx | 272 ++++++++++++++++++ tests/unit/withAgentAccessDenied.test.tsx | 5 +- 3 files changed, 282 insertions(+), 2 deletions(-) create mode 100644 tests/navigation/AgentCopilotRedirectTests.tsx diff --git a/src/libs/Navigation/AppNavigator/withAgentAccessDenied.tsx b/src/libs/Navigation/AppNavigator/withAgentAccessDenied.tsx index adbce8c44595..272963e72772 100644 --- a/src/libs/Navigation/AppNavigator/withAgentAccessDenied.tsx +++ b/src/libs/Navigation/AppNavigator/withAgentAccessDenied.tsx @@ -36,7 +36,12 @@ function withAgentAccessDenied(getComponent: () => React.ComponentType): () => R // forceReplace REPLACEs the stale guarded central-pane route instead of PUSHing Profile on // top of it, so back from Profile pops to the unguarded Account sidebar rather than the // guarded route that would re-fire this redirect. - const redirectToProfile = () => Navigation.navigate(ROUTES.SETTINGS_PROFILE.getRoute(), {forceReplace: true}); + const redirectToProfile = () => { + Navigation.popRootToTop(); + Navigation.navigate(ROUTES.SETTINGS_PROFILE.getRoute(), { + forceReplace: true, + }); + }; // The guarded screen can be open inside a modal/RHP (e.g. the agent-edit page the owner was // on when they tapped "Copilot into account"), or an unguarded RHP (e.g. the agent DM) can be diff --git a/tests/navigation/AgentCopilotRedirectTests.tsx b/tests/navigation/AgentCopilotRedirectTests.tsx new file mode 100644 index 000000000000..111fb668e62c --- /dev/null +++ b/tests/navigation/AgentCopilotRedirectTests.tsx @@ -0,0 +1,272 @@ +import {act, render, waitFor} from '@testing-library/react-native'; + +import useIsAgentAccount from '@hooks/useIsAgentAccount'; +import useResponsiveLayout from '@hooks/useResponsiveLayout'; + +import getIsNarrowLayout from '@libs/getIsNarrowLayout'; +import createRootStackNavigator from '@libs/Navigation/AppNavigator/createRootStackNavigator'; +import createSplitNavigator from '@libs/Navigation/AppNavigator/createSplitNavigator'; +import withAgentAccessDenied from '@libs/Navigation/AppNavigator/withAgentAccessDenied'; +import Navigation from '@libs/Navigation/Navigation'; +import navigationRef from '@libs/Navigation/navigationRef'; +import type {AuthScreensParamList, ReportsSplitNavigatorParamList, RightModalNavigatorParamList, SettingsSplitNavigatorParamList, TabNavigatorParamList} from '@libs/Navigation/types'; + +import CONST from '@src/CONST'; +import NAVIGATORS from '@src/NAVIGATORS'; +import SCREENS from '@src/SCREENS'; + +import type {InitialState, NavigatorScreenParams} from '@react-navigation/native'; + +import {createBottomTabNavigator} from '@react-navigation/bottom-tabs'; +import {NavigationContainer} from '@react-navigation/native'; +import React from 'react'; + +jest.mock('@hooks/useIsAgentAccount', () => jest.fn()); +jest.mock('@hooks/useResponsiveLayout', () => jest.fn()); +jest.mock('@libs/getIsNarrowLayout', () => jest.fn()); + +const mockedUseIsAgentAccount = jest.mocked(useIsAgentAccount); +const mockedUseResponsiveLayout = jest.mocked(useResponsiveLayout); +const mockedGetIsNarrowLayout = jest.mocked(getIsNarrowLayout); + +type TestRootParamList = AuthScreensParamList & { + [NAVIGATORS.REPORTS_SPLIT_NAVIGATOR]: NavigatorScreenParams; + [NAVIGATORS.SETTINGS_SPLIT_NAVIGATOR]: NavigatorScreenParams; +}; + +const RootStack = createRootStackNavigator(); +const TabNav = createBottomTabNavigator(); +const ReportsSplit = createSplitNavigator(); +const SettingsSplit = createSplitNavigator(); +const RightModalStack = createSplitNavigator(); + +const getEmptyComponent = () => jest.fn(); + +function AgentsPageStub() { + return null; +} + +function TestReportsSplitNavigator() { + return ( + + + + + ); +} + +function TestSettingsSplitNavigator() { + return ( + + + + AgentsPageStub)} + /> + + ); +} + +function TestRightModalNavigator() { + return ( + + + + + ); +} + +function TestTabNavigator() { + return ( + + + + + + ); +} + +function TestContainer({initialState}: {initialState: InitialState}) { + return ( + + + + + + + ); +} + +const settingsTabWithAgents = { + name: NAVIGATORS.TAB_NAVIGATOR, + state: { + index: 2, + routes: [ + {name: SCREENS.HOME}, + {name: NAVIGATORS.REPORTS_SPLIT_NAVIGATOR}, + { + name: NAVIGATORS.SETTINGS_SPLIT_NAVIGATOR, + state: { + index: 1, + routes: [{name: SCREENS.SETTINGS.ROOT}, {name: SCREENS.SETTINGS.AGENTS.ROOT}], + }, + }, + ], + }, +}; + +const agentEditModal = { + name: NAVIGATORS.RIGHT_MODAL_NAVIGATOR, + state: { + index: 0, + routes: [{name: SCREENS.RIGHT_MODAL.SETTINGS}], + }, +}; + +const agentChatTab = { + name: NAVIGATORS.TAB_NAVIGATOR, + state: { + index: 1, + routes: [ + {name: SCREENS.HOME}, + { + name: NAVIGATORS.REPORTS_SPLIT_NAVIGATOR, + state: { + index: 1, + routes: [{name: SCREENS.INBOX}, {name: SCREENS.REPORT, params: {reportID: '1'}}], + }, + }, + ], + }, +}; + +const profileModal = { + name: NAVIGATORS.RIGHT_MODAL_NAVIGATOR, + state: { + index: 0, + routes: [{name: SCREENS.RIGHT_MODAL.PROFILE}], + }, +}; + +function getRootState() { + return navigationRef.current?.getRootState(); +} + +function getSettingsSplitState() { + const tabState = getRootState()?.routes.at(0)?.state; + return tabState?.routes.find((route) => route.name === NAVIGATORS.SETTINGS_SPLIT_NAVIGATOR)?.state; +} + +async function expectAgentLandedOnProfileWithAccountUnderneath() { + // Then every root entry created during the owner's session is gone, so no guarded route is left to re-fire the redirect + await waitFor( + () => { + expect(getRootState()?.routes.length).toBe(1); + }, + {timeout: CONST.MAX_TRANSITION_START_WAIT_MS * 3}, + ); + + // Then the guarded agents route is replaced by Profile instead of Profile being pushed on top of it + await waitFor( + () => { + expect(getSettingsSplitState()?.routes.map((route) => route.name)).toEqual([SCREENS.SETTINGS.ROOT, SCREENS.SETTINGS.PROFILE.ROOT]); + }, + {timeout: CONST.MAX_TRANSITION_START_WAIT_MS * 3}, + ); + + // Then going back from Profile lands on the Account page rather than looping back into Profile + act(() => { + Navigation.goBack(); + }); + expect(getSettingsSplitState()?.routes.at(-1)?.name).toBe(SCREENS.SETTINGS.ROOT); +} + +describe('Agent copilot redirect', () => { + beforeEach(() => { + mockedGetIsNarrowLayout.mockReturnValue(true); + mockedUseResponsiveLayout.mockReturnValue({ + ...CONST.NAVIGATION_TESTS.DEFAULT_USE_RESPONSIVE_LAYOUT_VALUE, + shouldUseNarrowLayout: true, + }); + mockedUseIsAgentAccount.mockReturnValue(true); + }); + + it('lands the agent on Profile with the Account page underneath when copiloting from the agent chat profile', async () => { + // Given the stack built by Agents > agent > chat with agent > chat header, at the moment "Copilot into account" is tapped + render( + , + ); + + // When the session flips to an agent and the guard redirects + await expectAgentLandedOnProfileWithAccountUnderneath(); + }); + + it('lands the agent on Profile with the Account page underneath when copiloting straight from the agent page', async () => { + // Given the shorter stack built by Agents > agent, at the moment "Copilot into account" is tapped + render( + , + ); + + // When the session flips to an agent and the guard redirects + await expectAgentLandedOnProfileWithAccountUnderneath(); + }); +}); diff --git a/tests/unit/withAgentAccessDenied.test.tsx b/tests/unit/withAgentAccessDenied.test.tsx index bc321222ba93..20baaec0c208 100644 --- a/tests/unit/withAgentAccessDenied.test.tsx +++ b/tests/unit/withAgentAccessDenied.test.tsx @@ -27,6 +27,7 @@ jest.mock('@libs/Navigation/Navigation', () => ({ getActiveRoute: jest.fn(() => ''), isActiveRoute: jest.fn(() => false), isTopmostRouteModalScreen: jest.fn(() => false), + popRootToTop: jest.fn(), isNavigationReady: jest.fn(() => Promise.resolve()), })); @@ -52,7 +53,9 @@ jest.mock('@react-navigation/native', () => { }; }); -jest.mock('@hooks/useResponsiveLayout', () => () => ({shouldUseNarrowLayout: false})); +jest.mock('@hooks/useResponsiveLayout', () => () => ({ + shouldUseNarrowLayout: false, +})); function ProtectedContent() { return Protected Content;