Skip to content

Commit

Permalink
test: add test case for error updating in cache when mutate (#481)
Browse files Browse the repository at this point in the history
* test: add test case for error updating in cache when mutate

* resolve conflicts
  • Loading branch information
huozhi committed Jun 20, 2020
1 parent 7e45a66 commit c9672ae
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions test/use-swr.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 <div>{error ? error.message : `data: ${data}`}</div>
}
const { container } = render(<Page />)
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', () => {
Expand Down

0 comments on commit c9672ae

Please sign in to comment.