From fa89d7af12df0360dcb516535a0a9dccd4613ec6 Mon Sep 17 00:00:00 2001 From: FitseTLT Date: Thu, 19 Dec 2024 17:12:03 +0300 Subject: [PATCH 1/9] updated to request search on transaction or report action list change --- src/hooks/useSearchHighlightAndScroll.ts | 25 ++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/src/hooks/useSearchHighlightAndScroll.ts b/src/hooks/useSearchHighlightAndScroll.ts index a5937559fd2f..6cd53dcf9bd7 100644 --- a/src/hooks/useSearchHighlightAndScroll.ts +++ b/src/hooks/useSearchHighlightAndScroll.ts @@ -1,3 +1,4 @@ +import isEqual from 'lodash/isEqual'; import {useCallback, useEffect, useRef, useState} from 'react'; import type {OnyxCollection, OnyxEntry} from 'react-native-onyx'; import type {SearchQueryJSON} from '@components/Search/types'; @@ -34,20 +35,24 @@ function useSearchHighlightAndScroll({searchResults, transactions, previousTrans // Trigger search when a new report action is added while on chat or when a new transaction is added for the other search types. useEffect(() => { - const previousTransactionsLength = previousTransactions && Object.keys(previousTransactions).length; - const transactionsLength = transactions && Object.keys(transactions).length; + const previousTransactionIDList = Object.keys(previousTransactions ?? {}); + const transactionIDList = Object.keys(transactions ?? {}); - const reportActionsLength = reportActions && Object.values(reportActions).reduce((sum, curr) => sum + Object.keys(curr ?? {}).length, 0); - const prevReportActionsLength = previousReportActions && Object.values(previousReportActions).reduce((sum, curr) => sum + Object.keys(curr ?? {}).length, 0); - // Return early if search was already triggered or there's no change in current and previous data length - if (searchTriggeredRef.current || (!isChat && previousTransactionsLength === transactionsLength) || (isChat && reportActionsLength === prevReportActionsLength)) { + const reportActionIDList = Object.values(reportActions ?? {}) + .map((actions) => Object.keys(actions ?? {})) + .flat(); + const prevReportActionIDList = Object.values(previousReportActions ?? {}) + .map((actions) => Object.keys(actions ?? {})) + .flat(); + + if (searchTriggeredRef.current) { return; } - const newTransactionAdded = transactionsLength && typeof previousTransactionsLength === 'number' && transactionsLength > previousTransactionsLength; - const newReportActionAdded = reportActionsLength && typeof prevReportActionsLength === 'number' && reportActionsLength > prevReportActionsLength; + const hasTransactionChange = !isEqual(transactionIDList, previousTransactionIDList); + const hasReportActionChange = !isEqual(reportActionIDList, prevReportActionIDList); - // Check if a new transaction or report action was added - if ((!isChat && !!newTransactionAdded) || (isChat && !!newReportActionAdded)) { + // Check if there is a change in transaction or report action list + if ((!isChat && hasTransactionChange) || (isChat && hasReportActionChange)) { // Set the flag indicating the search is triggered by the hook triggeredByHookRef.current = true; From f2b8c3369643612a31dfc95bf0de67db3f63c1c1 Mon Sep 17 00:00:00 2001 From: FitseTLT Date: Thu, 19 Dec 2024 17:15:36 +0300 Subject: [PATCH 2/9] changed var name --- src/hooks/useSearchHighlightAndScroll.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/hooks/useSearchHighlightAndScroll.ts b/src/hooks/useSearchHighlightAndScroll.ts index 6cd53dcf9bd7..6110a5037a3a 100644 --- a/src/hooks/useSearchHighlightAndScroll.ts +++ b/src/hooks/useSearchHighlightAndScroll.ts @@ -41,7 +41,7 @@ function useSearchHighlightAndScroll({searchResults, transactions, previousTrans const reportActionIDList = Object.values(reportActions ?? {}) .map((actions) => Object.keys(actions ?? {})) .flat(); - const prevReportActionIDList = Object.values(previousReportActions ?? {}) + const previousReportActionIDList = Object.values(previousReportActions ?? {}) .map((actions) => Object.keys(actions ?? {})) .flat(); @@ -49,7 +49,7 @@ function useSearchHighlightAndScroll({searchResults, transactions, previousTrans return; } const hasTransactionChange = !isEqual(transactionIDList, previousTransactionIDList); - const hasReportActionChange = !isEqual(reportActionIDList, prevReportActionIDList); + const hasReportActionChange = !isEqual(reportActionIDList, previousReportActionIDList); // Check if there is a change in transaction or report action list if ((!isChat && hasTransactionChange) || (isChat && hasReportActionChange)) { From 08649be4f397817702b06cba556d53df43ce4eca Mon Sep 17 00:00:00 2001 From: FitseTLT Date: Thu, 26 Dec 2024 02:20:17 +0300 Subject: [PATCH 3/9] fix an edge case problem on deletion --- src/hooks/useSearchHighlightAndScroll.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/hooks/useSearchHighlightAndScroll.ts b/src/hooks/useSearchHighlightAndScroll.ts index 6110a5037a3a..c27552ef0547 100644 --- a/src/hooks/useSearchHighlightAndScroll.ts +++ b/src/hooks/useSearchHighlightAndScroll.ts @@ -27,6 +27,7 @@ function useSearchHighlightAndScroll({searchResults, transactions, previousTrans // Ref to track if the search was triggered by this hook const triggeredByHookRef = useRef(false); const searchTriggeredRef = useRef(false); + const hasItemBeenAddedRef = useRef(false); const previousSearchResults = usePrevious(searchResults?.data); const [newSearchResultKey, setNewSearchResultKey] = useState(null); const highlightedIDs = useRef>(new Set()); @@ -51,6 +52,11 @@ function useSearchHighlightAndScroll({searchResults, transactions, previousTrans const hasTransactionChange = !isEqual(transactionIDList, previousTransactionIDList); const hasReportActionChange = !isEqual(reportActionIDList, previousReportActionIDList); + // We only want to highlight new items only if addition of transactions or report actions triggered the search. + // This is because on deletion of items sometimes the BE returns old items in place of the deleted ones + // but we don't want to highlight these old items although they are new to the current search result. + hasItemBeenAddedRef.current = isChat ? reportActionIDList.length > previousReportActionIDList.length : transactionIDList.length > previousTransactionIDList.length; + // Check if there is a change in transaction or report action list if ((!isChat && hasTransactionChange) || (isChat && hasReportActionChange)) { // Set the flag indicating the search is triggered by the hook @@ -92,7 +98,7 @@ function useSearchHighlightAndScroll({searchResults, transactions, previousTrans // Find new report action IDs that are not in the previousReportActionIDs and not already highlighted const newReportActionIDs = currentReportActionIDs.filter((id) => !previousReportActionIDs.includes(id) && !highlightedIDs.current.has(id)); - if (!triggeredByHookRef.current || newReportActionIDs.length === 0) { + if (!triggeredByHookRef.current || newReportActionIDs.length === 0 || !hasItemBeenAddedRef.current) { return; } @@ -108,7 +114,7 @@ function useSearchHighlightAndScroll({searchResults, transactions, previousTrans // Find new transaction IDs that are not in the previousTransactionIDs and not already highlighted const newTransactionIDs = currentTransactionIDs.filter((id) => !previousTransactionIDs.includes(id) && !highlightedIDs.current.has(id)); - if (!triggeredByHookRef.current || newTransactionIDs.length === 0) { + if (!triggeredByHookRef.current || newTransactionIDs.length === 0 || !hasItemBeenAddedRef.current) { return; } From af28f6eae4e4e5c34c757121a00b4d39ac9fe88d Mon Sep 17 00:00:00 2001 From: FitseTLT Date: Thu, 26 Dec 2024 02:30:07 +0300 Subject: [PATCH 4/9] revert --- src/hooks/useSearchHighlightAndScroll.ts | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/hooks/useSearchHighlightAndScroll.ts b/src/hooks/useSearchHighlightAndScroll.ts index c27552ef0547..6110a5037a3a 100644 --- a/src/hooks/useSearchHighlightAndScroll.ts +++ b/src/hooks/useSearchHighlightAndScroll.ts @@ -27,7 +27,6 @@ function useSearchHighlightAndScroll({searchResults, transactions, previousTrans // Ref to track if the search was triggered by this hook const triggeredByHookRef = useRef(false); const searchTriggeredRef = useRef(false); - const hasItemBeenAddedRef = useRef(false); const previousSearchResults = usePrevious(searchResults?.data); const [newSearchResultKey, setNewSearchResultKey] = useState(null); const highlightedIDs = useRef>(new Set()); @@ -52,11 +51,6 @@ function useSearchHighlightAndScroll({searchResults, transactions, previousTrans const hasTransactionChange = !isEqual(transactionIDList, previousTransactionIDList); const hasReportActionChange = !isEqual(reportActionIDList, previousReportActionIDList); - // We only want to highlight new items only if addition of transactions or report actions triggered the search. - // This is because on deletion of items sometimes the BE returns old items in place of the deleted ones - // but we don't want to highlight these old items although they are new to the current search result. - hasItemBeenAddedRef.current = isChat ? reportActionIDList.length > previousReportActionIDList.length : transactionIDList.length > previousTransactionIDList.length; - // Check if there is a change in transaction or report action list if ((!isChat && hasTransactionChange) || (isChat && hasReportActionChange)) { // Set the flag indicating the search is triggered by the hook @@ -98,7 +92,7 @@ function useSearchHighlightAndScroll({searchResults, transactions, previousTrans // Find new report action IDs that are not in the previousReportActionIDs and not already highlighted const newReportActionIDs = currentReportActionIDs.filter((id) => !previousReportActionIDs.includes(id) && !highlightedIDs.current.has(id)); - if (!triggeredByHookRef.current || newReportActionIDs.length === 0 || !hasItemBeenAddedRef.current) { + if (!triggeredByHookRef.current || newReportActionIDs.length === 0) { return; } @@ -114,7 +108,7 @@ function useSearchHighlightAndScroll({searchResults, transactions, previousTrans // Find new transaction IDs that are not in the previousTransactionIDs and not already highlighted const newTransactionIDs = currentTransactionIDs.filter((id) => !previousTransactionIDs.includes(id) && !highlightedIDs.current.has(id)); - if (!triggeredByHookRef.current || newTransactionIDs.length === 0 || !hasItemBeenAddedRef.current) { + if (!triggeredByHookRef.current || newTransactionIDs.length === 0) { return; } From e077cd8fc8edcb2c96e9022dbc844f30e8592a7b Mon Sep 17 00:00:00 2001 From: FitseTLT Date: Thu, 26 Dec 2024 17:58:03 +0300 Subject: [PATCH 5/9] Revert "revert" This reverts commit af28f6eae4e4e5c34c757121a00b4d39ac9fe88d. --- src/hooks/useSearchHighlightAndScroll.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/hooks/useSearchHighlightAndScroll.ts b/src/hooks/useSearchHighlightAndScroll.ts index 6110a5037a3a..c27552ef0547 100644 --- a/src/hooks/useSearchHighlightAndScroll.ts +++ b/src/hooks/useSearchHighlightAndScroll.ts @@ -27,6 +27,7 @@ function useSearchHighlightAndScroll({searchResults, transactions, previousTrans // Ref to track if the search was triggered by this hook const triggeredByHookRef = useRef(false); const searchTriggeredRef = useRef(false); + const hasItemBeenAddedRef = useRef(false); const previousSearchResults = usePrevious(searchResults?.data); const [newSearchResultKey, setNewSearchResultKey] = useState(null); const highlightedIDs = useRef>(new Set()); @@ -51,6 +52,11 @@ function useSearchHighlightAndScroll({searchResults, transactions, previousTrans const hasTransactionChange = !isEqual(transactionIDList, previousTransactionIDList); const hasReportActionChange = !isEqual(reportActionIDList, previousReportActionIDList); + // We only want to highlight new items only if addition of transactions or report actions triggered the search. + // This is because on deletion of items sometimes the BE returns old items in place of the deleted ones + // but we don't want to highlight these old items although they are new to the current search result. + hasItemBeenAddedRef.current = isChat ? reportActionIDList.length > previousReportActionIDList.length : transactionIDList.length > previousTransactionIDList.length; + // Check if there is a change in transaction or report action list if ((!isChat && hasTransactionChange) || (isChat && hasReportActionChange)) { // Set the flag indicating the search is triggered by the hook @@ -92,7 +98,7 @@ function useSearchHighlightAndScroll({searchResults, transactions, previousTrans // Find new report action IDs that are not in the previousReportActionIDs and not already highlighted const newReportActionIDs = currentReportActionIDs.filter((id) => !previousReportActionIDs.includes(id) && !highlightedIDs.current.has(id)); - if (!triggeredByHookRef.current || newReportActionIDs.length === 0) { + if (!triggeredByHookRef.current || newReportActionIDs.length === 0 || !hasItemBeenAddedRef.current) { return; } @@ -108,7 +114,7 @@ function useSearchHighlightAndScroll({searchResults, transactions, previousTrans // Find new transaction IDs that are not in the previousTransactionIDs and not already highlighted const newTransactionIDs = currentTransactionIDs.filter((id) => !previousTransactionIDs.includes(id) && !highlightedIDs.current.has(id)); - if (!triggeredByHookRef.current || newTransactionIDs.length === 0) { + if (!triggeredByHookRef.current || newTransactionIDs.length === 0 || !hasItemBeenAddedRef.current) { return; } From 6c54952e3d021c442119c59346cfcbbbdc70d0b7 Mon Sep 17 00:00:00 2001 From: FitseTLT Date: Thu, 26 Dec 2024 18:08:30 +0300 Subject: [PATCH 6/9] minor fix --- src/hooks/useSearchHighlightAndScroll.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/hooks/useSearchHighlightAndScroll.ts b/src/hooks/useSearchHighlightAndScroll.ts index c27552ef0547..99d7df5ba481 100644 --- a/src/hooks/useSearchHighlightAndScroll.ts +++ b/src/hooks/useSearchHighlightAndScroll.ts @@ -52,13 +52,13 @@ function useSearchHighlightAndScroll({searchResults, transactions, previousTrans const hasTransactionChange = !isEqual(transactionIDList, previousTransactionIDList); const hasReportActionChange = !isEqual(reportActionIDList, previousReportActionIDList); - // We only want to highlight new items only if addition of transactions or report actions triggered the search. - // This is because on deletion of items sometimes the BE returns old items in place of the deleted ones - // but we don't want to highlight these old items although they are new to the current search result. - hasItemBeenAddedRef.current = isChat ? reportActionIDList.length > previousReportActionIDList.length : transactionIDList.length > previousTransactionIDList.length; - // Check if there is a change in transaction or report action list if ((!isChat && hasTransactionChange) || (isChat && hasReportActionChange)) { + // We only want to highlight new items only if addition of transactions or report actions triggered the search. + // This is because on deletion of items sometimes the BE returns old items in place of the deleted ones + // but we don't want to highlight these old items although they are new to the current search result. + hasItemBeenAddedRef.current = isChat ? reportActionIDList.length > previousReportActionIDList.length : transactionIDList.length > previousTransactionIDList.length; + // Set the flag indicating the search is triggered by the hook triggeredByHookRef.current = true; From d5bd3aec4188d452eb0724ec80e030bbb461982e Mon Sep 17 00:00:00 2001 From: FitseTLT Date: Mon, 13 Jan 2025 16:50:52 +0300 Subject: [PATCH 7/9] update based on comments --- src/hooks/useSearchHighlightAndScroll.ts | 30 ++++++++++++------------ 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/hooks/useSearchHighlightAndScroll.ts b/src/hooks/useSearchHighlightAndScroll.ts index 99d7df5ba481..2ce343256876 100644 --- a/src/hooks/useSearchHighlightAndScroll.ts +++ b/src/hooks/useSearchHighlightAndScroll.ts @@ -27,7 +27,7 @@ function useSearchHighlightAndScroll({searchResults, transactions, previousTrans // Ref to track if the search was triggered by this hook const triggeredByHookRef = useRef(false); const searchTriggeredRef = useRef(false); - const hasItemBeenAddedRef = useRef(false); + const hasNewItemsRef = useRef(false); const previousSearchResults = usePrevious(searchResults?.data); const [newSearchResultKey, setNewSearchResultKey] = useState(null); const highlightedIDs = useRef>(new Set()); @@ -36,28 +36,28 @@ function useSearchHighlightAndScroll({searchResults, transactions, previousTrans // Trigger search when a new report action is added while on chat or when a new transaction is added for the other search types. useEffect(() => { - const previousTransactionIDList = Object.keys(previousTransactions ?? {}); - const transactionIDList = Object.keys(transactions ?? {}); + const previousTransactionsIDs = Object.keys(previousTransactions ?? {}); + const transactionsIDs = Object.keys(transactions ?? {}); - const reportActionIDList = Object.values(reportActions ?? {}) + const reportActionsIDs = Object.values(reportActions ?? {}) .map((actions) => Object.keys(actions ?? {})) .flat(); - const previousReportActionIDList = Object.values(previousReportActions ?? {}) + const previousReportActionsIDs = Object.values(previousReportActions ?? {}) .map((actions) => Object.keys(actions ?? {})) .flat(); if (searchTriggeredRef.current) { return; } - const hasTransactionChange = !isEqual(transactionIDList, previousTransactionIDList); - const hasReportActionChange = !isEqual(reportActionIDList, previousReportActionIDList); + const hasTransactionsIDsChange = !isEqual(transactionsIDs, previousTransactionsIDs); + const hasReportActionsIDsChange = !isEqual(reportActionsIDs, previousReportActionsIDs); - // Check if there is a change in transaction or report action list - if ((!isChat && hasTransactionChange) || (isChat && hasReportActionChange)) { - // We only want to highlight new items only if addition of transactions or report actions triggered the search. - // This is because on deletion of items sometimes the BE returns old items in place of the deleted ones - // but we don't want to highlight these old items although they are new to the current search result. - hasItemBeenAddedRef.current = isChat ? reportActionIDList.length > previousReportActionIDList.length : transactionIDList.length > previousTransactionIDList.length; + // Check if there is a change in the transactions or report actions list + if ((!isChat && hasTransactionsIDsChange) || (isChat && hasReportActionsIDsChange)) { + // We only want to highlight new items if the addition of transactions or report actions triggered the search. + // This is because, on deletion of items, the backend sometimes returns old items in place of the deleted ones. + // We don't want to highlight these old items, even if they appear new in the current search results. + hasNewItemsRef.current = isChat ? reportActionsIDs.length > previousReportActionsIDs.length : transactionsIDs.length > previousTransactionsIDs.length; // Set the flag indicating the search is triggered by the hook triggeredByHookRef.current = true; @@ -98,7 +98,7 @@ function useSearchHighlightAndScroll({searchResults, transactions, previousTrans // Find new report action IDs that are not in the previousReportActionIDs and not already highlighted const newReportActionIDs = currentReportActionIDs.filter((id) => !previousReportActionIDs.includes(id) && !highlightedIDs.current.has(id)); - if (!triggeredByHookRef.current || newReportActionIDs.length === 0 || !hasItemBeenAddedRef.current) { + if (!triggeredByHookRef.current || newReportActionIDs.length === 0 || !hasNewItemsRef.current) { return; } @@ -114,7 +114,7 @@ function useSearchHighlightAndScroll({searchResults, transactions, previousTrans // Find new transaction IDs that are not in the previousTransactionIDs and not already highlighted const newTransactionIDs = currentTransactionIDs.filter((id) => !previousTransactionIDs.includes(id) && !highlightedIDs.current.has(id)); - if (!triggeredByHookRef.current || newTransactionIDs.length === 0 || !hasItemBeenAddedRef.current) { + if (!triggeredByHookRef.current || newTransactionIDs.length === 0 || !hasNewItemsRef.current) { return; } From d04b29b27a3ce75cd6f2fd9d62ae5b3c196870b5 Mon Sep 17 00:00:00 2001 From: FitseTLT Date: Mon, 13 Jan 2025 18:07:17 +0300 Subject: [PATCH 8/9] added test --- src/hooks/useSearchHighlightAndScroll.ts | 1 + tests/unit/useSearchHighlightAndScrollTest.ts | 197 ++++++++++++++++++ 2 files changed, 198 insertions(+) create mode 100644 tests/unit/useSearchHighlightAndScrollTest.ts diff --git a/src/hooks/useSearchHighlightAndScroll.ts b/src/hooks/useSearchHighlightAndScroll.ts index 2ce343256876..cce9dda11b03 100644 --- a/src/hooks/useSearchHighlightAndScroll.ts +++ b/src/hooks/useSearchHighlightAndScroll.ts @@ -227,3 +227,4 @@ function extractReportActionIDsFromSearchResults(searchResultsData: Partial { + const actualModule = jest.requireActual('@libs/actions/Search'); + return { + ...actualModule, + search: jest.fn(), + }; +}); + +describe('useSearchHighlightAndScroll', () => { + it('should trigger Search when transactionIDs list change', () => { + const initialProps: UseSearchHighlightAndScroll = { + searchResults: { + data: {personalDetailsList: {}}, + search: { + columnsToShow: { + shouldShowCategoryColumn: true, + shouldShowTagColumn: true, + shouldShowTaxColumn: true, + }, + hasMoreResults: false, + hasResults: true, + offset: 0, + status: 'all', + type: 'expense', + isLoading: false, + }, + }, + transactions: { + transactions_1: { + amount: -100, + bank: '', + billable: false, + cardID: 0, + cardName: 'Cash Expense', + cardNumber: '', + category: '', + comment: { + comment: '', + }, + created: '2025-01-08', + currency: 'ETB', + filename: 'w_c989c343d834d48a4e004c38d03c90bff9434768.png', + inserted: '2025-01-08 15:35:32', + managedCard: false, + merchant: 'g', + modifiedAmount: 0, + modifiedCreated: '', + modifiedCurrency: '', + modifiedMerchant: '', + originalAmount: 0, + originalCurrency: '', + parentTransactionID: '', + posted: '', + receipt: { + receiptID: 7409094723954473, + state: 'SCANCOMPLETE', + source: 'https://www.expensify.com/receipts/w_c989c343d834d48a4e004c38d03c90bff9434768.png', + }, + reimbursable: true, + reportID: '2309609540437471', + status: 'Posted', + tag: '', + transactionID: '1', + hasEReceipt: false, + }, + }, + previousTransactions: { + transactions_1: { + amount: -100, + bank: '', + billable: false, + cardID: 0, + cardName: 'Cash Expense', + cardNumber: '', + category: '', + comment: { + comment: '', + }, + created: '2025-01-08', + currency: 'ETB', + filename: 'w_c989c343d834d48a4e004c38d03c90bff9434768.png', + inserted: '2025-01-08 15:35:32', + managedCard: false, + merchant: 'g', + modifiedAmount: 0, + modifiedCreated: '', + modifiedCurrency: '', + modifiedMerchant: '', + originalAmount: 0, + originalCurrency: '', + parentTransactionID: '', + posted: '', + receipt: { + receiptID: 7409094723954473, + state: 'SCANCOMPLETE', + source: 'https://www.expensify.com/receipts/w_c989c343d834d48a4e004c38d03c90bff9434768.png', + }, + reimbursable: true, + reportID: '2309609540437471', + status: 'Posted', + tag: '', + transactionID: '1', + hasEReceipt: false, + }, + }, + reportActions: { + reportActions_209647397999267: { + 1: { + actionName: 'POLICYCHANGELOG_CORPORATE_UPGRADE', + reportActionID: '1', + created: '', + }, + }, + }, + previousReportActions: { + reportActions_209647397999267: { + 1: { + actionName: 'POLICYCHANGELOG_CORPORATE_UPGRADE', + reportActionID: '1', + created: '', + }, + }, + }, + queryJSON: { + type: 'expense', + status: 'all', + sortBy: 'date', + sortOrder: 'desc', + filters: {operator: 'and', left: 'tag', right: ''}, + inputQuery: 'type:expense status:all sortBy:date sortOrder:desc', + flatFilters: [], + hash: 243428839, + recentSearchHash: 422547233, + }, + offset: 0, + }; + const changedProp: UseSearchHighlightAndScroll = { + ...initialProps, + transactions: { + transactions_2: { + amount: -100, + bank: '', + billable: false, + cardID: 0, + cardName: 'Cash Expense', + cardNumber: '', + category: '', + comment: { + comment: '', + }, + created: '2025-01-08', + currency: 'ETB', + filename: 'w_c989c343d834d48a4e004c38d03c90bff9434768.png', + inserted: '2025-01-08 15:35:32', + managedCard: false, + merchant: 'g', + modifiedAmount: 0, + modifiedCreated: '', + modifiedCurrency: '', + modifiedMerchant: '', + originalAmount: 0, + originalCurrency: '', + parentTransactionID: '', + posted: '', + receipt: { + receiptID: 7409094723954473, + state: 'SCANCOMPLETE', + source: 'https://www.expensify.com/receipts/w_c989c343d834d48a4e004c38d03c90bff9434768.png', + }, + reimbursable: true, + reportID: '2309609540437471', + status: 'Posted', + tag: '', + transactionID: '2', + hasEReceipt: false, + }, + }, + }; + + const {rerender} = renderHook((prop: UseSearchHighlightAndScroll) => useSearchHighlightAndScroll(prop), { + initialProps, + }); + expect(Search.search).not.toHaveBeenCalled(); + + // When the transaction ids list change though it has the same length as previous value + rerender(changedProp); + + // Then Search will be triggerred. + expect(Search.search).toHaveBeenCalled(); + }); +}); From 744f23bb207f8d538588646fbcc9df64736b8127 Mon Sep 17 00:00:00 2001 From: FitseTLT Date: Mon, 13 Jan 2025 18:17:21 +0300 Subject: [PATCH 9/9] minor change --- tests/unit/useSearchHighlightAndScrollTest.ts | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/tests/unit/useSearchHighlightAndScrollTest.ts b/tests/unit/useSearchHighlightAndScrollTest.ts index 9f33cafc99ed..bc8ccd7ecb30 100644 --- a/tests/unit/useSearchHighlightAndScrollTest.ts +++ b/tests/unit/useSearchHighlightAndScrollTest.ts @@ -4,13 +4,7 @@ import useSearchHighlightAndScroll from '@hooks/useSearchHighlightAndScroll'; import type {UseSearchHighlightAndScroll} from '@hooks/useSearchHighlightAndScroll'; import * as Search from '@libs/actions/Search'; -jest.mock('@libs/actions/Search', () => { - const actualModule = jest.requireActual('@libs/actions/Search'); - return { - ...actualModule, - search: jest.fn(), - }; -}); +jest.mock('@libs/actions/Search'); describe('useSearchHighlightAndScroll', () => { it('should trigger Search when transactionIDs list change', () => {