|
| 1 | +import * as NativeNavigation from '@react-navigation/native'; |
| 2 | +import {fireEvent, render, screen} from '@testing-library/react-native'; |
| 3 | +import React, {act} from 'react'; |
| 4 | +import Onyx from 'react-native-onyx'; |
| 5 | +import ComposeProviders from '@components/ComposeProviders'; |
| 6 | +import {LocaleContextProvider} from '@components/LocaleContextProvider'; |
| 7 | +import OnyxListItemProvider from '@components/OnyxListItemProvider'; |
| 8 | +import ScreenWrapper from '@components/ScreenWrapper'; |
| 9 | +import {SearchContextProvider} from '@components/Search/SearchContext'; |
| 10 | +import TransactionGroupListItem from '@src/components/SelectionListWithSections/Search/TransactionGroupListItem'; |
| 11 | +import type {TransactionGroupListItemProps, TransactionListItemType, TransactionReportGroupListItemType} from '@src/components/SelectionListWithSections/types'; |
| 12 | +import CONST from '@src/CONST'; |
| 13 | +import ONYXKEYS from '@src/ONYXKEYS'; |
| 14 | +import waitForBatchedUpdatesWithAct from '../utils/waitForBatchedUpdatesWithAct'; |
| 15 | + |
| 16 | +jest.mock('@libs/actions/Search', () => ({ |
| 17 | + search: jest.fn(), |
| 18 | +})); |
| 19 | + |
| 20 | +jest.mock('@libs/SearchUIUtils', () => ({ |
| 21 | + getSections: jest.fn(() => []), |
| 22 | + isCorrectSearchUserName: jest.fn(() => true), |
| 23 | +})); |
| 24 | + |
| 25 | +const mockTransaction: TransactionListItemType = { |
| 26 | + accountID: 1, |
| 27 | + amount: 0, |
| 28 | + canDelete: true, |
| 29 | + canHold: true, |
| 30 | + canUnhold: false, |
| 31 | + category: '', |
| 32 | + convertedAmount: 1284, |
| 33 | + convertedCurrency: 'USD', |
| 34 | + created: '2025-09-19', |
| 35 | + currency: 'USD', |
| 36 | + managerID: 1, |
| 37 | + merchant: '(none)', |
| 38 | + modifiedAmount: -1284, |
| 39 | + modifiedCreated: '2025-09-07', |
| 40 | + modifiedCurrency: 'USD', |
| 41 | + modifiedMerchant: 'The Home Depot', |
| 42 | + policyID: '06F34677820A4D07', |
| 43 | + reportID: '515146912679679', |
| 44 | + reportType: 'expense', |
| 45 | + tag: '', |
| 46 | + transactionID: '1', |
| 47 | + transactionThreadReportID: '2925191332104975', |
| 48 | + transactionType: 'cash', |
| 49 | + action: 'approve', |
| 50 | + allActions: ['approve'], |
| 51 | + formattedFrom: 'Main Applause QA', |
| 52 | + formattedTo: 'Main Applause QA', |
| 53 | + formattedTotal: -1284, |
| 54 | + formattedMerchant: 'The Home Depot', |
| 55 | + date: '2025-09-07', |
| 56 | + shouldShowMerchant: true, |
| 57 | + shouldShowYear: true, |
| 58 | + keyForList: '1', |
| 59 | + isAmountColumnWide: false, |
| 60 | + isTaxAmountColumnWide: false, |
| 61 | + shouldAnimateInHighlight: false, |
| 62 | + report: { |
| 63 | + reportID: '515146912679679', |
| 64 | + }, |
| 65 | + from: { |
| 66 | + accountID: 1, |
| 67 | + avatar: 'https://d2k5nsl2zxldvw.cloudfront.net/images/avatars/default-avatar_15.png', |
| 68 | + displayName: 'Main Applause QA', |
| 69 | + }, |
| 70 | + to: { |
| 71 | + accountID: 1, |
| 72 | + avatar: 'https://d2k5nsl2zxldvw.cloudfront.net/images/avatars/default-avatar_15.png', |
| 73 | + displayName: 'Main Applause QA', |
| 74 | + }, |
| 75 | +}; |
| 76 | + |
| 77 | +const mockReport: TransactionReportGroupListItemType = { |
| 78 | + accountID: 1, |
| 79 | + chatReportID: '4735435600700077', |
| 80 | + chatType: undefined, |
| 81 | + created: '2025-09-19 20:00:47', |
| 82 | + currency: 'USD', |
| 83 | + isOneTransactionReport: true, |
| 84 | + isOwnPolicyExpenseChat: false, |
| 85 | + isPolicyExpenseChat: false, |
| 86 | + isWaitingOnBankAccount: false, |
| 87 | + managerID: 1, |
| 88 | + nonReimbursableTotal: 0, |
| 89 | + oldPolicyName: '', |
| 90 | + ownerAccountID: 1, |
| 91 | + parentReportActionID: '2454187434077044186', |
| 92 | + parentReportID: '4735435600700077', |
| 93 | + policyID: '06F34677820A4D07', |
| 94 | + private_isArchived: '', |
| 95 | + reportID: '515146912679679', |
| 96 | + reportName: 'Expense Report #515146912679679', |
| 97 | + stateNum: 1, |
| 98 | + statusNum: 1, |
| 99 | + total: -1284, |
| 100 | + type: 'expense', |
| 101 | + unheldTotal: -1284, |
| 102 | + from: { |
| 103 | + accountID: 1, |
| 104 | + avatar: 'https://d2k5nsl2zxldvw.cloudfront.net/images/avatars/default-avatar_15.png', |
| 105 | + displayName: 'Main Applause QA', |
| 106 | + }, |
| 107 | + to: { |
| 108 | + accountID: 1, |
| 109 | + avatar: 'https://d2k5nsl2zxldvw.cloudfront.net/images/avatars/default-avatar_15.png', |
| 110 | + displayName: 'Main Applause QA', |
| 111 | + }, |
| 112 | + transactions: [], |
| 113 | + groupedBy: 'reports', |
| 114 | + keyForList: '515146912679679', |
| 115 | +}; |
| 116 | + |
| 117 | +const createFakeTransactions = (numberOfTransactions: number): TransactionListItemType[] => { |
| 118 | + return Array.from({length: numberOfTransactions}, (_, index) => ({ |
| 119 | + ...mockTransaction, |
| 120 | + transactionID: index.toString(), |
| 121 | + })); |
| 122 | +}; |
| 123 | + |
| 124 | +const createFakeReport = (numberOfTransactions: number): TransactionReportGroupListItemType => { |
| 125 | + return { |
| 126 | + ...mockReport, |
| 127 | + transactions: createFakeTransactions(numberOfTransactions), |
| 128 | + }; |
| 129 | +}; |
| 130 | + |
| 131 | +describe('TransactionGroupListItem', () => { |
| 132 | + beforeAll(() => { |
| 133 | + Onyx.init({ |
| 134 | + keys: ONYXKEYS, |
| 135 | + evictableKeys: [ONYXKEYS.COLLECTION.REPORT_ACTIONS], |
| 136 | + }); |
| 137 | + jest.spyOn(NativeNavigation, 'useRoute').mockReturnValue({key: '', name: ''}); |
| 138 | + }); |
| 139 | + |
| 140 | + beforeEach(() => { |
| 141 | + jest.clearAllMocks(); |
| 142 | + return act(async () => { |
| 143 | + await Onyx.clear(); |
| 144 | + await waitForBatchedUpdatesWithAct(); |
| 145 | + }); |
| 146 | + }); |
| 147 | + |
| 148 | + const mockOnSelectRow = jest.fn(); |
| 149 | + const numberOfTransactions = 21; |
| 150 | + const report = createFakeReport(numberOfTransactions); |
| 151 | + |
| 152 | + const defaultProps: TransactionGroupListItemProps<TransactionReportGroupListItemType> = { |
| 153 | + item: report, |
| 154 | + showTooltip: false, |
| 155 | + onSelectRow: mockOnSelectRow, |
| 156 | + groupBy: CONST.SEARCH.GROUP_BY.REPORTS, |
| 157 | + canSelectMultiple: true, |
| 158 | + }; |
| 159 | + |
| 160 | + function TestWrapper({children}: {children: React.ReactNode}) { |
| 161 | + return ( |
| 162 | + <ComposeProviders components={[OnyxListItemProvider, LocaleContextProvider]}> |
| 163 | + <ScreenWrapper testID="test"> |
| 164 | + <SearchContextProvider>{children}</SearchContextProvider> |
| 165 | + </ScreenWrapper> |
| 166 | + </ComposeProviders> |
| 167 | + ); |
| 168 | + } |
| 169 | + |
| 170 | + const renderTransactionGroupListItem = () => { |
| 171 | + return render( |
| 172 | + <TransactionGroupListItem |
| 173 | + // eslint-disable-next-line react/jsx-props-no-spreading |
| 174 | + {...defaultProps} |
| 175 | + />, |
| 176 | + {wrapper: TestWrapper}, |
| 177 | + ); |
| 178 | + }; |
| 179 | + |
| 180 | + const expand = async () => { |
| 181 | + const expandButton = screen.getByLabelText('Expand'); |
| 182 | + fireEvent.press(expandButton); |
| 183 | + await waitForBatchedUpdatesWithAct(); |
| 184 | + }; |
| 185 | + |
| 186 | + const collapse = async () => { |
| 187 | + const collapseButton = screen.getByLabelText('Collapse'); |
| 188 | + fireEvent.press(collapseButton); |
| 189 | + await waitForBatchedUpdatesWithAct(); |
| 190 | + }; |
| 191 | + |
| 192 | + const showMore = async () => { |
| 193 | + const showMoreButton = screen.getByText('Show more'); |
| 194 | + fireEvent.press(showMoreButton); |
| 195 | + await waitForBatchedUpdatesWithAct(); |
| 196 | + }; |
| 197 | + |
| 198 | + const getVisibleTransactionRowsCount = () => screen.getAllByTestId('transaction-item-row').length; |
| 199 | + |
| 200 | + it('should render TransactionGroupListItem with groupBy reports', async () => { |
| 201 | + renderTransactionGroupListItem(); |
| 202 | + await waitForBatchedUpdatesWithAct(); |
| 203 | + |
| 204 | + expect(screen.getByRole(CONST.ROLE.CHECKBOX)).toBeTruthy(); |
| 205 | + expect(screen.getByRole(CONST.ROLE.CHECKBOX)).not.toBeChecked(); |
| 206 | + expect(screen.getByTestId('ReportSearchHeader')).toBeTruthy(); |
| 207 | + expect(screen.getByTestId('TotalCell')).toBeTruthy(); |
| 208 | + expect(screen.getByTestId('ActionCell')).toBeTruthy(); |
| 209 | + expect(screen.getByLabelText('Expand')).toBeTruthy(); |
| 210 | + expect(screen.queryByTestId(CONST.ANIMATED_COLLAPSIBLE_CONTENT_TEST_ID)).toBeNull(); |
| 211 | + }); |
| 212 | + |
| 213 | + it(`should toggle expansion state with ${CONST.TRANSACTION.RESULTS_PAGE_SIZE} items when Expand is triggered`, async () => { |
| 214 | + renderTransactionGroupListItem(); |
| 215 | + await waitForBatchedUpdatesWithAct(); |
| 216 | + await expand(); |
| 217 | + |
| 218 | + expect(screen.getByLabelText('Collapse')).toBeTruthy(); |
| 219 | + expect(screen.getByTestId(CONST.ANIMATED_COLLAPSIBLE_CONTENT_TEST_ID)).toBeTruthy(); |
| 220 | + |
| 221 | + expect(getVisibleTransactionRowsCount()).toBe(CONST.TRANSACTION.RESULTS_PAGE_SIZE); |
| 222 | + }); |
| 223 | + |
| 224 | + it('should show more transactions and hide button when show more button is triggered and limit of transactions is reached', async () => { |
| 225 | + renderTransactionGroupListItem(); |
| 226 | + await waitForBatchedUpdatesWithAct(); |
| 227 | + await expand(); |
| 228 | + await showMore(); |
| 229 | + |
| 230 | + expect(getVisibleTransactionRowsCount()).toBe(numberOfTransactions); |
| 231 | + |
| 232 | + const showMoreButtonSecond = screen.queryByText('Show more'); |
| 233 | + expect(showMoreButtonSecond).toBeNull(); |
| 234 | + }); |
| 235 | + |
| 236 | + it('should collapse the list when Collapse is triggered', async () => { |
| 237 | + renderTransactionGroupListItem(); |
| 238 | + await waitForBatchedUpdatesWithAct(); |
| 239 | + await expand(); |
| 240 | + await collapse(); |
| 241 | + |
| 242 | + expect(screen.getByLabelText('Expand')).toBeTruthy(); |
| 243 | + }); |
| 244 | + |
| 245 | + it(`should show only ${CONST.TRANSACTION.RESULTS_PAGE_SIZE} transactions when collapsed and expanded again`, async () => { |
| 246 | + renderTransactionGroupListItem(); |
| 247 | + await waitForBatchedUpdatesWithAct(); |
| 248 | + await expand(); |
| 249 | + await showMore(); |
| 250 | + await collapse(); |
| 251 | + await expand(); |
| 252 | + |
| 253 | + expect(getVisibleTransactionRowsCount()).toBe(CONST.TRANSACTION.RESULTS_PAGE_SIZE); |
| 254 | + expect(screen.getByText('Show more')).toBeTruthy(); |
| 255 | + }); |
| 256 | +}); |
0 commit comments