Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add test case for error updating in cache when mutate #481

Merged
merged 2 commits into from
Jun 20, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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