diff --git a/packages/react-query/src/__tests__/useQuery.test.tsx b/packages/react-query/src/__tests__/useQuery.test.tsx index 3c7f7562a8f..99f2f39d036 100644 --- a/packages/react-query/src/__tests__/useQuery.test.tsx +++ b/packages/react-query/src/__tests__/useQuery.test.tsx @@ -8,6 +8,7 @@ import { sleep, } from '@tanstack/query-test-utils' import { + IsRestoringProvider, QueryCache, QueryClient, dehydrate, @@ -6950,4 +6951,47 @@ describe('useQuery', () => { expect(fetchCount).toBe(initialFetchCount + 1) expect(queryFn).toHaveBeenCalledTimes(2) }) + + it('should not fetch for the duration of the restoring period when isRestoring is true', async () => { + const key = queryKey() + const queryFn = vi + .fn() + .mockImplementation(() => sleep(10).then(() => 'data')) + + function Page() { + const result = useQuery({ + queryKey: key, + queryFn, + }) + + return ( +
+
{result.status}
+
{result.fetchStatus}
+
{result.data ?? 'undefined'}
+
+ ) + } + + const rendered = renderWithClient( + queryClient, + + + , + ) + + await vi.advanceTimersByTimeAsync(0) + + expect(rendered.getByTestId('status')).toHaveTextContent('pending') + expect(rendered.getByTestId('fetchStatus')).toHaveTextContent('idle') + expect(rendered.getByTestId('data')).toHaveTextContent('undefined') + expect(queryFn).toHaveBeenCalledTimes(0) + + await vi.advanceTimersByTimeAsync(11) + + expect(rendered.getByTestId('status')).toHaveTextContent('pending') + expect(rendered.getByTestId('fetchStatus')).toHaveTextContent('idle') + expect(rendered.getByTestId('data')).toHaveTextContent('undefined') + expect(queryFn).toHaveBeenCalledTimes(0) + }) })