Repository navigation
fix: Directory list drops, repeats and mixes rows when paging #7760
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
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3ada1a6
fix: Directory list drops, repeats and mixes rows when paging
diegolmello 2b41495
chore: remove inaccurate comment on Directory initial search effect
diegolmello 621e619
fix: Directory drops repeated rooms within a page and stale pages dur…
diegolmello 0c5bef5
fix: hide previous Directory results while a new search is pending
diegolmello 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
There are no files selected for viewing
188 changes: 188 additions & 0 deletions
188
app/views/DirectoryView/hooks/__tests__/useDirectorySearch.test.ts
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,188 @@ | ||
| import { act, renderHook, waitFor } from '@testing-library/react-native'; | ||
|
|
||
| import { getDirectory } from '~/lib/services/restApi'; | ||
| import { useDirectorySearch } from '../useDirectorySearch'; | ||
|
|
||
| jest.mock('~/lib/services/restApi', () => ({ getDirectory: jest.fn() })); | ||
|
|
||
| const mockGetDirectory = getDirectory as jest.MockedFunction<typeof getDirectory>; | ||
|
|
||
| const rooms = (...ids: string[]) => ids.map(_id => ({ _id, name: _id })); | ||
|
|
||
| const pageResponse = (result: ReturnType<typeof rooms>, total: number) => | ||
| ({ success: true, result, total, count: result.length, offset: 0 }) as never; | ||
|
|
||
| const respondWith = (result: ReturnType<typeof rooms>, total: number) => | ||
| mockGetDirectory.mockResolvedValueOnce(pageResponse(result, total)); | ||
|
|
||
| const respondLater = () => { | ||
| let respond: (result: ReturnType<typeof rooms>, total: number) => void = () => {}; | ||
| mockGetDirectory.mockReturnValueOnce( | ||
| new Promise(resolve => { | ||
| respond = (result, total) => resolve(pageResponse(result, total)); | ||
| }) | ||
| ); | ||
| return (result: ReturnType<typeof rooms>, total: number) => respond(result, total); | ||
| }; | ||
|
|
||
| const runDebounced = async (trigger: () => void) => { | ||
| await act(async () => { | ||
| trigger(); | ||
| await jest.runOnlyPendingTimersAsync(); | ||
| }); | ||
| }; | ||
|
|
||
| const roomIds = (rows: { _id: string }[]) => rows.map(room => room._id); | ||
|
|
||
| describe('useDirectorySearch', () => { | ||
| beforeEach(() => { | ||
| jest.useFakeTimers(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| jest.useRealTimers(); | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| it('keeps one row per room when pages overlap', async () => { | ||
| respondWith(rooms('a', 'b', 'c'), 5); | ||
| const { result } = renderHook(() => useDirectorySearch('channels')); | ||
| await waitFor(() => expect(result.current.data).toHaveLength(3)); | ||
|
|
||
| respondWith(rooms('c', 'd', 'e'), 5); | ||
| await runDebounced(result.current.loadMore); | ||
|
|
||
| expect(roomIds(result.current.data)).toEqual(['a', 'b', 'c', 'd', 'e']); | ||
| }); | ||
|
|
||
| it('keeps one row per room when a page repeats a room', async () => { | ||
| respondWith(rooms('a', 'a', 'b'), 5); | ||
| const { result } = renderHook(() => useDirectorySearch('channels')); | ||
| await waitFor(() => expect(result.current.data).toHaveLength(2)); | ||
| expect(roomIds(result.current.data)).toEqual(['a', 'b']); | ||
|
|
||
| respondWith(rooms('c', 'd'), 5); | ||
| await runDebounced(result.current.loadMore); | ||
|
|
||
| expect(mockGetDirectory).toHaveBeenLastCalledWith(expect.objectContaining({ offset: 3 })); | ||
| expect(roomIds(result.current.data)).toEqual(['a', 'b', 'c', 'd']); | ||
| }); | ||
|
|
||
| it('requests the next page after every row the server returned', async () => { | ||
| respondWith(rooms('a', 'b', 'c'), 7); | ||
| const { result } = renderHook(() => useDirectorySearch('channels')); | ||
| await waitFor(() => expect(result.current.data).toHaveLength(3)); | ||
|
|
||
| respondWith(rooms('c', 'd', 'e'), 7); | ||
| await runDebounced(result.current.loadMore); | ||
| expect(result.current.data).toHaveLength(5); | ||
|
|
||
| respondWith(rooms('f'), 7); | ||
| await runDebounced(result.current.loadMore); | ||
|
|
||
| expect(mockGetDirectory).toHaveBeenLastCalledWith(expect.objectContaining({ offset: 6 })); | ||
| expect(result.current.data).toHaveLength(6); | ||
| }); | ||
|
|
||
| it('stops loading more once every server row was fetched', async () => { | ||
| respondWith(rooms('a', 'b'), 3); | ||
| const { result } = renderHook(() => useDirectorySearch('channels')); | ||
| await waitFor(() => expect(result.current.data).toHaveLength(2)); | ||
|
|
||
| respondWith(rooms('b'), 3); | ||
| await runDebounced(result.current.loadMore); | ||
| expect(mockGetDirectory).toHaveBeenCalledTimes(2); | ||
|
|
||
| await runDebounced(result.current.loadMore); | ||
|
|
||
| expect(mockGetDirectory).toHaveBeenCalledTimes(2); | ||
| }); | ||
|
|
||
| it('runs a new search even when the list asks for more rows right after it', async () => { | ||
| respondWith(rooms('a', 'b'), 10); | ||
| const { result } = renderHook(() => useDirectorySearch('channels')); | ||
| await waitFor(() => expect(result.current.data).toHaveLength(2)); | ||
|
|
||
| respondWith(rooms('x'), 1); | ||
| act(() => result.current.onSearchChangeText('x')); | ||
| await runDebounced(result.current.loadMore); | ||
|
|
||
| expect(mockGetDirectory).toHaveBeenLastCalledWith(expect.objectContaining({ text: 'x', offset: 0 })); | ||
| expect(roomIds(result.current.data)).toEqual(['x']); | ||
| }); | ||
|
|
||
| it('drops a page that arrives after a newer search started', async () => { | ||
| respondWith(rooms('a', 'b'), 10); | ||
| const { result } = renderHook(() => useDirectorySearch('channels')); | ||
| await waitFor(() => expect(result.current.data).toHaveLength(2)); | ||
|
|
||
| const respondToLoadMore = respondLater(); | ||
| await runDebounced(result.current.loadMore); | ||
|
|
||
| respondWith(rooms('x'), 1); | ||
| await runDebounced(() => result.current.onSearchChangeText('x')); | ||
| expect(roomIds(result.current.data)).toEqual(['x']); | ||
|
|
||
| await act(async () => { | ||
| respondToLoadMore(rooms('c', 'd'), 10); | ||
| await jest.runOnlyPendingTimersAsync(); | ||
| }); | ||
|
|
||
| expect(roomIds(result.current.data)).toEqual(['x']); | ||
| }); | ||
|
|
||
| it('drops a page that arrives while a newer search waits for its debounce', async () => { | ||
| respondWith(rooms('a', 'b'), 10); | ||
| const { result } = renderHook(() => useDirectorySearch('channels')); | ||
| await waitFor(() => expect(result.current.data).toHaveLength(2)); | ||
|
|
||
| const respondToLoadMore = respondLater(); | ||
| await runDebounced(result.current.loadMore); | ||
|
|
||
| act(() => result.current.onSearchChangeText('x')); | ||
| await act(async () => { | ||
| respondToLoadMore(rooms('c', 'd'), 10); | ||
| }); | ||
| expect(result.current.data).toEqual([]); | ||
|
|
||
| respondWith(rooms('x'), 1); | ||
| await act(async () => { | ||
| await jest.runOnlyPendingTimersAsync(); | ||
| }); | ||
|
|
||
| expect(mockGetDirectory).toHaveBeenLastCalledWith(expect.objectContaining({ text: 'x', offset: 0 })); | ||
| expect(roomIds(result.current.data)).toEqual(['x']); | ||
| expect(result.current.loading).toBe(false); | ||
| }); | ||
|
|
||
| it('hides the previous type rows while the new type waits for its debounce', async () => { | ||
| respondWith(rooms('a', 'b'), 2); | ||
| const { result } = renderHook(() => useDirectorySearch('channels')); | ||
| await waitFor(() => expect(result.current.data).toHaveLength(2)); | ||
|
|
||
| act(() => result.current.changeType('users')); | ||
|
|
||
| expect(result.current.type).toBe('users'); | ||
| expect(result.current.data).toEqual([]); | ||
| expect(result.current.loading).toBe(true); | ||
|
|
||
| respondWith(rooms('u'), 1); | ||
| await act(async () => { | ||
| await jest.runOnlyPendingTimersAsync(); | ||
| }); | ||
|
|
||
| expect(mockGetDirectory).toHaveBeenLastCalledWith(expect.objectContaining({ type: 'users', offset: 0 })); | ||
| expect(roomIds(result.current.data)).toEqual(['u']); | ||
| expect(result.current.loading).toBe(false); | ||
| }); | ||
|
|
||
| it('hides the previous search rows while the new text waits for its debounce', async () => { | ||
| respondWith(rooms('a', 'b'), 2); | ||
| const { result } = renderHook(() => useDirectorySearch('channels')); | ||
| await waitFor(() => expect(result.current.data).toHaveLength(2)); | ||
|
|
||
| act(() => result.current.onSearchChangeText('')); | ||
|
|
||
| expect(result.current.data).toEqual([]); | ||
| }); | ||
| }); |
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
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.