|
| 1 | +import {act, render, screen, waitFor} from '@testing-library/react-native'; |
| 2 | + |
| 3 | +import ComposeProviders from '@components/ComposeProviders'; |
| 4 | +import OnyxListItemProvider from '@components/OnyxListItemProvider'; |
| 5 | +import TransactionPreviewContent from '@components/ReportActionItem/TransactionPreview/TransactionPreviewContent'; |
| 6 | + |
| 7 | +import CONST from '@src/CONST'; |
| 8 | +import ONYXKEYS from '@src/ONYXKEYS'; |
| 9 | +import type {PersonalDetailsList, Policy, Report, ReportAction, Transaction, TransactionViolation} from '@src/types/onyx'; |
| 10 | + |
| 11 | +import React from 'react'; |
| 12 | +import Onyx from 'react-native-onyx'; |
| 13 | + |
| 14 | +import * as LHNTestUtils from '../utils/LHNTestUtils'; |
| 15 | +import * as TestHelper from '../utils/TestHelper'; |
| 16 | +import waitForBatchedUpdatesWithAct from '../utils/waitForBatchedUpdatesWithAct'; |
| 17 | + |
| 18 | +TestHelper.setupGlobalFetchMock(); |
| 19 | + |
| 20 | +jest.mock('@hooks/useScreenWrapperTransitionStatus', () => ({ |
| 21 | + __esModule: true, |
| 22 | + default: () => ({ |
| 23 | + didScreenTransitionEnd: true, |
| 24 | + }), |
| 25 | +})); |
| 26 | + |
| 27 | +// Expose the canEdit translation parameter in the rendered output so canEdit-dependent messages can be asserted. |
| 28 | +// The message must stay under CONST.REPORT_VIOLATIONS.RBR_MESSAGE_MAX_CHARACTERS_FOR_PREVIEW or the preview |
| 29 | +// swaps it for the generic "review required" message. |
| 30 | +jest.mock('@hooks/useLocalize', () => |
| 31 | + jest.fn(() => ({ |
| 32 | + translate: jest.fn((key: string, params?: Record<string, unknown>) => (params && 'canEdit' in params ? `smartscan#${String(params.canEdit)}` : key)), |
| 33 | + numberFormat: jest.fn((num: number) => num.toString()), |
| 34 | + toLocaleDigit: jest.fn((digit: string) => digit), |
| 35 | + localeCompare: jest.fn((a: string, b: string) => a.localeCompare(b)), |
| 36 | + })), |
| 37 | +); |
| 38 | + |
| 39 | +const currentUserAccountID = 20; |
| 40 | +const currentUserEmail = 'submitter@test.com'; |
| 41 | +const approverAccountID = 21; |
| 42 | +const approverEmail = 'approver@test.com'; |
| 43 | +const policyID = 'policy_tpc_test'; |
| 44 | +const expenseReportID = 'expense_tpc_123'; |
| 45 | +const transactionID = 'txn_tpc_test'; |
| 46 | + |
| 47 | +const corporatePolicy = { |
| 48 | + id: policyID, |
| 49 | + type: CONST.POLICY.TYPE.CORPORATE, |
| 50 | + role: CONST.POLICY.ROLE.USER, |
| 51 | + name: 'Corporate Policy', |
| 52 | + owner: '', |
| 53 | + outputCurrency: CONST.CURRENCY.USD, |
| 54 | + isPolicyExpenseChatEnabled: true, |
| 55 | + employeeList: { |
| 56 | + [currentUserEmail]: { |
| 57 | + email: currentUserEmail, |
| 58 | + role: CONST.POLICY.ROLE.USER, |
| 59 | + submitsTo: approverEmail, |
| 60 | + }, |
| 61 | + }, |
| 62 | +} as Policy; |
| 63 | + |
| 64 | +const expenseReport = { |
| 65 | + reportID: expenseReportID, |
| 66 | + type: CONST.REPORT.TYPE.EXPENSE, |
| 67 | + policyID, |
| 68 | + ownerAccountID: currentUserAccountID, |
| 69 | + managerID: approverAccountID, |
| 70 | + stateNum: CONST.REPORT.STATE_NUM.SUBMITTED, |
| 71 | + statusNum: CONST.REPORT.STATUS_NUM.SUBMITTED, |
| 72 | +} as Report; |
| 73 | + |
| 74 | +const moneyRequestAction = { |
| 75 | + ...LHNTestUtils.getFakeReportAction(), |
| 76 | + reportActionID: 'tpc_iou_action', |
| 77 | + reportID: expenseReportID, |
| 78 | + actionName: CONST.REPORT.ACTIONS.TYPE.IOU, |
| 79 | + actorAccountID: currentUserAccountID, |
| 80 | + originalMessage: { |
| 81 | + type: CONST.IOU.REPORT_ACTION_TYPE.CREATE, |
| 82 | + IOUTransactionID: transactionID, |
| 83 | + amount: 5000, |
| 84 | + currency: CONST.CURRENCY.USD, |
| 85 | + }, |
| 86 | +} as ReportAction; |
| 87 | + |
| 88 | +const transaction = { |
| 89 | + transactionID, |
| 90 | + reportID: expenseReportID, |
| 91 | + amount: 5000, |
| 92 | + currency: CONST.CURRENCY.USD, |
| 93 | + created: '2026-04-20', |
| 94 | + merchant: 'Coffee Shop', |
| 95 | + comment: {}, |
| 96 | +} as Transaction; |
| 97 | + |
| 98 | +const smartscanFailedViolation: TransactionViolation = { |
| 99 | + name: CONST.VIOLATIONS.SMARTSCAN_FAILED, |
| 100 | + type: CONST.VIOLATION_TYPES.VIOLATION, |
| 101 | + showInReview: true, |
| 102 | +}; |
| 103 | + |
| 104 | +const personalDetails: PersonalDetailsList = { |
| 105 | + [currentUserAccountID]: {accountID: currentUserAccountID, login: currentUserEmail, displayName: 'Submitter'}, |
| 106 | + [approverAccountID]: {accountID: approverAccountID, login: approverEmail, displayName: 'Approver'}, |
| 107 | +}; |
| 108 | + |
| 109 | +const renderTransactionPreviewContent = () => |
| 110 | + render( |
| 111 | + <ComposeProviders components={[OnyxListItemProvider]}> |
| 112 | + <TransactionPreviewContent |
| 113 | + action={moneyRequestAction} |
| 114 | + isWhisper={false} |
| 115 | + isHovered={false} |
| 116 | + chatReport={undefined} |
| 117 | + personalDetails={personalDetails} |
| 118 | + report={expenseReport} |
| 119 | + policy={corporatePolicy} |
| 120 | + transaction={transaction} |
| 121 | + violations={[smartscanFailedViolation]} |
| 122 | + transactionRawAmount={5000} |
| 123 | + offlineWithFeedbackOnClose={() => {}} |
| 124 | + containerStyles={[]} |
| 125 | + transactionPreviewWidth={303} |
| 126 | + isBillSplit={false} |
| 127 | + areThereDuplicates={false} |
| 128 | + sessionAccountID={currentUserAccountID} |
| 129 | + walletTermsErrors={undefined} |
| 130 | + reportPreviewAction={undefined} |
| 131 | + navigateToReviewFields={() => {}} |
| 132 | + routeName="Report" |
| 133 | + /> |
| 134 | + </ComposeProviders>, |
| 135 | + ); |
| 136 | + |
| 137 | +describe('TransactionPreviewContent', () => { |
| 138 | + beforeAll(() => { |
| 139 | + Onyx.init({ |
| 140 | + keys: ONYXKEYS, |
| 141 | + evictableKeys: [ONYXKEYS.COLLECTION.REPORT_ACTIONS], |
| 142 | + }); |
| 143 | + }); |
| 144 | + |
| 145 | + afterEach(async () => { |
| 146 | + await act(async () => { |
| 147 | + await Onyx.clear(); |
| 148 | + }); |
| 149 | + }); |
| 150 | + |
| 151 | + const seedOnyx = async (reportActions: Record<string, ReportAction>) => { |
| 152 | + await act(async () => { |
| 153 | + await Onyx.merge(ONYXKEYS.SESSION, {accountID: currentUserAccountID, email: currentUserEmail}); |
| 154 | + await Onyx.merge(ONYXKEYS.PERSONAL_DETAILS_LIST, personalDetails); |
| 155 | + await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`, corporatePolicy); |
| 156 | + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${expenseReportID}`, expenseReport); |
| 157 | + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${expenseReportID}`, reportActions); |
| 158 | + await Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`, transaction); |
| 159 | + }); |
| 160 | + await waitForBatchedUpdatesWithAct(); |
| 161 | + }; |
| 162 | + |
| 163 | + it('shows the editable smartscan-failed message for the submitter when the report has not been forwarded', async () => { |
| 164 | + await seedOnyx({ |
| 165 | + [moneyRequestAction.reportActionID]: moneyRequestAction, |
| 166 | + submitted: {...LHNTestUtils.getFakeReportAction(), reportActionID: 'submitted', actionName: CONST.REPORT.ACTIONS.TYPE.SUBMITTED, created: '2026-04-21 17:00:00'}, |
| 167 | + }); |
| 168 | + |
| 169 | + renderTransactionPreviewContent(); |
| 170 | + await waitForBatchedUpdatesWithAct(); |
| 171 | + |
| 172 | + await waitFor(() => { |
| 173 | + expect(screen.getByText('smartscan#true')).toBeOnTheScreen(); |
| 174 | + }); |
| 175 | + }); |
| 176 | + |
| 177 | + it('shows the read-only smartscan-failed message for the submitter after the report was forwarded since the last submit', async () => { |
| 178 | + await seedOnyx({ |
| 179 | + [moneyRequestAction.reportActionID]: moneyRequestAction, |
| 180 | + submitted: {...LHNTestUtils.getFakeReportAction(), reportActionID: 'submitted', actionName: CONST.REPORT.ACTIONS.TYPE.SUBMITTED, created: '2026-04-21 17:00:00'}, |
| 181 | + forwarded: {...LHNTestUtils.getFakeReportAction(), reportActionID: 'forwarded', actionName: CONST.REPORT.ACTIONS.TYPE.FORWARDED, created: '2026-04-21 17:10:00'}, |
| 182 | + }); |
| 183 | + |
| 184 | + renderTransactionPreviewContent(); |
| 185 | + await waitForBatchedUpdatesWithAct(); |
| 186 | + |
| 187 | + await waitFor(() => { |
| 188 | + expect(screen.getByText('smartscan#false')).toBeOnTheScreen(); |
| 189 | + }); |
| 190 | + }); |
| 191 | +}); |
0 commit comments