diff --git a/src/hooks/useSearchBulkActions.ts b/src/hooks/useSearchBulkActions.ts index b773dec8561e..4292c0c07f32 100644 --- a/src/hooks/useSearchBulkActions.ts +++ b/src/hooks/useSearchBulkActions.ts @@ -65,6 +65,7 @@ import { shouldShowMarkAsDone, } from '@libs/ReportUtils'; import {buildSearchQueryJSON, buildSearchQueryString, getFilterFromQuery, isDefaultExpensesQuery, serializeQueryJSONForBackend} from '@libs/SearchQueryUtils'; +import refreshSearchAfterReportAction from '@libs/SearchRefreshUtils'; import {getColumnsToShow, getSearchColumnTranslationKey, getSelectedGroupFilterEntry, getValidGroupBy, isGroupEntry, navigateToSearchRHP, shouldShowDeleteOption} from '@libs/SearchUIUtils'; import showConfirmModalAfterMoreMenuDismiss from '@libs/showConfirmModalAfterMoreMenuDismiss'; import playSound, {SOUNDS} from '@libs/Sound'; @@ -129,6 +130,7 @@ import usePermissions from './usePermissions'; import usePersonalPolicy from './usePersonalPolicy'; import usePolicyForMovingExpenses from './usePolicyForMovingExpenses'; import useRestrictedActionPolicyID from './useRestrictedActionPolicyID'; +import useSearchShouldCalculateTotals from './useSearchShouldCalculateTotals'; import useSelfDMReport from './useSelfDMReport'; import useSplitEffectivePolicy from './useSplitEffectivePolicy'; import useTheme from './useTheme'; @@ -424,7 +426,7 @@ function useSearchBulkActions({queryJSON}: UseSearchBulkActionsParams) { const {showDelegateNoAccessModal} = useDelegateNoAccessActions(); const {selectedTransactions, excludedTransactions = getEmptyObject(), selectedReports, areAllMatchingItemsSelected} = useSearchSelectionContext(); const {currentSearchResults} = useSearchResultsContext(); - const {currentSearchKey} = useSearchQueryContext(); + const {currentSearchKey, currentSearchQueryJSON} = useSearchQueryContext(); const {clearSelectedTransactions, selectAllMatchingItems} = useSearchSelectionActions(); const currentUserPersonalDetails = useCurrentUserPersonalDetails(); const {accountID, email, login: currentUserLogin, localCurrencyCode} = currentUserPersonalDetails; @@ -660,6 +662,7 @@ function useSearchBulkActions({queryJSON}: UseSearchBulkActionsParams) { }); const {hash} = queryJSON ?? {}; + const shouldCalculateTotalsOnRefresh = useSearchShouldCalculateTotals(currentSearchKey, hash, true); const isExpenseType = queryJSON?.type === CONST.SEARCH.DATA_TYPES.EXPENSE; const selectedTransactionsKeys = Object.keys(selectedTransactions ?? {}); // Use currentSearchResults, not the lastNonEmpty fallback: the export scope must reflect the query on screen now, @@ -2132,6 +2135,13 @@ function useSearchBulkActions({queryJSON}: UseSearchBulkActionsParams) { managerEmail, managerAccountID, ); + refreshSearchAfterReportAction({ + currentSearchQueryJSON, + currentSearchKey, + shouldCalculateTotals: shouldCalculateTotalsOnRefresh, + isOffline, + isLoading: !!currentSearchResults?.search?.isLoading, + }); clearSelectedTransactions(); }, }); @@ -2145,6 +2155,16 @@ function useSearchBulkActions({queryJSON}: UseSearchBulkActionsParams) { submitMoneyRequestOnSearch(hash, [item as Report], [policy], getLoginByAccountID(item.ownerAccountID, personalDetails)); } } + // Submitting only changes the report, so the rows keep serving the snapshot's pre-submit report + // context (which still offers Submit) until the snapshot is refetched, the same way approving and + // paying from Search already do. + refreshSearchAfterReportAction({ + currentSearchQueryJSON, + currentSearchKey, + shouldCalculateTotals: shouldCalculateTotalsOnRefresh, + isOffline, + isLoading: !!currentSearchResults?.search?.isLoading, + }); clearSelectedTransactions(); }, }); @@ -2544,6 +2564,9 @@ function useSearchBulkActions({queryJSON}: UseSearchBulkActionsParams) { noReportsShouldMarkAsDone, queryJSON?.groupBy, delegateAccountID, + currentSearchQueryJSON, + currentSearchResults?.search?.isLoading, + shouldCalculateTotalsOnRefresh, ]); const handleOfflineModalClose = useCallback(() => { diff --git a/src/hooks/useSearchHighlightAndScroll.ts b/src/hooks/useSearchHighlightAndScroll.ts index 0f1dd3cbe3b0..8c62c0eb3fdd 100644 --- a/src/hooks/useSearchHighlightAndScroll.ts +++ b/src/hooks/useSearchHighlightAndScroll.ts @@ -85,26 +85,30 @@ function useSearchHighlightAndScroll({ const previousTransactionIDsLocal = Object.keys(previousTransactions ?? {}); const transactionsIDs = Object.keys(transactions ?? {}); - const reportActionsIDs = Object.values(reportActions ?? {}) - .map((actions) => Object.keys(actions ?? {})) - .flat(); - const previousReportActionsIDs = Object.values(previousReportActions ?? {}) - .map((actions) => Object.keys(actions ?? {})) - .flat(); - // Only proceed if we have previous data to compare against // This prevents triggering on initial data load - if ((previousTransactionIDsLocal.length === 0 && previousReportActionsIDs.length === 0) || searchTriggeredRef.current) { + const hasPreviousReportActions = Object.values(previousReportActions ?? {}).some((actions) => Object.keys(actions ?? {}).length > 0); + if ((previousTransactionIDsLocal.length === 0 && !hasPreviousReportActions) || searchTriggeredRef.current) { return; } + // Only chat searches are driven by report actions, so the rest skip walking that collection entirely. + const reportActionsIDs = isChat ? Object.values(reportActions ?? {}).flatMap((actions) => Object.keys(actions ?? {})) : []; + const previousReportActionsIDs = isChat ? Object.values(previousReportActions ?? {}).flatMap((actions) => Object.keys(actions ?? {})) : []; + const previousTransactionsIDsSet = new Set(previousTransactionIDsLocal); const previousReportActionsIDsSet = new Set(previousReportActionsIDs); const hasTransactionsIDsChange = transactionsIDs.length !== previousTransactionIDsLocal.length || transactionsIDs.some((id) => !previousTransactionsIDsSet.has(id)); const hasReportActionsIDsChange = reportActionsIDs.some((id) => !previousReportActionsIDsSet.has(id)); + // Editing an expense that the results already show changes no transaction ID, so the ID checks above miss it. + // The rows and the footer total are served from the Search snapshot, and the footer total is computed by the + // server, so those edits only become visible after a refetch. + const hasChangedResultTransaction = + !isChat && !hasTransactionsIDsChange && hasChangedTransactionInSearchResults(transactions, previousTransactions, previousTransactionsIDsSet, searchResultsData); + // Check if there is a change in the transactions or report actions list - if ((!isChat && hasTransactionsIDsChange) || hasReportActionsIDsChange || hasPendingSearchRef.current) { + if ((isChat ? hasReportActionsIDsChange : hasTransactionsIDsChange || hasChangedResultTransaction) || hasPendingSearchRef.current) { // Skip if offline, or if the user has navigated to a different fullscreen page entirely. // An RHP layered on top of Search makes `isFocused` false but keeps Search as the topmost // fullscreen route, so we still want to refetch — otherwise the snapshot can't reflect @@ -114,21 +118,37 @@ function useSearchHighlightAndScroll({ hasPendingSearchRef.current = true; return; } + // A deferred refetch is its own reason to search. `usePrevious` advances while Search is inactive, so by + // the time it becomes active again the change that set the flag has washed out of the comparisons below. + const hadPendingSearch = hasPendingSearchRef.current; hasPendingSearchRef.current = false; - const newIDs = isChat ? reportActionsIDs : transactionsIDs; + // Transaction Onyx keys are `transactions_` but search results yield bare IDs, so read the ID off the value. + const addedTransactionIDs: string[] = []; + const currentTransactionIDs: string[] = []; + for (const [key, transaction] of Object.entries(transactions ?? {})) { + const transactionID = transaction?.transactionID; + if (!transactionID) { + continue; + } + currentTransactionIDs.push(transactionID); + if (!previousTransactionsIDsSet.has(key)) { + addedTransactionIDs.push(transactionID); + } + } + let currentSearchResultIDs: string[] = []; if (searchResultsData) { currentSearchResultIDs = isChat ? extractReportActionIDsFromSearchResults(searchResultsData) : extractTransactionIDsFromSearchResults(searchResultsData); } const existingSearchResultIDsSet = new Set(currentSearchResultIDs); - const hasAGenuinelyNewID = newIDs.some((id) => !existingSearchResultIDsSet.has(id)); + const hasAGenuinelyNewID = (isChat ? reportActionsIDs : addedTransactionIDs).some((id) => !existingSearchResultIDsSet.has(id)); // Only skip search if there are no new items AND search results aren't empty // This ensures deletions that result in empty data still trigger search - if (!hasAGenuinelyNewID && currentSearchResultIDs.length > 0) { - const newIDsSet = new Set(newIDs); - const hasDeletedID = currentSearchResultIDs.some((id) => !newIDsSet.has(id)); + if (!hasAGenuinelyNewID && !hasChangedResultTransaction && !hadPendingSearch && currentSearchResultIDs.length > 0) { + const currentIDsSet = new Set(isChat ? reportActionsIDs : currentTransactionIDs); + const hasDeletedID = currentSearchResultIDs.some((id) => !currentIDsSet.has(id)); if (!hasDeletedID) { return; } @@ -362,5 +382,39 @@ function extractReportActionIDsFromSearchResults(searchResultsData: Partial, + previousTransactions: OnyxCollection, + previousTransactionKeys: Set, + searchResultsData: Partial | undefined, +): boolean { + if (!searchResultsData) { + return false; + } + + const changedTransactionIDs: string[] = []; + for (const [key, transaction] of Object.entries(transactions ?? {})) { + if (!transaction?.transactionID || !previousTransactionKeys.has(key) || previousTransactions?.[key] === transaction) { + continue; + } + changedTransactionIDs.push(transaction.transactionID); + } + + // Walking the results is the expensive half, so only do it once something actually changed. + if (changedTransactionIDs.length === 0) { + return false; + } + + const searchResultIDs = new Set(extractTransactionIDsFromSearchResults(searchResultsData)); + return changedTransactionIDs.some((transactionID) => searchResultIDs.has(transactionID)); +} + export default useSearchHighlightAndScroll; export type {UseSearchHighlightAndScroll}; diff --git a/tests/unit/useSearchHighlightAndScrollTest.ts b/tests/unit/useSearchHighlightAndScrollTest.ts index 79fee85c4747..9ece9df78e55 100644 --- a/tests/unit/useSearchHighlightAndScrollTest.ts +++ b/tests/unit/useSearchHighlightAndScrollTest.ts @@ -1,15 +1,20 @@ /* eslint-disable @typescript-eslint/naming-convention */ import {renderHook} from '@testing-library/react-native'; +import type {SearchListItem} from '@components/Search/SearchList/ListItem/types'; + import useSearchHighlightAndScroll from '@hooks/useSearchHighlightAndScroll'; import type {UseSearchHighlightAndScroll} from '@hooks/useSearchHighlightAndScroll'; import {search} from '@libs/actions/Search'; import ONYXKEYS from '@src/ONYXKEYS'; +import type {Transaction} from '@src/types/onyx'; import Onyx from 'react-native-onyx'; +import createMock from '../utils/createMock'; + jest.mock('@libs/actions/Search'); jest.mock('@react-navigation/native', () => ({ useIsFocused: jest.fn(() => true), @@ -22,10 +27,14 @@ jest.mock('@rnmapbox/maps', () => ({ setAccessToken: jest.fn(), })); +let mockIsOffline = false; +jest.mock('@hooks/useNetwork', () => jest.fn(() => ({isOffline: mockIsOffline}))); + const mockUseIsFocused = jest.fn().mockReturnValue(true); afterEach(() => { jest.clearAllMocks(); + mockIsOffline = false; }); describe('useSearchHighlightAndScroll', () => { @@ -79,29 +88,25 @@ describe('useSearchHighlightAndScroll', () => { }); it('should trigger search when new transaction added and focused', () => { - const initialProps = { + const initialProps = createMock({ ...baseProps, - transactions: {'1': {transactionID: '1'}}, - previousTransactions: {'1': {transactionID: '1'}}, - }; + transactions: {transactions_1: {transactionID: '1'}}, + previousTransactions: {transactions_1: {transactionID: '1'}}, + }); const {rerender} = renderHook((props: UseSearchHighlightAndScroll) => useSearchHighlightAndScroll(props), { - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-expect-error initialProps, }); - const updatedProps = { + const updatedProps = createMock({ ...baseProps, transactions: { - '1': {transactionID: '1'}, - '2': {transactionID: '2'}, + transactions_1: {transactionID: '1'}, + transactions_2: {transactionID: '2'}, }, - previousTransactions: {'1': {transactionID: '1'}}, - }; + previousTransactions: {transactions_1: {transactionID: '1'}}, + }); - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-expect-error rerender(updatedProps); expect(search).toHaveBeenCalledWith({queryJSON: baseProps.queryJSON, searchKey: undefined, offset: 0, shouldCalculateTotals: false, isLoading: false}); }); @@ -113,13 +118,11 @@ describe('useSearchHighlightAndScroll', () => { initialProps: baseProps, }); - const updatedProps = { + const updatedProps = createMock({ ...baseProps, - transactions: {'1': {transactionID: '1'}}, - }; + transactions: {transactions_1: {transactionID: '1'}}, + }); - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-expect-error rerender(updatedProps); expect(search).not.toHaveBeenCalled(); }); @@ -127,71 +130,314 @@ describe('useSearchHighlightAndScroll', () => { it('should trigger search for chat when report actions added and focused', () => { mockUseIsFocused.mockReturnValue(true); - const chatProps = { + const chatProps = createMock({ ...baseProps, queryJSON: {...baseProps.queryJSON, type: 'chat' as const}, reportActions: { reportActions_1: { - '1': {actionName: 'EXISTING', reportActionID: '1'}, + '1': {actionName: 'CREATED', reportActionID: '1'}, }, }, previousReportActions: { reportActions_1: { - '1': {actionName: 'EXISTING', reportActionID: '1'}, + '1': {actionName: 'CREATED', reportActionID: '1'}, }, }, - }; + }); const {rerender} = renderHook((props: UseSearchHighlightAndScroll) => useSearchHighlightAndScroll(props), { - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-expect-error initialProps: chatProps, }); - const updatedProps = { + const updatedProps = createMock({ ...chatProps, reportActions: { reportActions_1: { - '1': {actionName: 'EXISTING', reportActionID: '1'}, + '1': {actionName: 'CREATED', reportActionID: '1'}, '2': {actionName: 'ADDCOMMENT', reportActionID: '2'}, }, }, - }; + }); - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-expect-error rerender(updatedProps); expect(search).toHaveBeenCalledWith({queryJSON: chatProps.queryJSON, searchKey: undefined, offset: 0, shouldCalculateTotals: false, isLoading: false}); }); it('should not trigger search when new transaction removed and focused', () => { - const initialProps = { + const initialProps = createMock({ ...baseProps, transactions: { - '1': {transactionID: '1'}, - '2': {transactionID: '2'}, + transactions_1: {transactionID: '1'}, + transactions_2: {transactionID: '2'}, }, previousTransactions: { - '1': {transactionID: '1'}, - '2': {transactionID: '2'}, + transactions_1: {transactionID: '1'}, + transactions_2: {transactionID: '2'}, }, - }; + }); const {rerender} = renderHook((props: UseSearchHighlightAndScroll) => useSearchHighlightAndScroll(props), { - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-expect-error initialProps, }); - const updatedProps = { + const updatedProps = createMock({ ...baseProps, transactions: { - '1': {transactionID: '1'}, + transactions_1: {transactionID: '1'}, }, - }; + }); + + rerender(updatedProps); + expect(search).not.toHaveBeenCalled(); + }); + + it('should trigger search when a transaction that is absent from the results is added', () => { + const initialProps = createMock({ + ...baseProps, + searchResults: { + ...baseProps.searchResults, + data: { + transactions_1: {transactionID: '1'}, + }, + }, + transactions: { + transactions_1: {transactionID: '1'}, + }, + previousTransactions: { + transactions_1: {transactionID: '1'}, + }, + }); + + const {rerender} = renderHook((props: UseSearchHighlightAndScroll) => useSearchHighlightAndScroll(props), { + initialProps, + }); + + const updatedProps = createMock({ + ...initialProps, + transactions: { + transactions_1: {transactionID: '1'}, + transactions_2: {transactionID: '2'}, + }, + }); + + rerender(updatedProps); + expect(search).toHaveBeenCalledWith({queryJSON: baseProps.queryJSON, searchKey: undefined, offset: 0, shouldCalculateTotals: false, isLoading: false}); + }); + + it('should not trigger search when the added transaction is already in the search results', () => { + // Onyx keeps one value object per collection member, so an unedited transaction has to keep its + // reference across renders — the hook reads that identity to tell an edit from an untouched row. + const transaction1 = createMock({transactionID: '1'}); + const initialProps = createMock({ + ...baseProps, + searchResults: { + ...baseProps.searchResults, + data: { + transactions_1: {transactionID: '1'}, + transactions_2: {transactionID: '2'}, + }, + }, + transactions: { + transactions_1: transaction1, + }, + previousTransactions: { + transactions_1: transaction1, + }, + }); + + const {rerender} = renderHook((props: UseSearchHighlightAndScroll) => useSearchHighlightAndScroll(props), { + initialProps, + }); + + const updatedProps = createMock({ + ...initialProps, + transactions: { + transactions_1: transaction1, + transactions_2: {transactionID: '2'}, + }, + }); + + rerender(updatedProps); + expect(search).not.toHaveBeenCalled(); + }); + + it('should trigger search when a transaction that is already in the results is edited', () => { + const transaction = createMock({transactionID: '1', amount: 100}); + const initialProps = createMock({ + ...baseProps, + searchResults: { + ...baseProps.searchResults, + data: { + transactions_1: {transactionID: '1'}, + }, + }, + transactions: {transactions_1: transaction}, + previousTransactions: {transactions_1: transaction}, + }); + + const {rerender} = renderHook((props: UseSearchHighlightAndScroll) => useSearchHighlightAndScroll(props), { + initialProps, + }); + + const updatedProps = createMock({ + ...initialProps, + transactions: {transactions_1: {transactionID: '1', amount: 250}}, + }); + + rerender(updatedProps); + expect(search).toHaveBeenCalledWith({queryJSON: baseProps.queryJSON, searchKey: undefined, offset: 0, shouldCalculateTotals: false, isLoading: false}); + }); + + it('should not trigger search when the edited transaction is absent from the results', () => { + const visibleTransaction = createMock({transactionID: '1', amount: 100}); + const filteredOutTransaction = createMock({transactionID: '99', amount: 100}); + const initialProps = createMock({ + ...baseProps, + searchResults: { + ...baseProps.searchResults, + data: { + transactions_1: {transactionID: '1'}, + }, + }, + transactions: {transactions_1: visibleTransaction, transactions_99: filteredOutTransaction}, + previousTransactions: {transactions_1: visibleTransaction, transactions_99: filteredOutTransaction}, + }); + + const {rerender} = renderHook((props: UseSearchHighlightAndScroll) => useSearchHighlightAndScroll(props), { + initialProps, + }); + + const updatedProps = createMock({ + ...initialProps, + transactions: {transactions_1: visibleTransaction, transactions_99: {transactionID: '99', amount: 250}}, + }); + + rerender(updatedProps); + expect(search).not.toHaveBeenCalled(); + }); + + it('should trigger the deferred search once Search is active again, after previousTransactions caught up', () => { + const transaction = createMock({transactionID: '1', amount: 100}); + const editedTransaction = createMock({transactionID: '1', amount: 250}); + const initialProps = createMock({ + ...baseProps, + searchResults: { + ...baseProps.searchResults, + data: { + transactions_1: {transactionID: '1'}, + }, + }, + transactions: {transactions_1: transaction}, + previousTransactions: {transactions_1: transaction}, + }); + + mockIsOffline = true; + const {rerender} = renderHook((props: UseSearchHighlightAndScroll) => useSearchHighlightAndScroll(props), { + initialProps, + }); + + const editedProps = createMock({...initialProps, transactions: {transactions_1: editedTransaction}}); + + rerender(editedProps); + expect(search).not.toHaveBeenCalled(); + + // `usePrevious` catches up while Search is inactive, so the edit is no longer visible to the comparisons. + const settledProps = createMock({...editedProps, previousTransactions: {transactions_1: editedTransaction}}); + + rerender(settledProps); + expect(search).not.toHaveBeenCalled(); + + mockIsOffline = false; + + rerender({...settledProps}); + expect(search).toHaveBeenCalledTimes(1); + }); + + it('should not trigger search on a non-chat search when a report action was added and Onyx holds a transaction the query filters out', () => { + const transaction1 = createMock({transactionID: '1'}); + const transaction99 = createMock({transactionID: '99'}); + const initialProps = createMock({ + ...baseProps, + searchResults: { + ...baseProps.searchResults, + data: { + transactions_1: transaction1, + }, + }, + // `transactions_99` is held by the client but filtered out by the query, the normal paginated/filtered state. + transactions: { + transactions_1: transaction1, + transactions_99: transaction99, + }, + previousTransactions: { + transactions_1: transaction1, + transactions_99: transaction99, + }, + reportActions: { + reportActions_1: { + '1': {actionName: 'CREATED', reportActionID: '1'}, + }, + }, + previousReportActions: { + reportActions_1: { + '1': {actionName: 'CREATED', reportActionID: '1'}, + }, + }, + }); + + const {rerender} = renderHook((props: UseSearchHighlightAndScroll) => useSearchHighlightAndScroll(props), { + initialProps, + }); + + const updatedProps = createMock({ + ...initialProps, + reportActions: { + reportActions_1: { + '1': {actionName: 'CREATED', reportActionID: '1'}, + '2': {actionName: 'ADDCOMMENT', reportActionID: '2'}, + }, + }, + }); + + rerender(updatedProps); + expect(search).not.toHaveBeenCalled(); + }); + + it('should not trigger search when the added transaction is already in the results and Onyx holds one the query filters out', () => { + const transaction1 = createMock({transactionID: '1'}); + const transaction99 = createMock({transactionID: '99'}); + const initialProps = createMock({ + ...baseProps, + searchResults: { + ...baseProps.searchResults, + data: { + transactions_1: {transactionID: '1'}, + transactions_2: {transactionID: '2'}, + }, + }, + transactions: { + transactions_1: transaction1, + transactions_99: transaction99, + }, + previousTransactions: { + transactions_1: transaction1, + transactions_99: transaction99, + }, + }); + + const {rerender} = renderHook((props: UseSearchHighlightAndScroll) => useSearchHighlightAndScroll(props), { + initialProps, + }); + + const updatedProps = createMock({ + ...initialProps, + transactions: { + transactions_1: transaction1, + transactions_99: transaction99, + transactions_2: {transactionID: '2'}, + }, + }); - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-expect-error rerender(updatedProps); expect(search).not.toHaveBeenCalled(); }); @@ -199,40 +445,36 @@ describe('useSearchHighlightAndScroll', () => { it('should not trigger search for chat when report actions removed and focused', () => { mockUseIsFocused.mockReturnValue(true); - const chatProps = { + const chatProps = createMock({ ...baseProps, queryJSON: {...baseProps.queryJSON, type: 'chat' as const}, reportActions: { reportActions_1: { - '1': {actionName: 'EXISTING', reportActionID: '1'}, + '1': {actionName: 'CREATED', reportActionID: '1'}, '2': {actionName: 'ADDCOMMENT', reportActionID: '2'}, }, }, previousReportActions: { reportActions_1: { - '1': {actionName: 'EXISTING', reportActionID: '1'}, + '1': {actionName: 'CREATED', reportActionID: '1'}, '2': {actionName: 'ADDCOMMENT', reportActionID: '2'}, }, }, - }; + }); const {rerender} = renderHook((props: UseSearchHighlightAndScroll) => useSearchHighlightAndScroll(props), { - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-expect-error initialProps: chatProps, }); - const updatedProps = { + const updatedProps = createMock({ ...chatProps, reportActions: { reportActions_1: { - '1': {actionName: 'EXISTING', reportActionID: '1'}, + '1': {actionName: 'CREATED', reportActionID: '1'}, }, }, - }; + }); - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-expect-error rerender(updatedProps); expect(search).not.toHaveBeenCalled(); }); @@ -241,7 +483,7 @@ describe('useSearchHighlightAndScroll', () => { const {rerender, result} = renderHook((props: UseSearchHighlightAndScroll) => useSearchHighlightAndScroll(props), { initialProps: baseProps, }); - const updatedProps = { + const updatedProps = createMock({ ...baseProps, searchResults: { ...baseProps.searchResults, @@ -255,16 +497,14 @@ describe('useSearchHighlightAndScroll', () => { }, }, transactions: { - '1': {transactionID: '1'}, - '2': {transactionID: '2'}, - '3': {transactionID: '3'}, + transactions_1: {transactionID: '1'}, + transactions_2: {transactionID: '2'}, + transactions_3: {transactionID: '3'}, }, previousTransactions: { - '1': {transactionID: '1'}, + transactions_1: {transactionID: '1'}, }, - }; - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-expect-error + }); rerender(updatedProps); expect(result.current.newSearchResultKeys?.size).toBe(2); @@ -277,12 +517,11 @@ describe('useSearchHighlightAndScroll', () => { focusTextInput: jest.fn(), }; - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-expect-error - result.current.handleSelectionListScroll([{transactionID: '1'}, {transactionID: '2'}], ref); - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-expect-error - result.current.handleSelectionListScroll([{transactionID: '2'}, {transactionID: '1'}], ref); + const listItem1 = createMock({transactionID: '1'}); + const listItem2 = createMock({transactionID: '2'}); + + result.current.handleSelectionListScroll([listItem1, listItem2], ref); + result.current.handleSelectionListScroll([listItem2, listItem1], ref); expect(scrollToIndex).not.toHaveBeenCalled(); }); @@ -302,7 +541,7 @@ describe('useSearchHighlightAndScroll', () => { const {rerender, result} = renderHook((props: UseSearchHighlightAndScroll) => useSearchHighlightAndScroll(props), { initialProps: baseProps, }); - const updatedProps1 = { + const updatedProps1 = createMock({ ...baseProps, searchResults: { ...baseProps.searchResults, @@ -316,14 +555,14 @@ describe('useSearchHighlightAndScroll', () => { }, }, transactions: { - '1': {transactionID: '1'}, - '2': {transactionID: '2'}, - '3': {transactionID: '3'}, + transactions_1: {transactionID: '1'}, + transactions_2: {transactionID: '2'}, + transactions_3: {transactionID: '3'}, }, previousTransactions: { - '1': {transactionID: '1'}, + transactions_1: {transactionID: '1'}, }, - } as unknown as UseSearchHighlightAndScroll; + }); // When there is no data yet, even if the transactionID has been added to manual highlight transactionIDs, // it still will not be included in newSearchResultKeys. @@ -332,7 +571,7 @@ describe('useSearchHighlightAndScroll', () => { expect([...(result.current.newSearchResultKeys ?? new Set())]).not.toContain('transactions_3'); // When the data contains the highlight transactionID, it will be highlighted. - const updatedProps2 = { + const updatedProps2 = createMock({ ...updatedProps1, searchResults: { ...updatedProps1.searchResults, @@ -348,7 +587,7 @@ describe('useSearchHighlightAndScroll', () => { }, }, }, - } as unknown as UseSearchHighlightAndScroll; + }); rerender(updatedProps2); expect(result.current.newSearchResultKeys?.size).toBe(1); @@ -358,52 +597,48 @@ describe('useSearchHighlightAndScroll', () => { }); it('should return multiple new search result keys when there are multiple new chats', () => { - const chatProps = { + const chatProps = createMock({ ...baseProps, queryJSON: {...baseProps.queryJSON, type: 'chat' as const}, reportActions: { reportActions_1: { - '1': {actionName: 'EXISTING', reportActionID: '1'}, + '1': {actionName: 'CREATED', reportActionID: '1'}, }, }, - }; + }); const {rerender, result} = renderHook((props: UseSearchHighlightAndScroll) => useSearchHighlightAndScroll(props), { - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-expect-error initialProps: chatProps, }); - const updatedProps = { + const updatedProps = createMock({ ...chatProps, searchResults: { ...baseProps.searchResults, data: { reportActions_1: { - '1': {actionName: 'EXISTING', reportActionID: '1'}, + '1': {actionName: 'CREATED', reportActionID: '1'}, }, reportActions_2: { - '2': {actionName: 'EXISTING', reportActionID: '2'}, + '2': {actionName: 'CREATED', reportActionID: '2'}, }, }, }, reportActions: { reportActions_1: { - '1': {actionName: 'EXISTING', reportActionID: '1'}, + '1': {actionName: 'CREATED', reportActionID: '1'}, }, reportActions_2: { - '2': {actionName: 'EXISTING', reportActionID: '2'}, + '2': {actionName: 'CREATED', reportActionID: '2'}, }, reportActions_3: { - '3': {actionName: 'EXISTING', reportActionID: '3'}, + '3': {actionName: 'CREATED', reportActionID: '3'}, }, }, previousReportActions: { reportActions_1: { - '1': {actionName: 'EXISTING', reportActionID: '1'}, + '1': {actionName: 'CREATED', reportActionID: '1'}, }, }, - }; - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-expect-error + }); rerender(updatedProps); expect(result.current.newSearchResultKeys?.size).toBe(2); });