From 35fd722c7d61206169e7a4b8ca82049b4a9c8fe1 Mon Sep 17 00:00:00 2001 From: TaduJR Date: Tue, 20 Jan 2026 14:13:23 +0300 Subject: [PATCH 1/4] test: Clean up SettlementButton --- src/components/SettlementButton/index.tsx | 104 +-- tests/ui/components/SettlementButtonTest.tsx | 756 +++++++++++++++++++ 2 files changed, 793 insertions(+), 67 deletions(-) create mode 100644 tests/ui/components/SettlementButtonTest.tsx diff --git a/src/components/SettlementButton/index.tsx b/src/components/SettlementButton/index.tsx index ab5ae68d523c..11e6498b0a05 100644 --- a/src/components/SettlementButton/index.tsx +++ b/src/components/SettlementButton/index.tsx @@ -1,7 +1,7 @@ import {isUserValidatedSelector} from '@selectors/Account'; import isEmpty from 'lodash/isEmpty'; import truncate from 'lodash/truncate'; -import React, {useCallback, useContext, useMemo} from 'react'; +import React, {useContext} from 'react'; import type {GestureResponderEvent} from 'react-native'; import type {TupleToUnion} from 'type-fest'; import ButtonWithDropdownMenu from '@components/ButtonWithDropdownMenu'; @@ -45,7 +45,6 @@ import ROUTES from '@src/ROUTES'; import type {AccountData, BankAccount, LastPaymentMethodType, Policy} from '@src/types/onyx'; import type {PaymentMethodType} from '@src/types/onyx/OriginalMessage'; import {isEmptyObject} from '@src/types/utils/EmptyObject'; -import isLoadingOnyxValue from '@src/types/utils/isLoadingOnyxValue'; import type SettlementButtonProps from './types'; type KYCFlowEvent = GestureResponderEvent | KeyboardEvent | undefined; @@ -113,16 +112,12 @@ function SettlementButton({ const [userWallet] = useOnyx(ONYXKEYS.USER_WALLET, {canBeMissing: true}); const hasActivatedWallet = ([CONST.WALLET.TIER_NAME.GOLD, CONST.WALLET.TIER_NAME.PLATINUM] as string[]).includes(userWallet?.tierName ?? ''); const paymentMethods = useSettlementButtonPaymentMethods(hasActivatedWallet, translate); - const [lastPaymentMethods, lastPaymentMethodResult] = useOnyx(ONYXKEYS.NVP_LAST_PAYMENT_METHOD, {canBeMissing: true}); + const [lastPaymentMethods] = useOnyx(ONYXKEYS.NVP_LAST_PAYMENT_METHOD, {canBeMissing: true}); const [personalPolicyID] = useOnyx(ONYXKEYS.PERSONAL_POLICY_ID, {canBeMissing: true}); - const lastPaymentMethod = useMemo(() => { - if (!iouReport?.type) { - return; - } - - return getLastPolicyPaymentMethod(policyIDKey, personalPolicyID, lastPaymentMethods, iouReport?.type as keyof LastPaymentMethodType, isIOUReport(iouReport)); - }, [policyIDKey, iouReport, lastPaymentMethods, personalPolicyID]); + const lastPaymentMethod = iouReport?.type + ? getLastPolicyPaymentMethod(policyIDKey, personalPolicyID, lastPaymentMethods, iouReport?.type as keyof LastPaymentMethodType, isIOUReport(iouReport)) + : undefined; const lastBankAccountID = getLastPolicyBankAccountID(policyIDKey, lastPaymentMethods, iouReport?.type as keyof LastPaymentMethodType); const [fundList] = useOnyx(ONYXKEYS.FUND_LIST, {canBeMissing: true}); @@ -136,7 +131,6 @@ function SettlementButton({ const personalPolicy = usePolicy(personalPolicyID); const hasPreferredPaymentMethod = !!lastPaymentMethod; - const isLoadingLastPaymentMethod = isLoadingOnyxValue(lastPaymentMethodResult); const lastPaymentPolicy = usePolicy(lastPaymentMethod); const [bankAccountList] = useOnyx(ONYXKEYS.BANK_ACCOUNT_LIST, {canBeMissing: true}); @@ -190,7 +184,7 @@ function SettlementButton({ return formattedPaymentMethods.filter((ba) => (ba.accountData as AccountData)?.type === CONST.BANK_ACCOUNT.TYPE.PERSONAL); } - const checkForNecessaryAction = useCallback(() => { + const checkForNecessaryAction = () => { if (isDelegateAccessRestricted) { showDelegateNoAccessModal(); return true; @@ -212,41 +206,38 @@ function SettlementButton({ } return false; - }, [policy, isAccountLocked, isUserValidated, chatReportID, reportID, showLockedAccountModal, isDelegateAccessRestricted, showDelegateNoAccessModal]); - - const getPaymentSubitems = useCallback( - (payAsBusiness: boolean) => { - const requiredAccountType = payAsBusiness ? CONST.BANK_ACCOUNT.TYPE.BUSINESS : CONST.BANK_ACCOUNT.TYPE.PERSONAL; - - return formattedPaymentMethods - .filter((method) => { - const accountData = method?.accountData as AccountData; - return accountData?.type === requiredAccountType; - }) - .map((formattedPaymentMethod) => ({ - text: formattedPaymentMethod?.title ?? '', - description: formattedPaymentMethod?.description ?? '', - icon: formattedPaymentMethod?.icon, - shouldUpdateSelectedIndex: true, - onSelected: () => { - if (checkForNecessaryAction()) { - return; - } - onPress(CONST.IOU.PAYMENT_TYPE.EXPENSIFY, payAsBusiness, formattedPaymentMethod.methodID, formattedPaymentMethod.accountType, undefined); - }, - iconStyles: formattedPaymentMethod?.iconStyles, - iconHeight: formattedPaymentMethod?.iconSize, - iconWidth: formattedPaymentMethod?.iconSize, - value: CONST.IOU.PAYMENT_TYPE.EXPENSIFY, - })); - }, - [formattedPaymentMethods, onPress, checkForNecessaryAction], - ); + }; + + const getPaymentSubitems = (payAsBusiness: boolean) => { + const requiredAccountType = payAsBusiness ? CONST.BANK_ACCOUNT.TYPE.BUSINESS : CONST.BANK_ACCOUNT.TYPE.PERSONAL; + + return formattedPaymentMethods + .filter((method) => { + const accountData = method?.accountData as AccountData; + return accountData?.type === requiredAccountType; + }) + .map((formattedPaymentMethod) => ({ + text: formattedPaymentMethod?.title ?? '', + description: formattedPaymentMethod?.description ?? '', + icon: formattedPaymentMethod?.icon, + shouldUpdateSelectedIndex: true, + onSelected: () => { + if (checkForNecessaryAction()) { + return; + } + onPress(CONST.IOU.PAYMENT_TYPE.EXPENSIFY, payAsBusiness, formattedPaymentMethod.methodID, formattedPaymentMethod.accountType, undefined); + }, + iconStyles: formattedPaymentMethod?.iconStyles, + iconHeight: formattedPaymentMethod?.iconSize, + iconWidth: formattedPaymentMethod?.iconSize, + value: CONST.IOU.PAYMENT_TYPE.EXPENSIFY, + })); + }; const personalBankAccountList = getLatestPersonalBankAccount(); const latestBankItem = getLatestBankAccountItem(); - const paymentButtonOptions = useMemo(() => { + const getPaymentButtonOptions = (): Array> => { const buttonOptions: Array> = []; const shortFormPayElsewhereButton = { @@ -419,30 +410,9 @@ function SettlementButton({ } return buttonOptions; - // We don't want to reorder the options when the preferred payment method changes while the button is still visible except for component initialization when the last payment method is not initialized yet. - // We need to be sure that onPress should be wrapped in an useCallback to prevent unnecessary updates. - // eslint-disable-next-line react-hooks/exhaustive-deps - }, [ - paymentMethods, - isLoadingLastPaymentMethod, - iouReport, - translate, - formattedAmount, - shouldDisableApproveButton, - isInvoiceReport, - currency, - shouldHidePaymentOptions, - shouldShowApproveButton, - shouldShowPayWithExpensifyOption, - shouldShowPayElsewhereOption, - chatReport, - onPress, - onlyShowPayElsewhere, - latestBankItem, - activeAdminPolicies, - checkForNecessaryAction, - icons, - ]); + }; + + const paymentButtonOptions = getPaymentButtonOptions(); const selectPaymentType = (event: KYCFlowEvent, iouPaymentType: PaymentMethodType) => { if (iouPaymentType === CONST.IOU.REPORT_ACTION_TYPE.APPROVE) { diff --git a/tests/ui/components/SettlementButtonTest.tsx b/tests/ui/components/SettlementButtonTest.tsx new file mode 100644 index 000000000000..eba0734ed1ea --- /dev/null +++ b/tests/ui/components/SettlementButtonTest.tsx @@ -0,0 +1,756 @@ +/* eslint-disable react/jsx-props-no-spreading -- Using spread for defaultProps in tests for cleaner test code */ +import {act, render, screen} from '@testing-library/react-native'; +import React from 'react'; +import Onyx from 'react-native-onyx'; +import {CurrentUserPersonalDetailsProvider} from '@components/CurrentUserPersonalDetailsProvider'; +import {LocaleContextProvider} from '@components/LocaleContextProvider'; +import OnyxListItemProvider from '@components/OnyxListItemProvider'; +import SettlementButton from '@components/SettlementButton'; +import CONST from '@src/CONST'; +import ONYXKEYS from '@src/ONYXKEYS'; +import ROUTES from '@src/ROUTES'; +import type {BankAccountList, LastPaymentMethod, Policy, Report} from '@src/types/onyx'; +import {signInWithTestUser, translateLocal} from '../../utils/TestHelper'; +import waitForBatchedUpdatesWithAct from '../../utils/waitForBatchedUpdatesWithAct'; + +// Mock Navigation +jest.mock('@libs/Navigation/Navigation', () => { + const mockRef = { + getCurrentRoute: jest.fn(() => ({ + name: 'Report', + params: {}, + })), + getState: jest.fn(() => ({})), + }; + return { + navigate: jest.fn(), + goBack: jest.fn(), + navigationRef: mockRef, + getActiveRoute: jest.fn(() => '/r/123'), + }; +}); + +jest.mock('@libs/Navigation/navigationRef', () => ({ + getCurrentRoute: jest.fn(() => ({ + name: 'Report', + params: {}, + })), + getState: jest.fn(() => ({})), +})); + +jest.mock('@react-navigation/native', () => { + const mockRef = { + getCurrentRoute: jest.fn(() => ({ + name: 'Report', + params: {}, + })), + getState: jest.fn(() => ({})), + }; + return { + createNavigationContainerRef: jest.fn(() => mockRef), + useIsFocused: () => true, + useNavigation: () => ({navigate: jest.fn(), addListener: jest.fn()}), + useFocusEffect: jest.fn(), + usePreventRemove: jest.fn(), + }; +}); + +jest.mock('@src/hooks/useResponsiveLayout'); + +jest.mock('@components/ProductTrainingContext', () => ({ + useProductTrainingContext: () => [false], +})); + +// Test constants +const ACCOUNT_ID = 1; +const ACCOUNT_LOGIN = 'test@user.com'; +const REPORT_ID = '123'; +const CHAT_REPORT_ID = '456'; +const POLICY_ID = 'test-policy-id'; +const BANK_ACCOUNT_ID = 12345; + +// Helper to create a basic policy +function createTestPolicy(overrides: Partial = {}): Policy { + return { + id: POLICY_ID, + name: 'Test Policy', + type: CONST.POLICY.TYPE.CORPORATE, + owner: ACCOUNT_LOGIN, + ownerAccountID: ACCOUNT_ID, + outputCurrency: 'USD', + role: CONST.POLICY.ROLE.ADMIN, + isPolicyExpenseChatEnabled: true, + ...overrides, + } as Policy; +} + +// Helper to create IOU report +function createIOUReport(overrides: Partial = {}): Report { + return { + reportID: REPORT_ID, + type: CONST.REPORT.TYPE.IOU, + ownerAccountID: ACCOUNT_ID, + managerID: 2, + currency: 'USD', + total: 10000, + stateNum: CONST.REPORT.STATE_NUM.SUBMITTED, + statusNum: CONST.REPORT.STATUS_NUM.REIMBURSED, + ...overrides, + } as Report; +} + +// Helper to create expense report +function createExpenseReport(overrides: Partial = {}): Report { + return { + reportID: REPORT_ID, + type: CONST.REPORT.TYPE.EXPENSE, + policyID: POLICY_ID, + ownerAccountID: ACCOUNT_ID, + managerID: 2, + currency: 'USD', + total: 10000, + stateNum: CONST.REPORT.STATE_NUM.SUBMITTED, + statusNum: CONST.REPORT.STATUS_NUM.REIMBURSED, + ...overrides, + } as Report; +} + +// Helper to create invoice report +function createInvoiceReport(overrides: Partial = {}): Report { + return { + reportID: REPORT_ID, + type: CONST.REPORT.TYPE.INVOICE, + policyID: POLICY_ID, + ownerAccountID: ACCOUNT_ID, + managerID: 2, + currency: 'USD', + total: 10000, + stateNum: CONST.REPORT.STATE_NUM.SUBMITTED, + statusNum: CONST.REPORT.STATUS_NUM.REIMBURSED, + ...overrides, + } as Report; +} + +// Helper to create chat report +function createChatReport(overrides: Partial = {}): Report { + return { + reportID: CHAT_REPORT_ID, + type: CONST.REPORT.TYPE.CHAT, + policyID: POLICY_ID, + chatType: CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT, + ...overrides, + } as Report; +} + +// Helper to create bank account list +function createBankAccountList(accountNumber = '1234'): BankAccountList { + return { + [BANK_ACCOUNT_ID]: { + accountData: { + accountNumber, + bankAccountID: BANK_ACCOUNT_ID, + state: CONST.BANK_ACCOUNT.STATE.OPEN, + type: CONST.BANK_ACCOUNT.TYPE.BUSINESS, + routingNumber: '123456789', + }, + isDefault: true, + methodID: BANK_ACCOUNT_ID, + accountType: CONST.PAYMENT_METHODS.PERSONAL_BANK_ACCOUNT, + title: `Bank Account ****${accountNumber}`, + description: 'Business Account', + bankCurrency: 'USD', + bankCountry: 'US', + }, + }; +} + +// Helper to create personal bank account +function createPersonalBankAccount(accountNumber = '5678'): BankAccountList { + const personalBankAccountID = 67890; + return { + [personalBankAccountID]: { + accountData: { + accountNumber, + bankAccountID: personalBankAccountID, + state: CONST.BANK_ACCOUNT.STATE.OPEN, + type: CONST.BANK_ACCOUNT.TYPE.PERSONAL, + routingNumber: '987654321', + }, + isDefault: false, + methodID: personalBankAccountID, + accountType: CONST.PAYMENT_METHODS.PERSONAL_BANK_ACCOUNT, + title: `Personal Account ****${accountNumber}`, + description: 'Personal Account', + bankCurrency: 'USD', + bankCountry: 'US', + }, + }; +} + +// Wrapper component with all required providers +function SettlementButtonWrapper({children}: {children: React.ReactNode}) { + return ( + + + {children} + + + ); +} + +// Default props for SettlementButton +const defaultProps = { + onPress: jest.fn(), + enablePaymentsRoute: ROUTES.ENABLE_PAYMENTS as typeof ROUTES.ENABLE_PAYMENTS, + policyID: POLICY_ID, + chatReportID: CHAT_REPORT_ID, + formattedAmount: '$100.00', +}; + +describe('SettlementButton', () => { + beforeEach(async () => { + jest.clearAllMocks(); + Onyx.init({ + keys: ONYXKEYS, + evictableKeys: [ONYXKEYS.COLLECTION.REPORT_ACTIONS], + }); + await signInWithTestUser(ACCOUNT_ID, ACCOUNT_LOGIN); + }); + + afterEach(async () => { + await Onyx.clear(); + }); + + describe('customText (button label)', () => { + it('displays "Pay" when shouldUseShortForm is true', async () => { + const iouReport = createIOUReport(); + + await act(async () => { + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, iouReport); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${CHAT_REPORT_ID}`, createChatReport()); + }); + + render( + + + , + ); + + await waitForBatchedUpdatesWithAct(); + + expect(screen.getByText(translateLocal('iou.pay'))).toBeTruthy(); + }); + + it('displays "Mark as paid" when lastPaymentMethod is ELSEWHERE', async () => { + const iouReport = createIOUReport(); + + await act(async () => { + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, iouReport); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${CHAT_REPORT_ID}`, createChatReport()); + // LastPaymentMethodType expects PaymentInformation objects with a 'name' property + await Onyx.merge(ONYXKEYS.NVP_LAST_PAYMENT_METHOD, { + [CONST.POLICY.ID_FAKE]: { + lastUsed: {name: CONST.IOU.PAYMENT_TYPE.ELSEWHERE}, + iou: {name: CONST.IOU.PAYMENT_TYPE.ELSEWHERE}, + expense: {name: CONST.IOU.PAYMENT_TYPE.ELSEWHERE}, + invoice: CONST.IOU.PAYMENT_TYPE.ELSEWHERE, + }, + }); + }); + + render( + + + , + ); + + await waitForBatchedUpdatesWithAct(); + + // payElsewhere returns "Mark $X as paid" format + expect(screen.getByText(translateLocal('iou.payElsewhere', {formattedAmount: '$100.00'}))).toBeTruthy(); + }); + + it('displays "Pay $100.00" (settlePayment) by default', async () => { + const iouReport = createIOUReport(); + + await act(async () => { + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, iouReport); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${CHAT_REPORT_ID}`, createChatReport()); + }); + + render( + + + , + ); + + await waitForBatchedUpdatesWithAct(); + + expect(screen.getByText(translateLocal('iou.settlePayment', {formattedAmount: '$100.00'}))).toBeTruthy(); + }); + }); + + describe('secondLineText (subtitle) for IOU Reports', () => { + it('returns undefined when shouldUseShortForm is true', async () => { + const iouReport = createIOUReport(); + const bankAccountList = createPersonalBankAccount(); + + await act(async () => { + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, iouReport); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${CHAT_REPORT_ID}`, createChatReport()); + await Onyx.merge(ONYXKEYS.BANK_ACCOUNT_LIST, bankAccountList); + await Onyx.merge(ONYXKEYS.NVP_LAST_PAYMENT_METHOD, { + [CONST.POLICY.ID_FAKE]: { + lastUsed: {name: CONST.IOU.PAYMENT_TYPE.EXPENSIFY}, + iou: {name: CONST.IOU.PAYMENT_TYPE.EXPENSIFY}, + expense: {name: CONST.IOU.PAYMENT_TYPE.EXPENSIFY}, + invoice: CONST.IOU.PAYMENT_TYPE.EXPENSIFY, + }, + } as LastPaymentMethod); + }); + + render( + + + , + ); + + await waitForBatchedUpdatesWithAct(); + + // With shortForm, there should be no subtitle (Wallet text should not appear) + expect(screen.queryByText(translateLocal('common.wallet'))).toBeNull(); + }); + + it('returns undefined when lastPaymentMethod is ELSEWHERE', async () => { + const iouReport = createIOUReport(); + + await act(async () => { + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, iouReport); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${CHAT_REPORT_ID}`, createChatReport()); + await Onyx.merge(ONYXKEYS.NVP_LAST_PAYMENT_METHOD, { + [CONST.POLICY.ID_FAKE]: { + lastUsed: {name: CONST.IOU.PAYMENT_TYPE.ELSEWHERE}, + iou: {name: CONST.IOU.PAYMENT_TYPE.ELSEWHERE}, + expense: {name: CONST.IOU.PAYMENT_TYPE.ELSEWHERE}, + invoice: CONST.IOU.PAYMENT_TYPE.ELSEWHERE, + }, + }); + }); + + render( + + + , + ); + + await waitForBatchedUpdatesWithAct(); + + // When paying elsewhere, no subtitle should be shown + expect(screen.queryByText(translateLocal('common.wallet'))).toBeNull(); + }); + + it('shows "Wallet" for IOU report with EXPENSIFY payment method and personal bank accounts', async () => { + const iouReport = createIOUReport(); + const bankAccountList = createPersonalBankAccount(); + + await act(async () => { + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, iouReport); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${CHAT_REPORT_ID}`, createChatReport()); + await Onyx.merge(ONYXKEYS.BANK_ACCOUNT_LIST, bankAccountList); + await Onyx.merge(ONYXKEYS.NVP_LAST_PAYMENT_METHOD, { + [CONST.POLICY.ID_FAKE]: { + lastUsed: {name: CONST.IOU.PAYMENT_TYPE.EXPENSIFY}, + iou: {name: CONST.IOU.PAYMENT_TYPE.EXPENSIFY}, + expense: {name: CONST.IOU.PAYMENT_TYPE.EXPENSIFY}, + invoice: CONST.IOU.PAYMENT_TYPE.EXPENSIFY, + }, + }); + }); + + render( + + + , + ); + + await waitForBatchedUpdatesWithAct(); + + expect(screen.getByText(translateLocal('common.wallet'))).toBeTruthy(); + }); + }); + + describe('secondLineText (subtitle) for Expense Reports', () => { + it('shows bank account last four digits for expense report with policy achAccount', async () => { + const expenseReport = createExpenseReport(); + const policy = createTestPolicy({ + achAccount: { + bankAccountID: BANK_ACCOUNT_ID, + accountNumber: '9876', + routingNumber: '123456789', + addressName: 'Test Business', + bankName: 'Test Bank', + reimburser: 'reimburser@test.com', + state: CONST.BANK_ACCOUNT.STATE.OPEN, + }, + }); + const bankAccountList = createBankAccountList('9876'); + + await act(async () => { + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, expenseReport); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${CHAT_REPORT_ID}`, createChatReport()); + await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${POLICY_ID}`, policy); + await Onyx.merge(ONYXKEYS.BANK_ACCOUNT_LIST, bankAccountList); + await Onyx.merge(ONYXKEYS.NVP_LAST_PAYMENT_METHOD, { + [POLICY_ID]: { + lastUsed: {name: CONST.IOU.PAYMENT_TYPE.VBBA}, + iou: {name: CONST.IOU.PAYMENT_TYPE.VBBA}, + expense: {name: CONST.IOU.PAYMENT_TYPE.VBBA}, + invoice: CONST.IOU.PAYMENT_TYPE.VBBA, + }, + }); + }); + + render( + + + , + ); + + await waitForBatchedUpdatesWithAct(); + + // Should show bank account last 4 digits + expect(screen.getByText(translateLocal('paymentMethodList.bankAccountLastFour', '9876'))).toBeTruthy(); + }); + + it('shows bank account not wallet for expense reports with hasIntentToPay (PR #78915 regression test)', async () => { + // This test validates the fix from PR #78915 + // When hasIntentToPay is true (single payment method), expense reports should show bank account, not wallet + const expenseReport = createExpenseReport(); + const policy = createTestPolicy({ + achAccount: { + bankAccountID: BANK_ACCOUNT_ID, + accountNumber: '9876', + routingNumber: '123456789', + addressName: 'Test Business', + bankName: 'Test Bank', + reimburser: 'reimburser@test.com', + state: CONST.BANK_ACCOUNT.STATE.OPEN, + }, + }); + const bankAccountList = { + ...createBankAccountList('9876'), + ...createPersonalBankAccount('5678'), + }; + + await act(async () => { + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, expenseReport); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${CHAT_REPORT_ID}`, createChatReport()); + await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${POLICY_ID}`, policy); + await Onyx.merge(ONYXKEYS.BANK_ACCOUNT_LIST, bankAccountList); + // No explicit lastPaymentMethod - hasIntentToPay will be true + // This is the exact scenario PR #78915 fixed + }); + + render( + + + , + ); + + await waitForBatchedUpdatesWithAct(); + + // CRITICAL: Should show bank account, NOT wallet for expense reports + expect(screen.getByText(translateLocal('paymentMethodList.bankAccountLastFour', '9876'))).toBeTruthy(); + expect(screen.queryByText(translateLocal('common.wallet'))).toBeNull(); + }); + + it('shows bank account for expense report with hasIntentToPay and policy achAccount', async () => { + const expenseReport = createExpenseReport(); + const policy = createTestPolicy({ + achAccount: { + bankAccountID: BANK_ACCOUNT_ID, + accountNumber: '4321', + routingNumber: '123456789', + addressName: 'Test Business', + bankName: 'Test Bank', + reimburser: 'reimburser@test.com', + state: CONST.BANK_ACCOUNT.STATE.OPEN, + }, + }); + const bankAccountList = createBankAccountList('4321'); + + await act(async () => { + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, expenseReport); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${CHAT_REPORT_ID}`, createChatReport()); + await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${POLICY_ID}`, policy); + await Onyx.merge(ONYXKEYS.BANK_ACCOUNT_LIST, bankAccountList); + // No lastPaymentMethod - hasIntentToPay will be true with single payment method + }); + + render( + + + , + ); + + await waitForBatchedUpdatesWithAct(); + + // Should show bank account, not wallet + expect(screen.getByText(translateLocal('paymentMethodList.bankAccountLastFour', '4321'))).toBeTruthy(); + expect(screen.queryByText(translateLocal('common.wallet'))).toBeNull(); + }); + }); + + describe('secondLineText (subtitle) for Invoice Reports', () => { + // Note: Invoice reports require complex setup including: + // - Invoice receiver on the chat report + // - Proper invoice room configuration + // - Personal details for the receiver + // These tests verify the invoice button renders without errors + + it('renders payment button for invoice report', async () => { + const invoiceReport = createInvoiceReport(); + const chatReport = createChatReport({ + invoiceReceiver: { + type: CONST.REPORT.INVOICE_RECEIVER_TYPE.INDIVIDUAL, + accountID: 2, + }, + }); + const bankAccountList = createBankAccountList('8888'); + + await act(async () => { + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, invoiceReport); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${CHAT_REPORT_ID}`, chatReport); + await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${POLICY_ID}`, createTestPolicy()); + await Onyx.merge(ONYXKEYS.BANK_ACCOUNT_LIST, bankAccountList); + }); + + render( + + + , + ); + + await waitForBatchedUpdatesWithAct(); + + // Invoice should show a payment button (settlePayment format) + expect(screen.getByText(translateLocal('iou.settlePayment', {formattedAmount: '$100.00'}))).toBeTruthy(); + }); + }); + + describe('paymentButtonOptions', () => { + it('shows only Approve button when shouldHidePaymentOptions and shouldShowApproveButton are true', async () => { + const expenseReport = createExpenseReport(); + + await act(async () => { + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, expenseReport); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${CHAT_REPORT_ID}`, createChatReport()); + await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${POLICY_ID}`, createTestPolicy()); + }); + + render( + + + , + ); + + await waitForBatchedUpdatesWithAct(); + + // Should show approve button + expect(screen.getByText(translateLocal('iou.approve', {formattedAmount: '$100.00'}))).toBeTruthy(); + }); + + it('shows only Pay elsewhere button when onlyShowPayElsewhere is true', async () => { + const iouReport = createIOUReport(); + + await act(async () => { + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, iouReport); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${CHAT_REPORT_ID}`, createChatReport()); + }); + + render( + + + , + ); + + await waitForBatchedUpdatesWithAct(); + + // Should show pay elsewhere + expect(screen.getByText(translateLocal('iou.payElsewhere', {formattedAmount: ''}))).toBeTruthy(); + }); + + it('shows short form Pay button when onlyShowPayElsewhere and shouldUseShortForm are true', async () => { + const iouReport = createIOUReport(); + + await act(async () => { + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, iouReport); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${CHAT_REPORT_ID}`, createChatReport()); + }); + + render( + + + , + ); + + await waitForBatchedUpdatesWithAct(); + + // Should show short form "Pay" button + expect(screen.getByText(translateLocal('iou.pay'))).toBeTruthy(); + }); + + it('does NOT show wallet option for expense reports in dropdown', async () => { + const expenseReport = createExpenseReport(); + const policy = createTestPolicy({ + achAccount: { + bankAccountID: BANK_ACCOUNT_ID, + accountNumber: '9876', + routingNumber: '123456789', + addressName: 'Test Business', + bankName: 'Test Bank', + reimburser: 'reimburser@test.com', + state: CONST.BANK_ACCOUNT.STATE.OPEN, + }, + }); + const bankAccountList = { + ...createBankAccountList('9876'), + ...createPersonalBankAccount('5678'), + }; + + await act(async () => { + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, expenseReport); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${CHAT_REPORT_ID}`, createChatReport()); + await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${POLICY_ID}`, policy); + await Onyx.merge(ONYXKEYS.BANK_ACCOUNT_LIST, bankAccountList); + }); + + render( + + + , + ); + + await waitForBatchedUpdatesWithAct(); + + // Wallet option should NOT appear for expense reports + expect(screen.queryByText(translateLocal('iou.settleWallet', {formattedAmount: ''}))).toBeNull(); + }); + }); + + describe('special cases', () => { + it('returns undefined for secondLineText when only ELSEWHERE options are available', async () => { + const iouReport = createIOUReport(); + + await act(async () => { + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, iouReport); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${CHAT_REPORT_ID}`, createChatReport()); + // No bank accounts configured - only ELSEWHERE option available + }); + + render( + + + , + ); + + await waitForBatchedUpdatesWithAct(); + + // No subtitle should be shown when only ELSEWHERE is available + expect(screen.queryByText(translateLocal('common.wallet'))).toBeNull(); + }); + + it('shows policy name when lastPaymentPolicy exists', async () => { + const iouReport = createIOUReport(); + const policy = createTestPolicy({name: 'My Workspace'}); + + await act(async () => { + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, iouReport); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${CHAT_REPORT_ID}`, createChatReport()); + await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${POLICY_ID}`, policy); + // When lastPaymentMethod points to a policyID instead of a payment type, + // it shows that policy's name as the subtitle + await Onyx.merge(ONYXKEYS.NVP_LAST_PAYMENT_METHOD, { + [CONST.POLICY.ID_FAKE]: { + lastUsed: {name: POLICY_ID}, + iou: {name: POLICY_ID}, + expense: {name: POLICY_ID}, + invoice: POLICY_ID, + }, + }); + }); + + render( + + + , + ); + + await waitForBatchedUpdatesWithAct(); + + // Should show the policy name as subtitle + expect(screen.getByText('My Workspace')).toBeTruthy(); + }); + }); +}); From 75be11b7e4450a03c9b8ef94f99951b8a1398017 Mon Sep 17 00:00:00 2001 From: TaduJR Date: Tue, 20 Jan 2026 15:14:05 +0300 Subject: [PATCH 2/4] test: Clean up SettlementButton add more tests --- tests/ui/components/SettlementButtonTest.tsx | 1056 +++++++++++++----- 1 file changed, 748 insertions(+), 308 deletions(-) diff --git a/tests/ui/components/SettlementButtonTest.tsx b/tests/ui/components/SettlementButtonTest.tsx index eba0734ed1ea..e1056d209922 100644 --- a/tests/ui/components/SettlementButtonTest.tsx +++ b/tests/ui/components/SettlementButtonTest.tsx @@ -1,5 +1,5 @@ /* eslint-disable react/jsx-props-no-spreading -- Using spread for defaultProps in tests for cleaner test code */ -import {act, render, screen} from '@testing-library/react-native'; +import {act, fireEvent, render, screen} from '@testing-library/react-native'; import React from 'react'; import Onyx from 'react-native-onyx'; import {CurrentUserPersonalDetailsProvider} from '@components/CurrentUserPersonalDetailsProvider'; @@ -13,7 +13,6 @@ import type {BankAccountList, LastPaymentMethod, Policy, Report} from '@src/type import {signInWithTestUser, translateLocal} from '../../utils/TestHelper'; import waitForBatchedUpdatesWithAct from '../../utils/waitForBatchedUpdatesWithAct'; -// Mock Navigation jest.mock('@libs/Navigation/Navigation', () => { const mockRef = { getCurrentRoute: jest.fn(() => ({ @@ -61,7 +60,6 @@ jest.mock('@components/ProductTrainingContext', () => ({ useProductTrainingContext: () => [false], })); -// Test constants const ACCOUNT_ID = 1; const ACCOUNT_LOGIN = 'test@user.com'; const REPORT_ID = '123'; @@ -69,9 +67,13 @@ const CHAT_REPORT_ID = '456'; const POLICY_ID = 'test-policy-id'; const BANK_ACCOUNT_ID = 12345; -// Helper to create a basic policy +/** + * Helper to create a basic policy for testing. + * Uses Partial internally but casts to Policy for test convenience. + * This is acceptable in tests where we only need specific fields. + */ function createTestPolicy(overrides: Partial = {}): Policy { - return { + const basePolicy: Partial = { id: POLICY_ID, name: 'Test Policy', type: CONST.POLICY.TYPE.CORPORATE, @@ -80,13 +82,15 @@ function createTestPolicy(overrides: Partial = {}): Policy { outputCurrency: 'USD', role: CONST.POLICY.ROLE.ADMIN, isPolicyExpenseChatEnabled: true, - ...overrides, - } as Policy; + }; + return {...basePolicy, ...overrides} as Policy; } -// Helper to create IOU report +/** + * Helper to create IOU report for testing personal money requests. + */ function createIOUReport(overrides: Partial = {}): Report { - return { + const baseReport: Partial = { reportID: REPORT_ID, type: CONST.REPORT.TYPE.IOU, ownerAccountID: ACCOUNT_ID, @@ -95,13 +99,15 @@ function createIOUReport(overrides: Partial = {}): Report { total: 10000, stateNum: CONST.REPORT.STATE_NUM.SUBMITTED, statusNum: CONST.REPORT.STATUS_NUM.REIMBURSED, - ...overrides, - } as Report; + }; + return {...baseReport, ...overrides} as Report; } -// Helper to create expense report +/** + * Helper to create expense report for testing workspace reimbursements. + */ function createExpenseReport(overrides: Partial = {}): Report { - return { + const baseReport: Partial = { reportID: REPORT_ID, type: CONST.REPORT.TYPE.EXPENSE, policyID: POLICY_ID, @@ -111,13 +117,15 @@ function createExpenseReport(overrides: Partial = {}): Report { total: 10000, stateNum: CONST.REPORT.STATE_NUM.SUBMITTED, statusNum: CONST.REPORT.STATUS_NUM.REIMBURSED, - ...overrides, - } as Report; + }; + return {...baseReport, ...overrides} as Report; } -// Helper to create invoice report +/** + * Helper to create invoice report for testing billing. + */ function createInvoiceReport(overrides: Partial = {}): Report { - return { + const baseReport: Partial = { reportID: REPORT_ID, type: CONST.REPORT.TYPE.INVOICE, policyID: POLICY_ID, @@ -127,22 +135,26 @@ function createInvoiceReport(overrides: Partial = {}): Report { total: 10000, stateNum: CONST.REPORT.STATE_NUM.SUBMITTED, statusNum: CONST.REPORT.STATUS_NUM.REIMBURSED, - ...overrides, - } as Report; + }; + return {...baseReport, ...overrides} as Report; } -// Helper to create chat report +/** + * Helper to create chat report for testing report relationships. + */ function createChatReport(overrides: Partial = {}): Report { - return { + const baseReport: Partial = { reportID: CHAT_REPORT_ID, type: CONST.REPORT.TYPE.CHAT, policyID: POLICY_ID, chatType: CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT, - ...overrides, - } as Report; + }; + return {...baseReport, ...overrides} as Report; } -// Helper to create bank account list +/** + * Helper to create business bank account list for testing. + */ function createBankAccountList(accountNumber = '1234'): BankAccountList { return { [BANK_ACCOUNT_ID]: { @@ -164,7 +176,9 @@ function createBankAccountList(accountNumber = '1234'): BankAccountList { }; } -// Helper to create personal bank account +/** + * Helper to create personal bank account for testing wallet payments. + */ function createPersonalBankAccount(accountNumber = '5678'): BankAccountList { const personalBankAccountID = 67890; return { @@ -187,7 +201,9 @@ function createPersonalBankAccount(accountNumber = '5678'): BankAccountList { }; } -// Wrapper component with all required providers +/** + * Wrapper component with all required providers for rendering SettlementButton. + */ function SettlementButtonWrapper({children}: {children: React.ReactNode}) { return ( @@ -198,7 +214,37 @@ function SettlementButtonWrapper({children}: {children: React.ReactNode}) { ); } -// Default props for SettlementButton +type OnyxSetupParams = { + report?: Report; + chatReport?: Report; + policy?: Policy; + bankAccountList?: BankAccountList; + lastPaymentMethod?: LastPaymentMethod; +}; + +/** + * Helper to set up Onyx state for tests, reducing boilerplate. + */ +async function setupOnyxState({report, chatReport, policy, bankAccountList, lastPaymentMethod}: OnyxSetupParams) { + await act(async () => { + if (report) { + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${report.reportID}`, report); + } + if (chatReport) { + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${chatReport.reportID}`, chatReport); + } + if (policy) { + await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${policy.id}`, policy); + } + if (bankAccountList) { + await Onyx.merge(ONYXKEYS.BANK_ACCOUNT_LIST, bankAccountList); + } + if (lastPaymentMethod) { + await Onyx.merge(ONYXKEYS.NVP_LAST_PAYMENT_METHOD, lastPaymentMethod); + } + }); +} + const defaultProps = { onPress: jest.fn(), enablePaymentsRoute: ROUTES.ENABLE_PAYMENTS as typeof ROUTES.ENABLE_PAYMENTS, @@ -221,13 +267,13 @@ describe('SettlementButton', () => { await Onyx.clear(); }); - describe('customText (button label)', () => { - it('displays "Pay" when shouldUseShortForm is true', async () => { + describe('button label', () => { + it('displays short form "Pay" when shouldUseShortForm is true', async () => { const iouReport = createIOUReport(); - await act(async () => { - await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, iouReport); - await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${CHAT_REPORT_ID}`, createChatReport()); + await setupOnyxState({ + report: iouReport, + chatReport: createChatReport(), }); render( @@ -245,21 +291,20 @@ describe('SettlementButton', () => { expect(screen.getByText(translateLocal('iou.pay'))).toBeTruthy(); }); - it('displays "Mark as paid" when lastPaymentMethod is ELSEWHERE', async () => { + it('displays "Pay elsewhere" (iou.payElsewhere) when last payment method is ELSEWHERE', async () => { const iouReport = createIOUReport(); - await act(async () => { - await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, iouReport); - await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${CHAT_REPORT_ID}`, createChatReport()); - // LastPaymentMethodType expects PaymentInformation objects with a 'name' property - await Onyx.merge(ONYXKEYS.NVP_LAST_PAYMENT_METHOD, { + await setupOnyxState({ + report: iouReport, + chatReport: createChatReport(), + lastPaymentMethod: { [CONST.POLICY.ID_FAKE]: { lastUsed: {name: CONST.IOU.PAYMENT_TYPE.ELSEWHERE}, iou: {name: CONST.IOU.PAYMENT_TYPE.ELSEWHERE}, expense: {name: CONST.IOU.PAYMENT_TYPE.ELSEWHERE}, invoice: CONST.IOU.PAYMENT_TYPE.ELSEWHERE, }, - }); + }, }); render( @@ -274,16 +319,15 @@ describe('SettlementButton', () => { await waitForBatchedUpdatesWithAct(); - // payElsewhere returns "Mark $X as paid" format expect(screen.getByText(translateLocal('iou.payElsewhere', {formattedAmount: '$100.00'}))).toBeTruthy(); }); - it('displays "Pay $100.00" (settlePayment) by default', async () => { + it('displays "Pay $100.00" by default', async () => { const iouReport = createIOUReport(); - await act(async () => { - await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, iouReport); - await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${CHAT_REPORT_ID}`, createChatReport()); + await setupOnyxState({ + report: iouReport, + chatReport: createChatReport(), }); render( @@ -301,181 +345,659 @@ describe('SettlementButton', () => { }); }); - describe('secondLineText (subtitle) for IOU Reports', () => { - it('returns undefined when shouldUseShortForm is true', async () => { - const iouReport = createIOUReport(); - const bankAccountList = createPersonalBankAccount(); + describe('button subtitle', () => { + describe('IOU reports', () => { + it('shows no subtitle when shouldUseShortForm is true', async () => { + const iouReport = createIOUReport(); + const bankAccountList = createPersonalBankAccount(); + + await act(async () => { + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, iouReport); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${CHAT_REPORT_ID}`, createChatReport()); + await Onyx.merge(ONYXKEYS.BANK_ACCOUNT_LIST, bankAccountList); + await Onyx.merge(ONYXKEYS.NVP_LAST_PAYMENT_METHOD, { + [CONST.POLICY.ID_FAKE]: { + lastUsed: {name: CONST.IOU.PAYMENT_TYPE.EXPENSIFY}, + iou: {name: CONST.IOU.PAYMENT_TYPE.EXPENSIFY}, + expense: {name: CONST.IOU.PAYMENT_TYPE.EXPENSIFY}, + invoice: CONST.IOU.PAYMENT_TYPE.EXPENSIFY, + }, + } as LastPaymentMethod); + }); - await act(async () => { - await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, iouReport); - await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${CHAT_REPORT_ID}`, createChatReport()); - await Onyx.merge(ONYXKEYS.BANK_ACCOUNT_LIST, bankAccountList); - await Onyx.merge(ONYXKEYS.NVP_LAST_PAYMENT_METHOD, { - [CONST.POLICY.ID_FAKE]: { - lastUsed: {name: CONST.IOU.PAYMENT_TYPE.EXPENSIFY}, - iou: {name: CONST.IOU.PAYMENT_TYPE.EXPENSIFY}, - expense: {name: CONST.IOU.PAYMENT_TYPE.EXPENSIFY}, - invoice: CONST.IOU.PAYMENT_TYPE.EXPENSIFY, - }, - } as LastPaymentMethod); + render( + + + , + ); + + await waitForBatchedUpdatesWithAct(); + + expect(screen.queryByText(translateLocal('common.wallet'))).toBeNull(); }); - render( - - - , - ); + it('shows no subtitle when last payment method is ELSEWHERE', async () => { + const iouReport = createIOUReport(); + + await act(async () => { + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, iouReport); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${CHAT_REPORT_ID}`, createChatReport()); + await Onyx.merge(ONYXKEYS.NVP_LAST_PAYMENT_METHOD, { + [CONST.POLICY.ID_FAKE]: { + lastUsed: {name: CONST.IOU.PAYMENT_TYPE.ELSEWHERE}, + iou: {name: CONST.IOU.PAYMENT_TYPE.ELSEWHERE}, + expense: {name: CONST.IOU.PAYMENT_TYPE.ELSEWHERE}, + invoice: CONST.IOU.PAYMENT_TYPE.ELSEWHERE, + }, + }); + }); - await waitForBatchedUpdatesWithAct(); + render( + + + , + ); - // With shortForm, there should be no subtitle (Wallet text should not appear) - expect(screen.queryByText(translateLocal('common.wallet'))).toBeNull(); + await waitForBatchedUpdatesWithAct(); + + expect(screen.queryByText(translateLocal('common.wallet'))).toBeNull(); + }); + + it('shows no subtitle when only ELSEWHERE option is available', async () => { + const iouReport = createIOUReport(); + + await act(async () => { + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, iouReport); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${CHAT_REPORT_ID}`, createChatReport()); + }); + + render( + + + , + ); + + await waitForBatchedUpdatesWithAct(); + + expect(screen.queryByText(translateLocal('common.wallet'))).toBeNull(); + }); + + it('shows "Wallet" when payment method is EXPENSIFY and personal bank accounts exist', async () => { + const iouReport = createIOUReport(); + const bankAccountList = createPersonalBankAccount(); + + await act(async () => { + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, iouReport); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${CHAT_REPORT_ID}`, createChatReport()); + await Onyx.merge(ONYXKEYS.BANK_ACCOUNT_LIST, bankAccountList); + await Onyx.merge(ONYXKEYS.NVP_LAST_PAYMENT_METHOD, { + [CONST.POLICY.ID_FAKE]: { + lastUsed: {name: CONST.IOU.PAYMENT_TYPE.EXPENSIFY}, + iou: {name: CONST.IOU.PAYMENT_TYPE.EXPENSIFY}, + expense: {name: CONST.IOU.PAYMENT_TYPE.EXPENSIFY}, + invoice: CONST.IOU.PAYMENT_TYPE.EXPENSIFY, + }, + }); + }); + + render( + + + , + ); + + await waitForBatchedUpdatesWithAct(); + + expect(screen.getByText(translateLocal('common.wallet'))).toBeTruthy(); + }); + + it('shows no wallet when payment method is EXPENSIFY but no personal bank accounts exist', async () => { + const iouReport = createIOUReport(); + + await act(async () => { + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, iouReport); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${CHAT_REPORT_ID}`, createChatReport()); + await Onyx.merge(ONYXKEYS.NVP_LAST_PAYMENT_METHOD, { + [CONST.POLICY.ID_FAKE]: { + lastUsed: {name: CONST.IOU.PAYMENT_TYPE.EXPENSIFY}, + iou: {name: CONST.IOU.PAYMENT_TYPE.EXPENSIFY}, + expense: {name: CONST.IOU.PAYMENT_TYPE.EXPENSIFY}, + invoice: CONST.IOU.PAYMENT_TYPE.EXPENSIFY, + }, + }); + }); + + render( + + + , + ); + + await waitForBatchedUpdatesWithAct(); + + expect(screen.queryByText(translateLocal('common.wallet'))).toBeNull(); + }); + + it('shows no wallet when only business bank accounts exist', async () => { + const iouReport = createIOUReport(); + const bankAccountList = createBankAccountList('9999'); + + await act(async () => { + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, iouReport); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${CHAT_REPORT_ID}`, createChatReport()); + await Onyx.merge(ONYXKEYS.BANK_ACCOUNT_LIST, bankAccountList); + }); + + render( + + + , + ); + + await waitForBatchedUpdatesWithAct(); + + expect(screen.queryByText(translateLocal('common.wallet'))).toBeNull(); + expect(screen.queryByText(translateLocal('iou.settleWallet', {formattedAmount: ''}))).toBeNull(); + }); + + it('shows policy name when last payment method references a policy', async () => { + const iouReport = createIOUReport(); + const policy = createTestPolicy({name: 'My Workspace'}); + + await act(async () => { + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, iouReport); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${CHAT_REPORT_ID}`, createChatReport()); + await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${POLICY_ID}`, policy); + await Onyx.merge(ONYXKEYS.NVP_LAST_PAYMENT_METHOD, { + [CONST.POLICY.ID_FAKE]: { + lastUsed: {name: POLICY_ID}, + iou: {name: POLICY_ID}, + expense: {name: POLICY_ID}, + invoice: POLICY_ID, + }, + }); + }); + + render( + + + , + ); + + await waitForBatchedUpdatesWithAct(); + + expect(screen.getByText('My Workspace')).toBeTruthy(); + }); + + it('does not show bank account last four digits (only expense reports show this)', async () => { + const iouReport = createIOUReport(); + const bankAccountList = createBankAccountList('7777'); + + await act(async () => { + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, iouReport); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${CHAT_REPORT_ID}`, createChatReport()); + await Onyx.merge(ONYXKEYS.BANK_ACCOUNT_LIST, bankAccountList); + await Onyx.merge(ONYXKEYS.NVP_LAST_PAYMENT_METHOD, { + [CONST.POLICY.ID_FAKE]: { + lastUsed: {name: CONST.IOU.PAYMENT_TYPE.VBBA}, + iou: {name: CONST.IOU.PAYMENT_TYPE.VBBA}, + expense: {name: CONST.IOU.PAYMENT_TYPE.VBBA}, + invoice: CONST.IOU.PAYMENT_TYPE.VBBA, + }, + }); + }); + + render( + + + , + ); + + await waitForBatchedUpdatesWithAct(); + + expect(screen.queryByText(translateLocal('paymentMethodList.bankAccountLastFour', '7777'))).toBeNull(); + }); }); - it('returns undefined when lastPaymentMethod is ELSEWHERE', async () => { - const iouReport = createIOUReport(); + describe('expense reports', () => { + it('shows bank account last four digits when policy has achAccount', async () => { + const expenseReport = createExpenseReport(); + const policy = createTestPolicy({ + achAccount: { + bankAccountID: BANK_ACCOUNT_ID, + accountNumber: '9876', + routingNumber: '123456789', + addressName: 'Test Business', + bankName: 'Test Bank', + reimburser: 'reimburser@test.com', + state: CONST.BANK_ACCOUNT.STATE.OPEN, + }, + }); + const bankAccountList = createBankAccountList('9876'); + + await act(async () => { + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, expenseReport); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${CHAT_REPORT_ID}`, createChatReport()); + await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${POLICY_ID}`, policy); + await Onyx.merge(ONYXKEYS.BANK_ACCOUNT_LIST, bankAccountList); + await Onyx.merge(ONYXKEYS.NVP_LAST_PAYMENT_METHOD, { + [POLICY_ID]: { + lastUsed: {name: CONST.IOU.PAYMENT_TYPE.VBBA}, + iou: {name: CONST.IOU.PAYMENT_TYPE.VBBA}, + expense: {name: CONST.IOU.PAYMENT_TYPE.VBBA}, + invoice: CONST.IOU.PAYMENT_TYPE.VBBA, + }, + }); + }); - await act(async () => { - await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, iouReport); - await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${CHAT_REPORT_ID}`, createChatReport()); - await Onyx.merge(ONYXKEYS.NVP_LAST_PAYMENT_METHOD, { - [CONST.POLICY.ID_FAKE]: { - lastUsed: {name: CONST.IOU.PAYMENT_TYPE.ELSEWHERE}, - iou: {name: CONST.IOU.PAYMENT_TYPE.ELSEWHERE}, - expense: {name: CONST.IOU.PAYMENT_TYPE.ELSEWHERE}, - invoice: CONST.IOU.PAYMENT_TYPE.ELSEWHERE, + render( + + + , + ); + + await waitForBatchedUpdatesWithAct(); + + expect(screen.getByText(translateLocal('paymentMethodList.bankAccountLastFour', '9876'))).toBeTruthy(); + }); + + it('shows bank account not wallet even with personal bank accounts', async () => { + const expenseReport = createExpenseReport(); + const policy = createTestPolicy({ + achAccount: { + bankAccountID: BANK_ACCOUNT_ID, + accountNumber: '9876', + routingNumber: '123456789', + addressName: 'Test Business', + bankName: 'Test Bank', + reimburser: 'reimburser@test.com', + state: CONST.BANK_ACCOUNT.STATE.OPEN, }, }); + const bankAccountList = { + ...createBankAccountList('9876'), + ...createPersonalBankAccount('5678'), + }; + + await act(async () => { + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, expenseReport); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${CHAT_REPORT_ID}`, createChatReport()); + await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${POLICY_ID}`, policy); + await Onyx.merge(ONYXKEYS.BANK_ACCOUNT_LIST, bankAccountList); + }); + + render( + + + , + ); + + await waitForBatchedUpdatesWithAct(); + + expect(screen.getByText(translateLocal('paymentMethodList.bankAccountLastFour', '9876'))).toBeTruthy(); + expect(screen.queryByText(translateLocal('common.wallet'))).toBeNull(); }); - render( - - - , - ); + it('shows bank account when hasIntentToPay is true with policy achAccount', async () => { + const expenseReport = createExpenseReport(); + const policy = createTestPolicy({ + achAccount: { + bankAccountID: BANK_ACCOUNT_ID, + accountNumber: '4321', + routingNumber: '123456789', + addressName: 'Test Business', + bankName: 'Test Bank', + reimburser: 'reimburser@test.com', + state: CONST.BANK_ACCOUNT.STATE.OPEN, + }, + }); + const bankAccountList = createBankAccountList('4321'); - await waitForBatchedUpdatesWithAct(); + await act(async () => { + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, expenseReport); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${CHAT_REPORT_ID}`, createChatReport()); + await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${POLICY_ID}`, policy); + await Onyx.merge(ONYXKEYS.BANK_ACCOUNT_LIST, bankAccountList); + }); - // When paying elsewhere, no subtitle should be shown - expect(screen.queryByText(translateLocal('common.wallet'))).toBeNull(); - }); + render( + + + , + ); - it('shows "Wallet" for IOU report with EXPENSIFY payment method and personal bank accounts', async () => { - const iouReport = createIOUReport(); - const bankAccountList = createPersonalBankAccount(); + await waitForBatchedUpdatesWithAct(); - await act(async () => { - await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, iouReport); - await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${CHAT_REPORT_ID}`, createChatReport()); - await Onyx.merge(ONYXKEYS.BANK_ACCOUNT_LIST, bankAccountList); - await Onyx.merge(ONYXKEYS.NVP_LAST_PAYMENT_METHOD, { - [CONST.POLICY.ID_FAKE]: { - lastUsed: {name: CONST.IOU.PAYMENT_TYPE.EXPENSIFY}, - iou: {name: CONST.IOU.PAYMENT_TYPE.EXPENSIFY}, - expense: {name: CONST.IOU.PAYMENT_TYPE.EXPENSIFY}, - invoice: CONST.IOU.PAYMENT_TYPE.EXPENSIFY, + expect(screen.getByText(translateLocal('paymentMethodList.bankAccountLastFour', '4321'))).toBeTruthy(); + expect(screen.queryByText(translateLocal('common.wallet'))).toBeNull(); + }); + + it('shows bank account even when hasIntentToPay but no personal bank accounts', async () => { + const expenseReport = createExpenseReport(); + const policy = createTestPolicy({ + achAccount: { + bankAccountID: BANK_ACCOUNT_ID, + accountNumber: '1111', + routingNumber: '123456789', + addressName: 'Test Business', + bankName: 'Test Bank', + reimburser: 'reimburser@test.com', + state: CONST.BANK_ACCOUNT.STATE.OPEN, }, }); + const bankAccountList = createBankAccountList('1111'); + + await act(async () => { + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, expenseReport); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${CHAT_REPORT_ID}`, createChatReport()); + await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${POLICY_ID}`, policy); + await Onyx.merge(ONYXKEYS.BANK_ACCOUNT_LIST, bankAccountList); + }); + + render( + + + , + ); + + await waitForBatchedUpdatesWithAct(); + + expect(screen.getByText(translateLocal('paymentMethodList.bankAccountLastFour', '1111'))).toBeTruthy(); + expect(screen.queryByText(translateLocal('common.wallet'))).toBeNull(); }); - render( - - - , - ); + it('renders button when VBBA payment method but achAccount has no account number', async () => { + const expenseReport = createExpenseReport(); + const policy = createTestPolicy({ + achAccount: { + bankAccountID: BANK_ACCOUNT_ID, + routingNumber: '123456789', + addressName: 'Test Business', + bankName: 'Test Bank', + reimburser: 'reimburser@test.com', + state: CONST.BANK_ACCOUNT.STATE.OPEN, + } as Policy['achAccount'], + }); - await waitForBatchedUpdatesWithAct(); + await act(async () => { + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, expenseReport); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${CHAT_REPORT_ID}`, createChatReport()); + await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${POLICY_ID}`, policy); + await Onyx.merge(ONYXKEYS.NVP_LAST_PAYMENT_METHOD, { + [POLICY_ID]: { + lastUsed: {name: CONST.IOU.PAYMENT_TYPE.VBBA}, + iou: {name: CONST.IOU.PAYMENT_TYPE.VBBA}, + expense: {name: CONST.IOU.PAYMENT_TYPE.VBBA}, + invoice: CONST.IOU.PAYMENT_TYPE.VBBA, + }, + }); + }); + + render( + + + , + ); + + await waitForBatchedUpdatesWithAct(); + + expect(screen.getByText(translateLocal('iou.settlePayment', {formattedAmount: '$100.00'}))).toBeTruthy(); + }); - expect(screen.getByText(translateLocal('common.wallet'))).toBeTruthy(); + it('shows bank account from formattedPaymentMethods when achAccount has no account number but hasIntentToPay', async () => { + const expenseReport = createExpenseReport(); + const policy = createTestPolicy({ + achAccount: { + bankAccountID: BANK_ACCOUNT_ID, + routingNumber: '123456789', + addressName: 'Test Business', + bankName: 'Test Bank', + reimburser: 'reimburser@test.com', + state: CONST.BANK_ACCOUNT.STATE.OPEN, + } as Policy['achAccount'], + }); + const bankAccountList = createBankAccountList('3333'); + + await act(async () => { + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, expenseReport); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${CHAT_REPORT_ID}`, createChatReport()); + await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${POLICY_ID}`, policy); + await Onyx.merge(ONYXKEYS.BANK_ACCOUNT_LIST, bankAccountList); + }); + + render( + + + , + ); + + await waitForBatchedUpdatesWithAct(); + + expect(screen.getByText(translateLocal('paymentMethodList.bankAccountLastFour', '3333'))).toBeTruthy(); + }); + + it('shows bank account when business bank matches policy achAccount via lastBankAccountID', async () => { + const expenseReport = createExpenseReport(); + const policy = createTestPolicy({ + achAccount: { + bankAccountID: BANK_ACCOUNT_ID, + accountNumber: '2222', + routingNumber: '123456789', + addressName: 'Test Business', + bankName: 'Test Bank', + reimburser: 'reimburser@test.com', + state: CONST.BANK_ACCOUNT.STATE.OPEN, + }, + }); + const bankAccountList = createBankAccountList('2222'); + + await act(async () => { + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, expenseReport); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${CHAT_REPORT_ID}`, createChatReport()); + await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${POLICY_ID}`, policy); + await Onyx.merge(ONYXKEYS.BANK_ACCOUNT_LIST, bankAccountList); + await Onyx.merge(ONYXKEYS.NVP_LAST_PAYMENT_METHOD, { + [POLICY_ID]: { + lastUsed: {name: 'customPaymentMethod', bankAccountID: BANK_ACCOUNT_ID}, + iou: {name: 'customPaymentMethod', bankAccountID: BANK_ACCOUNT_ID}, + expense: {name: 'customPaymentMethod', bankAccountID: BANK_ACCOUNT_ID}, + invoice: 'customPaymentMethod', + }, + }); + }); + + render( + + + , + ); + + await waitForBatchedUpdatesWithAct(); + + expect(screen.getByText(translateLocal('paymentMethodList.bankAccountLastFour', '2222'))).toBeTruthy(); + }); }); - }); - describe('secondLineText (subtitle) for Expense Reports', () => { - it('shows bank account last four digits for expense report with policy achAccount', async () => { - const expenseReport = createExpenseReport(); - const policy = createTestPolicy({ - achAccount: { - bankAccountID: BANK_ACCOUNT_ID, - accountNumber: '9876', - routingNumber: '123456789', - addressName: 'Test Business', - bankName: 'Test Bank', - reimburser: 'reimburser@test.com', - state: CONST.BANK_ACCOUNT.STATE.OPEN, - }, + describe('invoice reports', () => { + it('renders payment button for invoice report', async () => { + const invoiceReport = createInvoiceReport(); + const chatReport = createChatReport({ + invoiceReceiver: { + type: CONST.REPORT.INVOICE_RECEIVER_TYPE.INDIVIDUAL, + accountID: 2, + }, + }); + const bankAccountList = createBankAccountList('8888'); + + await act(async () => { + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, invoiceReport); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${CHAT_REPORT_ID}`, chatReport); + await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${POLICY_ID}`, createTestPolicy()); + await Onyx.merge(ONYXKEYS.BANK_ACCOUNT_LIST, bankAccountList); + }); + + render( + + + , + ); + + await waitForBatchedUpdatesWithAct(); + + expect(screen.getByText(translateLocal('iou.settlePayment', {formattedAmount: '$100.00'}))).toBeTruthy(); }); - const bankAccountList = createBankAccountList('9876'); - await act(async () => { - await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, expenseReport); - await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${CHAT_REPORT_ID}`, createChatReport()); - await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${POLICY_ID}`, policy); - await Onyx.merge(ONYXKEYS.BANK_ACCOUNT_LIST, bankAccountList); - await Onyx.merge(ONYXKEYS.NVP_LAST_PAYMENT_METHOD, { - [POLICY_ID]: { - lastUsed: {name: CONST.IOU.PAYMENT_TYPE.VBBA}, - iou: {name: CONST.IOU.PAYMENT_TYPE.VBBA}, - expense: {name: CONST.IOU.PAYMENT_TYPE.VBBA}, - invoice: CONST.IOU.PAYMENT_TYPE.VBBA, + it('shows business bank label for invoice with business bank account and hasIntentToPay', async () => { + const invoiceReport = createInvoiceReport(); + const chatReport = createChatReport({ + invoiceReceiver: { + type: CONST.REPORT.INVOICE_RECEIVER_TYPE.INDIVIDUAL, + accountID: 2, }, }); + const policy = createTestPolicy({ + achAccount: { + bankAccountID: BANK_ACCOUNT_ID, + accountNumber: '4444', + routingNumber: '123456789', + addressName: 'Test Business', + bankName: 'Test Bank', + reimburser: 'reimburser@test.com', + state: CONST.BANK_ACCOUNT.STATE.OPEN, + }, + }); + const bankAccountList = createBankAccountList('4444'); + + await act(async () => { + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, invoiceReport); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${CHAT_REPORT_ID}`, chatReport); + await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${POLICY_ID}`, policy); + await Onyx.merge(ONYXKEYS.BANK_ACCOUNT_LIST, bankAccountList); + }); + + render( + + + , + ); + + await waitForBatchedUpdatesWithAct(); + + expect(screen.getByText(translateLocal('iou.invoiceBusinessBank', '4444'))).toBeTruthy(); }); - render( - - - , - ); + it('shows personal bank label for invoice with personal bank account and hasIntentToPay', async () => { + const invoiceReport = createInvoiceReport(); + const chatReport = createChatReport({ + invoiceReceiver: { + type: CONST.REPORT.INVOICE_RECEIVER_TYPE.INDIVIDUAL, + accountID: 2, + }, + }); + const policy = createTestPolicy({ + achAccount: { + bankAccountID: BANK_ACCOUNT_ID, + accountNumber: '5555', + routingNumber: '123456789', + addressName: 'Test Business', + bankName: 'Test Bank', + reimburser: 'reimburser@test.com', + state: CONST.BANK_ACCOUNT.STATE.OPEN, + }, + }); + const bankAccountList = createPersonalBankAccount('5555'); - await waitForBatchedUpdatesWithAct(); + await act(async () => { + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, invoiceReport); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${CHAT_REPORT_ID}`, chatReport); + await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${POLICY_ID}`, policy); + await Onyx.merge(ONYXKEYS.BANK_ACCOUNT_LIST, bankAccountList); + }); - // Should show bank account last 4 digits - expect(screen.getByText(translateLocal('paymentMethodList.bankAccountLastFour', '9876'))).toBeTruthy(); + render( + + + , + ); + + await waitForBatchedUpdatesWithAct(); + + expect(screen.getByText(translateLocal('iou.invoicePersonalBank', '5555'))).toBeTruthy(); + }); }); + }); - it('shows bank account not wallet for expense reports with hasIntentToPay (PR #78915 regression test)', async () => { - // This test validates the fix from PR #78915 - // When hasIntentToPay is true (single payment method), expense reports should show bank account, not wallet + describe('payment options dropdown', () => { + it('shows approve button when shouldHidePaymentOptions and shouldShowApproveButton are true', async () => { const expenseReport = createExpenseReport(); - const policy = createTestPolicy({ - achAccount: { - bankAccountID: BANK_ACCOUNT_ID, - accountNumber: '9876', - routingNumber: '123456789', - addressName: 'Test Business', - bankName: 'Test Bank', - reimburser: 'reimburser@test.com', - state: CONST.BANK_ACCOUNT.STATE.OPEN, - }, - }); - const bankAccountList = { - ...createBankAccountList('9876'), - ...createPersonalBankAccount('5678'), - }; await act(async () => { await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, expenseReport); await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${CHAT_REPORT_ID}`, createChatReport()); - await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${POLICY_ID}`, policy); - await Onyx.merge(ONYXKEYS.BANK_ACCOUNT_LIST, bankAccountList); - // No explicit lastPaymentMethod - hasIntentToPay will be true - // This is the exact scenario PR #78915 fixed + await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${POLICY_ID}`, createTestPolicy()); }); render( @@ -483,38 +1005,24 @@ describe('SettlementButton', () => { , ); await waitForBatchedUpdatesWithAct(); - // CRITICAL: Should show bank account, NOT wallet for expense reports - expect(screen.getByText(translateLocal('paymentMethodList.bankAccountLastFour', '9876'))).toBeTruthy(); - expect(screen.queryByText(translateLocal('common.wallet'))).toBeNull(); + expect(screen.getByText(translateLocal('iou.approve', {formattedAmount: '$100.00'}))).toBeTruthy(); }); - it('shows bank account for expense report with hasIntentToPay and policy achAccount', async () => { + it('does not show approve button when shouldShowApproveButton is false', async () => { const expenseReport = createExpenseReport(); - const policy = createTestPolicy({ - achAccount: { - bankAccountID: BANK_ACCOUNT_ID, - accountNumber: '4321', - routingNumber: '123456789', - addressName: 'Test Business', - bankName: 'Test Bank', - reimburser: 'reimburser@test.com', - state: CONST.BANK_ACCOUNT.STATE.OPEN, - }, - }); - const bankAccountList = createBankAccountList('4321'); await act(async () => { await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, expenseReport); await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${CHAT_REPORT_ID}`, createChatReport()); - await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${POLICY_ID}`, policy); - await Onyx.merge(ONYXKEYS.BANK_ACCOUNT_LIST, bankAccountList); - // No lastPaymentMethod - hasIntentToPay will be true with single payment method + await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${POLICY_ID}`, createTestPolicy()); }); render( @@ -522,91 +1030,80 @@ describe('SettlementButton', () => { , ); await waitForBatchedUpdatesWithAct(); - // Should show bank account, not wallet - expect(screen.getByText(translateLocal('paymentMethodList.bankAccountLastFour', '4321'))).toBeTruthy(); - expect(screen.queryByText(translateLocal('common.wallet'))).toBeNull(); + expect(screen.queryByText(translateLocal('iou.approve', {formattedAmount: '$100.00'}))).toBeNull(); }); - }); - describe('secondLineText (subtitle) for Invoice Reports', () => { - // Note: Invoice reports require complex setup including: - // - Invoice receiver on the chat report - // - Proper invoice room configuration - // - Personal details for the receiver - // These tests verify the invoice button renders without errors - - it('renders payment button for invoice report', async () => { - const invoiceReport = createInvoiceReport(); - const chatReport = createChatReport({ - invoiceReceiver: { - type: CONST.REPORT.INVOICE_RECEIVER_TYPE.INDIVIDUAL, - accountID: 2, - }, - }); - const bankAccountList = createBankAccountList('8888'); + it('does not show payment options when shouldHidePaymentOptions is true without approve button', async () => { + const expenseReport = createExpenseReport(); await act(async () => { - await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, invoiceReport); - await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${CHAT_REPORT_ID}`, chatReport); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, expenseReport); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${CHAT_REPORT_ID}`, createChatReport()); await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${POLICY_ID}`, createTestPolicy()); - await Onyx.merge(ONYXKEYS.BANK_ACCOUNT_LIST, bankAccountList); }); render( , ); await waitForBatchedUpdatesWithAct(); - // Invoice should show a payment button (settlePayment format) - expect(screen.getByText(translateLocal('iou.settlePayment', {formattedAmount: '$100.00'}))).toBeTruthy(); + expect(screen.queryByText(translateLocal('iou.approve', {formattedAmount: '$100.00'}))).toBeNull(); }); - }); - describe('paymentButtonOptions', () => { - it('shows only Approve button when shouldHidePaymentOptions and shouldShowApproveButton are true', async () => { - const expenseReport = createExpenseReport(); + it('shows pay elsewhere button when onlyShowPayElsewhere is true', async () => { + const iouReport = createIOUReport(); await act(async () => { - await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, expenseReport); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, iouReport); await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${CHAT_REPORT_ID}`, createChatReport()); - await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${POLICY_ID}`, createTestPolicy()); }); render( , ); await waitForBatchedUpdatesWithAct(); - // Should show approve button - expect(screen.getByText(translateLocal('iou.approve', {formattedAmount: '$100.00'}))).toBeTruthy(); + expect(screen.getByText(translateLocal('iou.payElsewhere', {formattedAmount: ''}))).toBeTruthy(); }); - it('shows only Pay elsewhere button when onlyShowPayElsewhere is true', async () => { + it('shows no subtitle when shouldHidePaymentOptions and onlyShowPayElsewhere are true', async () => { const iouReport = createIOUReport(); + const bankAccountList = createPersonalBankAccount(); await act(async () => { await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, iouReport); await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${CHAT_REPORT_ID}`, createChatReport()); + await Onyx.merge(ONYXKEYS.BANK_ACCOUNT_LIST, bankAccountList); + await Onyx.merge(ONYXKEYS.NVP_LAST_PAYMENT_METHOD, { + [CONST.POLICY.ID_FAKE]: { + lastUsed: {name: CONST.IOU.PAYMENT_TYPE.EXPENSIFY}, + iou: {name: CONST.IOU.PAYMENT_TYPE.EXPENSIFY}, + expense: {name: CONST.IOU.PAYMENT_TYPE.EXPENSIFY}, + invoice: CONST.IOU.PAYMENT_TYPE.EXPENSIFY, + }, + }); }); render( @@ -614,6 +1111,8 @@ describe('SettlementButton', () => { , @@ -621,11 +1120,10 @@ describe('SettlementButton', () => { await waitForBatchedUpdatesWithAct(); - // Should show pay elsewhere - expect(screen.getByText(translateLocal('iou.payElsewhere', {formattedAmount: ''}))).toBeTruthy(); + expect(screen.queryByText(translateLocal('common.wallet'))).toBeNull(); }); - it('shows short form Pay button when onlyShowPayElsewhere and shouldUseShortForm are true', async () => { + it('shows short form pay button when onlyShowPayElsewhere and shouldUseShortForm are true', async () => { const iouReport = createIOUReport(); await act(async () => { @@ -646,11 +1144,10 @@ describe('SettlementButton', () => { await waitForBatchedUpdatesWithAct(); - // Should show short form "Pay" button expect(screen.getByText(translateLocal('iou.pay'))).toBeTruthy(); }); - it('does NOT show wallet option for expense reports in dropdown', async () => { + it('does not show wallet option for expense reports (subtitle shows bank account)', async () => { const expenseReport = createExpenseReport(); const policy = createTestPolicy({ achAccount: { @@ -668,11 +1165,11 @@ describe('SettlementButton', () => { ...createPersonalBankAccount('5678'), }; - await act(async () => { - await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, expenseReport); - await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${CHAT_REPORT_ID}`, createChatReport()); - await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${POLICY_ID}`, policy); - await Onyx.merge(ONYXKEYS.BANK_ACCOUNT_LIST, bankAccountList); + await setupOnyxState({ + report: expenseReport, + chatReport: createChatReport(), + policy, + bankAccountList, }); render( @@ -686,71 +1183,14 @@ describe('SettlementButton', () => { await waitForBatchedUpdatesWithAct(); - // Wallet option should NOT appear for expense reports - expect(screen.queryByText(translateLocal('iou.settleWallet', {formattedAmount: ''}))).toBeNull(); - }); - }); - - describe('special cases', () => { - it('returns undefined for secondLineText when only ELSEWHERE options are available', async () => { - const iouReport = createIOUReport(); - - await act(async () => { - await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, iouReport); - await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${CHAT_REPORT_ID}`, createChatReport()); - // No bank accounts configured - only ELSEWHERE option available - }); - - render( - - - , - ); - - await waitForBatchedUpdatesWithAct(); - - // No subtitle should be shown when only ELSEWHERE is available + expect(screen.getByText(translateLocal('paymentMethodList.bankAccountLastFour', '9876'))).toBeTruthy(); expect(screen.queryByText(translateLocal('common.wallet'))).toBeNull(); - }); - - it('shows policy name when lastPaymentPolicy exists', async () => { - const iouReport = createIOUReport(); - const policy = createTestPolicy({name: 'My Workspace'}); - - await act(async () => { - await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, iouReport); - await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${CHAT_REPORT_ID}`, createChatReport()); - await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${POLICY_ID}`, policy); - // When lastPaymentMethod points to a policyID instead of a payment type, - // it shows that policy's name as the subtitle - await Onyx.merge(ONYXKEYS.NVP_LAST_PAYMENT_METHOD, { - [CONST.POLICY.ID_FAKE]: { - lastUsed: {name: POLICY_ID}, - iou: {name: POLICY_ID}, - expense: {name: POLICY_ID}, - invoice: POLICY_ID, - }, - }); - }); - - render( - - - , - ); + const payButton = screen.getByText(translateLocal('iou.settlePayment', {formattedAmount: '$100.00'})); + fireEvent.press(payButton); await waitForBatchedUpdatesWithAct(); - // Should show the policy name as subtitle - expect(screen.getByText('My Workspace')).toBeTruthy(); + expect(screen.queryByText(translateLocal('iou.settleWallet', {formattedAmount: ''}))).toBeNull(); }); }); }); From f3de0e0e6817a57bc54f93bb1b99c374add9ee20 Mon Sep 17 00:00:00 2001 From: TaduJR Date: Wed, 21 Jan 2026 00:35:17 +0300 Subject: [PATCH 3/4] refactor SettlementButton: inline functions, add tests, remove redundancy --- src/components/SettlementButton/index.tsx | 359 ++++++++++------------ 1 file changed, 158 insertions(+), 201 deletions(-) diff --git a/src/components/SettlementButton/index.tsx b/src/components/SettlementButton/index.tsx index 11e6498b0a05..2d7071ba7b89 100644 --- a/src/components/SettlementButton/index.tsx +++ b/src/components/SettlementButton/index.tsx @@ -1,5 +1,4 @@ import {isUserValidatedSelector} from '@selectors/Account'; -import isEmpty from 'lodash/isEmpty'; import truncate from 'lodash/truncate'; import React, {useContext} from 'react'; import type {GestureResponderEvent} from 'react-native'; @@ -42,15 +41,11 @@ import {approveMoneyRequest} from '@userActions/IOU'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import ROUTES from '@src/ROUTES'; -import type {AccountData, BankAccount, LastPaymentMethodType, Policy} from '@src/types/onyx'; +import type {AccountData, BankAccount, LastPaymentMethodType} from '@src/types/onyx'; import type {PaymentMethodType} from '@src/types/onyx/OriginalMessage'; import {isEmptyObject} from '@src/types/utils/EmptyObject'; import type SettlementButtonProps from './types'; -type KYCFlowEvent = GestureResponderEvent | KeyboardEvent | undefined; - -type TriggerKYCFlow = (params: ContinueActionParams) => void; - type CurrencyType = TupleToUnion; function SettlementButton({ addDebitCardRoute = ROUTES.IOU_SEND_ADD_DEBIT_CARD, @@ -156,33 +151,33 @@ function SettlementButton({ const shouldShowPayWithExpensifyOption = !shouldHidePaymentOptions; const shouldShowPayElsewhereOption = !shouldHidePaymentOptions && !isInvoiceReport; - function getLatestBankAccountItem() { - if (!policy?.achAccount?.bankAccountID) { - return; + const personalBankAccountList: typeof formattedPaymentMethods = []; + const policyBankAccounts: typeof formattedPaymentMethods = []; + for (const method of formattedPaymentMethods) { + const accountData = method.accountData as AccountData | undefined; + if (accountData?.type === CONST.BANK_ACCOUNT.TYPE.PERSONAL) { + personalBankAccountList.push(method); + } + if (policy?.achAccount?.bankAccountID && method.methodID === policy.achAccount.bankAccountID && accountData?.state === CONST.BANK_ACCOUNT.STATE.OPEN) { + policyBankAccounts.push(method); } - const policyBankAccounts = formattedPaymentMethods.filter( - (method) => method.methodID === policy?.achAccount?.bankAccountID && (method.accountData as AccountData)?.state === CONST.BANK_ACCOUNT.STATE.OPEN, - ); - - return policyBankAccounts.map((formattedPaymentMethod) => { - const {icon, iconStyles, iconSize, title, description, methodID} = formattedPaymentMethod ?? {}; - - return { - text: title ?? '', - description: description ?? '', - icon: typeof icon === 'number' ? icons.Bank : icon, - iconStyles: typeof icon === 'number' ? undefined : iconStyles, - iconSize: typeof icon === 'number' ? undefined : iconSize, - onSelected: () => onPress(CONST.IOU.PAYMENT_TYPE.EXPENSIFY, true, undefined), - methodID, - value: CONST.PAYMENT_METHODS.BUSINESS_BANK_ACCOUNT, - }; - }); } - function getLatestPersonalBankAccount() { - return formattedPaymentMethods.filter((ba) => (ba.accountData as AccountData)?.type === CONST.BANK_ACCOUNT.TYPE.PERSONAL); - } + const latestBankItems = policyBankAccounts.map((formattedPaymentMethod) => { + const {icon, iconStyles, iconSize, title, description, methodID} = formattedPaymentMethod ?? {}; + + return { + text: title ?? '', + description: description ?? '', + icon: typeof icon === 'number' ? icons.Bank : icon, + iconStyles: typeof icon === 'number' ? undefined : iconStyles, + iconSize: typeof icon === 'number' ? undefined : iconSize, + onSelected: () => onPress(CONST.IOU.PAYMENT_TYPE.EXPENSIFY, true, undefined), + methodID, + value: CONST.PAYMENT_METHODS.BUSINESS_BANK_ACCOUNT, + }; + }); + const firstBankItem = latestBankItems.at(0); const checkForNecessaryAction = () => { if (isDelegateAccessRestricted) { @@ -208,107 +203,73 @@ function SettlementButton({ return false; }; - const getPaymentSubitems = (payAsBusiness: boolean) => { - const requiredAccountType = payAsBusiness ? CONST.BANK_ACCOUNT.TYPE.BUSINESS : CONST.BANK_ACCOUNT.TYPE.PERSONAL; - - return formattedPaymentMethods - .filter((method) => { - const accountData = method?.accountData as AccountData; - return accountData?.type === requiredAccountType; - }) - .map((formattedPaymentMethod) => ({ - text: formattedPaymentMethod?.title ?? '', - description: formattedPaymentMethod?.description ?? '', - icon: formattedPaymentMethod?.icon, - shouldUpdateSelectedIndex: true, - onSelected: () => { - if (checkForNecessaryAction()) { - return; - } - onPress(CONST.IOU.PAYMENT_TYPE.EXPENSIFY, payAsBusiness, formattedPaymentMethod.methodID, formattedPaymentMethod.accountType, undefined); - }, - iconStyles: formattedPaymentMethod?.iconStyles, - iconHeight: formattedPaymentMethod?.iconSize, - iconWidth: formattedPaymentMethod?.iconSize, - value: CONST.IOU.PAYMENT_TYPE.EXPENSIFY, - })); + const approveButtonOption = { + text: translate('iou.approve', {formattedAmount}), + icon: icons.ThumbsUp, + value: CONST.IOU.REPORT_ACTION_TYPE.APPROVE, + disabled: !!shouldDisableApproveButton, }; - const personalBankAccountList = getLatestPersonalBankAccount(); - const latestBankItem = getLatestBankAccountItem(); + const canUseWallet = !isExpenseReport && !isInvoiceReport && isCurrencySupportedForGlobalReimbursement(currency as CurrencyType); + const canUseBusinessBankAccount = isExpenseReport || (isIOUReport(iouReport) && reportID && !hasRequestFromCurrentAccount(reportID, accountID ?? CONST.DEFAULT_NUMBER_ID)); + const canUsePersonalBankAccount = shouldShowPersonalBankAccountOption || isIOUReport(iouReport); + const isPersonalOnlyOption = canUsePersonalBankAccount && !canUseBusinessBankAccount; - const getPaymentButtonOptions = (): Array> => { - const buttonOptions: Array> = []; + let paymentButtonOptions: Array>; + // Only show the Approve button if the user cannot pay the expense + if (shouldHidePaymentOptions && shouldShowApproveButton) { + paymentButtonOptions = [approveButtonOption]; + } else if (onlyShowPayElsewhere) { const shortFormPayElsewhereButton = { text: translate('iou.pay'), icon: icons.CheckCircle, value: CONST.IOU.PAYMENT_TYPE.ELSEWHERE, shouldUpdateSelectedIndex: false, }; - - const approveButtonOption = { - text: translate('iou.approve', {formattedAmount}), - icon: icons.ThumbsUp, - value: CONST.IOU.REPORT_ACTION_TYPE.APPROVE, - disabled: !!shouldDisableApproveButton, - }; - - const canUseWallet = !isExpenseReport && !isInvoiceReport && isCurrencySupportedForGlobalReimbursement(currency as CurrencyType); - const canUseBusinessBankAccount = isExpenseReport || (isIOUReport(iouReport) && reportID && !hasRequestFromCurrentAccount(reportID, accountID ?? CONST.DEFAULT_NUMBER_ID)); - - const canUsePersonalBankAccount = shouldShowPersonalBankAccountOption || isIOUReport(iouReport); - - const isPersonalOnlyOption = canUsePersonalBankAccount && !canUseBusinessBankAccount; - - // Only show the Approve button if the user cannot pay the expense - if (shouldHidePaymentOptions && shouldShowApproveButton) { - return [approveButtonOption]; - } - - if (onlyShowPayElsewhere) { - return [shouldUseShortForm ? shortFormPayElsewhereButton : paymentMethods[CONST.IOU.PAYMENT_TYPE.ELSEWHERE]]; - } + paymentButtonOptions = [shouldUseShortForm ? shortFormPayElsewhereButton : paymentMethods[CONST.IOU.PAYMENT_TYPE.ELSEWHERE]]; + } else { + paymentButtonOptions = []; // To achieve the one tap pay experience we need to choose the correct payment type as default. if (canUseWallet) { if (personalBankAccountList.length && canUsePersonalBankAccount) { - buttonOptions.push({ + paymentButtonOptions.push({ text: translate('iou.settleWallet', {formattedAmount: ''}), value: CONST.PAYMENT_METHODS.PERSONAL_BANK_ACCOUNT, icon: icons.Wallet, }); } else if (canUsePersonalBankAccount) { - buttonOptions.push(paymentMethods[CONST.PAYMENT_METHODS.PERSONAL_BANK_ACCOUNT]); + paymentButtonOptions.push(paymentMethods[CONST.PAYMENT_METHODS.PERSONAL_BANK_ACCOUNT]); } if (activeAdminPolicies.length === 0 && !isPersonalOnlyOption) { - buttonOptions.push(paymentMethods[CONST.PAYMENT_METHODS.BUSINESS_BANK_ACCOUNT]); + paymentButtonOptions.push(paymentMethods[CONST.PAYMENT_METHODS.BUSINESS_BANK_ACCOUNT]); } } const shouldShowBusinessBankAccountOptions = isExpenseReport && shouldShowPayWithExpensifyOption && !isPersonalOnlyOption; if (shouldShowBusinessBankAccountOptions) { - if (!isEmpty(latestBankItem) && latestBankItem) { - buttonOptions.push({ - text: latestBankItem.at(0)?.text ?? '', - icon: latestBankItem.at(0)?.icon, - additionalIconStyles: latestBankItem.at(0)?.iconStyles, - iconWidth: latestBankItem.at(0)?.iconSize, - iconHeight: latestBankItem.at(0)?.iconSize, + if (firstBankItem) { + paymentButtonOptions.push({ + text: firstBankItem.text, + icon: firstBankItem.icon, + additionalIconStyles: firstBankItem.iconStyles, + iconWidth: firstBankItem.iconSize, + iconHeight: firstBankItem.iconSize, value: CONST.PAYMENT_METHODS.BUSINESS_BANK_ACCOUNT, - description: latestBankItem.at(0)?.description, + description: firstBankItem.description, }); } else { - buttonOptions.push(paymentMethods[CONST.PAYMENT_METHODS.BUSINESS_BANK_ACCOUNT]); + paymentButtonOptions.push(paymentMethods[CONST.PAYMENT_METHODS.BUSINESS_BANK_ACCOUNT]); } } if ((hasMultiplePolicies || hasSinglePolicy) && canUseWallet && !isPersonalOnlyOption) { for (const p of activeAdminPolicies) { const policyName = p.name; - buttonOptions.push({ + paymentButtonOptions.push({ text: translate('iou.payWithPolicy', {policyName: truncate(policyName, {length: CONST.ADDITIONAL_ALLOWED_CHARACTERS}), formattedAmount: ''}), icon: icons.Building, value: p.id, @@ -318,9 +279,9 @@ function SettlementButton({ } if (shouldShowPayElsewhereOption) { - buttonOptions.push({ + paymentButtonOptions.push({ ...paymentMethods[CONST.IOU.PAYMENT_TYPE.ELSEWHERE], - ...(!buttonOptions.length && shouldUseShortForm ? {text: translate('iou.pay')} : {}), + ...(!paymentButtonOptions.length && shouldUseShortForm ? {text: translate('iou.pay')} : {}), }); } @@ -337,29 +298,53 @@ function SettlementButton({ // For individual receivers, allow if user has an active admin policy with supported currency OR user's local currency is supported const isPolicyCurrencySupported = invoiceReceiverPolicy ? isInvoiceReceiverPolicyCurrencySupported : canUseActivePolicy || isUserCurrencySupported; - const getInvoicesOptions = (payAsBusiness: boolean) => { - const getPolicyID = () => { - if (chatReport?.invoiceReceiver?.type === CONST.REPORT.INVOICE_RECEIVER_TYPE.BUSINESS) { - return chatReport?.invoiceReceiver?.policyID; - } - - if (canUseActivePolicy) { - return activePolicy.id; - } + const getPaymentSubitems = (payAsBusiness: boolean) => { + const requiredAccountType = payAsBusiness ? CONST.BANK_ACCOUNT.TYPE.BUSINESS : CONST.BANK_ACCOUNT.TYPE.PERSONAL; + + return formattedPaymentMethods + .filter((method) => { + const accountData = method?.accountData as AccountData; + return accountData?.type === requiredAccountType; + }) + .map((formattedPaymentMethod) => ({ + text: formattedPaymentMethod?.title ?? '', + description: formattedPaymentMethod?.description ?? '', + icon: formattedPaymentMethod?.icon, + shouldUpdateSelectedIndex: true, + onSelected: () => { + if (checkForNecessaryAction()) { + return; + } + onPress(CONST.IOU.PAYMENT_TYPE.EXPENSIFY, payAsBusiness, formattedPaymentMethod.methodID, formattedPaymentMethod.accountType, undefined); + }, + iconStyles: formattedPaymentMethod?.iconStyles, + iconHeight: formattedPaymentMethod?.iconSize, + iconWidth: formattedPaymentMethod?.iconSize, + value: CONST.IOU.PAYMENT_TYPE.EXPENSIFY, + })); + }; - return createWorkspace({ + const getInvoicesOptions = (payAsBusiness: boolean) => { + let invoicePolicyID: string | undefined; + if (chatReport?.invoiceReceiver?.type === CONST.REPORT.INVOICE_RECEIVER_TYPE.BUSINESS) { + invoicePolicyID = chatReport?.invoiceReceiver?.policyID; + } else if (canUseActivePolicy) { + invoicePolicyID = activePolicy.id; + } else { + invoicePolicyID = createWorkspace({ introSelected, activePolicyID, currentUserAccountIDParam: currentUserPersonalDetails.accountID, currentUserEmailParam: currentUserPersonalDetails.email ?? '', }).policyID; - }; + } + const addBankAccountItem = { text: translate('bankAccount.addBankAccount'), icon: icons.Bank, onSelected: () => { if (payAsBusiness) { - navigateToBankAccountRoute(getPolicyID()); + navigateToBankAccountRoute(invoicePolicyID); } else { Navigation.navigate(ROUTES.SETTINGS_ADD_BANK_ACCOUNT.route); } @@ -385,14 +370,14 @@ function SettlementButton({ }; if (isIndividualInvoiceRoomUtil(chatReport)) { - buttonOptions.push({ + paymentButtonOptions.push({ text: translate('iou.settlePersonal', {formattedAmount}), icon: icons.User, value: hasIntentToPay ? CONST.IOU.PAYMENT_TYPE.EXPENSIFY : (lastPaymentMethod ?? CONST.IOU.PAYMENT_TYPE.ELSEWHERE), backButtonText: translate('iou.individual'), subMenuItems: getInvoicesOptions(false), }); - buttonOptions.push({ + paymentButtonOptions.push({ text: translate('iou.settleBusiness', {formattedAmount}), icon: icons.Building, value: hasIntentToPay ? CONST.IOU.PAYMENT_TYPE.EXPENSIFY : (lastPaymentMethod ?? CONST.IOU.PAYMENT_TYPE.ELSEWHERE), @@ -401,20 +386,40 @@ function SettlementButton({ }); } else { // If there is pay as business option, we should show the submenu items instead. - buttonOptions.push(...getInvoicesOptions(true)); + paymentButtonOptions.push(...getInvoicesOptions(true)); } } if (shouldShowApproveButton) { - buttonOptions.push(approveButtonOption); + paymentButtonOptions.push(approveButtonOption); } + } - return buttonOptions; - }; + const handlePaymentSelection = (event: GestureResponderEvent | KeyboardEvent | undefined, selectedOption: string, triggerKYCFlow: (params: ContinueActionParams) => void) => { + if (checkForNecessaryAction()) { + return; + } + + const {paymentType, selectedPolicy: paymentSelectedPolicy, shouldSelectPaymentMethod} = getActivePaymentType(selectedOption, activeAdminPolicies, latestBankItems, policyIDKey); - const paymentButtonOptions = getPaymentButtonOptions(); + // Payment type for 'Pay via workspace' option is "Elsewhere" but selected option points to one of workspaces where user is admin + const isPayingViaWorkspace = paymentType === CONST.IOU.PAYMENT_TYPE.ELSEWHERE && activeAdminPolicies.find((activeAdminPolicy) => activeAdminPolicy.id === selectedOption); + const isPayingWithMethod = paymentType !== CONST.IOU.PAYMENT_TYPE.ELSEWHERE; - const selectPaymentType = (event: KYCFlowEvent, iouPaymentType: PaymentMethodType) => { + if ((!!paymentSelectedPolicy || shouldSelectPaymentMethod) && (isPayingWithMethod || isPayingViaWorkspace)) { + triggerKYCFlow({ + event, + iouPaymentType: paymentType as PaymentMethodType, + paymentMethod: selectedOption as PaymentMethod, + policy: paymentSelectedPolicy ?? (event ? lastPaymentPolicy : undefined), + }); + if (paymentType === CONST.IOU.PAYMENT_TYPE.EXPENSIFY || paymentType === CONST.IOU.PAYMENT_TYPE.VBBA) { + setPersonalBankAccountContinueKYCOnSuccess(ROUTES.ENABLE_PAYMENTS); + } + return; + } + + const iouPaymentType = selectedOption as PaymentMethodType; if (iouPaymentType === CONST.IOU.REPORT_ACTION_TYPE.APPROVE) { if (confirmApproval) { confirmApproval(); @@ -448,44 +453,28 @@ function SettlementButton({ onPress(iouPaymentType, false); } }; - const selectPaymentMethod = (event: KYCFlowEvent, paymentType: string, triggerKYCFlow: TriggerKYCFlow, paymentMethod?: PaymentMethod, selectedPolicy?: Policy) => { - triggerKYCFlow({ - event, - iouPaymentType: paymentType as PaymentMethodType, - paymentMethod, - policy: selectedPolicy ?? (event ? lastPaymentPolicy : undefined), - }); - if (paymentType === CONST.IOU.PAYMENT_TYPE.EXPENSIFY || paymentType === CONST.IOU.PAYMENT_TYPE.VBBA) { - setPersonalBankAccountContinueKYCOnSuccess(ROUTES.ENABLE_PAYMENTS); - } - }; - - const getCustomText = () => { - if (shouldUseShortForm) { - return translate('iou.pay'); - } - - if (lastPaymentMethod === CONST.IOU.PAYMENT_TYPE.ELSEWHERE) { - return translate('iou.payElsewhere', {formattedAmount}); - } - return translate('iou.settlePayment', {formattedAmount}); - }; - - const getSecondaryText = (): string | undefined => { - if ( - shouldUseShortForm || - lastPaymentMethod === CONST.IOU.PAYMENT_TYPE.ELSEWHERE || - (paymentButtonOptions.length === 1 && paymentButtonOptions.every((option) => option.value === CONST.IOU.PAYMENT_TYPE.ELSEWHERE)) || - (shouldHidePaymentOptions && (shouldShowApproveButton || onlyShowPayElsewhere)) - ) { - return undefined; - } - - if (lastPaymentPolicy) { - return lastPaymentPolicy.name; - } + let customText: string; + if (shouldUseShortForm) { + customText = translate('iou.pay'); + } else if (lastPaymentMethod === CONST.IOU.PAYMENT_TYPE.ELSEWHERE) { + customText = translate('iou.payElsewhere', {formattedAmount}); + } else { + customText = translate('iou.settlePayment', {formattedAmount}); + } + let secondaryTextValue: string | undefined; + const shouldHideSecondaryText = + shouldUseShortForm || + lastPaymentMethod === CONST.IOU.PAYMENT_TYPE.ELSEWHERE || + (paymentButtonOptions.length === 1 && paymentButtonOptions.every((option) => option.value === CONST.IOU.PAYMENT_TYPE.ELSEWHERE)) || + (shouldHidePaymentOptions && (shouldShowApproveButton || onlyShowPayElsewhere)); + + if (shouldHideSecondaryText) { + secondaryTextValue = undefined; + } else if (lastPaymentPolicy) { + secondaryTextValue = lastPaymentPolicy.name; + } else { const bankAccountToDisplay = hasIntentToPay ? ((formattedPaymentMethods.find((method) => method.methodID === policy?.achAccount?.bankAccountID) ?? formattedPaymentMethods.at(0)) as BankAccount) : bankAccount; @@ -493,66 +482,35 @@ function SettlementButton({ // Handle bank account payments first (expense reports require bank account, never wallet) if ((lastPaymentMethod === CONST.IOU.PAYMENT_TYPE.VBBA || (hasIntentToPay && isExpenseReport)) && !!policy?.achAccount) { if (policy?.achAccount?.accountNumber) { - return translate('paymentMethodList.bankAccountLastFour', policy?.achAccount?.accountNumber?.slice(-4)); - } - - if (!bankAccountToDisplay?.accountData?.accountNumber) { - return; + secondaryTextValue = translate('paymentMethodList.bankAccountLastFour', policy?.achAccount?.accountNumber?.slice(-4)); + } else if (bankAccountToDisplay?.accountData?.accountNumber) { + secondaryTextValue = translate('paymentMethodList.bankAccountLastFour', bankAccountToDisplay?.accountData?.accountNumber?.slice(-4)); } - - return translate('paymentMethodList.bankAccountLastFour', bankAccountToDisplay?.accountData?.accountNumber?.slice(-4)); - } - - // Handle wallet payments for IOUs and bank account display for invoices - if (lastPaymentMethod === CONST.IOU.PAYMENT_TYPE.EXPENSIFY || (hasIntentToPay && isInvoiceReport)) { + } else if (lastPaymentMethod === CONST.IOU.PAYMENT_TYPE.EXPENSIFY || (hasIntentToPay && isInvoiceReport)) { + // Handle wallet payments for IOUs and bank account display for invoices if (isInvoiceReport) { const isBusinessBankAccount = bankAccountToDisplay?.accountData?.type === CONST.BANK_ACCOUNT.TYPE.BUSINESS; - return translate(isBusinessBankAccount ? 'iou.invoiceBusinessBank' : 'iou.invoicePersonalBank', bankAccountToDisplay?.accountData?.accountNumber?.slice(-4) ?? ''); - } - - if (!personalBankAccountList.length) { - return; + secondaryTextValue = translate( + isBusinessBankAccount ? 'iou.invoiceBusinessBank' : 'iou.invoicePersonalBank', + bankAccountToDisplay?.accountData?.accountNumber?.slice(-4) ?? '', + ); + } else if (personalBankAccountList.length) { + secondaryTextValue = translate('common.wallet'); } - - return translate('common.wallet'); - } - - if (bankAccount?.accountData?.type === CONST.BANK_ACCOUNT.TYPE.BUSINESS && bankAccount?.methodID === policy?.achAccount?.bankAccountID && isExpenseReportUtil(iouReport)) { - return translate('paymentMethodList.bankAccountLastFour', bankAccount?.accountData?.accountNumber?.slice(-4) ?? ''); - } - - return undefined; - }; - - const handlePaymentSelection = (event: GestureResponderEvent | KeyboardEvent | undefined, selectedOption: string, triggerKYCFlow: (params: ContinueActionParams) => void) => { - if (checkForNecessaryAction()) { - return; - } - - const {paymentType, selectedPolicy, shouldSelectPaymentMethod} = getActivePaymentType(selectedOption, activeAdminPolicies, latestBankItem, policyIDKey); - - // Payment type for 'Pay via workspace' option is "Elsewhere" but selected option points to one of workspaces where user is admin - const isPayingViaWorkspace = paymentType === CONST.IOU.PAYMENT_TYPE.ELSEWHERE && activeAdminPolicies.find((activeAdminPolicy) => activeAdminPolicy.id === selectedOption); - const isPayingWithMethod = paymentType !== CONST.IOU.PAYMENT_TYPE.ELSEWHERE; - - if ((!!selectedPolicy || shouldSelectPaymentMethod) && (isPayingWithMethod || isPayingViaWorkspace)) { - selectPaymentMethod(event, paymentType, triggerKYCFlow, selectedOption as PaymentMethod, selectedPolicy); - return; + } else if (bankAccount?.accountData?.type === CONST.BANK_ACCOUNT.TYPE.BUSINESS && bankAccount?.methodID === policy?.achAccount?.bankAccountID && isExpenseReportUtil(iouReport)) { + secondaryTextValue = translate('paymentMethodList.bankAccountLastFour', bankAccount?.accountData?.accountNumber?.slice(-4) ?? ''); } + } - selectPaymentType(event, selectedOption as PaymentMethodType); - }; - - const customText = getCustomText(); - const secondaryText = truncate(getSecondaryText(), {length: CONST.FORM_CHARACTER_LIMIT}); + const secondaryText = truncate(secondaryTextValue, {length: CONST.FORM_CHARACTER_LIMIT}); const defaultSelectedIndex = paymentButtonOptions.findIndex((paymentOption) => { if (lastPaymentMethod === CONST.IOU.PAYMENT_TYPE.ELSEWHERE) { return paymentOption.value === CONST.IOU.PAYMENT_TYPE.ELSEWHERE; } - if (latestBankItem?.length) { - return paymentOption.value === latestBankItem.at(0)?.value; + if (firstBankItem) { + return paymentOption.value === firstBankItem.value; } if (lastPaymentPolicy?.id) { @@ -604,7 +562,6 @@ function SettlementButton({ if (paymentButtonOptions.length === 1) { return; } - handlePaymentSelection(undefined, option.value, triggerKYCFlow); }} style={style} From 1e1274751437be09e554a82dbf451c36bad038d1 Mon Sep 17 00:00:00 2001 From: TaduJR Date: Wed, 21 Jan 2026 01:24:37 +0300 Subject: [PATCH 4/4] refactor: SettlementButton and standardize test setup --- tests/ui/components/SettlementButtonTest.tsx | 483 +++++++++---------- 1 file changed, 228 insertions(+), 255 deletions(-) diff --git a/tests/ui/components/SettlementButtonTest.tsx b/tests/ui/components/SettlementButtonTest.tsx index e1056d209922..ae18bbaa0058 100644 --- a/tests/ui/components/SettlementButtonTest.tsx +++ b/tests/ui/components/SettlementButtonTest.tsx @@ -349,20 +349,19 @@ describe('SettlementButton', () => { describe('IOU reports', () => { it('shows no subtitle when shouldUseShortForm is true', async () => { const iouReport = createIOUReport(); - const bankAccountList = createPersonalBankAccount(); - await act(async () => { - await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, iouReport); - await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${CHAT_REPORT_ID}`, createChatReport()); - await Onyx.merge(ONYXKEYS.BANK_ACCOUNT_LIST, bankAccountList); - await Onyx.merge(ONYXKEYS.NVP_LAST_PAYMENT_METHOD, { + await setupOnyxState({ + report: iouReport, + chatReport: createChatReport(), + bankAccountList: createPersonalBankAccount(), + lastPaymentMethod: { [CONST.POLICY.ID_FAKE]: { lastUsed: {name: CONST.IOU.PAYMENT_TYPE.EXPENSIFY}, iou: {name: CONST.IOU.PAYMENT_TYPE.EXPENSIFY}, expense: {name: CONST.IOU.PAYMENT_TYPE.EXPENSIFY}, invoice: CONST.IOU.PAYMENT_TYPE.EXPENSIFY, }, - } as LastPaymentMethod); + }, }); render( @@ -384,17 +383,17 @@ describe('SettlementButton', () => { it('shows no subtitle when last payment method is ELSEWHERE', async () => { const iouReport = createIOUReport(); - await act(async () => { - await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, iouReport); - await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${CHAT_REPORT_ID}`, createChatReport()); - await Onyx.merge(ONYXKEYS.NVP_LAST_PAYMENT_METHOD, { + await setupOnyxState({ + report: iouReport, + chatReport: createChatReport(), + lastPaymentMethod: { [CONST.POLICY.ID_FAKE]: { lastUsed: {name: CONST.IOU.PAYMENT_TYPE.ELSEWHERE}, iou: {name: CONST.IOU.PAYMENT_TYPE.ELSEWHERE}, expense: {name: CONST.IOU.PAYMENT_TYPE.ELSEWHERE}, invoice: CONST.IOU.PAYMENT_TYPE.ELSEWHERE, }, - }); + }, }); render( @@ -415,9 +414,9 @@ describe('SettlementButton', () => { it('shows no subtitle when only ELSEWHERE option is available', async () => { const iouReport = createIOUReport(); - await act(async () => { - await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, iouReport); - await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${CHAT_REPORT_ID}`, createChatReport()); + await setupOnyxState({ + report: iouReport, + chatReport: createChatReport(), }); render( @@ -437,20 +436,19 @@ describe('SettlementButton', () => { it('shows "Wallet" when payment method is EXPENSIFY and personal bank accounts exist', async () => { const iouReport = createIOUReport(); - const bankAccountList = createPersonalBankAccount(); - await act(async () => { - await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, iouReport); - await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${CHAT_REPORT_ID}`, createChatReport()); - await Onyx.merge(ONYXKEYS.BANK_ACCOUNT_LIST, bankAccountList); - await Onyx.merge(ONYXKEYS.NVP_LAST_PAYMENT_METHOD, { + await setupOnyxState({ + report: iouReport, + chatReport: createChatReport(), + bankAccountList: createPersonalBankAccount(), + lastPaymentMethod: { [CONST.POLICY.ID_FAKE]: { lastUsed: {name: CONST.IOU.PAYMENT_TYPE.EXPENSIFY}, iou: {name: CONST.IOU.PAYMENT_TYPE.EXPENSIFY}, expense: {name: CONST.IOU.PAYMENT_TYPE.EXPENSIFY}, invoice: CONST.IOU.PAYMENT_TYPE.EXPENSIFY, }, - }); + }, }); render( @@ -471,17 +469,17 @@ describe('SettlementButton', () => { it('shows no wallet when payment method is EXPENSIFY but no personal bank accounts exist', async () => { const iouReport = createIOUReport(); - await act(async () => { - await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, iouReport); - await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${CHAT_REPORT_ID}`, createChatReport()); - await Onyx.merge(ONYXKEYS.NVP_LAST_PAYMENT_METHOD, { + await setupOnyxState({ + report: iouReport, + chatReport: createChatReport(), + lastPaymentMethod: { [CONST.POLICY.ID_FAKE]: { lastUsed: {name: CONST.IOU.PAYMENT_TYPE.EXPENSIFY}, iou: {name: CONST.IOU.PAYMENT_TYPE.EXPENSIFY}, expense: {name: CONST.IOU.PAYMENT_TYPE.EXPENSIFY}, invoice: CONST.IOU.PAYMENT_TYPE.EXPENSIFY, }, - }); + }, }); render( @@ -501,12 +499,11 @@ describe('SettlementButton', () => { it('shows no wallet when only business bank accounts exist', async () => { const iouReport = createIOUReport(); - const bankAccountList = createBankAccountList('9999'); - await act(async () => { - await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, iouReport); - await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${CHAT_REPORT_ID}`, createChatReport()); - await Onyx.merge(ONYXKEYS.BANK_ACCOUNT_LIST, bankAccountList); + await setupOnyxState({ + report: iouReport, + chatReport: createChatReport(), + bankAccountList: createBankAccountList('9999'), }); render( @@ -527,20 +524,19 @@ describe('SettlementButton', () => { it('shows policy name when last payment method references a policy', async () => { const iouReport = createIOUReport(); - const policy = createTestPolicy({name: 'My Workspace'}); - await act(async () => { - await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, iouReport); - await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${CHAT_REPORT_ID}`, createChatReport()); - await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${POLICY_ID}`, policy); - await Onyx.merge(ONYXKEYS.NVP_LAST_PAYMENT_METHOD, { + await setupOnyxState({ + report: iouReport, + chatReport: createChatReport(), + policy: createTestPolicy({name: 'My Workspace'}), + lastPaymentMethod: { [CONST.POLICY.ID_FAKE]: { lastUsed: {name: POLICY_ID}, iou: {name: POLICY_ID}, expense: {name: POLICY_ID}, invoice: POLICY_ID, }, - }); + }, }); render( @@ -560,20 +556,19 @@ describe('SettlementButton', () => { it('does not show bank account last four digits (only expense reports show this)', async () => { const iouReport = createIOUReport(); - const bankAccountList = createBankAccountList('7777'); - await act(async () => { - await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, iouReport); - await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${CHAT_REPORT_ID}`, createChatReport()); - await Onyx.merge(ONYXKEYS.BANK_ACCOUNT_LIST, bankAccountList); - await Onyx.merge(ONYXKEYS.NVP_LAST_PAYMENT_METHOD, { + await setupOnyxState({ + report: iouReport, + chatReport: createChatReport(), + bankAccountList: createBankAccountList('7777'), + lastPaymentMethod: { [CONST.POLICY.ID_FAKE]: { lastUsed: {name: CONST.IOU.PAYMENT_TYPE.VBBA}, iou: {name: CONST.IOU.PAYMENT_TYPE.VBBA}, expense: {name: CONST.IOU.PAYMENT_TYPE.VBBA}, invoice: CONST.IOU.PAYMENT_TYPE.VBBA, }, - }); + }, }); render( @@ -595,32 +590,30 @@ describe('SettlementButton', () => { describe('expense reports', () => { it('shows bank account last four digits when policy has achAccount', async () => { const expenseReport = createExpenseReport(); - const policy = createTestPolicy({ - achAccount: { - bankAccountID: BANK_ACCOUNT_ID, - accountNumber: '9876', - routingNumber: '123456789', - addressName: 'Test Business', - bankName: 'Test Bank', - reimburser: 'reimburser@test.com', - state: CONST.BANK_ACCOUNT.STATE.OPEN, - }, - }); - const bankAccountList = createBankAccountList('9876'); - - await act(async () => { - await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, expenseReport); - await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${CHAT_REPORT_ID}`, createChatReport()); - await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${POLICY_ID}`, policy); - await Onyx.merge(ONYXKEYS.BANK_ACCOUNT_LIST, bankAccountList); - await Onyx.merge(ONYXKEYS.NVP_LAST_PAYMENT_METHOD, { + + await setupOnyxState({ + report: expenseReport, + chatReport: createChatReport(), + policy: createTestPolicy({ + achAccount: { + bankAccountID: BANK_ACCOUNT_ID, + accountNumber: '9876', + routingNumber: '123456789', + addressName: 'Test Business', + bankName: 'Test Bank', + reimburser: 'reimburser@test.com', + state: CONST.BANK_ACCOUNT.STATE.OPEN, + }, + }), + bankAccountList: createBankAccountList('9876'), + lastPaymentMethod: { [POLICY_ID]: { lastUsed: {name: CONST.IOU.PAYMENT_TYPE.VBBA}, iou: {name: CONST.IOU.PAYMENT_TYPE.VBBA}, expense: {name: CONST.IOU.PAYMENT_TYPE.VBBA}, invoice: CONST.IOU.PAYMENT_TYPE.VBBA, }, - }); + }, }); render( @@ -639,28 +632,26 @@ describe('SettlementButton', () => { it('shows bank account not wallet even with personal bank accounts', async () => { const expenseReport = createExpenseReport(); - const policy = createTestPolicy({ - achAccount: { - bankAccountID: BANK_ACCOUNT_ID, - accountNumber: '9876', - routingNumber: '123456789', - addressName: 'Test Business', - bankName: 'Test Bank', - reimburser: 'reimburser@test.com', - state: CONST.BANK_ACCOUNT.STATE.OPEN, + + await setupOnyxState({ + report: expenseReport, + chatReport: createChatReport(), + policy: createTestPolicy({ + achAccount: { + bankAccountID: BANK_ACCOUNT_ID, + accountNumber: '9876', + routingNumber: '123456789', + addressName: 'Test Business', + bankName: 'Test Bank', + reimburser: 'reimburser@test.com', + state: CONST.BANK_ACCOUNT.STATE.OPEN, + }, + }), + bankAccountList: { + ...createBankAccountList('9876'), + ...createPersonalBankAccount('5678'), }, }); - const bankAccountList = { - ...createBankAccountList('9876'), - ...createPersonalBankAccount('5678'), - }; - - await act(async () => { - await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, expenseReport); - await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${CHAT_REPORT_ID}`, createChatReport()); - await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${POLICY_ID}`, policy); - await Onyx.merge(ONYXKEYS.BANK_ACCOUNT_LIST, bankAccountList); - }); render( @@ -679,24 +670,22 @@ describe('SettlementButton', () => { it('shows bank account when hasIntentToPay is true with policy achAccount', async () => { const expenseReport = createExpenseReport(); - const policy = createTestPolicy({ - achAccount: { - bankAccountID: BANK_ACCOUNT_ID, - accountNumber: '4321', - routingNumber: '123456789', - addressName: 'Test Business', - bankName: 'Test Bank', - reimburser: 'reimburser@test.com', - state: CONST.BANK_ACCOUNT.STATE.OPEN, - }, - }); - const bankAccountList = createBankAccountList('4321'); - await act(async () => { - await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, expenseReport); - await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${CHAT_REPORT_ID}`, createChatReport()); - await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${POLICY_ID}`, policy); - await Onyx.merge(ONYXKEYS.BANK_ACCOUNT_LIST, bankAccountList); + await setupOnyxState({ + report: expenseReport, + chatReport: createChatReport(), + policy: createTestPolicy({ + achAccount: { + bankAccountID: BANK_ACCOUNT_ID, + accountNumber: '4321', + routingNumber: '123456789', + addressName: 'Test Business', + bankName: 'Test Bank', + reimburser: 'reimburser@test.com', + state: CONST.BANK_ACCOUNT.STATE.OPEN, + }, + }), + bankAccountList: createBankAccountList('4321'), }); render( @@ -716,24 +705,22 @@ describe('SettlementButton', () => { it('shows bank account even when hasIntentToPay but no personal bank accounts', async () => { const expenseReport = createExpenseReport(); - const policy = createTestPolicy({ - achAccount: { - bankAccountID: BANK_ACCOUNT_ID, - accountNumber: '1111', - routingNumber: '123456789', - addressName: 'Test Business', - bankName: 'Test Bank', - reimburser: 'reimburser@test.com', - state: CONST.BANK_ACCOUNT.STATE.OPEN, - }, - }); - const bankAccountList = createBankAccountList('1111'); - await act(async () => { - await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, expenseReport); - await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${CHAT_REPORT_ID}`, createChatReport()); - await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${POLICY_ID}`, policy); - await Onyx.merge(ONYXKEYS.BANK_ACCOUNT_LIST, bankAccountList); + await setupOnyxState({ + report: expenseReport, + chatReport: createChatReport(), + policy: createTestPolicy({ + achAccount: { + bankAccountID: BANK_ACCOUNT_ID, + accountNumber: '1111', + routingNumber: '123456789', + addressName: 'Test Business', + bankName: 'Test Bank', + reimburser: 'reimburser@test.com', + state: CONST.BANK_ACCOUNT.STATE.OPEN, + }, + }), + bankAccountList: createBankAccountList('1111'), }); render( @@ -753,29 +740,28 @@ describe('SettlementButton', () => { it('renders button when VBBA payment method but achAccount has no account number', async () => { const expenseReport = createExpenseReport(); - const policy = createTestPolicy({ - achAccount: { - bankAccountID: BANK_ACCOUNT_ID, - routingNumber: '123456789', - addressName: 'Test Business', - bankName: 'Test Bank', - reimburser: 'reimburser@test.com', - state: CONST.BANK_ACCOUNT.STATE.OPEN, - } as Policy['achAccount'], - }); - await act(async () => { - await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, expenseReport); - await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${CHAT_REPORT_ID}`, createChatReport()); - await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${POLICY_ID}`, policy); - await Onyx.merge(ONYXKEYS.NVP_LAST_PAYMENT_METHOD, { + await setupOnyxState({ + report: expenseReport, + chatReport: createChatReport(), + policy: createTestPolicy({ + achAccount: { + bankAccountID: BANK_ACCOUNT_ID, + routingNumber: '123456789', + addressName: 'Test Business', + bankName: 'Test Bank', + reimburser: 'reimburser@test.com', + state: CONST.BANK_ACCOUNT.STATE.OPEN, + } as Policy['achAccount'], + }), + lastPaymentMethod: { [POLICY_ID]: { lastUsed: {name: CONST.IOU.PAYMENT_TYPE.VBBA}, iou: {name: CONST.IOU.PAYMENT_TYPE.VBBA}, expense: {name: CONST.IOU.PAYMENT_TYPE.VBBA}, invoice: CONST.IOU.PAYMENT_TYPE.VBBA, }, - }); + }, }); render( @@ -794,23 +780,21 @@ describe('SettlementButton', () => { it('shows bank account from formattedPaymentMethods when achAccount has no account number but hasIntentToPay', async () => { const expenseReport = createExpenseReport(); - const policy = createTestPolicy({ - achAccount: { - bankAccountID: BANK_ACCOUNT_ID, - routingNumber: '123456789', - addressName: 'Test Business', - bankName: 'Test Bank', - reimburser: 'reimburser@test.com', - state: CONST.BANK_ACCOUNT.STATE.OPEN, - } as Policy['achAccount'], - }); - const bankAccountList = createBankAccountList('3333'); - await act(async () => { - await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, expenseReport); - await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${CHAT_REPORT_ID}`, createChatReport()); - await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${POLICY_ID}`, policy); - await Onyx.merge(ONYXKEYS.BANK_ACCOUNT_LIST, bankAccountList); + await setupOnyxState({ + report: expenseReport, + chatReport: createChatReport(), + policy: createTestPolicy({ + achAccount: { + bankAccountID: BANK_ACCOUNT_ID, + routingNumber: '123456789', + addressName: 'Test Business', + bankName: 'Test Bank', + reimburser: 'reimburser@test.com', + state: CONST.BANK_ACCOUNT.STATE.OPEN, + } as Policy['achAccount'], + }), + bankAccountList: createBankAccountList('3333'), }); render( @@ -829,32 +813,30 @@ describe('SettlementButton', () => { it('shows bank account when business bank matches policy achAccount via lastBankAccountID', async () => { const expenseReport = createExpenseReport(); - const policy = createTestPolicy({ - achAccount: { - bankAccountID: BANK_ACCOUNT_ID, - accountNumber: '2222', - routingNumber: '123456789', - addressName: 'Test Business', - bankName: 'Test Bank', - reimburser: 'reimburser@test.com', - state: CONST.BANK_ACCOUNT.STATE.OPEN, - }, - }); - const bankAccountList = createBankAccountList('2222'); - - await act(async () => { - await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, expenseReport); - await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${CHAT_REPORT_ID}`, createChatReport()); - await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${POLICY_ID}`, policy); - await Onyx.merge(ONYXKEYS.BANK_ACCOUNT_LIST, bankAccountList); - await Onyx.merge(ONYXKEYS.NVP_LAST_PAYMENT_METHOD, { + + await setupOnyxState({ + report: expenseReport, + chatReport: createChatReport(), + policy: createTestPolicy({ + achAccount: { + bankAccountID: BANK_ACCOUNT_ID, + accountNumber: '2222', + routingNumber: '123456789', + addressName: 'Test Business', + bankName: 'Test Bank', + reimburser: 'reimburser@test.com', + state: CONST.BANK_ACCOUNT.STATE.OPEN, + }, + }), + bankAccountList: createBankAccountList('2222'), + lastPaymentMethod: { [POLICY_ID]: { lastUsed: {name: 'customPaymentMethod', bankAccountID: BANK_ACCOUNT_ID}, iou: {name: 'customPaymentMethod', bankAccountID: BANK_ACCOUNT_ID}, expense: {name: 'customPaymentMethod', bankAccountID: BANK_ACCOUNT_ID}, invoice: 'customPaymentMethod', }, - }); + }, }); render( @@ -875,19 +857,17 @@ describe('SettlementButton', () => { describe('invoice reports', () => { it('renders payment button for invoice report', async () => { const invoiceReport = createInvoiceReport(); - const chatReport = createChatReport({ - invoiceReceiver: { - type: CONST.REPORT.INVOICE_RECEIVER_TYPE.INDIVIDUAL, - accountID: 2, - }, - }); - const bankAccountList = createBankAccountList('8888'); - await act(async () => { - await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, invoiceReport); - await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${CHAT_REPORT_ID}`, chatReport); - await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${POLICY_ID}`, createTestPolicy()); - await Onyx.merge(ONYXKEYS.BANK_ACCOUNT_LIST, bankAccountList); + await setupOnyxState({ + report: invoiceReport, + chatReport: createChatReport({ + invoiceReceiver: { + type: CONST.REPORT.INVOICE_RECEIVER_TYPE.INDIVIDUAL, + accountID: 2, + }, + }), + policy: createTestPolicy(), + bankAccountList: createBankAccountList('8888'), }); render( @@ -906,30 +886,27 @@ describe('SettlementButton', () => { it('shows business bank label for invoice with business bank account and hasIntentToPay', async () => { const invoiceReport = createInvoiceReport(); - const chatReport = createChatReport({ - invoiceReceiver: { - type: CONST.REPORT.INVOICE_RECEIVER_TYPE.INDIVIDUAL, - accountID: 2, - }, - }); - const policy = createTestPolicy({ - achAccount: { - bankAccountID: BANK_ACCOUNT_ID, - accountNumber: '4444', - routingNumber: '123456789', - addressName: 'Test Business', - bankName: 'Test Bank', - reimburser: 'reimburser@test.com', - state: CONST.BANK_ACCOUNT.STATE.OPEN, - }, - }); - const bankAccountList = createBankAccountList('4444'); - await act(async () => { - await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, invoiceReport); - await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${CHAT_REPORT_ID}`, chatReport); - await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${POLICY_ID}`, policy); - await Onyx.merge(ONYXKEYS.BANK_ACCOUNT_LIST, bankAccountList); + await setupOnyxState({ + report: invoiceReport, + chatReport: createChatReport({ + invoiceReceiver: { + type: CONST.REPORT.INVOICE_RECEIVER_TYPE.INDIVIDUAL, + accountID: 2, + }, + }), + policy: createTestPolicy({ + achAccount: { + bankAccountID: BANK_ACCOUNT_ID, + accountNumber: '4444', + routingNumber: '123456789', + addressName: 'Test Business', + bankName: 'Test Bank', + reimburser: 'reimburser@test.com', + state: CONST.BANK_ACCOUNT.STATE.OPEN, + }, + }), + bankAccountList: createBankAccountList('4444'), }); render( @@ -948,30 +925,27 @@ describe('SettlementButton', () => { it('shows personal bank label for invoice with personal bank account and hasIntentToPay', async () => { const invoiceReport = createInvoiceReport(); - const chatReport = createChatReport({ - invoiceReceiver: { - type: CONST.REPORT.INVOICE_RECEIVER_TYPE.INDIVIDUAL, - accountID: 2, - }, - }); - const policy = createTestPolicy({ - achAccount: { - bankAccountID: BANK_ACCOUNT_ID, - accountNumber: '5555', - routingNumber: '123456789', - addressName: 'Test Business', - bankName: 'Test Bank', - reimburser: 'reimburser@test.com', - state: CONST.BANK_ACCOUNT.STATE.OPEN, - }, - }); - const bankAccountList = createPersonalBankAccount('5555'); - await act(async () => { - await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, invoiceReport); - await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${CHAT_REPORT_ID}`, chatReport); - await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${POLICY_ID}`, policy); - await Onyx.merge(ONYXKEYS.BANK_ACCOUNT_LIST, bankAccountList); + await setupOnyxState({ + report: invoiceReport, + chatReport: createChatReport({ + invoiceReceiver: { + type: CONST.REPORT.INVOICE_RECEIVER_TYPE.INDIVIDUAL, + accountID: 2, + }, + }), + policy: createTestPolicy({ + achAccount: { + bankAccountID: BANK_ACCOUNT_ID, + accountNumber: '5555', + routingNumber: '123456789', + addressName: 'Test Business', + bankName: 'Test Bank', + reimburser: 'reimburser@test.com', + state: CONST.BANK_ACCOUNT.STATE.OPEN, + }, + }), + bankAccountList: createPersonalBankAccount('5555'), }); render( @@ -994,10 +968,10 @@ describe('SettlementButton', () => { it('shows approve button when shouldHidePaymentOptions and shouldShowApproveButton are true', async () => { const expenseReport = createExpenseReport(); - await act(async () => { - await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, expenseReport); - await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${CHAT_REPORT_ID}`, createChatReport()); - await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${POLICY_ID}`, createTestPolicy()); + await setupOnyxState({ + report: expenseReport, + chatReport: createChatReport(), + policy: createTestPolicy(), }); render( @@ -1019,10 +993,10 @@ describe('SettlementButton', () => { it('does not show approve button when shouldShowApproveButton is false', async () => { const expenseReport = createExpenseReport(); - await act(async () => { - await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, expenseReport); - await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${CHAT_REPORT_ID}`, createChatReport()); - await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${POLICY_ID}`, createTestPolicy()); + await setupOnyxState({ + report: expenseReport, + chatReport: createChatReport(), + policy: createTestPolicy(), }); render( @@ -1043,10 +1017,10 @@ describe('SettlementButton', () => { it('does not show payment options when shouldHidePaymentOptions is true without approve button', async () => { const expenseReport = createExpenseReport(); - await act(async () => { - await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, expenseReport); - await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${CHAT_REPORT_ID}`, createChatReport()); - await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${POLICY_ID}`, createTestPolicy()); + await setupOnyxState({ + report: expenseReport, + chatReport: createChatReport(), + policy: createTestPolicy(), }); render( @@ -1068,9 +1042,9 @@ describe('SettlementButton', () => { it('shows pay elsewhere button when onlyShowPayElsewhere is true', async () => { const iouReport = createIOUReport(); - await act(async () => { - await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, iouReport); - await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${CHAT_REPORT_ID}`, createChatReport()); + await setupOnyxState({ + report: iouReport, + chatReport: createChatReport(), }); render( @@ -1090,20 +1064,19 @@ describe('SettlementButton', () => { it('shows no subtitle when shouldHidePaymentOptions and onlyShowPayElsewhere are true', async () => { const iouReport = createIOUReport(); - const bankAccountList = createPersonalBankAccount(); - await act(async () => { - await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, iouReport); - await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${CHAT_REPORT_ID}`, createChatReport()); - await Onyx.merge(ONYXKEYS.BANK_ACCOUNT_LIST, bankAccountList); - await Onyx.merge(ONYXKEYS.NVP_LAST_PAYMENT_METHOD, { + await setupOnyxState({ + report: iouReport, + chatReport: createChatReport(), + bankAccountList: createPersonalBankAccount(), + lastPaymentMethod: { [CONST.POLICY.ID_FAKE]: { lastUsed: {name: CONST.IOU.PAYMENT_TYPE.EXPENSIFY}, iou: {name: CONST.IOU.PAYMENT_TYPE.EXPENSIFY}, expense: {name: CONST.IOU.PAYMENT_TYPE.EXPENSIFY}, invoice: CONST.IOU.PAYMENT_TYPE.EXPENSIFY, }, - }); + }, }); render( @@ -1126,9 +1099,9 @@ describe('SettlementButton', () => { it('shows short form pay button when onlyShowPayElsewhere and shouldUseShortForm are true', async () => { const iouReport = createIOUReport(); - await act(async () => { - await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, iouReport); - await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${CHAT_REPORT_ID}`, createChatReport()); + await setupOnyxState({ + report: iouReport, + chatReport: createChatReport(), }); render(