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
9 changes: 7 additions & 2 deletions src/components/ValidateCode/ExpiredValidateCodeModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -45,7 +46,11 @@ function ExpiredValidateCodeModal() {
<TextLink
onPress={() => {
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')}
Expand Down
59 changes: 48 additions & 11 deletions tests/ui/ValidateLoginPageTest.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
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';
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';
Expand All @@ -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
Expand Down Expand Up @@ -65,21 +68,24 @@ jest.mock('@userActions/Session', () => ({
initAutoAuthState: jest.fn(),
signInWithValidateCode: jest.fn(),
handleExitToNavigation: jest.fn(),
beginSignIn: jest.fn(),
}));

const RootStack = createPlatformStackNavigator<PublicScreensParamList>();

const renderPage = (initialParams: PublicScreensParamList[typeof SCREENS.VALIDATE_LOGIN]) => {
return render(
<NavigationContainer>
<RootStack.Navigator>
<RootStack.Screen
name={SCREENS.VALIDATE_LOGIN}
component={ValidateLoginPage}
initialParams={initialParams}
/>
</RootStack.Navigator>
</NavigationContainer>,
<LocaleContextProvider>
<NavigationContainer>
<RootStack.Navigator>
<RootStack.Screen
name={SCREENS.VALIDATE_LOGIN}
component={ValidateLoginPage}
initialParams={initialParams}
/>
</RootStack.Navigator>
</NavigationContainer>
</LocaleContextProvider>,
);
};

Expand Down Expand Up @@ -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
Expand Down
Loading