Skip to content
Merged
Show file tree
Hide file tree
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
7 changes: 6 additions & 1 deletion src/tests/suspense.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { render, waitForElement, fireEvent, cleanup } from '@testing-library/react'
import {
render,
waitForElement,
fireEvent,
cleanup,
} from '@testing-library/react'
import * as React from 'react'

import { useQuery, ReactQueryCacheProvider, queryCache } from '../index'
Expand Down
75 changes: 74 additions & 1 deletion src/tests/useQuery.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { render, act, waitForElement, fireEvent, cleanup } from '@testing-library/react'
import {
render,
act,
waitForElement,
fireEvent,
cleanup,
waitForDomChange,
} from '@testing-library/react'
import { ErrorBoundary } from 'react-error-boundary'
import * as React from 'react'

Expand Down Expand Up @@ -998,4 +1005,70 @@ describe('useQuery', () => {

await waitForElement(() => rendered.getByText('rendered'))
})

it('should update data upon interval changes', async () => {
let count = 0
function Page() {
const [int, setInt] = React.useState(200)
const { data } = useQuery('/api', () => count++, {
refetchInterval: int,
})
return (
<div onClick={() => setInt(num => (num < 400 ? num + 100 : 0))}>
count: {data}
</div>
)
}
const { container } = render(<Page />)
expect(container.firstChild.textContent).toEqual('count: ')
await waitForDomChange({ container }) // mount
expect(container.firstChild.textContent).toEqual('count: 0')
await act(() => {
return new Promise(res => setTimeout(res, 210))
})
expect(container.firstChild.textContent).toEqual('count: 1')
await act(() => {
return new Promise(res => setTimeout(res, 50))
})
expect(container.firstChild.textContent).toEqual('count: 1')
await act(() => {
return new Promise(res => setTimeout(res, 150))
})
expect(container.firstChild.textContent).toEqual('count: 2')
await act(() => {
fireEvent.click(container.firstElementChild)
// it will clear 200ms timer and setup a new 300ms timer
return new Promise(res => setTimeout(res, 200))
})
expect(container.firstChild.textContent).toEqual('count: 2')
await act(() => {
return new Promise(res => setTimeout(res, 110))
})
expect(container.firstChild.textContent).toEqual('count: 3')
await act(() => {
// wait for new 300ms timer
return new Promise(res => setTimeout(res, 310))
})
expect(container.firstChild.textContent).toEqual('count: 4')
await act(() => {
fireEvent.click(container.firstElementChild)
// it will clear 300ms timer and setup a new 400ms timer
return new Promise(res => setTimeout(res, 300))
})
expect(container.firstChild.textContent).toEqual('count: 4')
await act(() => {
return new Promise(res => setTimeout(res, 110))
})
expect(container.firstChild.textContent).toEqual('count: 5')
await act(() => {
fireEvent.click(container.firstElementChild)
// it will clear 400ms timer and stop
return new Promise(res => setTimeout(res, 110))
})
expect(container.firstChild.textContent).toEqual('count: 5')
await act(() => {
return new Promise(res => setTimeout(res, 110))
})
expect(container.firstChild.textContent).toEqual('count: 5')
})
})