|
1 | | -import { render, act, waitForElement, fireEvent, cleanup } from '@testing-library/react' |
| 1 | +import { |
| 2 | + render, |
| 3 | + act, |
| 4 | + waitForElement, |
| 5 | + fireEvent, |
| 6 | + cleanup, |
| 7 | + waitForDomChange, |
| 8 | +} from '@testing-library/react' |
2 | 9 | import { ErrorBoundary } from 'react-error-boundary' |
3 | 10 | import * as React from 'react' |
4 | 11 |
|
@@ -998,4 +1005,70 @@ describe('useQuery', () => { |
998 | 1005 |
|
999 | 1006 | await waitForElement(() => rendered.getByText('rendered')) |
1000 | 1007 | }) |
| 1008 | + |
| 1009 | + it('should update data upon interval changes', async () => { |
| 1010 | + let count = 0 |
| 1011 | + function Page() { |
| 1012 | + const [int, setInt] = React.useState(200) |
| 1013 | + const { data } = useQuery('/api', () => count++, { |
| 1014 | + refetchInterval: int, |
| 1015 | + }) |
| 1016 | + return ( |
| 1017 | + <div onClick={() => setInt(num => (num < 400 ? num + 100 : 0))}> |
| 1018 | + count: {data} |
| 1019 | + </div> |
| 1020 | + ) |
| 1021 | + } |
| 1022 | + const { container } = render(<Page />) |
| 1023 | + expect(container.firstChild.textContent).toEqual('count: ') |
| 1024 | + await waitForDomChange({ container }) // mount |
| 1025 | + expect(container.firstChild.textContent).toEqual('count: 0') |
| 1026 | + await act(() => { |
| 1027 | + return new Promise(res => setTimeout(res, 210)) |
| 1028 | + }) |
| 1029 | + expect(container.firstChild.textContent).toEqual('count: 1') |
| 1030 | + await act(() => { |
| 1031 | + return new Promise(res => setTimeout(res, 50)) |
| 1032 | + }) |
| 1033 | + expect(container.firstChild.textContent).toEqual('count: 1') |
| 1034 | + await act(() => { |
| 1035 | + return new Promise(res => setTimeout(res, 150)) |
| 1036 | + }) |
| 1037 | + expect(container.firstChild.textContent).toEqual('count: 2') |
| 1038 | + await act(() => { |
| 1039 | + fireEvent.click(container.firstElementChild) |
| 1040 | + // it will clear 200ms timer and setup a new 300ms timer |
| 1041 | + return new Promise(res => setTimeout(res, 200)) |
| 1042 | + }) |
| 1043 | + expect(container.firstChild.textContent).toEqual('count: 2') |
| 1044 | + await act(() => { |
| 1045 | + return new Promise(res => setTimeout(res, 110)) |
| 1046 | + }) |
| 1047 | + expect(container.firstChild.textContent).toEqual('count: 3') |
| 1048 | + await act(() => { |
| 1049 | + // wait for new 300ms timer |
| 1050 | + return new Promise(res => setTimeout(res, 310)) |
| 1051 | + }) |
| 1052 | + expect(container.firstChild.textContent).toEqual('count: 4') |
| 1053 | + await act(() => { |
| 1054 | + fireEvent.click(container.firstElementChild) |
| 1055 | + // it will clear 300ms timer and setup a new 400ms timer |
| 1056 | + return new Promise(res => setTimeout(res, 300)) |
| 1057 | + }) |
| 1058 | + expect(container.firstChild.textContent).toEqual('count: 4') |
| 1059 | + await act(() => { |
| 1060 | + return new Promise(res => setTimeout(res, 110)) |
| 1061 | + }) |
| 1062 | + expect(container.firstChild.textContent).toEqual('count: 5') |
| 1063 | + await act(() => { |
| 1064 | + fireEvent.click(container.firstElementChild) |
| 1065 | + // it will clear 400ms timer and stop |
| 1066 | + return new Promise(res => setTimeout(res, 110)) |
| 1067 | + }) |
| 1068 | + expect(container.firstChild.textContent).toEqual('count: 5') |
| 1069 | + await act(() => { |
| 1070 | + return new Promise(res => setTimeout(res, 110)) |
| 1071 | + }) |
| 1072 | + expect(container.firstChild.textContent).toEqual('count: 5') |
| 1073 | + }) |
1001 | 1074 | }) |
0 commit comments