Repository navigation
Move debounce from SelectionListWithModal to useSearchResults
#90361
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
mountiny
merged 5 commits into
Expensify:main
from
callstack-internal:VickyStash/bugfix/90206-update-SelectionListWithModal
May 14, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8cbb8f1
Move debounce from SelectionListWithModal to useSearchResults
VickyStash a54a2f8
Lint fixes
VickyStash 57c0536
Merge branch 'main' into VickyStash/bugfix/90206-update-SelectionList…
VickyStash 5201a4b
Add useSearchResults tests
VickyStash d8e86e0
Re-run checks
VickyStash File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| import {act, renderHook} from '@testing-library/react-native'; | ||
| import useSearchResults from '@hooks/useSearchResults'; | ||
| import CONST from '@src/CONST'; | ||
|
|
||
| type Item = {id: number; name: string}; | ||
|
|
||
| const buildItems = (count: number, prefix = 'item'): Item[] => Array.from({length: count}, (_, i) => ({id: i, name: `${prefix}-${i}`})); | ||
|
|
||
| const filterByName = (item: Item, query: string) => item.name.toLowerCase().includes(query); | ||
|
|
||
| describe('useSearchResults', () => { | ||
| beforeEach(() => { | ||
| jest.useFakeTimers(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| jest.clearAllTimers(); | ||
| jest.useRealTimers(); | ||
| }); | ||
|
|
||
| it('returns the full list when the input is empty', () => { | ||
| const data = buildItems(5); | ||
| const {result} = renderHook(() => useSearchResults(data, filterByName)); | ||
|
|
||
| expect(result.current[0]).toBe(''); | ||
| expect(result.current[2]).toEqual(data); | ||
| }); | ||
|
|
||
| it('updates the input value immediately but defers filtering until the debounce settles', () => { | ||
| const data = buildItems(5); | ||
| const {result} = renderHook(() => useSearchResults(data, filterByName)); | ||
|
|
||
| act(() => { | ||
| result.current[1]('item-2'); | ||
| }); | ||
|
|
||
| // The typed value is visible right away but the filtered result still reflects the full list. | ||
| expect(result.current[0]).toBe('item-2'); | ||
| expect(result.current[2]).toEqual(data); | ||
|
|
||
| act(() => { | ||
| jest.advanceTimersByTime(CONST.TIMING.SEARCH_OPTION_LIST_DEBOUNCE_TIME); | ||
| }); | ||
|
|
||
| expect(result.current[2]).toEqual([{id: 2, name: 'item-2'}]); | ||
| }); | ||
|
|
||
| it('restores the full list instantly when the input is cleared, without waiting for the debounce', () => { | ||
| const data = buildItems(5); | ||
| const {result} = renderHook(() => useSearchResults(data, filterByName)); | ||
|
|
||
| act(() => { | ||
| result.current[1]('item-2'); | ||
| jest.advanceTimersByTime(CONST.TIMING.SEARCH_OPTION_LIST_DEBOUNCE_TIME); | ||
| }); | ||
| expect(result.current[2]).toHaveLength(1); | ||
|
|
||
| act(() => { | ||
| result.current[1](''); | ||
| }); | ||
| expect(result.current[2]).toEqual(data); | ||
| }); | ||
|
|
||
| it('applies the sort comparator to the filtered results', () => { | ||
| const data: Item[] = [ | ||
| {id: 1, name: 'banana'}, | ||
| {id: 2, name: 'apple'}, | ||
| {id: 3, name: 'cherry'}, | ||
| ]; | ||
| const sortAlpha = (items: Item[]) => [...items].sort((a, b) => a.name.localeCompare(b.name)); | ||
| const {result} = renderHook(() => useSearchResults(data, filterByName, sortAlpha)); | ||
|
|
||
| expect(result.current[2].map((i) => i.name)).toEqual(['apple', 'banana', 'cherry']); | ||
| }); | ||
|
|
||
| it('applies the preFilter before the text filter', () => { | ||
| const data: Item[] = [ | ||
| {id: 1, name: 'apple-good'}, | ||
| {id: 2, name: 'apple-bad'}, | ||
| {id: 3, name: 'banana-good'}, | ||
| ]; | ||
| const preFilter = (item: Item) => item.name.endsWith('-good'); | ||
| const {result} = renderHook(() => useSearchResults(data, filterByName, undefined, preFilter)); | ||
|
|
||
| expect(result.current[2].map((i) => i.id)).toEqual([1, 3]); | ||
|
|
||
| act(() => { | ||
| result.current[1]('apple'); | ||
| jest.advanceTimersByTime(CONST.TIMING.SEARCH_OPTION_LIST_DEBOUNCE_TIME); | ||
| }); | ||
| expect(result.current[2].map((i) => i.id)).toEqual([1]); | ||
| }); | ||
|
|
||
| describe('auto-clear when data crosses SEARCH_ITEM_LIMIT', () => { | ||
| const aboveLimit = CONST.SEARCH_ITEM_LIMIT + 5; | ||
| const belowLimit = CONST.SEARCH_ITEM_LIMIT - 5; | ||
|
|
||
| it('clears the input when the dataset shrinks from above the limit to at or below the limit', () => { | ||
| const {result, rerender} = renderHook(({data}) => useSearchResults(data, filterByName), { | ||
| initialProps: {data: buildItems(aboveLimit)}, | ||
| }); | ||
|
|
||
| act(() => { | ||
| result.current[1]('item'); | ||
| jest.advanceTimersByTime(CONST.TIMING.SEARCH_OPTION_LIST_DEBOUNCE_TIME); | ||
| }); | ||
| expect(result.current[0]).toBe('item'); | ||
|
|
||
| rerender({data: buildItems(belowLimit)}); | ||
|
|
||
| // The search bar is no longer shown for small datasets, so any leftover query must be wiped. | ||
| expect(result.current[0]).toBe(''); | ||
| }); | ||
|
|
||
| it('does not clear the input when the dataset stays above the limit', () => { | ||
| const {result, rerender} = renderHook(({data}) => useSearchResults(data, filterByName), { | ||
| initialProps: {data: buildItems(aboveLimit)}, | ||
| }); | ||
|
|
||
| act(() => { | ||
| result.current[1]('item'); | ||
| jest.advanceTimersByTime(CONST.TIMING.SEARCH_OPTION_LIST_DEBOUNCE_TIME); | ||
| }); | ||
|
|
||
| rerender({data: buildItems(aboveLimit + 1)}); | ||
|
|
||
| expect(result.current[0]).toBe('item'); | ||
| }); | ||
|
|
||
| it('does not clear the input when the dataset stays at or below the limit', () => { | ||
| const {result, rerender} = renderHook(({data}) => useSearchResults(data, filterByName), { | ||
| initialProps: {data: buildItems(belowLimit)}, | ||
| }); | ||
|
|
||
| act(() => { | ||
| result.current[1]('item'); | ||
| jest.advanceTimersByTime(CONST.TIMING.SEARCH_OPTION_LIST_DEBOUNCE_TIME); | ||
| }); | ||
|
|
||
| rerender({data: buildItems(belowLimit - 1)}); | ||
|
|
||
| expect(result.current[0]).toBe('item'); | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.