Repository navigation
Fix Android device back button on the public room sign-in modal #98401
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
chuckdries
merged 3 commits into
Expensify:main
from
ahmdshrif:sherif/fix-96869-signin-modal-back
Aug 12, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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})); | ||
| 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}); | ||
| } | ||
| }); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this test,
useSession()returnsundefined, butSignInModaltreats anything other than an anonymous token as signed in and runswaitForIdle().then(() => openApp(true)). BecauseopenAppis not mocked here, rendering the modal can enqueue a realOpenApprequest and mutate Onyx during this unit test, making the test flaky and capable of leaking state into later assertions. Return an anonymous session (or mockopenApp) so this back-button test does not exercise the sign-in completion path.Useful? React with 👍 / 👎.