diff --git a/src/hooks/useDeferUntilScreenRevealed.ts b/src/hooks/useDeferUntilScreenRevealed.ts new file mode 100644 index 000000000000..7ebb01d692da --- /dev/null +++ b/src/hooks/useDeferUntilScreenRevealed.ts @@ -0,0 +1,30 @@ +import type {Primitive} from 'type-fest'; + +import {useState} from 'react'; + +import useIsScreenFocused from './useIsScreenFocused'; +import useResponsiveLayout from './useResponsiveLayout'; + +/** + * Keeps a screen showing what the user last saw while the RHP covers part of it. + * Returns the live value while the screen has focus, and the value from when it last had focus otherwise, + * so changes made from the RHP show once it closes instead of shifting the layout visible beside it. + * Only primitives are accepted, since a value rebuilt on every render would never match the stored one and would set state on every render. + */ +function useDeferUntilScreenRevealed(value: TValue): TValue { + const isFocused = useIsScreenFocused(); + const {shouldUseNarrowLayout} = useResponsiveLayout(); + const [deferredValue, setDeferredValue] = useState(value); + + // On narrow layout the RHP covers the whole screen, so changes can apply while it is hidden. + // Holding them would make the screen jump as the user navigates back to it. + const shouldUseLiveValue = isFocused || shouldUseNarrowLayout; + + if (shouldUseLiveValue && !Object.is(deferredValue, value)) { + setDeferredValue(value); + } + + return shouldUseLiveValue ? value : deferredValue; +} + +export default useDeferUntilScreenRevealed; diff --git a/src/hooks/useDismissWiseGlobalReimbursementsBanners.ts b/src/hooks/useDismissWiseGlobalReimbursementsBanners.ts deleted file mode 100644 index a7a275eb308d..000000000000 --- a/src/hooks/useDismissWiseGlobalReimbursementsBanners.ts +++ /dev/null @@ -1,15 +0,0 @@ -import {dismissWiseGlobalReimbursementsBanners} from '@userActions/WiseOnboarding'; - -import {useEffect} from 'react'; - -/** Dismisses the Wallet and Workspace banners that promote Wise global reimbursements once `shouldDismiss` turns true. */ -function useDismissWiseGlobalReimbursementsBanners(shouldDismiss: boolean) { - useEffect(() => { - if (!shouldDismiss) { - return; - } - dismissWiseGlobalReimbursementsBanners(); - }, [shouldDismiss]); -} - -export default useDismissWiseGlobalReimbursementsBanners; diff --git a/src/libs/BankAccountUtils.ts b/src/libs/BankAccountUtils.ts index c5490af73a76..1e28eb5993ec 100644 --- a/src/libs/BankAccountUtils.ts +++ b/src/libs/BankAccountUtils.ts @@ -290,10 +290,24 @@ function isWiseOnboardingSubmitted(accountData: AccountData | undefined): boolea return !!accountData?.additionalData?.wise?.status; } +/** + * Whether the admin saved part of the Wise onboarding for any of their bank accounts. The server sets Wise as the provider on the first saved page, + * and from then on Wallet shows the account's setup so the admin can return to it. + * An account deleted offline still counts until the server removes it, so the banners don't show up before the deletion goes through. + */ +function hasStartedWiseOnboarding(bankAccountList: OnyxEntry): boolean { + return Object.values(bankAccountList ?? {}).some((bankAccount) => bankAccount?.accountData?.additionalData?.globalReimbursementProvider === CONST.GLOBAL_REIMBURSEMENT_PROVIDER.WISE); +} + /** * Picks the bank account the Wallet global reimbursements banner points to: the most recently added eligible account. */ function getWiseGlobalReimbursementsBannerBankAccountID(bankAccountList: OnyxEntry): number | undefined { + // Once any account has a Wise setup, Wallet shows where it stands, so the banner has nothing left to promote + if (hasStartedWiseOnboarding(bankAccountList)) { + return undefined; + } + const newest = Object.values(bankAccountList ?? {}).reduce((latest, bankAccount) => { // Accounts deleted offline stay in the list until the request syncs if (bankAccount.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE) { @@ -334,6 +348,11 @@ function getWorkspaceWiseGlobalReimbursementsBannerBankAccountID(policy: OnyxEnt return undefined; } + // Once any account has a Wise setup, Wallet shows where it stands, so the banner has nothing left to promote + if (hasStartedWiseOnboarding(bankAccountList)) { + return undefined; + } + // Admins without a copy of the account can't see its currency, type or verifications, so the banner stays hidden for them const bankAccount = bankAccountList?.[bankAccountID]; if (bankAccount?.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE || !isBankAccountEligibleForGlobalReimbursements(bankAccount?.accountData, bankAccount?.bankCurrency)) { diff --git a/src/libs/actions/WiseOnboarding.ts b/src/libs/actions/WiseOnboarding.ts index 4280e47261f1..fe8bc4c57499 100644 --- a/src/libs/actions/WiseOnboarding.ts +++ b/src/libs/actions/WiseOnboarding.ts @@ -7,7 +7,6 @@ import {getMicroSecondOnyxErrorWithTranslationKey} from '@libs/ErrorUtils'; import getWiseOnboardingPageParams from '@libs/getWiseOnboardingPageParams'; import {rand64} from '@libs/NumberUtils'; -import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import type {DynamicFormPage} from '@src/types/onyx'; import type {OnyxData} from '@src/types/onyx/Request'; @@ -15,7 +14,6 @@ import type {OnyxData} from '@src/types/onyx/Request'; import Onyx from 'react-native-onyx'; import {openReimbursementAccountPage} from './BankAccounts'; -import {dismissProductTraining} from './Welcome'; /** * Marks the page as unsaved until its own save succeeds, since `errors` is shared by every page and the next page's save clears it. It also covers a save @@ -118,11 +116,6 @@ function clearWiseIntroReadBankAccountID() { Onyx.set(ONYXKEYS.RAM_ONLY_WISE_INTRO_READ_BANK_ACCOUNT_ID, null); } -function dismissWiseGlobalReimbursementsBanners() { - dismissProductTraining(CONST.WISE_GLOBAL_REIMBURSEMENTS_WALLET_BANNER); - dismissProductTraining(CONST.WISE_GLOBAL_REIMBURSEMENTS_WORKSPACE_BANNER); -} - /** Submits the business to Wise with the answers its pages saved. Auth creates the Wise profile and opens Wise's review in the same request. */ function submitWiseBusinessOnboarding(params: SubmitWiseBusinessOnboardingParams) { const onyxData: OnyxData = { @@ -165,11 +158,4 @@ function clearWiseBusinessOnboardingSubmission() { } export default saveWiseOnboardingPage; -export { - clearWiseIntroReadBankAccountID, - clearWiseBusinessOnboardingSubmission, - clearWiseOnboardingUnsavedPages, - dismissWiseGlobalReimbursementsBanners, - openReimbursementAccountPageForWiseIntro, - submitWiseBusinessOnboarding, -}; +export {clearWiseIntroReadBankAccountID, clearWiseBusinessOnboardingSubmission, clearWiseOnboardingUnsavedPages, openReimbursementAccountPageForWiseIntro, submitWiseBusinessOnboarding}; diff --git a/src/pages/settings/Wallet/EnableGlobalReimbursements/Wise/WiseBusinessOnboardingPage.tsx b/src/pages/settings/Wallet/EnableGlobalReimbursements/Wise/WiseBusinessOnboardingPage.tsx index bea9fb77c590..4f8e3fa80321 100644 --- a/src/pages/settings/Wallet/EnableGlobalReimbursements/Wise/WiseBusinessOnboardingPage.tsx +++ b/src/pages/settings/Wallet/EnableGlobalReimbursements/Wise/WiseBusinessOnboardingPage.tsx @@ -5,7 +5,6 @@ import FullScreenLoadingIndicator from '@components/FullscreenLoadingIndicator'; import Header from '@components/Header'; import ScreenWrapper from '@components/ScreenWrapper'; -import useDismissWiseGlobalReimbursementsBanners from '@hooks/useDismissWiseGlobalReimbursementsBanners'; import useDynamicBackPath from '@hooks/useDynamicBackPath'; import useLocalize from '@hooks/useLocalize'; import useNetwork from '@hooks/useNetwork'; @@ -66,6 +65,10 @@ function WiseBusinessOnboardingPage({route}: WiseBusinessOnboardingPageProps) { const [businessAccount, businessAccountMetadata] = useOnyx(ONYXKEYS.REIMBURSEMENT_ACCOUNT, {selector: businessAccountSelector}); const {isSaving, saveError, unsavedPageKeys} = useWiseOnboardingSaveStatus(); const bankAccount = bankAccountList?.[bankAccountID]; + // A selector, so the check is cached now that only the early return below reads it + const [isEligible = false] = useOnyx(ONYXKEYS.BANK_ACCOUNT_LIST, { + selector: (list) => isBankAccountEligibleForGlobalReimbursements(list?.[bankAccountID]?.accountData, list?.[bankAccountID]?.bankCurrency), + }); const {translate} = useLocalize(); const {isOffline} = useNetwork(); @@ -187,10 +190,6 @@ function WiseBusinessOnboardingPage({route}: WiseBusinessOnboardingPageProps) { setIsPrefillDone(true); } - // Starting onboarding for any account retires both banners for good, and the writes wait for the form so the read is not queued behind them - const isEligible = isBankAccountEligibleForGlobalReimbursements(bankAccount?.accountData, bankAccount?.bankCurrency); - useDismissWiseGlobalReimbursementsBanners(isNewVisit && isPrefillDone && isEligible); - useEffect(() => { if (!isPrefillPending) { return; diff --git a/src/pages/settings/Wallet/WalletWiseGlobalReimbursementsBanner.tsx b/src/pages/settings/Wallet/WalletWiseGlobalReimbursementsBanner.tsx index 3989069a6774..f29583f3cc2d 100644 --- a/src/pages/settings/Wallet/WalletWiseGlobalReimbursementsBanner.tsx +++ b/src/pages/settings/Wallet/WalletWiseGlobalReimbursementsBanner.tsx @@ -1,5 +1,6 @@ import WiseGlobalReimbursementsBanner from '@components/WiseGlobalReimbursementsBanner'; +import useDeferUntilScreenRevealed from '@hooks/useDeferUntilScreenRevealed'; import useLayoutSpacing from '@hooks/useLayoutSpacing'; import useOnyx from '@hooks/useOnyx'; import usePermissions from '@hooks/usePermissions'; @@ -30,7 +31,8 @@ function WalletWiseGlobalReimbursementsBanner() { const [bankAccountList] = useOnyx(ONYXKEYS.BANK_ACCOUNT_LIST); const [isDismissed = true] = useOnyx(ONYXKEYS.NVP_DISMISSED_PRODUCT_TRAINING, {selector: isBannerDismissedSelector}); - const bankAccountID = getWiseGlobalReimbursementsBannerBankAccountID(bankAccountList); + // Saving the first onboarding page in the RHP retires the banner, so it waits for the RHP to close instead of vanishing behind it + const bankAccountID = useDeferUntilScreenRevealed(getWiseGlobalReimbursementsBannerBankAccountID(bankAccountList)); if (!isBetaEnabled(CONST.BETAS.WISE_REIMBURSEMENTS) || shouldSuppressPromotionalUI || isDismissed || !bankAccountID) { return null; diff --git a/src/pages/settings/Wallet/WiseGlobalReimbursementsIntroPage.tsx b/src/pages/settings/Wallet/WiseGlobalReimbursementsIntroPage.tsx index f1fd00769025..1f5a88acbf86 100644 --- a/src/pages/settings/Wallet/WiseGlobalReimbursementsIntroPage.tsx +++ b/src/pages/settings/Wallet/WiseGlobalReimbursementsIntroPage.tsx @@ -25,8 +25,6 @@ import type {SettingsNavigatorParamList} from '@navigation/types'; import variables from '@styles/variables'; -import {dismissWiseGlobalReimbursementsBanners} from '@userActions/WiseOnboarding'; - import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import {DYNAMIC_ROUTES} from '@src/ROUTES'; @@ -112,13 +110,10 @@ function WiseGlobalReimbursementsIntroPage({route}: WiseGlobalReimbursementsIntr } text={translate(isOnboardingSubmitted || isWiseOnboardingPendingSubmit(bankAccount) ? 'common.continue' : 'walletPage.wiseGlobalReimbursementsIntro.getStarted')} onPress={() => { - // A submitted business goes to its status page, which leaves the banners alone, so they are dismissed here. The form dismisses them itself - if (isOnboardingSubmitted) { - dismissWiseGlobalReimbursementsBanners(); - Navigation.navigate(createDynamicRoute(DYNAMIC_ROUTES.WISE_ONBOARDING_STATUS.path)); - return; - } - Navigation.navigate(createDynamicRoute(DYNAMIC_ROUTES.WISE_BUSINESS_ONBOARDING.getRoute())); + // A submitted business has nothing left to submit, so it sees where its onboarding stands instead of ending on the terms + Navigation.navigate( + createDynamicRoute(isOnboardingSubmitted ? DYNAMIC_ROUTES.WISE_ONBOARDING_STATUS.path : DYNAMIC_ROUTES.WISE_BUSINESS_ONBOARDING.getRoute()), + ); }} /> diff --git a/src/pages/workspace/workflows/WorkspaceWiseGlobalReimbursementsBanner.tsx b/src/pages/workspace/workflows/WorkspaceWiseGlobalReimbursementsBanner.tsx index 2bce00f4dbbd..885acd8951e1 100644 --- a/src/pages/workspace/workflows/WorkspaceWiseGlobalReimbursementsBanner.tsx +++ b/src/pages/workspace/workflows/WorkspaceWiseGlobalReimbursementsBanner.tsx @@ -1,6 +1,7 @@ import WiseGlobalReimbursementsBanner from '@components/WiseGlobalReimbursementsBanner'; import useCurrentUserPersonalDetails from '@hooks/useCurrentUserPersonalDetails'; +import useDeferUntilScreenRevealed from '@hooks/useDeferUntilScreenRevealed'; import useOnyx from '@hooks/useOnyx'; import usePermissions from '@hooks/usePermissions'; import usePolicy from '@hooks/usePolicy'; @@ -40,7 +41,8 @@ function WorkspaceWiseGlobalReimbursementsBanner({policyID}: WorkspaceWiseGlobal const [bankAccountList] = useOnyx(ONYXKEYS.BANK_ACCOUNT_LIST); const [isDismissed = true] = useOnyx(ONYXKEYS.NVP_DISMISSED_PRODUCT_TRAINING, {selector: isBannerDismissedSelector}); - const bankAccountID = getWorkspaceWiseGlobalReimbursementsBannerBankAccountID(policy, bankAccountList); + // Saving the first onboarding page in the RHP retires the banner, so it waits for the RHP to close instead of vanishing behind it + const bankAccountID = useDeferUntilScreenRevealed(getWorkspaceWiseGlobalReimbursementsBannerBankAccountID(policy, bankAccountList)); if (!isBetaEnabled(CONST.BETAS.WISE_REIMBURSEMENTS) || !isPolicyAdmin(policy, currentUserLogin) || shouldSuppressPromotionalUI || isDismissed || !bankAccountID) { return null; diff --git a/tests/ui/pages/settings/Wallet/WiseBusinessOnboardingPageTest.tsx b/tests/ui/pages/settings/Wallet/WiseBusinessOnboardingPageTest.tsx index 798444ce3736..b8d82ff2d89c 100644 --- a/tests/ui/pages/settings/Wallet/WiseBusinessOnboardingPageTest.tsx +++ b/tests/ui/pages/settings/Wallet/WiseBusinessOnboardingPageTest.tsx @@ -475,14 +475,11 @@ describe('WiseBusinessOnboardingPage', () => { expect(screen.getByTestId(FLOW_TEST_ID)).toBeOnTheScreen(); }); - it('retires the Wallet and workspace banners once the form shows, not while the loader waits for the read', async () => { + it('leaves the Wallet and workspace banners alone when the form shows, since they hide once a page is saved', async () => { // Given a visit whose read of the bank account details has not returned await renderPage({shouldLeaveReadRunning: true}); expect(screen.getByTestId(LOADER_TEST_ID)).toBeOnTheScreen(); - // Then neither banner is dismissed yet, since a write queued ahead of the read would hold the loader - expect(await readDismissedProductTraining()).toBeUndefined(); - // When the read returns and the form shows await act(() => Onyx.merge(ONYXKEYS.REIMBURSEMENT_ACCOUNT, {isLoading: true})); await waitForBatchedUpdatesWithAct(); @@ -490,31 +487,7 @@ describe('WiseBusinessOnboardingPage', () => { await waitForBatchedUpdatesWithAct(); expect(screen.getByTestId(FLOW_TEST_ID)).toBeOnTheScreen(); - // Then both banners are dismissed, since onboarding has started - const dismissed = await readDismissedProductTraining(); - expect(dismissed?.[CONST.WISE_GLOBAL_REIMBURSEMENTS_WALLET_BANNER]).toMatchObject({dismissedMethod: 'click'}); - expect(dismissed?.[CONST.WISE_GLOBAL_REIMBURSEMENTS_WORKSPACE_BANNER]).toMatchObject({dismissedMethod: 'click'}); - }); - - it('does not retire the banners again when Next or Back mounts the page on one of its pages', async () => { - // Given the details read earlier in the visit - // When the page mounts on the business page, as Next does - await renderPage({reimbursementAccount: REIMBURSEMENT_ACCOUNT, subPage: 'business'}); - expect(screen.getByTestId(FLOW_TEST_ID)).toBeOnTheScreen(); - - // Then no dismiss is written, since the visit already did when its form opened - expect(await readDismissedProductTraining()).toBeUndefined(); - }); - - it('does not retire the banners for a bank account that cannot use global reimbursements', async () => { - // Given a business account in euros, which Wise reimbursements do not cover - const euroBankAccount: BankAccount = {...ELIGIBLE_BANK_ACCOUNT, bankCurrency: CONST.CURRENCY.EUR}; - - // When the page opens for it - await renderPage({bankAccount: euroBankAccount}); - - // Then onboarding has not started, so both banners stay - expect(screen.getByTestId(NOT_FOUND_TEST_ID)).toBeOnTheScreen(); + // Then neither banner is dismissed, so an admin who leaves before saving still sees them expect(await readDismissedProductTraining()).toBeUndefined(); }); diff --git a/tests/ui/pages/settings/Wallet/WiseGlobalReimbursementsIntroPageTest.tsx b/tests/ui/pages/settings/Wallet/WiseGlobalReimbursementsIntroPageTest.tsx index 3e8d6af8abfd..2e09928facdc 100644 --- a/tests/ui/pages/settings/Wallet/WiseGlobalReimbursementsIntroPageTest.tsx +++ b/tests/ui/pages/settings/Wallet/WiseGlobalReimbursementsIntroPageTest.tsx @@ -137,13 +137,12 @@ describe('WiseGlobalReimbursementsIntroPage', () => { it.each([ [ - 'retires both banners when Continue opens the status page of a submitted onboarding', + 'leaves the banners alone when Continue opens the status page of a submitted onboarding', CONST.GLOBAL_REIMBURSEMENT_PROVIDER.WISE, {status: CONST.WISE.ONBOARDING_STATUS.IN_REVIEW}, - true, ], - ['leaves the banners to the form when Get started opens it', undefined, undefined, false], - ])('%s', async (_label, globalReimbursementProvider, wise, shouldDismiss) => { + ['leaves the banners alone when Get started opens the form', undefined, undefined], + ])('%s', async (_label, globalReimbursementProvider, wise) => { // Given the intro for a bank account at some point of its Wise onboarding await renderPage(createBusinessBankAccount(globalReimbursementProvider, wise)); @@ -151,7 +150,7 @@ describe('WiseGlobalReimbursementsIntroPage', () => { fireEvent.press(screen.getByTestId(PRIMARY_BUTTON_TEST_ID)); await waitForBatchedUpdatesWithAct(); - // Then only the status page path dismisses them here, since the form dismisses them itself once it opens + // Then neither banner is dismissed, since they hide on their own once any account has a saved Wise setup let dismissedProductTraining: OnyxEntry; await TestHelper.getOnyxData({ key: ONYXKEYS.NVP_DISMISSED_PRODUCT_TRAINING, @@ -159,7 +158,7 @@ describe('WiseGlobalReimbursementsIntroPage', () => { dismissedProductTraining = value; }, }); - expect(!!dismissedProductTraining?.[CONST.WISE_GLOBAL_REIMBURSEMENTS_WALLET_BANNER]).toBe(shouldDismiss); - expect(!!dismissedProductTraining?.[CONST.WISE_GLOBAL_REIMBURSEMENTS_WORKSPACE_BANNER]).toBe(shouldDismiss); + expect(dismissedProductTraining?.[CONST.WISE_GLOBAL_REIMBURSEMENTS_WALLET_BANNER]).toBeUndefined(); + expect(dismissedProductTraining?.[CONST.WISE_GLOBAL_REIMBURSEMENTS_WORKSPACE_BANNER]).toBeUndefined(); }); }); diff --git a/tests/unit/BankAccountUtilsTest.ts b/tests/unit/BankAccountUtilsTest.ts index 5385bc7d08d6..b78ad531d0a7 100644 --- a/tests/unit/BankAccountUtilsTest.ts +++ b/tests/unit/BankAccountUtilsTest.ts @@ -1308,6 +1308,37 @@ describe('BankAccountUtils', () => { expect(getWiseGlobalReimbursementsBannerBankAccountID(bankAccountList)).toBe(2); }); + it('returns undefined once Wise onboarding started for any account', () => { + // Given an eligible account and an older one whose first Wise onboarding page was saved + const bankAccountList = createMock({ + accountOne: buildBankAccount(1, '2026-05-01 10:00:00'), + accountTwo: buildBankAccount(2, '2026-03-01 10:00:00', { + additionalData: {currency: CONST.CURRENCY.USD, globalReimbursementProvider: CONST.GLOBAL_REIMBURSEMENT_PROVIDER.WISE}, + }), + }); + + // When picking the account for the Wallet banner + // Then nothing is returned, because Wallet already offers to continue the started onboarding + expect(getWiseGlobalReimbursementsBannerBankAccountID(bankAccountList)).toBeUndefined(); + }); + + it('returns undefined while the account with started onboarding is pending deletion', () => { + // Given an eligible account and an account with started Wise onboarding that was deleted offline + const bankAccountList = createMock({ + accountOne: buildBankAccount(1, '2026-03-01 10:00:00'), + accountTwo: { + ...buildBankAccount(2, '2026-05-01 10:00:00', { + additionalData: {currency: CONST.CURRENCY.USD, globalReimbursementProvider: CONST.GLOBAL_REIMBURSEMENT_PROVIDER.WISE}, + }), + pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE, + }, + }); + + // When picking the account for the Wallet banner + // Then nothing is returned, so the banner doesn't show up before the server confirms the deletion + expect(getWiseGlobalReimbursementsBannerBankAccountID(bankAccountList)).toBeUndefined(); + }); + it('returns undefined when no account is eligible', () => { // Given only an account already set up for global reimbursements const bankAccountList = createMock({ @@ -1382,6 +1413,25 @@ describe('BankAccountUtils', () => { // Then nothing is returned, so the banner never points to an account queued for removal expect(getWorkspaceWiseGlobalReimbursementsBannerBankAccountID(policy, bankAccountList)).toBeUndefined(); }); + + it('returns undefined once Wise onboarding started for any account', () => { + // Given an eligible connected account and another account whose first Wise onboarding page was saved + const otherBankAccountID = 2; + const bankAccountList = createMock({ + [bankAccountID]: {accountData: eligibleAccountData}, + [otherBankAccountID]: { + accountData: { + ...eligibleAccountData, + bankAccountID: otherBankAccountID, + additionalData: {currency: CONST.CURRENCY.USD, globalReimbursementProvider: CONST.GLOBAL_REIMBURSEMENT_PROVIDER.WISE}, + }, + }, + }); + + // When picking the account for the Workspace banner + // Then nothing is returned, because Wallet already offers to continue the started onboarding + expect(getWorkspaceWiseGlobalReimbursementsBannerBankAccountID(policy, bankAccountList)).toBeUndefined(); + }); }); describe('isWiseOnboardingMoreInfoNeeded', () => { diff --git a/tests/unit/useDeferUntilScreenRevealedTest.ts b/tests/unit/useDeferUntilScreenRevealedTest.ts new file mode 100644 index 000000000000..17added695b6 --- /dev/null +++ b/tests/unit/useDeferUntilScreenRevealedTest.ts @@ -0,0 +1,84 @@ +import {renderHook} from '@testing-library/react-native'; + +import useDeferUntilScreenRevealed from '@hooks/useDeferUntilScreenRevealed'; + +let mockIsFocused = true; +jest.mock('@hooks/useIsScreenFocused', () => ({ + __esModule: true, + default: () => mockIsFocused, +})); + +let mockShouldUseNarrowLayout = false; +jest.mock('@hooks/useResponsiveLayout', () => ({ + __esModule: true, + default: () => ({shouldUseNarrowLayout: mockShouldUseNarrowLayout}), +})); + +type HookProps = {value: number | undefined}; + +function renderBankAccountIDHook(initialBankAccountID: number | undefined) { + const initialProps: HookProps = {value: initialBankAccountID}; + return renderHook(({value}: HookProps) => useDeferUntilScreenRevealed(value), {initialProps}); +} + +describe('useDeferUntilScreenRevealed', () => { + beforeEach(() => { + mockIsFocused = true; + mockShouldUseNarrowLayout = false; + }); + + it('returns the live value while the screen has focus', () => { + // Given a focused screen showing a banner for bank account 1 + const {result, rerender} = renderBankAccountIDHook(1); + + // When the banner's bank account changes + rerender({value: 2}); + + // Then the change shows right away, because the user is looking at the screen + expect(result.current).toBe(2); + }); + + it('keeps the last focused value while another screen covers it', () => { + // Given a focused screen showing a banner for bank account 1 + const {result, rerender} = renderBankAccountIDHook(1); + + // When the RHP covers the screen and saving onboarding there retires the banner + mockIsFocused = false; + rerender({value: undefined}); + + // Then the banner stays, so it doesn't vanish behind the RHP + expect(result.current).toBe(1); + }); + + it('catches up once the screen is focused again', () => { + // Given a covered screen that kept bank account 1 while the banner was retired + const {result, rerender} = renderBankAccountIDHook(1); + mockIsFocused = false; + rerender({value: undefined}); + + // When the RHP closes and the screen is focused again + mockIsFocused = true; + rerender({value: undefined}); + + // Then the banner goes away, along with the RHP closing + expect(result.current).toBeUndefined(); + + // And it stays gone if the screen is covered again later + mockIsFocused = false; + rerender({value: undefined}); + expect(result.current).toBeUndefined(); + }); + + it('returns the live value on narrow layout even while another screen covers it', () => { + // Given a narrow layout screen showing a banner for bank account 1 + mockShouldUseNarrowLayout = true; + const {result, rerender} = renderBankAccountIDHook(1); + + // When the full screen RHP covers it and saving onboarding there retires the banner + mockIsFocused = false; + rerender({value: undefined}); + + // Then the banner goes away while hidden, so it doesn't jump as the user navigates back + expect(result.current).toBeUndefined(); + }); +});