Repository navigation
Prevent resubmitting an international bank account after success #106752
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
+262
−3
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
185 changes: 185 additions & 0 deletions
185
tests/unit/pages/InternationalDepositAccountContentTest.tsx
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,185 @@ | ||
| import {act, render} from '@testing-library/react-native'; | ||
|
|
||
| import ComposeProviders from '@components/ComposeProviders'; | ||
| import {LocaleContextProvider} from '@components/LocaleContextProvider'; | ||
| import OnyxListItemProvider from '@components/OnyxListItemProvider'; | ||
|
|
||
| import useAndroidBackButtonHandler from '@hooks/useAndroidBackButtonHandler'; | ||
| import useRootNavigationState from '@hooks/useRootNavigationState'; | ||
| import useSubPage from '@hooks/useSubPage'; | ||
| import type {SubPageProps} from '@hooks/useSubPage/types'; | ||
|
|
||
| import {clearCorpayBankAccountFields} from '@libs/actions/BankAccounts'; | ||
| import {clearDraftValues} from '@libs/actions/FormActions'; | ||
| import Navigation from '@libs/Navigation/Navigation'; | ||
| import createPlatformStackNavigator from '@libs/Navigation/PlatformStackNavigation/createPlatformStackNavigator'; | ||
| import type {SettingsNavigatorParamList} from '@libs/Navigation/types'; | ||
|
|
||
| import InternationalDepositAccountContent from '@pages/settings/Wallet/InternationalDepositAccount/InternationalDepositAccountContent'; | ||
|
|
||
| import CONST from '@src/CONST'; | ||
| import NAVIGATORS from '@src/NAVIGATORS'; | ||
| import ONYXKEYS from '@src/ONYXKEYS'; | ||
| import ROUTES from '@src/ROUTES'; | ||
| import SCREENS from '@src/SCREENS'; | ||
|
|
||
| import {PortalProvider} from '@gorhom/portal'; | ||
| import {NavigationContainer, useIsFocused, useRoute} from '@react-navigation/native'; | ||
| import React from 'react'; | ||
| import Onyx from 'react-native-onyx'; | ||
|
|
||
| import createMock from '../../utils/createMock'; | ||
|
|
||
| jest.mock('@hooks/useAndroidBackButtonHandler', () => jest.fn()); | ||
| jest.mock('@hooks/useRootNavigationState', () => jest.fn()); | ||
| jest.mock('@hooks/useSubPage', () => jest.fn()); | ||
| jest.mock('@react-navigation/native', () => ({ | ||
| ...jest.requireActual<Record<string, unknown>>('@react-navigation/native'), | ||
| useIsFocused: jest.fn(), | ||
| useRoute: jest.fn(), | ||
| })); | ||
| jest.mock('@libs/actions/BankAccounts', () => ({clearCorpayBankAccountFields: jest.fn()})); | ||
| jest.mock('@libs/actions/FormActions', () => ({ | ||
| ...jest.requireActual<Record<string, unknown>>('@libs/actions/FormActions'), | ||
| clearDraftValues: jest.fn(), | ||
| })); | ||
|
|
||
| const mockStep = jest.fn<null, [SubPageProps]>(() => null); | ||
| const nextPage = jest.fn(); | ||
| const prevPage = jest.fn(); | ||
| const goBackSpy = jest.spyOn(Navigation, 'goBack').mockImplementation(() => {}); | ||
| const Stack = createPlatformStackNavigator<SettingsNavigatorParamList>(); | ||
|
|
||
| function TestFlow() { | ||
| return ( | ||
| <ComposeProviders components={[OnyxListItemProvider, LocaleContextProvider]}> | ||
| <PortalProvider> | ||
| <NavigationContainer> | ||
| <Stack.Navigator> | ||
| <Stack.Screen name={SCREENS.SETTINGS.ADD_BANK_ACCOUNT}> | ||
| {() => ( | ||
| <InternationalDepositAccountContent | ||
| privatePersonalDetails={undefined} | ||
| corpayFields={undefined} | ||
| bankAccountList={undefined} | ||
| draftValues={undefined} | ||
| country={CONST.COUNTRY.GB} | ||
| isAccountLoading={false} | ||
| /> | ||
| )} | ||
| </Stack.Screen> | ||
| </Stack.Navigator> | ||
| </NavigationContainer> | ||
| </PortalProvider> | ||
| </ComposeProviders> | ||
| ); | ||
| } | ||
|
|
||
| function configureStep(pageIndex: number = CONST.CORPAY_FIELDS.INDEXES.MAPPING.CONFIRMATION, isEditing = false) { | ||
| jest.mocked(useSubPage).mockReturnValue(createMock<ReturnType<typeof useSubPage>>({CurrentPage: mockStep, nextPage, prevPage, pageIndex, isEditing, isRedirecting: false})); | ||
| } | ||
|
|
||
| describe('InternationalDepositAccountContent completion navigation', () => { | ||
| beforeAll(() => Onyx.init({keys: ONYXKEYS})); | ||
|
|
||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| jest.mocked(useIsFocused).mockReturnValue(true); | ||
| jest.mocked(useRoute).mockReturnValue({key: 'bank-account', name: 'bank-account'}); | ||
| jest.mocked(useRootNavigationState).mockReturnValue({name: NAVIGATORS.SETTINGS_SPLIT_NAVIGATOR}); | ||
| configureStep(); | ||
| }); | ||
|
|
||
| it('exits when the completed confirmation regains focus without advancing again', () => { | ||
| // Given confirmation advanced after a successful creation, not merely after pressing Confirm | ||
| const {rerender} = render(<TestFlow />); | ||
| act(() => mockStep.mock.lastCall?.[0].onNext()); | ||
| expect(nextPage).toHaveBeenCalledTimes(1); | ||
| expect(goBackSpy).not.toHaveBeenCalled(); | ||
|
|
||
| // When success covers confirmation and browser Back restores its focus | ||
| jest.mocked(useIsFocused).mockReturnValue(false); | ||
| rerender(<TestFlow />); | ||
| jest.mocked(useIsFocused).mockReturnValue(true); | ||
| rerender(<TestFlow />); | ||
|
|
||
| // Then the existing finish path clears the draft and exits rather than opening success again | ||
| expect(goBackSpy).toHaveBeenCalledTimes(1); | ||
| expect(goBackSpy).toHaveBeenCalledWith(ROUTES.SETTINGS_WALLET); | ||
| expect(clearDraftValues).toHaveBeenCalledWith(ONYXKEYS.FORMS.INTERNATIONAL_BANK_ACCOUNT_FORM); | ||
| expect(clearCorpayBankAccountFields).toHaveBeenCalledTimes(1); | ||
| expect(nextPage).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('exits when confirmation loses focus before creation succeeds and then regains focus', () => { | ||
| // Given confirmation was covered by an edit while its creation request was still pending | ||
| const {rerender} = render(<TestFlow />); | ||
| jest.mocked(useIsFocused).mockReturnValue(false); | ||
| rerender(<TestFlow />); | ||
|
|
||
| // When creation succeeds while confirmation is unfocused, then confirmation regains focus | ||
| act(() => mockStep.mock.lastCall?.[0].onNext()); | ||
| expect(goBackSpy).not.toHaveBeenCalled(); | ||
| jest.mocked(useIsFocused).mockReturnValue(true); | ||
| rerender(<TestFlow />); | ||
|
|
||
| // Then the completed flow exits rather than allowing the same account to be submitted again | ||
| expect(goBackSpy).toHaveBeenCalledTimes(1); | ||
| expect(goBackSpy).toHaveBeenCalledWith(ROUTES.SETTINGS_WALLET); | ||
| expect(clearDraftValues).toHaveBeenCalledWith(ONYXKEYS.FORMS.INTERNATIONAL_BANK_ACCOUNT_FORM); | ||
| expect(nextPage).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('does not exit an unfinished confirmation when it regains focus', () => { | ||
| // Given confirmation has not advanced, including when a creation request fails | ||
| const {rerender} = render(<TestFlow />); | ||
|
|
||
| // When an edit or another screen covers it and the user comes back | ||
| jest.mocked(useIsFocused).mockReturnValue(false); | ||
| rerender(<TestFlow />); | ||
| jest.mocked(useIsFocused).mockReturnValue(true); | ||
| rerender(<TestFlow />); | ||
|
|
||
| // Then its draft is retained and the flow is not exited | ||
| expect(goBackSpy).not.toHaveBeenCalled(); | ||
| expect(clearDraftValues).not.toHaveBeenCalled(); | ||
| expect(clearCorpayBankAccountFields).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('does not mark an edit return as a successful submission', () => { | ||
| // Given an edited bank-details step that returns to confirmation without creating an account | ||
| configureStep(CONST.CORPAY_FIELDS.INDEXES.MAPPING.BANK_ACCOUNT_DETAILS, true); | ||
| const {rerender} = render(<TestFlow />); | ||
|
|
||
| // When the edit is saved and that screen later regains focus | ||
| act(() => mockStep.mock.lastCall?.[0].onNext()); | ||
| jest.mocked(useIsFocused).mockReturnValue(false); | ||
| rerender(<TestFlow />); | ||
| jest.mocked(useIsFocused).mockReturnValue(true); | ||
| rerender(<TestFlow />); | ||
|
|
||
| // Then only the normal edit return happens, without completed-account cleanup | ||
| expect(goBackSpy).toHaveBeenCalledTimes(1); | ||
| expect(goBackSpy).toHaveBeenCalledWith(ROUTES.SETTINGS_ADD_BANK_ACCOUNT.getRoute(undefined, CONST.CORPAY_FIELDS.PAGE_NAME.CONFIRM)); | ||
| expect(clearDraftValues).not.toHaveBeenCalled(); | ||
| expect(nextPage).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('exits on Android Back from success instead of returning to confirmation', () => { | ||
| // Given the success screen for an account that has already been created | ||
| configureStep(CONST.CORPAY_FIELDS.INDEXES.MAPPING.SUCCESS); | ||
| render(<TestFlow />); | ||
|
|
||
| // When its registered Android back callback runs | ||
| let wasHandled: boolean | undefined; | ||
| act(() => { | ||
| wasHandled = jest.mocked(useAndroidBackButtonHandler).mock.lastCall?.[0](); | ||
| }); | ||
|
|
||
| // Then the whole flow is finished and no previous wizard step is opened | ||
| expect(wasHandled).toBe(true); | ||
| expect(goBackSpy).toHaveBeenCalledWith(ROUTES.SETTINGS_WALLET); | ||
| expect(clearDraftValues).toHaveBeenCalledWith(ONYXKEYS.FORMS.INTERNATIONAL_BANK_ACCOUNT_FORM); | ||
| expect(prevPage).not.toHaveBeenCalled(); | ||
| }); | ||
| }); |
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.
Uh oh!
There was an error while loading. Please reload this page.