Skip to content

Commit 5c73084

Browse files
committed
Add test for refetchInterval
Close #405
1 parent 588d54c commit 5c73084

File tree

1 file changed

+74
-1
lines changed

1 file changed

+74
-1
lines changed

src/tests/useQuery.test.js

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import { render, act, waitForElement, fireEvent } from '@testing-library/react'
1+
import {
2+
render,
3+
act,
4+
waitForElement,
5+
fireEvent,
6+
waitForDomChange,
7+
} from '@testing-library/react'
28
import { ErrorBoundary } from 'react-error-boundary'
39
import * as React from 'react'
410

@@ -865,4 +871,71 @@ describe('useQuery', () => {
865871

866872
await waitForElement(() => rendered.getByText('rendered'))
867873
})
874+
875+
it('should update data upon interval changes', async () => {
876+
let count = 0
877+
function Page() {
878+
const [int, setInt] = React.useState(200)
879+
const { data } = useQuery('/api', () => count++, {
880+
refetchInterval: int,
881+
})
882+
return (
883+
<div onClick={() => setInt(num => (num < 400 ? num + 100 : 0))}>
884+
count: {data}
885+
</div>
886+
)
887+
}
888+
const { container } = render(<Page />)
889+
expect(container.firstChild.textContent).toMatchInlineSnapshot(`"count: "`)
890+
891+
await waitForDomChange({ container }) // mount
892+
expect(container.firstChild.textContent).toMatchInlineSnapshot(`"count: 0"`)
893+
await act(() => {
894+
return new Promise(res => setTimeout(res, 210))
895+
})
896+
expect(container.firstChild.textContent).toMatchInlineSnapshot(`"count: 1"`)
897+
await act(() => {
898+
return new Promise(res => setTimeout(res, 50))
899+
})
900+
expect(container.firstChild.textContent).toMatchInlineSnapshot(`"count: 1"`)
901+
await act(() => {
902+
return new Promise(res => setTimeout(res, 150))
903+
})
904+
expect(container.firstChild.textContent).toMatchInlineSnapshot(`"count: 2"`)
905+
await act(() => {
906+
fireEvent.click(container.firstElementChild)
907+
// it will clear 200ms timer and setup a new 300ms timer
908+
return new Promise(res => setTimeout(res, 200))
909+
})
910+
expect(container.firstChild.textContent).toMatchInlineSnapshot(`"count: 2"`)
911+
await act(() => {
912+
return new Promise(res => setTimeout(res, 110))
913+
})
914+
expect(container.firstChild.textContent).toMatchInlineSnapshot(`"count: 3"`)
915+
await act(() => {
916+
// wait for new 300ms timer
917+
return new Promise(res => setTimeout(res, 310))
918+
})
919+
expect(container.firstChild.textContent).toMatchInlineSnapshot(`"count: 4"`)
920+
await act(() => {
921+
fireEvent.click(container.firstElementChild)
922+
// it will clear 300ms timer and setup a new 400ms timer
923+
return new Promise(res => setTimeout(res, 300))
924+
})
925+
expect(container.firstChild.textContent).toMatchInlineSnapshot(`"count: 4"`)
926+
await act(() => {
927+
return new Promise(res => setTimeout(res, 110))
928+
})
929+
expect(container.firstChild.textContent).toMatchInlineSnapshot(`"count: 5"`)
930+
await act(() => {
931+
fireEvent.click(container.firstElementChild)
932+
// it will clear 400ms timer and stop
933+
return new Promise(res => setTimeout(res, 110))
934+
})
935+
expect(container.firstChild.textContent).toMatchInlineSnapshot(`"count: 5"`)
936+
await act(() => {
937+
return new Promise(res => setTimeout(res, 110))
938+
})
939+
expect(container.firstChild.textContent).toMatchInlineSnapshot(`"count: 5"`)
940+
})
868941
})

0 commit comments

Comments
 (0)