diff --git a/src/components/ValidateCode/ExpiredValidateCodeModal.tsx b/src/components/ValidateCode/ExpiredValidateCodeModal.tsx index 2cad1ca73fe9..4df546804634 100644 --- a/src/components/ValidateCode/ExpiredValidateCodeModal.tsx +++ b/src/components/ValidateCode/ExpiredValidateCodeModal.tsx @@ -8,12 +8,13 @@ import useOnyx from '@hooks/useOnyx'; import useTheme from '@hooks/useTheme'; import useThemeStyles from '@hooks/useThemeStyles'; -import Navigation from '@libs/Navigation/Navigation'; +import Navigation, {navigationRef} from '@libs/Navigation/Navigation'; import variables from '@styles/variables'; import {beginSignIn} from '@userActions/Session'; +import NAVIGATORS from '@src/NAVIGATORS'; import ONYXKEYS from '@src/ONYXKEYS'; import React from 'react'; @@ -45,7 +46,11 @@ function ExpiredValidateCodeModal() { { beginSignIn(credentials?.login ?? ''); - Navigation.setNavigationActionToMicrotaskQueue(Navigation.goBack); + // navigate/goBack no-op from the public /v/ route when it's the stack root (magic link opened + // in a fresh tab), so reset the stack to TAB_NAVIGATOR (which hosts the public SignInPage). + Navigation.isNavigationReady().then(() => { + navigationRef.reset({index: 0, routes: [{name: NAVIGATORS.TAB_NAVIGATOR}]}); + }); }} > {translate('validateCodeModal.requestOneHere')} diff --git a/tests/ui/ValidateLoginPageTest.tsx b/tests/ui/ValidateLoginPageTest.tsx index 787f07239622..ec22be9b291f 100644 --- a/tests/ui/ValidateLoginPageTest.tsx +++ b/tests/ui/ValidateLoginPageTest.tsx @@ -1,4 +1,6 @@ -import {act, render, screen} from '@testing-library/react-native'; +import {act, fireEvent, render, screen} from '@testing-library/react-native'; + +import {LocaleContextProvider} from '@components/LocaleContextProvider'; import Navigation from '@libs/Navigation/Navigation'; import createPlatformStackNavigator from '@libs/Navigation/PlatformStackNavigation/createPlatformStackNavigator'; @@ -6,7 +8,7 @@ import type {PublicScreensParamList} from '@libs/Navigation/types'; import ValidateLoginPage from '@pages/ValidateLoginPage/index.web'; -import {handleExitToNavigation} from '@userActions/Session'; +import {beginSignIn, handleExitToNavigation} from '@userActions/Session'; import CONST from '@src/CONST'; import NAVIGATORS from '@src/NAVIGATORS'; @@ -18,6 +20,7 @@ import {NavigationContainer} from '@react-navigation/native'; import React from 'react'; import Onyx from 'react-native-onyx'; +import {translateLocal} from '../utils/TestHelper'; import waitForBatchedUpdatesWithAct from '../utils/waitForBatchedUpdatesWithAct'; // Controllable deferred so tests can assert the *ordering* guarantee (navigate only after @@ -65,21 +68,24 @@ jest.mock('@userActions/Session', () => ({ initAutoAuthState: jest.fn(), signInWithValidateCode: jest.fn(), handleExitToNavigation: jest.fn(), + beginSignIn: jest.fn(), })); const RootStack = createPlatformStackNavigator(); const renderPage = (initialParams: PublicScreensParamList[typeof SCREENS.VALIDATE_LOGIN]) => { return render( - - - - - , + + + + + + + , ); }; @@ -248,6 +254,37 @@ describe('ValidateLoginPage', () => { expect(mockNavigationReset).toHaveBeenCalledWith({index: 0, routes: [{name: NAVIGATORS.TAB_NAVIGATOR}]}); }); + it('Should request a new code and reset to the sign-in page when "request one here" is pressed on the expired-code page', async () => { + // Fresh-tab repro: the magic code expired (FAILED) so ExpiredValidateCodeModal renders. Pressing + // "request one here" must both request a new code and surface the sign-in page. goBack no-ops from + // the root /v/ route, so we reset the public stack to TAB_NAVIGATOR (which hosts SignInPage). + await act(async () => { + await Onyx.set(ONYXKEYS.SESSION, { + autoAuthState: CONST.AUTO_AUTH_STATE.FAILED, + }); + await Onyx.set(ONYXKEYS.CREDENTIALS, { + login: 'test@example.com', + }); + }); + + renderPage({accountID: '1', validateCode: '123456'}); + await waitForBatchedUpdatesWithAct(); + + // TextLink's tap handler calls event.preventDefault(), so pass a minimal synthetic event. + fireEvent.press(screen.getByText(translateLocal('validateCodeModal.requestOneHere')), {preventDefault: () => {}}); + + expect(beginSignIn).toHaveBeenCalledWith('test@example.com'); + + // Resolve the navigation-ready gate, then the press handler resets the public stack to SignInPage. + await act(async () => { + mockIsNavigationReady.resolve(); + await Promise.resolve(); + }); + await waitForBatchedUpdatesWithAct(); + + expect(mockNavigationReset).toHaveBeenCalledWith({index: 0, routes: [{name: NAVIGATORS.TAB_NAVIGATOR}]}); + }); + it('Should route an exitTo 2FA magic link to the sign-in page AND keep the deferred exitTo navigation', async () => { // exitTo deep link on a 2FA account: the user must still reach the authenticator form (not the // dead-end "2FA required" modal), so we reset to the sign-in page. We also register