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 race condition test #195

Merged
merged 1 commit into from
Dec 11, 2019
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
35 changes: 35 additions & 0 deletions test/use-swr.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,41 @@ describe('useSWR - revalidate', () => {
})
expect(container.firstChild.textContent).toMatchInlineSnapshot(`"1, 1"`)
})

it('should respect sequences of revalidation calls (cope with race condition)', async () => {
let faster = false

function Page() {
const { data, revalidate } = useSWR(
'race',
() =>
new Promise(res => {
const value = faster ? 1 : 0
setTimeout(() => res(value), faster ? 100 : 200)
})
)

return <button onClick={revalidate}>{data}</button>
}

const { container } = render(<Page />)

expect(container.firstChild.textContent).toMatchInlineSnapshot(`""`)
await waitForDomChange({ container })
expect(container.firstChild.textContent).toMatchInlineSnapshot(`"0"`)

await act(async () => {
// trigger the slower revalidation
faster = false
fireEvent.click(container.firstElementChild)
await new Promise(res => setTimeout(res, 10))
// trigger the faster revalidation
faster = true
fireEvent.click(container.firstElementChild)
return new Promise(res => setTimeout(res, 210))
})
expect(container.firstChild.textContent).toMatchInlineSnapshot(`"1"`)
})
})

describe('useSWR - error', () => {
Expand Down