From 046ebfa73de83b57685be8da8531b02a14298b75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Obudzi=C5=84ski?= Date: Thu, 13 Aug 2026 11:24:55 +0200 Subject: [PATCH 1/4] fix: reland stopping Search re-firing an identical query on unrelated Onyx updates --- src/hooks/useSearchHighlightAndScroll.ts | 37 +-- tests/unit/useSearchHighlightAndScrollTest.ts | 214 +++++++++++++++--- 2 files changed, 205 insertions(+), 46 deletions(-) diff --git a/src/hooks/useSearchHighlightAndScroll.ts b/src/hooks/useSearchHighlightAndScroll.ts index 0f1dd3cbe3b0..83a5e0e5a0c7 100644 --- a/src/hooks/useSearchHighlightAndScroll.ts +++ b/src/hooks/useSearchHighlightAndScroll.ts @@ -85,26 +85,24 @@ 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)); // Check if there is a change in the transactions or report actions list - if ((!isChat && hasTransactionsIDsChange) || hasReportActionsIDsChange || hasPendingSearchRef.current) { + if ((isChat ? hasReportActionsIDsChange : hasTransactionsIDsChange) || 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 @@ -116,19 +114,32 @@ function useSearchHighlightAndScroll({ } 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)); + const currentIDsSet = new Set(isChat ? reportActionsIDs : currentTransactionIDs); + const hasDeletedID = currentSearchResultIDs.some((id) => !currentIDsSet.has(id)); if (!hasDeletedID) { return; } diff --git a/tests/unit/useSearchHighlightAndScrollTest.ts b/tests/unit/useSearchHighlightAndScrollTest.ts index 79fee85c4747..ed1ef5e2c899 100644 --- a/tests/unit/useSearchHighlightAndScrollTest.ts +++ b/tests/unit/useSearchHighlightAndScrollTest.ts @@ -1,4 +1,6 @@ /* eslint-disable @typescript-eslint/naming-convention */ +// These tests build the hook props partially, so every rerender/initialProps call needs a @ts-expect-error. +/* eslint-disable @typescript-eslint/ban-ts-comment */ import {renderHook} from '@testing-library/react-native'; import useSearchHighlightAndScroll from '@hooks/useSearchHighlightAndScroll'; @@ -81,12 +83,11 @@ describe('useSearchHighlightAndScroll', () => { it('should trigger search when new transaction added and focused', () => { const initialProps = { ...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, }); @@ -94,13 +95,12 @@ describe('useSearchHighlightAndScroll', () => { const updatedProps = { ...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}); @@ -115,10 +115,9 @@ describe('useSearchHighlightAndScroll', () => { const updatedProps = { ...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(); @@ -143,7 +142,6 @@ describe('useSearchHighlightAndScroll', () => { }; const {rerender} = renderHook((props: UseSearchHighlightAndScroll) => useSearchHighlightAndScroll(props), { - // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-expect-error initialProps: chatProps, }); @@ -158,7 +156,6 @@ describe('useSearchHighlightAndScroll', () => { }, }; - // 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}); @@ -168,17 +165,16 @@ describe('useSearchHighlightAndScroll', () => { const initialProps = { ...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, }); @@ -186,11 +182,170 @@ describe('useSearchHighlightAndScroll', () => { const updatedProps = { ...baseProps, transactions: { - '1': {transactionID: '1'}, + transactions_1: {transactionID: '1'}, + }, + }; + + // @ts-expect-error + rerender(updatedProps); + expect(search).not.toHaveBeenCalled(); + }); + + it('should trigger search when a transaction that is absent from the results is added', () => { + const initialProps = { + ...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), { + // @ts-expect-error + initialProps, + }); + + const updatedProps = { + ...initialProps, + transactions: { + transactions_1: {transactionID: '1'}, + transactions_2: {transactionID: '2'}, + }, + }; + + // @ts-expect-error + 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', () => { + const initialProps = { + ...baseProps, + searchResults: { + ...baseProps.searchResults, + data: { + transactions_1: {transactionID: '1'}, + transactions_2: {transactionID: '2'}, + }, + }, + transactions: { + transactions_1: {transactionID: '1'}, + }, + previousTransactions: { + transactions_1: {transactionID: '1'}, + }, + }; + + const {rerender} = renderHook((props: UseSearchHighlightAndScroll) => useSearchHighlightAndScroll(props), { + // @ts-expect-error + initialProps, + }); + + const updatedProps = { + ...initialProps, + transactions: { + transactions_1: {transactionID: '1'}, + transactions_2: {transactionID: '2'}, + }, + }; + + // @ts-expect-error + rerender(updatedProps); + expect(search).not.toHaveBeenCalled(); + }); + + 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 initialProps = { + ...baseProps, + searchResults: { + ...baseProps.searchResults, + data: { + transactions_1: {transactionID: '1'}, + }, + }, + // `transactions_99` is held by the client but filtered out by the query, the normal paginated/filtered state. + transactions: { + transactions_1: {transactionID: '1'}, + transactions_99: {transactionID: '99'}, + }, + previousTransactions: { + transactions_1: {transactionID: '1'}, + transactions_99: {transactionID: '99'}, + }, + reportActions: { + reportActions_1: { + '1': {actionName: 'EXISTING', reportActionID: '1'}, + }, + }, + previousReportActions: { + reportActions_1: { + '1': {actionName: 'EXISTING', reportActionID: '1'}, + }, + }, + }; + + const {rerender} = renderHook((props: UseSearchHighlightAndScroll) => useSearchHighlightAndScroll(props), { + // @ts-expect-error + initialProps, + }); + + const updatedProps = { + ...initialProps, + reportActions: { + reportActions_1: { + '1': {actionName: 'EXISTING', reportActionID: '1'}, + '2': {actionName: 'ADDCOMMENT', reportActionID: '2'}, + }, + }, + }; + + // @ts-expect-error + 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 initialProps = { + ...baseProps, + searchResults: { + ...baseProps.searchResults, + data: { + transactions_1: {transactionID: '1'}, + transactions_2: {transactionID: '2'}, + }, + }, + transactions: { + transactions_1: {transactionID: '1'}, + transactions_99: {transactionID: '99'}, + }, + previousTransactions: { + transactions_1: {transactionID: '1'}, + transactions_99: {transactionID: '99'}, + }, + }; + + const {rerender} = renderHook((props: UseSearchHighlightAndScroll) => useSearchHighlightAndScroll(props), { + // @ts-expect-error + initialProps, + }); + + const updatedProps = { + ...initialProps, + transactions: { + transactions_1: {transactionID: '1'}, + transactions_99: {transactionID: '99'}, + transactions_2: {transactionID: '2'}, }, }; - // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-expect-error rerender(updatedProps); expect(search).not.toHaveBeenCalled(); @@ -217,7 +372,6 @@ describe('useSearchHighlightAndScroll', () => { }; const {rerender} = renderHook((props: UseSearchHighlightAndScroll) => useSearchHighlightAndScroll(props), { - // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-expect-error initialProps: chatProps, }); @@ -231,7 +385,6 @@ describe('useSearchHighlightAndScroll', () => { }, }; - // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-expect-error rerender(updatedProps); expect(search).not.toHaveBeenCalled(); @@ -255,15 +408,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,10 +429,8 @@ 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); @@ -316,12 +466,12 @@ 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; @@ -368,7 +518,6 @@ describe('useSearchHighlightAndScroll', () => { }, }; const {rerender, result} = renderHook((props: UseSearchHighlightAndScroll) => useSearchHighlightAndScroll(props), { - // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-expect-error initialProps: chatProps, }); @@ -402,7 +551,6 @@ describe('useSearchHighlightAndScroll', () => { }, }, }; - // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-expect-error rerender(updatedProps); expect(result.current.newSearchResultKeys?.size).toBe(2); From 65a7566c024adf6cb0f6a2c910cf81c34b37660e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Obudzi=C5=84ski?= Date: Thu, 13 Aug 2026 11:25:06 +0200 Subject: [PATCH 2/4] fix: refetch Search when a visible expense is edited or submitted from Search --- src/hooks/useSearchBulkActions.ts | 25 ++++- src/hooks/useSearchHighlightAndScroll.ts | 44 ++++++++- tests/unit/useSearchHighlightAndScrollTest.ts | 92 ++++++++++++++++--- 3 files changed, 145 insertions(+), 16 deletions(-) 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 83a5e0e5a0c7..06c502dd6372 100644 --- a/src/hooks/useSearchHighlightAndScroll.ts +++ b/src/hooks/useSearchHighlightAndScroll.ts @@ -101,8 +101,14 @@ function useSearchHighlightAndScroll({ 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 ? hasReportActionsIDsChange : hasTransactionsIDsChange) || 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 @@ -137,7 +143,7 @@ function useSearchHighlightAndScroll({ // 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) { + if (!hasAGenuinelyNewID && !hasChangedResultTransaction && currentSearchResultIDs.length > 0) { const currentIDsSet = new Set(isChat ? reportActionsIDs : currentTransactionIDs); const hasDeletedID = currentSearchResultIDs.some((id) => !currentIDsSet.has(id)); if (!hasDeletedID) { @@ -373,5 +379,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 ed1ef5e2c899..039a4206395c 100644 --- a/tests/unit/useSearchHighlightAndScrollTest.ts +++ b/tests/unit/useSearchHighlightAndScrollTest.ts @@ -227,6 +227,9 @@ describe('useSearchHighlightAndScroll', () => { }); 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 = {transactionID: '1'}; const initialProps = { ...baseProps, searchResults: { @@ -237,10 +240,10 @@ describe('useSearchHighlightAndScroll', () => { }, }, transactions: { - transactions_1: {transactionID: '1'}, + transactions_1: transaction1, }, previousTransactions: { - transactions_1: {transactionID: '1'}, + transactions_1: transaction1, }, }; @@ -252,7 +255,7 @@ describe('useSearchHighlightAndScroll', () => { const updatedProps = { ...initialProps, transactions: { - transactions_1: {transactionID: '1'}, + transactions_1: transaction1, transactions_2: {transactionID: '2'}, }, }; @@ -262,7 +265,68 @@ describe('useSearchHighlightAndScroll', () => { expect(search).not.toHaveBeenCalled(); }); + it('should trigger search when a transaction that is already in the results is edited', () => { + const transaction = {transactionID: '1', amount: 100}; + const initialProps = { + ...baseProps, + searchResults: { + ...baseProps.searchResults, + data: { + transactions_1: {transactionID: '1'}, + }, + }, + transactions: {transactions_1: transaction}, + previousTransactions: {transactions_1: transaction}, + }; + + const {rerender} = renderHook((props: UseSearchHighlightAndScroll) => useSearchHighlightAndScroll(props), { + // @ts-expect-error + initialProps, + }); + + const updatedProps = { + ...initialProps, + transactions: {transactions_1: {transactionID: '1', amount: 250}}, + }; + + // @ts-expect-error + 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 = {transactionID: '1', amount: 100}; + const filteredOutTransaction = {transactionID: '99', amount: 100}; + const initialProps = { + ...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), { + // @ts-expect-error + initialProps, + }); + + const updatedProps = { + ...initialProps, + transactions: {transactions_1: visibleTransaction, transactions_99: {transactionID: '99', amount: 250}}, + }; + + // @ts-expect-error + rerender(updatedProps); + expect(search).not.toHaveBeenCalled(); + }); + 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 = {transactionID: '1'}; + const transaction99 = {transactionID: '99'}; const initialProps = { ...baseProps, searchResults: { @@ -273,12 +337,12 @@ describe('useSearchHighlightAndScroll', () => { }, // `transactions_99` is held by the client but filtered out by the query, the normal paginated/filtered state. transactions: { - transactions_1: {transactionID: '1'}, - transactions_99: {transactionID: '99'}, + transactions_1: transaction1, + transactions_99: transaction99, }, previousTransactions: { - transactions_1: {transactionID: '1'}, - transactions_99: {transactionID: '99'}, + transactions_1: transaction1, + transactions_99: transaction99, }, reportActions: { reportActions_1: { @@ -313,6 +377,8 @@ describe('useSearchHighlightAndScroll', () => { }); it('should not trigger search when the added transaction is already in the results and Onyx holds one the query filters out', () => { + const transaction1 = {transactionID: '1'}; + const transaction99 = {transactionID: '99'}; const initialProps = { ...baseProps, searchResults: { @@ -323,12 +389,12 @@ describe('useSearchHighlightAndScroll', () => { }, }, transactions: { - transactions_1: {transactionID: '1'}, - transactions_99: {transactionID: '99'}, + transactions_1: transaction1, + transactions_99: transaction99, }, previousTransactions: { - transactions_1: {transactionID: '1'}, - transactions_99: {transactionID: '99'}, + transactions_1: transaction1, + transactions_99: transaction99, }, }; @@ -340,8 +406,8 @@ describe('useSearchHighlightAndScroll', () => { const updatedProps = { ...initialProps, transactions: { - transactions_1: {transactionID: '1'}, - transactions_99: {transactionID: '99'}, + transactions_1: transaction1, + transactions_99: transaction99, transactions_2: {transactionID: '2'}, }, }; From ec2da361cad0ba9921356ce96b313b9a0dc99a6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Obudzi=C5=84ski?= Date: Thu, 13 Aug 2026 14:02:35 +0200 Subject: [PATCH 3/4] fix: honor a deferred Search refetch once Search becomes active again --- src/hooks/useSearchHighlightAndScroll.ts | 5 ++- tests/unit/useSearchHighlightAndScrollTest.ts | 45 +++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/src/hooks/useSearchHighlightAndScroll.ts b/src/hooks/useSearchHighlightAndScroll.ts index 06c502dd6372..8c62c0eb3fdd 100644 --- a/src/hooks/useSearchHighlightAndScroll.ts +++ b/src/hooks/useSearchHighlightAndScroll.ts @@ -118,6 +118,9 @@ 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; // Transaction Onyx keys are `transactions_` but search results yield bare IDs, so read the ID off the value. @@ -143,7 +146,7 @@ function useSearchHighlightAndScroll({ // 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 && !hasChangedResultTransaction && currentSearchResultIDs.length > 0) { + if (!hasAGenuinelyNewID && !hasChangedResultTransaction && !hadPendingSearch && currentSearchResultIDs.length > 0) { const currentIDsSet = new Set(isChat ? reportActionsIDs : currentTransactionIDs); const hasDeletedID = currentSearchResultIDs.some((id) => !currentIDsSet.has(id)); if (!hasDeletedID) { diff --git a/tests/unit/useSearchHighlightAndScrollTest.ts b/tests/unit/useSearchHighlightAndScrollTest.ts index 039a4206395c..0d36e666f6ca 100644 --- a/tests/unit/useSearchHighlightAndScrollTest.ts +++ b/tests/unit/useSearchHighlightAndScrollTest.ts @@ -24,10 +24,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', () => { @@ -324,6 +328,47 @@ describe('useSearchHighlightAndScroll', () => { expect(search).not.toHaveBeenCalled(); }); + it('should trigger the deferred search once Search is active again, after previousTransactions caught up', () => { + const transaction = {transactionID: '1', amount: 100}; + const editedTransaction = {transactionID: '1', amount: 250}; + const initialProps = { + ...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), { + // @ts-expect-error + initialProps, + }); + + const editedProps = {...initialProps, transactions: {transactions_1: editedTransaction}}; + + // @ts-expect-error + 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 = {...editedProps, previousTransactions: {transactions_1: editedTransaction}}; + + // @ts-expect-error + rerender(settledProps); + expect(search).not.toHaveBeenCalled(); + + mockIsOffline = false; + + // @ts-expect-error + 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 = {transactionID: '1'}; const transaction99 = {transactionID: '99'}; From c944b43332110da9f50e359a5cabd98a8b4c13fc Mon Sep 17 00:00:00 2001 From: Adam Horodyski Date: Fri, 14 Aug 2026 18:11:29 +0200 Subject: [PATCH 4/4] test: type the Search highlight hook props with createMock Replaces the file-level `ban-ts-comment` disable, all 31 `@ts-expect-error` comments and the two `as unknown as` casts with `createMock`, the pattern the rest of the suite already uses. The props are now type checked at every call site instead of being waved through. Two knock-on changes the typing forced: - `actionName: 'EXISTING'` is not a `ReportActionName`, so the pre-existing report actions now use `'CREATED'`. The hook never reads `actionName`. - The results snapshot in the report action test reuses `transaction1` instead of repeating the literal, per review. Co-Authored-By: Claude Opus 5 (1M context) --- tests/unit/useSearchHighlightAndScrollTest.ts | 210 ++++++++---------- 1 file changed, 93 insertions(+), 117 deletions(-) diff --git a/tests/unit/useSearchHighlightAndScrollTest.ts b/tests/unit/useSearchHighlightAndScrollTest.ts index 0d36e666f6ca..9ece9df78e55 100644 --- a/tests/unit/useSearchHighlightAndScrollTest.ts +++ b/tests/unit/useSearchHighlightAndScrollTest.ts @@ -1,17 +1,20 @@ /* eslint-disable @typescript-eslint/naming-convention */ -// These tests build the hook props partially, so every rerender/initialProps call needs a @ts-expect-error. -/* eslint-disable @typescript-eslint/ban-ts-comment */ 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), @@ -85,27 +88,25 @@ describe('useSearchHighlightAndScroll', () => { }); it('should trigger search when new transaction added and focused', () => { - const initialProps = { + const initialProps = createMock({ ...baseProps, transactions: {transactions_1: {transactionID: '1'}}, previousTransactions: {transactions_1: {transactionID: '1'}}, - }; + }); const {rerender} = renderHook((props: UseSearchHighlightAndScroll) => useSearchHighlightAndScroll(props), { - // @ts-expect-error initialProps, }); - const updatedProps = { + const updatedProps = createMock({ ...baseProps, transactions: { transactions_1: {transactionID: '1'}, transactions_2: {transactionID: '2'}, }, previousTransactions: {transactions_1: {transactionID: '1'}}, - }; + }); - // @ts-expect-error rerender(updatedProps); expect(search).toHaveBeenCalledWith({queryJSON: baseProps.queryJSON, searchKey: undefined, offset: 0, shouldCalculateTotals: false, isLoading: false}); }); @@ -117,12 +118,11 @@ describe('useSearchHighlightAndScroll', () => { initialProps: baseProps, }); - const updatedProps = { + const updatedProps = createMock({ ...baseProps, transactions: {transactions_1: {transactionID: '1'}}, - }; + }); - // @ts-expect-error rerender(updatedProps); expect(search).not.toHaveBeenCalled(); }); @@ -130,43 +130,41 @@ 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), { - // @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'}, }, }, - }; + }); - // @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: { transactions_1: {transactionID: '1'}, @@ -176,27 +174,25 @@ describe('useSearchHighlightAndScroll', () => { transactions_1: {transactionID: '1'}, transactions_2: {transactionID: '2'}, }, - }; + }); const {rerender} = renderHook((props: UseSearchHighlightAndScroll) => useSearchHighlightAndScroll(props), { - // @ts-expect-error initialProps, }); - const updatedProps = { + const updatedProps = createMock({ ...baseProps, transactions: { transactions_1: {transactionID: '1'}, }, - }; + }); - // @ts-expect-error rerender(updatedProps); expect(search).not.toHaveBeenCalled(); }); it('should trigger search when a transaction that is absent from the results is added', () => { - const initialProps = { + const initialProps = createMock({ ...baseProps, searchResults: { ...baseProps.searchResults, @@ -210,22 +206,20 @@ describe('useSearchHighlightAndScroll', () => { previousTransactions: { transactions_1: {transactionID: '1'}, }, - }; + }); const {rerender} = renderHook((props: UseSearchHighlightAndScroll) => useSearchHighlightAndScroll(props), { - // @ts-expect-error initialProps, }); - const updatedProps = { + const updatedProps = createMock({ ...initialProps, transactions: { transactions_1: {transactionID: '1'}, transactions_2: {transactionID: '2'}, }, - }; + }); - // @ts-expect-error rerender(updatedProps); expect(search).toHaveBeenCalledWith({queryJSON: baseProps.queryJSON, searchKey: undefined, offset: 0, shouldCalculateTotals: false, isLoading: false}); }); @@ -233,8 +227,8 @@ describe('useSearchHighlightAndScroll', () => { 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 = {transactionID: '1'}; - const initialProps = { + const transaction1 = createMock({transactionID: '1'}); + const initialProps = createMock({ ...baseProps, searchResults: { ...baseProps.searchResults, @@ -249,29 +243,27 @@ describe('useSearchHighlightAndScroll', () => { previousTransactions: { transactions_1: transaction1, }, - }; + }); const {rerender} = renderHook((props: UseSearchHighlightAndScroll) => useSearchHighlightAndScroll(props), { - // @ts-expect-error initialProps, }); - const updatedProps = { + const updatedProps = createMock({ ...initialProps, transactions: { transactions_1: transaction1, transactions_2: {transactionID: '2'}, }, - }; + }); - // @ts-expect-error rerender(updatedProps); expect(search).not.toHaveBeenCalled(); }); it('should trigger search when a transaction that is already in the results is edited', () => { - const transaction = {transactionID: '1', amount: 100}; - const initialProps = { + const transaction = createMock({transactionID: '1', amount: 100}); + const initialProps = createMock({ ...baseProps, searchResults: { ...baseProps.searchResults, @@ -281,27 +273,25 @@ describe('useSearchHighlightAndScroll', () => { }, transactions: {transactions_1: transaction}, previousTransactions: {transactions_1: transaction}, - }; + }); const {rerender} = renderHook((props: UseSearchHighlightAndScroll) => useSearchHighlightAndScroll(props), { - // @ts-expect-error initialProps, }); - const updatedProps = { + const updatedProps = createMock({ ...initialProps, transactions: {transactions_1: {transactionID: '1', amount: 250}}, - }; + }); - // @ts-expect-error 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 = {transactionID: '1', amount: 100}; - const filteredOutTransaction = {transactionID: '99', amount: 100}; - const initialProps = { + const visibleTransaction = createMock({transactionID: '1', amount: 100}); + const filteredOutTransaction = createMock({transactionID: '99', amount: 100}); + const initialProps = createMock({ ...baseProps, searchResults: { ...baseProps.searchResults, @@ -311,27 +301,25 @@ describe('useSearchHighlightAndScroll', () => { }, transactions: {transactions_1: visibleTransaction, transactions_99: filteredOutTransaction}, previousTransactions: {transactions_1: visibleTransaction, transactions_99: filteredOutTransaction}, - }; + }); const {rerender} = renderHook((props: UseSearchHighlightAndScroll) => useSearchHighlightAndScroll(props), { - // @ts-expect-error initialProps, }); - const updatedProps = { + const updatedProps = createMock({ ...initialProps, transactions: {transactions_1: visibleTransaction, transactions_99: {transactionID: '99', amount: 250}}, - }; + }); - // @ts-expect-error rerender(updatedProps); expect(search).not.toHaveBeenCalled(); }); it('should trigger the deferred search once Search is active again, after previousTransactions caught up', () => { - const transaction = {transactionID: '1', amount: 100}; - const editedTransaction = {transactionID: '1', amount: 250}; - const initialProps = { + const transaction = createMock({transactionID: '1', amount: 100}); + const editedTransaction = createMock({transactionID: '1', amount: 250}); + const initialProps = createMock({ ...baseProps, searchResults: { ...baseProps.searchResults, @@ -341,43 +329,39 @@ describe('useSearchHighlightAndScroll', () => { }, transactions: {transactions_1: transaction}, previousTransactions: {transactions_1: transaction}, - }; + }); mockIsOffline = true; const {rerender} = renderHook((props: UseSearchHighlightAndScroll) => useSearchHighlightAndScroll(props), { - // @ts-expect-error initialProps, }); - const editedProps = {...initialProps, transactions: {transactions_1: editedTransaction}}; + const editedProps = createMock({...initialProps, transactions: {transactions_1: editedTransaction}}); - // @ts-expect-error 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 = {...editedProps, previousTransactions: {transactions_1: editedTransaction}}; + const settledProps = createMock({...editedProps, previousTransactions: {transactions_1: editedTransaction}}); - // @ts-expect-error rerender(settledProps); expect(search).not.toHaveBeenCalled(); mockIsOffline = false; - // @ts-expect-error 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 = {transactionID: '1'}; - const transaction99 = {transactionID: '99'}; - const initialProps = { + const transaction1 = createMock({transactionID: '1'}); + const transaction99 = createMock({transactionID: '99'}); + const initialProps = createMock({ ...baseProps, searchResults: { ...baseProps.searchResults, data: { - transactions_1: {transactionID: '1'}, + transactions_1: transaction1, }, }, // `transactions_99` is held by the client but filtered out by the query, the normal paginated/filtered state. @@ -391,40 +375,38 @@ describe('useSearchHighlightAndScroll', () => { }, 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), { - // @ts-expect-error initialProps, }); - const updatedProps = { + const updatedProps = createMock({ ...initialProps, reportActions: { reportActions_1: { - '1': {actionName: 'EXISTING', reportActionID: '1'}, + '1': {actionName: 'CREATED', reportActionID: '1'}, '2': {actionName: 'ADDCOMMENT', reportActionID: '2'}, }, }, - }; + }); - // @ts-expect-error 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 = {transactionID: '1'}; - const transaction99 = {transactionID: '99'}; - const initialProps = { + const transaction1 = createMock({transactionID: '1'}); + const transaction99 = createMock({transactionID: '99'}); + const initialProps = createMock({ ...baseProps, searchResults: { ...baseProps.searchResults, @@ -441,23 +423,21 @@ describe('useSearchHighlightAndScroll', () => { transactions_1: transaction1, transactions_99: transaction99, }, - }; + }); const {rerender} = renderHook((props: UseSearchHighlightAndScroll) => useSearchHighlightAndScroll(props), { - // @ts-expect-error initialProps, }); - const updatedProps = { + const updatedProps = createMock({ ...initialProps, transactions: { transactions_1: transaction1, transactions_99: transaction99, transactions_2: {transactionID: '2'}, }, - }; + }); - // @ts-expect-error rerender(updatedProps); expect(search).not.toHaveBeenCalled(); }); @@ -465,38 +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), { - // @ts-expect-error initialProps: chatProps, }); - const updatedProps = { + const updatedProps = createMock({ ...chatProps, reportActions: { reportActions_1: { - '1': {actionName: 'EXISTING', reportActionID: '1'}, + '1': {actionName: 'CREATED', reportActionID: '1'}, }, }, - }; + }); - // @ts-expect-error rerender(updatedProps); expect(search).not.toHaveBeenCalled(); }); @@ -505,7 +483,7 @@ describe('useSearchHighlightAndScroll', () => { const {rerender, result} = renderHook((props: UseSearchHighlightAndScroll) => useSearchHighlightAndScroll(props), { initialProps: baseProps, }); - const updatedProps = { + const updatedProps = createMock({ ...baseProps, searchResults: { ...baseProps.searchResults, @@ -526,8 +504,7 @@ describe('useSearchHighlightAndScroll', () => { previousTransactions: { transactions_1: {transactionID: '1'}, }, - }; - // @ts-expect-error + }); rerender(updatedProps); expect(result.current.newSearchResultKeys?.size).toBe(2); @@ -540,10 +517,11 @@ describe('useSearchHighlightAndScroll', () => { focusTextInput: jest.fn(), }; - // @ts-expect-error - result.current.handleSelectionListScroll([{transactionID: '1'}, {transactionID: '2'}], ref); - // @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(); }); @@ -563,7 +541,7 @@ describe('useSearchHighlightAndScroll', () => { const {rerender, result} = renderHook((props: UseSearchHighlightAndScroll) => useSearchHighlightAndScroll(props), { initialProps: baseProps, }); - const updatedProps1 = { + const updatedProps1 = createMock({ ...baseProps, searchResults: { ...baseProps.searchResults, @@ -584,7 +562,7 @@ describe('useSearchHighlightAndScroll', () => { previousTransactions: { 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. @@ -593,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, @@ -609,7 +587,7 @@ describe('useSearchHighlightAndScroll', () => { }, }, }, - } as unknown as UseSearchHighlightAndScroll; + }); rerender(updatedProps2); expect(result.current.newSearchResultKeys?.size).toBe(1); @@ -619,50 +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), { - // @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'}, }, }, - }; - // @ts-expect-error + }); rerender(updatedProps); expect(result.current.newSearchResultKeys?.size).toBe(2); });