diff --git a/app/src/hooks/customHooks.js b/app/src/hooks/customHooks.js index 668aab21..8836aab2 100644 --- a/app/src/hooks/customHooks.js +++ b/app/src/hooks/customHooks.js @@ -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]); diff --git a/app/src/hooks/customHooks.test.js b/app/src/hooks/customHooks.test.js index 707a5b63..0df9eaab 100644 --- a/app/src/hooks/customHooks.test.js +++ b/app/src/hooks/customHooks.test.js @@ -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 () => {