diff --git a/test/use-swr.test.tsx b/test/use-swr.test.tsx index 38cb74710..6071d2373 100644 --- a/test/use-swr.test.tsx +++ b/test/use-swr.test.tsx @@ -1412,9 +1412,9 @@ describe('useSWR - local mutation', () => { ) }) - it('should support async mutation', async () => { + it('should support async mutation with promise', async () => { function Page() { - const { data } = useSWR('mutate-1', () => 0, { + const { data } = useSWR('mutate-promise', () => 0, { dedupingInterval: 0 }) return
data: {data}
@@ -1428,7 +1428,7 @@ describe('useSWR - local mutation', () => { await act(() => { // mutate and revalidate return mutate( - 'mutate-1', + 'mutate-promise', new Promise(res => setTimeout(() => res(999), 100)), false ) @@ -1437,6 +1437,31 @@ describe('useSWR - local mutation', () => { expect(container.textContent).toMatchInlineSnapshot(`"data: 999"`) }) + it('should support async mutation with async function', async () => { + function Page() { + const { data } = useSWR('mutate-async-fn', () => 0, { + dedupingInterval: 0 + }) + return
data: {data}
+ } + const { container } = render() + + // hydration + expect(container.textContent).toMatchInlineSnapshot(`"data: "`) + await waitForDomChange({ container }) // mount + expect(container.textContent).toMatchInlineSnapshot(`"data: 0"`) + await act(() => { + // mutate and revalidate + return mutate( + 'mutate-async-fn', + async () => new Promise(res => setTimeout(() => res(999), 100)), + false + ) + }) + await act(() => new Promise(res => setTimeout(res, 110))) + expect(container.textContent).toMatchInlineSnapshot(`"data: 999"`) + }) + it('should trigger on mutation without data', async () => { let value = 0