Skip to content

Commit 38a50cf

Browse files
Improve documentation for issue Expensify#57605:
* Add unit tests * Add warning comment
1 parent 49ed78a commit 38a50cf

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

src/hooks/useSearchHighlightAndScroll.ts

+5
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ function useSearchHighlightAndScroll({searchResults, transactions, previousTrans
5252
const hasTransactionsIDsChange = !isEqual(transactionsIDs, previousTransactionsIDs);
5353
const hasReportActionsIDsChange = !isEqual(reportActionsIDs, previousReportActionsIDs);
5454

55+
// NOTE: This if statement should NOT assume report actions can only change
56+
// in one type, i.e.: isChat && hasReportActionsIDsChange
57+
// because they can also change in other types such as CONST.SEARCH.DATA_TYPES.EXPENSE.
58+
// Assuming they can only change in one type leads to issues such as
59+
// https://github.com/Expensify/App/issues/57605
5560
// Check if there is a change in the transactions or report actions list
5661
if ((!isChat && hasTransactionsIDsChange) || hasReportActionsIDsChange) {
5762
// We only want to highlight new items if the addition of transactions or report actions triggered the search.

tests/unit/useSearchHighlightAndScrollTest.ts

+86
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import * as Search from '@libs/actions/Search';
77
jest.mock('@libs/actions/Search');
88
jest.mock('@src/components/ConfirmedRoute.tsx');
99

10+
afterEach(() => {
11+
jest.clearAllMocks();
12+
});
13+
1014
describe('useSearchHighlightAndScroll', () => {
1115
it('should trigger Search when transactionIDs list change', () => {
1216
const initialProps: UseSearchHighlightAndScroll = {
@@ -189,4 +193,86 @@ describe('useSearchHighlightAndScroll', () => {
189193
// Then Search will be triggerred.
190194
expect(Search.search).toHaveBeenCalled();
191195
});
196+
197+
it('should trigger Search when report actions change', () => {
198+
const initialProps: UseSearchHighlightAndScroll = {
199+
searchResults: {
200+
data: {personalDetailsList: {}},
201+
search: {
202+
columnsToShow: {
203+
shouldShowCategoryColumn: true,
204+
shouldShowTagColumn: true,
205+
shouldShowTaxColumn: true,
206+
},
207+
hasMoreResults: false,
208+
hasResults: true,
209+
offset: 0,
210+
status: 'all',
211+
type: 'expense',
212+
isLoading: false,
213+
},
214+
},
215+
transactions: {},
216+
previousTransactions: {},
217+
reportActions: {
218+
reportActions_209647397999267: {
219+
1: {
220+
actionName: 'POLICYCHANGELOG_CORPORATE_UPGRADE',
221+
reportActionID: '1',
222+
created: '',
223+
},
224+
},
225+
},
226+
previousReportActions: {
227+
reportActions_209647397999267: {
228+
1: {
229+
actionName: 'POLICYCHANGELOG_CORPORATE_UPGRADE',
230+
reportActionID: '1',
231+
created: '',
232+
},
233+
},
234+
},
235+
queryJSON: {
236+
type: 'expense',
237+
status: 'all',
238+
sortBy: 'date',
239+
sortOrder: 'desc',
240+
filters: {operator: 'and', left: 'tag', right: ''},
241+
inputQuery: 'type:expense status:all sortBy:date sortOrder:desc',
242+
flatFilters: [],
243+
hash: 243428839,
244+
recentSearchHash: 422547233,
245+
},
246+
offset: 0,
247+
};
248+
249+
const changedProps: UseSearchHighlightAndScroll = {
250+
...initialProps,
251+
reportActions: {
252+
reportActions_209647397999268: {
253+
1: {
254+
actionName: 'POLICYCHANGELOG_CORPORATE_UPGRADE',
255+
reportActionID: '1',
256+
created: '',
257+
},
258+
2: {
259+
actionName: 'ADDCOMMENT',
260+
reportActionID: '2',
261+
created: '',
262+
},
263+
},
264+
},
265+
};
266+
267+
const {rerender} = renderHook((props: UseSearchHighlightAndScroll) => useSearchHighlightAndScroll(props), {
268+
initialProps,
269+
});
270+
expect(Search.search).not.toHaveBeenCalled();
271+
272+
// When report actions change
273+
rerender(changedProps);
274+
275+
// Then Search will be triggered
276+
expect(Search.search).toHaveBeenCalled();
277+
});
192278
});

0 commit comments

Comments
 (0)