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
8 changes: 0 additions & 8 deletions src/pages/signin/SignInModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import HeaderWithBackButton from '@components/HeaderWithBackButton';
import {useSession} from '@components/OnyxListItemProvider';
import ScreenWrapper from '@components/ScreenWrapper';

import useAndroidBackButtonHandler from '@hooks/useAndroidBackButtonHandler';
import useOnyx from '@hooks/useOnyx';
import useStyleUtils from '@hooks/useStyleUtils';
import useTheme from '@hooks/useTheme';
Expand Down Expand Up @@ -36,13 +35,6 @@ function SignInModal() {
// More info https://github.com/Expensify/App/pull/62799#issuecomment-2943136220.
const SignInPageBase = useMemo(() => (isMobileSafari() ? SignInPageWrapped : SignInPage), []);

// The SignInPage (child component of SignInModal) uses useAndroidBackButtonHandler, which adds a hardwareBackPress listener that remains active in the SignInModal.
// Use of useAndroidBackButtonHandler with a returning true callback disables the default SignInModal hardware Android button behaviour, leaving only SignInPage handling (https://github.com/Expensify/App/issues/69391).
// The SignInPage Android back button behavior needs to remain because it is a fix for issue (https://github.com/Expensify/App/issues/67883) that occurs in the SignInModal.
useAndroidBackButtonHandler(() => {
return true;
});

useEffect(() => {
const isAnonymousUser = session?.authTokenType === CONST.AUTH_TOKEN_TYPES.ANONYMOUS;
if (!isAnonymousUser) {
Expand Down
10 changes: 7 additions & 3 deletions src/pages/signin/SignInPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import useThemeStyles from '@hooks/useThemeStyles';

import {isClientTheLeader as isClientTheLeaderActiveClientManager} from '@libs/ActiveClientManager';
import Log from '@libs/Log';
import Navigation from '@libs/Navigation/Navigation';
import Navigation, {navigationRef} from '@libs/Navigation/Navigation';
import Visibility from '@libs/Visibility';

import {clearSignInData, isSupportalSession as isSupportalSessionUtils} from '@userActions/Session';
Expand Down Expand Up @@ -58,7 +58,7 @@ type SignInPageProps = {
};

type SignInPageRef = {
navigateBack: () => void;
navigateBack: () => boolean;
};

type RenderOption = {
Expand Down Expand Up @@ -327,8 +327,12 @@ function SignInPage({ref, shouldResetTabTitle = true}: SignInPageProps) {
return true;
}

// Report the press as handled whenever we actually navigate, otherwise react-navigation's own
// BackHandler listener pops a second time (https://github.com/Expensify/App/issues/69391).
// At the public sign-in root there is nothing to pop, so return false and let Android background the app.
const didNavigate = !!navigationRef.current?.canGoBack();
Navigation.goBack();
return false;
return didNavigate;
};
useImperativeHandle(ref, () => ({
navigateBack,
Expand Down
113 changes: 113 additions & 0 deletions tests/unit/SignInBackButtonTest.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import {render} from '@testing-library/react-native';

import SignInModal from '@pages/signin/SignInModal';
import type {SignInPageRef} from '@pages/signin/SignInPage';
import {SignInPage} from '@pages/signin/SignInPage';

import React, {createRef} from 'react';

const mockGoBack = jest.fn();
let mockCanGoBack = true;

jest.mock('@libs/Navigation/Navigation', () => ({
__esModule: true,
// Referenced lazily: jest hoists this factory above the mock declarations below.
default: {
goBack: () => {
mockGoBack();
},
},
navigationRef: {
get current() {
return {canGoBack: () => mockCanGoBack};
},
},
}));

// Record the callbacks handed to the hook so the test can replay them. Jest resolves the platform-neutral
// no-op variant, so the callbacks are never invoked for real here.
const mockUseAndroidBackButtonHandler = jest.fn((callback: () => boolean) => callback);
jest.mock('@hooks/useAndroidBackButtonHandler', () => ({
__esModule: true,
default: (callback: () => boolean) => {
mockUseAndroidBackButtonHandler(callback);
},
}));

// SignInPage renders a full themed layout with form children. None of it participates in navigateBack.
jest.mock('@pages/signin/SignInPageLayout', () => 'SignInPageLayout');
jest.mock('@pages/signin/LoginForm', () => 'LoginForm');
jest.mock('@pages/signin/ValidateCodeForm', () => 'ValidateCodeForm');
jest.mock('@pages/signin/UnlinkLoginForm', () => 'UnlinkLoginForm');
jest.mock('@pages/signin/ChooseSSOOrValidateCode', () => 'ChooseSSOOrValidateCode');
jest.mock('@pages/signin/EmailDeliveryFailurePage', () => 'EmailDeliveryFailurePage');
jest.mock('@pages/signin/SMSDeliveryFailurePage', () => 'SMSDeliveryFailurePage');
jest.mock('@pages/signin/SignUpWelcomeForm', () => 'SignUpWelcomeForm');
jest.mock('@components/ColorSchemeWrapper', () => 'ColorSchemeWrapper');
jest.mock('@components/CustomStatusBarAndBackground', () => 'CustomStatusBarAndBackground');

// SignInModal reads the session to decide when to dismiss itself. The back listener does not depend on it.
jest.mock('@components/OnyxListItemProvider', () => ({useSession: () => undefined}));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Mock the anonymous session for the modal case

In this test, useSession() returns undefined, but SignInModal treats anything other than an anonymous token as signed in and runs waitForIdle().then(() => openApp(true)). Because openApp is not mocked here, rendering the modal can enqueue a real OpenApp request and mutate Onyx during this unit test, making the test flaky and capable of leaking state into later assertions. Return an anonymous session (or mock openApp) so this back-button test does not exercise the sign-in completion path.

Useful? React with 👍 / 👎.

jest.mock('@components/ScreenWrapper', () => 'ScreenWrapper');
jest.mock('@components/HeaderWithBackButton', () => 'HeaderWithBackButton');

describe('sign-in Android hardware back handling', () => {
beforeEach(() => {
jest.clearAllMocks();
mockCanGoBack = true;
});

describe('SignInPage.navigateBack on the login form', () => {
const renderSignInPage = () => {
const ref = createRef<SignInPageRef>();
render(<SignInPage ref={ref} />);
return ref;
};

it('reports the press as handled when there is something to pop', () => {
const ref = renderSignInPage();

expect(ref.current?.navigateBack()).toBe(true);
expect(mockGoBack).toHaveBeenCalledTimes(1);
});

it('reports the press as unhandled at the public sign-in root so Android can background the app', () => {
mockCanGoBack = false;
const ref = renderSignInPage();

expect(ref.current?.navigateBack()).toBe(false);
});

it('still calls goBack when it reports the press as unhandled', () => {
// goBack() also runs clearSelectedText() and the shouldPopToSidebar branch, which navigate even when
// canGoBack() is false. Only the returned boolean may depend on canGoBack().
mockCanGoBack = false;
const ref = renderSignInPage();

ref.current?.navigateBack();

expect(mockGoBack).toHaveBeenCalledTimes(1);
});
});

describe('SignInModal', () => {
it('registers no back listener that consumes the press without navigating', () => {
// SignInModal used to register a second listener that unconditionally returned true. Because React
// Native dispatches subscriptions in reverse order and stops at the first true, that no-op could swallow
// the press before SignInPage.navigateBack ran, leaving the device back button dead
// (https://github.com/Expensify/App/issues/96869).
render(<SignInModal />);

const handlers = mockUseAndroidBackButtonHandler.mock.calls.map(([callback]) => callback);
expect(handlers.length).toBeGreaterThan(0);

for (const handler of handlers) {
mockGoBack.mockClear();
const consumed = handler();

// On the login form every registered handler must actually navigate before claiming the press.
expect({consumed, navigated: mockGoBack.mock.calls.length > 0}).toEqual({consumed: true, navigated: true});
}
});
});
});
Loading