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

add test for mutate with async function #739

Merged
merged 1 commit into from
Oct 31, 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
31 changes: 28 additions & 3 deletions test/use-swr.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 <div>data: {data}</div>
Expand All @@ -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
)
Expand All @@ -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 <div>data: {data}</div>
}
const { container } = render(<Page />)

// 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

Expand Down