Skip to content

Commit 808d84a

Browse files
test(react-query/useQuery): add test for not fetching during restoring period when 'isRestoring' is true (#10166)
* test(react-query/useQuery): add test for not fetching during restoring period when 'isRestoring' is true * ci: apply automated fixes --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent 01ffe4f commit 808d84a

1 file changed

Lines changed: 44 additions & 0 deletions

File tree

packages/react-query/src/__tests__/useQuery.test.tsx

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
sleep,
99
} from '@tanstack/query-test-utils'
1010
import {
11+
IsRestoringProvider,
1112
QueryCache,
1213
QueryClient,
1314
dehydrate,
@@ -6950,4 +6951,47 @@ describe('useQuery', () => {
69506951
expect(fetchCount).toBe(initialFetchCount + 1)
69516952
expect(queryFn).toHaveBeenCalledTimes(2)
69526953
})
6954+
6955+
it('should not fetch for the duration of the restoring period when isRestoring is true', async () => {
6956+
const key = queryKey()
6957+
const queryFn = vi
6958+
.fn()
6959+
.mockImplementation(() => sleep(10).then(() => 'data'))
6960+
6961+
function Page() {
6962+
const result = useQuery({
6963+
queryKey: key,
6964+
queryFn,
6965+
})
6966+
6967+
return (
6968+
<div>
6969+
<div data-testid="status">{result.status}</div>
6970+
<div data-testid="fetchStatus">{result.fetchStatus}</div>
6971+
<div data-testid="data">{result.data ?? 'undefined'}</div>
6972+
</div>
6973+
)
6974+
}
6975+
6976+
const rendered = renderWithClient(
6977+
queryClient,
6978+
<IsRestoringProvider value={true}>
6979+
<Page />
6980+
</IsRestoringProvider>,
6981+
)
6982+
6983+
await vi.advanceTimersByTimeAsync(0)
6984+
6985+
expect(rendered.getByTestId('status')).toHaveTextContent('pending')
6986+
expect(rendered.getByTestId('fetchStatus')).toHaveTextContent('idle')
6987+
expect(rendered.getByTestId('data')).toHaveTextContent('undefined')
6988+
expect(queryFn).toHaveBeenCalledTimes(0)
6989+
6990+
await vi.advanceTimersByTimeAsync(11)
6991+
6992+
expect(rendered.getByTestId('status')).toHaveTextContent('pending')
6993+
expect(rendered.getByTestId('fetchStatus')).toHaveTextContent('idle')
6994+
expect(rendered.getByTestId('data')).toHaveTextContent('undefined')
6995+
expect(queryFn).toHaveBeenCalledTimes(0)
6996+
})
69536997
})

0 commit comments

Comments
 (0)