Skip to content

Commit 2a592d2

Browse files
authored
test(react-query/suspense): add test cases for 'static' staleTime with number and function (#10089)
1 parent 7e3ea62 commit 2a592d2

File tree

1 file changed

+54
-1
lines changed

1 file changed

+54
-1
lines changed

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

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { act, render } from '@testing-library/react'
33
import { Suspense } from 'react'
44
import { queryKey, sleep } from '@tanstack/query-test-utils'
55
import { QueryClient, QueryClientProvider, useSuspenseQuery } from '..'
6+
import type { StaleTime } from '@tanstack/query-core'
67
import type { QueryKey } from '..'
78

89
function renderWithSuspense(client: QueryClient, ui: React.ReactNode) {
@@ -16,7 +17,7 @@ function renderWithSuspense(client: QueryClient, ui: React.ReactNode) {
1617
function createTestQuery(options: {
1718
fetchCount: { count: number }
1819
queryKey: QueryKey
19-
staleTime?: number | (() => number)
20+
staleTime?: StaleTime | (() => StaleTime)
2021
}) {
2122
return function TestComponent() {
2223
const { data } = useSuspenseQuery({
@@ -156,6 +157,58 @@ describe('Suspense Timer Tests', () => {
156157
expect(fetchCount.count).toBe(1)
157158
})
158159

160+
it('should preserve staleTime when value is static', async () => {
161+
const TestComponent = createTestQuery({
162+
fetchCount,
163+
queryKey: queryKey(),
164+
staleTime: 'static',
165+
})
166+
167+
const rendered = renderWithSuspense(queryClient, <TestComponent />)
168+
169+
expect(rendered.getByText('loading')).toBeInTheDocument()
170+
await act(() => vi.advanceTimersByTimeAsync(10))
171+
expect(rendered.getByText('data: data')).toBeInTheDocument()
172+
173+
rendered.rerender(
174+
<QueryClientProvider client={queryClient}>
175+
<Suspense fallback="loading">
176+
<TestComponent />
177+
</Suspense>
178+
</QueryClientProvider>,
179+
)
180+
181+
await act(() => vi.advanceTimersByTimeAsync(2000))
182+
183+
expect(fetchCount.count).toBe(1)
184+
})
185+
186+
it('should preserve staleTime when function returns static', async () => {
187+
const TestComponent = createTestQuery({
188+
fetchCount,
189+
queryKey: queryKey(),
190+
staleTime: () => 'static',
191+
})
192+
193+
const rendered = renderWithSuspense(queryClient, <TestComponent />)
194+
195+
expect(rendered.getByText('loading')).toBeInTheDocument()
196+
await act(() => vi.advanceTimersByTimeAsync(10))
197+
expect(rendered.getByText('data: data')).toBeInTheDocument()
198+
199+
rendered.rerender(
200+
<QueryClientProvider client={queryClient}>
201+
<Suspense fallback="loading">
202+
<TestComponent />
203+
</Suspense>
204+
</QueryClientProvider>,
205+
)
206+
207+
await act(() => vi.advanceTimersByTimeAsync(2000))
208+
209+
expect(fetchCount.count).toBe(1)
210+
})
211+
159212
it('should respect staleTime when function returns value greater than 1000ms', async () => {
160213
const TestComponent = createTestQuery({
161214
fetchCount,

0 commit comments

Comments
 (0)