From c9672aebaaa71829c44a57f1a1641710de922f94 Mon Sep 17 00:00:00 2001 From: Jiachi Date: Sun, 21 Jun 2020 02:45:16 +0800 Subject: [PATCH] test: add test case for error updating in cache when mutate (#481) * test: add test case for error updating in cache when mutate * resolve conflicts --- test/use-swr.test.tsx | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/test/use-swr.test.tsx b/test/use-swr.test.tsx index ef9e509ec..3c84e1412 100644 --- a/test/use-swr.test.tsx +++ b/test/use-swr.test.tsx @@ -1262,6 +1262,35 @@ describe('useSWR - local mutation', () => { expect(promise).resolves.toBe(1) // the return value should be the new cache expect(container.firstChild.textContent).toMatchInlineSnapshot(`"data: 1"`) }) + + it('should update error in cache when mutate failed with error', async () => { + let value = 0 + const key = 'mutate-4' + const message = 'mutate-error' + function Page() { + const { data, error } = useSWR(key, () => value) + return
{error ? error.message : `data: ${data}`}
+ } + const { container } = render() + await waitForDomChange({ container }) + expect(container.firstChild.textContent).toMatchInlineSnapshot(`"data: 0"`) + await act(async () => { + await mutate( + key, + () => { + throw new Error(message) + }, + false + ) + }) + + const [, , keyErr] = cache.serializeKey(key) + const error = cache.get(keyErr) + expect(error.message).toMatchInlineSnapshot(`"${message}"`) + expect(container.firstChild.textContent).toMatchInlineSnapshot( + `"${message}"` + ) + }) }) describe('useSWR - context configs', () => {