mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-10-26 19:01:39 -04:00
app: useSearchChannels follows its caller's tags
The hook read its tag argument only as the initial state, so the Other tab never refetched when the filter modal changed the URL's tags while the tab stayed mounted; the list, including a stale "No Channels", was whatever the mount fetched. The internal tag state now follows the argument's contents, keyed on the serialized list because the caller builds a new array every render. Tests change the hook argument after a loaded result and expect a second search and a reset to pending, check that a new array with the same tags does not refetch, and keep the slow-response drop. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
parent
770e80fa3e
commit
841f8d90f6
2 changed files with 41 additions and 5 deletions
|
|
@ -2107,6 +2107,14 @@ export const useSearchChannels = (defaultTagNames) => {
|
|||
}
|
||||
};
|
||||
|
||||
// The caller's tags come from the URL and change in place while the view stays mounted, so
|
||||
// follow their contents. Keyed on the serialized list because the caller builds a new array
|
||||
// every render; adopting it by reference would refetch forever.
|
||||
const tagKey = JSON.stringify(defaultTagNames || []);
|
||||
useEffect(() => {
|
||||
setTagNames(prev => JSON.stringify(prev) === tagKey ? prev : JSON.parse(tagKey));
|
||||
}, [tagKey]);
|
||||
|
||||
useEffect(() => {
|
||||
localSearchChannels();
|
||||
}, [tagNames]);
|
||||
|
|
|
|||
|
|
@ -482,16 +482,44 @@ describe('useSearchChannels', () => {
|
|||
}
|
||||
});
|
||||
|
||||
test('changing tags resets to pending, and a slow response for the old tags is dropped', async () => {
|
||||
test('a changed tag argument refetches and resets a loaded result to pending', async () => {
|
||||
// The Other tab passes the URL's tags on every render; the filter modal changes them in
|
||||
// place while the tab stays mounted.
|
||||
searchChannels.mockResolvedValue({channels: [{id: 1, name: 'Old'}]});
|
||||
const {result, rerender} = renderHook(({tags}) => useSearchChannels(tags), {initialProps: {tags: ['old']}});
|
||||
await act(async () => {
|
||||
});
|
||||
expect(result.current.channels).toEqual([{id: 1, name: 'Old'}]);
|
||||
expect(searchChannels).toHaveBeenCalledTimes(1);
|
||||
|
||||
searchChannels.mockReturnValue(new Promise(() => {
|
||||
}));
|
||||
rerender({tags: ['new']});
|
||||
expect(searchChannels).toHaveBeenCalledTimes(2);
|
||||
expect(searchChannels).toHaveBeenLastCalledWith(['new']);
|
||||
expect(result.current.channels).toBeNull();
|
||||
expect(result.current.loading).toBe(true);
|
||||
});
|
||||
|
||||
test('a new array with the same tags does not refetch', async () => {
|
||||
searchChannels.mockResolvedValue({channels: []});
|
||||
const {rerender} = renderHook(({tags}) => useSearchChannels(tags), {initialProps: {tags: ['a']}});
|
||||
await act(async () => {
|
||||
});
|
||||
rerender({tags: ['a']});
|
||||
await act(async () => {
|
||||
});
|
||||
expect(searchChannels).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
test('a slow response for the old tags is dropped after the tags change', async () => {
|
||||
let resolveOld;
|
||||
searchChannels.mockReturnValueOnce(new Promise(res => resolveOld = res));
|
||||
const {result} = renderHook(() => useSearchChannels(['old']));
|
||||
const {result, rerender} = renderHook(({tags}) => useSearchChannels(tags), {initialProps: {tags: ['old']}});
|
||||
expect(result.current.loading).toBe(true);
|
||||
|
||||
searchChannels.mockResolvedValue({channels: [{id: 2, name: 'New'}]});
|
||||
act(() => {
|
||||
result.current.setTagNames(['new']);
|
||||
});
|
||||
rerender({tags: ['new']});
|
||||
expect(result.current.channels).toBeNull();
|
||||
expect(result.current.loading).toBe(true);
|
||||
await act(async () => {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue