Skip to content

Commit 2be25ca

Browse files
authored
test(react-query/HydrationBoundary): add tests for 'enabled: false' and 'staleTime: Infinity' (#9969)
* test(react-query/HydrationBoundary): add tests for 'enabled: false' and 'staleTime: Infinity' * chore: trigger CI * test(react-query/HydrationBoundary): add delayed assertions for 'enabled: false' and 'staleTime: Infinity' tests
1 parent e63673b commit 2be25ca

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

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

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,4 +480,64 @@ describe('React hydration', () => {
480480
prefetchQueryClient.clear()
481481
clientQueryClient.clear()
482482
})
483+
484+
test('should not refetch when query has enabled set to false', async () => {
485+
const queryFn = vi.fn()
486+
const queryClient = new QueryClient()
487+
488+
function Page() {
489+
const { data } = useQuery({
490+
queryKey: ['string'],
491+
queryFn,
492+
enabled: false,
493+
})
494+
return <div>{JSON.stringify(data)}</div>
495+
}
496+
497+
const rendered = render(
498+
<QueryClientProvider client={queryClient}>
499+
<HydrationBoundary state={JSON.parse(stringifiedState)}>
500+
<Page />
501+
</HydrationBoundary>
502+
</QueryClientProvider>,
503+
)
504+
505+
expect(rendered.getByText('["stringCached"]')).toBeInTheDocument()
506+
507+
await vi.advanceTimersByTimeAsync(11)
508+
expect(queryFn).toHaveBeenCalledTimes(0)
509+
expect(rendered.getByText('["stringCached"]')).toBeInTheDocument()
510+
511+
queryClient.clear()
512+
})
513+
514+
test('should not refetch when query has staleTime set to Infinity', async () => {
515+
const queryFn = vi.fn()
516+
const queryClient = new QueryClient()
517+
518+
function Page() {
519+
const { data } = useQuery({
520+
queryKey: ['string'],
521+
queryFn,
522+
staleTime: Infinity,
523+
})
524+
return <div>{JSON.stringify(data)}</div>
525+
}
526+
527+
const rendered = render(
528+
<QueryClientProvider client={queryClient}>
529+
<HydrationBoundary state={JSON.parse(stringifiedState)}>
530+
<Page />
531+
</HydrationBoundary>
532+
</QueryClientProvider>,
533+
)
534+
535+
expect(rendered.getByText('["stringCached"]')).toBeInTheDocument()
536+
537+
await vi.advanceTimersByTimeAsync(11)
538+
expect(queryFn).toHaveBeenCalledTimes(0)
539+
expect(rendered.getByText('["stringCached"]')).toBeInTheDocument()
540+
541+
queryClient.clear()
542+
})
483543
})

0 commit comments

Comments
 (0)