diff --git a/test/use-swr-loading.test.tsx b/test/use-swr-loading.test.tsx index 80f788254..4c7241f68 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 + // eslint-disable-next-line react-hooks/rules-of-hooks + } = 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') + }) })