Skip to content

Instantly share code, notes, and snippets.

@promer94
Created December 18, 2019 09:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save promer94/4765055a5cd491ff2ca2c338a4021d1d to your computer and use it in GitHub Desktop.
Save promer94/4765055a5cd491ff2ca2c338a4021d1d to your computer and use it in GitHub Desktop.
it('should update data when polling interval has changed', async () => {
let a = 0
function App() {
const [int, setInt] = React.useState(200)
const { data } = useSWR('/api', () => a++, {
refreshInterval: int,
dedupingInterval: 100
})
return <div onClick={() => setInt(int + 100)}>count: {data}</div>
}
const { container } = render(<App />)
expect(container.firstChild.textContent).toMatchInlineSnapshot(`"count: "`)
await waitForDomChange({ container }) // mount
expect(container.firstChild.textContent).toMatchInlineSnapshot(`"count: 0"`)
await act(() => {
return new Promise(res => setTimeout(res, 210))
}) // update
expect(container.firstChild.textContent).toMatchInlineSnapshot(`"count: 1"`)
await act(() => {
return new Promise(res => setTimeout(res, 50))
}) // update
expect(container.firstChild.textContent).toMatchInlineSnapshot(`"count: 1"`)
await act(() => {
return new Promise(res => setTimeout(res, 150))
}) // update
expect(container.firstChild.textContent).toMatchInlineSnapshot(`"count: 2"`)
await act(() => {
fireEvent.click(container.firstElementChild)
return new Promise(res => setTimeout(res, 200))
})
expect(container.firstChild.textContent).toMatchInlineSnapshot(`"count: 3"`)
await act(() => {
return new Promise(res => setTimeout(res, 200))
}) // update
expect(container.firstChild.textContent).toMatchInlineSnapshot(`"count: 3"`)
await act(() => {
return new Promise(res => setTimeout(res, 100))
}) // update
expect(container.firstChild.textContent).toMatchInlineSnapshot(`"count: 4"`)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment