From 90c379727f17c817e6871039be3f5afb50c82789 Mon Sep 17 00:00:00 2001 From: Toru Kobayashi Date: Thu, 1 Sep 2022 01:24:36 +0900 Subject: [PATCH 1/2] test: add more isLoading tests for the case a key is null or an error --- test/use-swr-loading.test.tsx | 43 +++++++++++++++++++++++++++++++---- 1 file changed, 39 insertions(+), 4 deletions(-) diff --git a/test/use-swr-loading.test.tsx b/test/use-swr-loading.test.tsx index 80f788254..c2907ceb5 100644 --- a/test/use-swr-loading.test.tsx +++ b/test/use-swr-loading.test.tsx @@ -321,14 +321,24 @@ describe('useSWR - loading', () => { it('isLoading and isValidating should always respect cache value', async () => { const key = createKey() const Page = () => { - const { data } = useSWR(key, () => createResponse('result', { delay: 10 })) - const { data: response } = useSWR(data, () => createResponse('data', { delay: 10 })) + const { data } = useSWR(key, () => + createResponse('result', { delay: 10 }) + ) + const { data: response } = useSWR(data, () => + createResponse('data', { delay: 10 }) + ) // eslint-disable-next-line react/display-name const Component = ((_: any) => () => { // eslint-disable-next-line react-hooks/rules-of-hooks - const { data: result, isLoading, isValidating } = useSWR(key, () => createResponse('result', { delay: 10 })) + const { + data: result, + isLoading, + isValidating + } = useSWR(key, () => createResponse('result', { delay: 10 })) return ( -
{`result is ${result ? result : 'null'},${isLoading},${isValidating}`}
+
{`result is ${ + result ? result : 'null' + },${isLoading},${isValidating}`}
) })(response) return @@ -337,4 +347,29 @@ describe('useSWR - loading', () => { screen.getByText('result is null,true,true') await screen.findByText('result is result,false,false') }) + + it('isLoading should be false when key is null', () => { + function Page() { + const { isLoading } = useSWR(null, () => 'data') + return
isLoading:{String(isLoading)}
+ } + + renderWithConfig() + screen.getByText('isLoading:false') + }) + + it('isLoading should be false when the key function throws an error', () => { + function Page() { + const { isLoading } = useSWR( + () => { + throw new Error('error') + }, + () => 'data' + ) + return
isLoading:{String(isLoading)}
+ } + + renderWithConfig() + screen.getByText('isLoading:false') + }) }) From e335f0c0b0c06b5ae256ddde5b0841d73a5552ef Mon Sep 17 00:00:00 2001 From: Toru Kobayashi Date: Thu, 1 Sep 2022 01:33:37 +0900 Subject: [PATCH 2/2] fix: lint error caused by format --- test/use-swr-loading.test.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/use-swr-loading.test.tsx b/test/use-swr-loading.test.tsx index c2907ceb5..4c7241f68 100644 --- a/test/use-swr-loading.test.tsx +++ b/test/use-swr-loading.test.tsx @@ -329,11 +329,11 @@ describe('useSWR - loading', () => { ) // eslint-disable-next-line react/display-name const Component = ((_: any) => () => { - // eslint-disable-next-line react-hooks/rules-of-hooks const { data: result, isLoading, isValidating + // eslint-disable-next-line react-hooks/rules-of-hooks } = useSWR(key, () => createResponse('result', { delay: 10 })) return (
{`result is ${