Skip to content

Commit e7db184

Browse files
sukvvonDogPawHat
authored andcommitted
test(preact-query/suspense): add test cases for 'static' staleTime with number and function (#10134)
1 parent ffc6c4a commit e7db184

File tree

1 file changed

+54
-1
lines changed

1 file changed

+54
-1
lines changed

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

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Suspense } from 'preact/compat'
55
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
66

77
import { QueryClient, QueryClientProvider, useSuspenseQuery } from '..'
8+
import type { StaleTime } from '@tanstack/query-core'
89
import type { QueryKey } from '..'
910

1011
function renderWithSuspense(client: QueryClient, ui: ComponentChildren) {
@@ -18,7 +19,7 @@ function renderWithSuspense(client: QueryClient, ui: ComponentChildren) {
1819
function createTestQuery(options: {
1920
fetchCount: { count: number }
2021
queryKey: QueryKey
21-
staleTime?: number | (() => number)
22+
staleTime?: StaleTime | (() => StaleTime)
2223
}) {
2324
return function TestComponent() {
2425
const { data } = useSuspenseQuery({
@@ -158,6 +159,58 @@ describe('Suspense Timer Tests', () => {
158159
expect(fetchCount.count).toBe(1)
159160
})
160161

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

0 commit comments

Comments
 (0)