From 20b12c36ab0f2161346b4d6c26db44051eaa9d04 Mon Sep 17 00:00:00 2001 From: Toru Kobayashi Date: Wed, 9 Feb 2022 01:32:24 +0900 Subject: [PATCH] test: add a test for the behavior of revalidateOnMount when the key has been changed --- test/use-swr-integration.test.tsx | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/test/use-swr-integration.test.tsx b/test/use-swr-integration.test.tsx index ef68bd8a4..0143e0970 100644 --- a/test/use-swr-integration.test.tsx +++ b/test/use-swr-integration.test.tsx @@ -72,6 +72,28 @@ describe('useSWR', () => { expect(fetch).not.toHaveBeenCalled() }) + it('should call fetch function when revalidateOnMount is false and key has been changed', async () => { + const fetch = jest.fn(() => 'SWR') + + function Page() { + const [key, setKey] = useState(createKey()) + const { data } = useSWR(key, fetch, { + revalidateOnMount: false + }) + return
setKey(createKey)}>hello,{data}
+ } + + renderWithConfig() + + await screen.findByText('hello,') + expect(fetch).not.toHaveBeenCalled() + + // the key has been changed + fireEvent.click(screen.getByText('hello,')) + + await screen.findByText('hello,SWR') + }) + it('should call fetch function when revalidateOnMount is true even if fallbackData is set', async () => { const fetch = jest.fn(() => 'SWR')