diff --git a/src/libs/actions/BankAccounts.ts b/src/libs/actions/BankAccounts.ts index fbeebe8fc84f..d12a784afb70 100644 --- a/src/libs/actions/BankAccounts.ts +++ b/src/libs/actions/BankAccounts.ts @@ -291,9 +291,23 @@ function setPersonalBankAccountContinueKYCOnSuccess(onSuccessFallbackRoute: Rout Onyx.merge(ONYXKEYS.PERSONAL_BANK_ACCOUNT, {onSuccessFallbackRoute}); } -function clearPersonalBankAccount() { +/** + * Clears personal bank account state. Pass `shouldPreserveAccountData=true` to only clear UI/error + * fields while keeping routing and context fields intact. + */ +function clearPersonalBankAccount(shouldPreserveAccountData = false) { clearPlaid(); - Onyx.set(ONYXKEYS.PERSONAL_BANK_ACCOUNT, null); + if (shouldPreserveAccountData) { + Onyx.merge(ONYXKEYS.PERSONAL_BANK_ACCOUNT, { + shouldShowSuccess: null, + isLoading: null, + errors: null, + errorFields: null, + updateError: null, + }); + } else { + Onyx.set(ONYXKEYS.PERSONAL_BANK_ACCOUNT, null); + } Onyx.set(ONYXKEYS.FORMS.PERSONAL_BANK_ACCOUNT_FORM_DRAFT, null); clearPersonalBankAccountSetupType(); } diff --git a/src/pages/settings/Wallet/InternationalDepositAccount/subPages/AccountFlowEntryPoint.tsx b/src/pages/settings/Wallet/InternationalDepositAccount/subPages/AccountFlowEntryPoint.tsx index 689eed9b392b..27911fcfffaa 100644 --- a/src/pages/settings/Wallet/InternationalDepositAccount/subPages/AccountFlowEntryPoint.tsx +++ b/src/pages/settings/Wallet/InternationalDepositAccount/subPages/AccountFlowEntryPoint.tsx @@ -41,7 +41,7 @@ function AccountFlowEntryPoint({policyName = '', onBackButtonPress}: AccountFlow const [isPlaidDisabled] = useOnyx(ONYXKEYS.IS_PLAID_DISABLED); useEffect(() => { - clearPersonalBankAccount(); + clearPersonalBankAccount(true); }, []); const handleConnectManually = () => { diff --git a/tests/actions/BankAccountsTest.ts b/tests/actions/BankAccountsTest.ts index 612dd7948009..4fa2bf8aed4e 100644 --- a/tests/actions/BankAccountsTest.ts +++ b/tests/actions/BankAccountsTest.ts @@ -1,8 +1,9 @@ import Onyx from 'react-native-onyx'; -import {connectBankAccountWithPlaid} from '@libs/actions/BankAccounts'; +import {clearPersonalBankAccount, connectBankAccountWithPlaid} from '@libs/actions/BankAccounts'; import {WRITE_COMMANDS} from '@libs/API/types'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; +import ROUTES from '@src/ROUTES'; import type {ReimbursementAccountForm} from '@src/types/form/ReimbursementAccountForm'; import type PlaidBankAccount from '@src/types/onyx/PlaidBankAccount'; import getOnyxValue from '../utils/getOnyxValue'; @@ -109,4 +110,49 @@ describe('actions/BankAccounts', () => { }); }); }); + + describe('clearPersonalBankAccount', () => { + test('wipes the entire PERSONAL_BANK_ACCOUNT key by default', async () => { + await Onyx.set(ONYXKEYS.PERSONAL_BANK_ACCOUNT, {onSuccessFallbackRoute: ROUTES.ENABLE_PAYMENTS, shouldShowSuccess: true}); + await waitForBatchedUpdates(); + + clearPersonalBankAccount(); + await waitForBatchedUpdates(); + + expect(await getOnyxValue(ONYXKEYS.PERSONAL_BANK_ACCOUNT)).toBeFalsy(); + }); + + test('preserves navigation fields and clears form/status fields when shouldPreserveAccountData=true', async () => { + await Onyx.set(ONYXKEYS.PERSONAL_BANK_ACCOUNT, { + onSuccessFallbackRoute: ROUTES.ENABLE_PAYMENTS, + exitReportID: 'report123', + shouldShowSuccess: true, + isLoading: true, + errors: {field: 'error'}, + updateError: 'addPersonalBankAccount.updatePersonalInfoFailure', + }); + await waitForBatchedUpdates(); + + clearPersonalBankAccount(true); + await waitForBatchedUpdates(); + + const result = await getOnyxValue(ONYXKEYS.PERSONAL_BANK_ACCOUNT); + expect(result?.onSuccessFallbackRoute).toBe(ROUTES.ENABLE_PAYMENTS); + expect(result?.exitReportID).toBe('report123'); + expect(result?.shouldShowSuccess).toBeFalsy(); + expect(result?.isLoading).toBeFalsy(); + expect(result?.errors).toBeFalsy(); + expect(result?.updateError).toBeFalsy(); + }); + + test('always clears the form draft regardless of the flag', async () => { + await Onyx.set(ONYXKEYS.FORMS.PERSONAL_BANK_ACCOUNT_FORM_DRAFT, {setupType: CONST.BANK_ACCOUNT.SETUP_TYPE.MANUAL}); + await waitForBatchedUpdates(); + + clearPersonalBankAccount(true); + await waitForBatchedUpdates(); + + expect((await getOnyxValue(ONYXKEYS.FORMS.PERSONAL_BANK_ACCOUNT_FORM_DRAFT))?.setupType).toBeFalsy(); + }); + }); });