Open History to see the last commit
1import { queryKey, sleep } from '@tanstack/query-test-utils'
2import { render } from '@testing-library/preact'
3import type { ComponentChildren } from 'preact'
4import { Suspense } from 'preact/compat'
5import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
6β
7import { QueryClient, QueryClientProvider, useSuspenseQuery } from '..'
8import type { StaleTime } from '@tanstack/query-core'
9import type { QueryKey } from '..'
10β
11function renderWithSuspense(client: QueryClient, ui: ComponentChildren) {
12 return render(
13 <QueryClientProvider client={client}>
14 <Suspense fallback="loading">{ui}</Suspense>
15 </QueryClientProvider>,
16 )
17}
18β
19function createTestQuery(options: {
20 fetchCount: { count: number }
21 queryKey: QueryKey
22 staleTime?: StaleTime | (() => StaleTime)
23}) {
24 return function TestComponent() {
25 const { data } = useSuspenseQuery({
26 queryKey: options.queryKey,
27 queryFn: () =>
28 sleep(10).then(() => {
29 options.fetchCount.count++
30 return 'data'
31 }),
32 staleTime: options.staleTime,
33 })
34 return <div>data: {data}</div>
35 }
36}
37β
38describe('Suspense Timer Tests', () => {
39 let queryClient: QueryClient
40 let fetchCount: { count: number }
41β
42 beforeEach(() => {
43 vi.useFakeTimers()
44 queryClient = new QueryClient({
45 defaultOptions: {
46 queries: {
47 retry: false,
48 },
49 },
50 })
51 fetchCount = { count: 0 }
52 })
53β
54 afterEach(() => {
55 queryClient.clear()
56 vi.useRealTimers()
57 })
58β
59 it('should enforce minimum staleTime of 1000ms when using suspense with number', async () => {
60 const TestComponent = createTestQuery({
61 fetchCount,
62 queryKey: queryKey(),
63 staleTime: 10,
64 })
65β
66 const rendered = renderWithSuspense(queryClient, <TestComponent />)
67β
68 expect(rendered.getByText('loading')).toBeInTheDocument()
69 await vi.advanceTimersByTimeAsync(10)
70 expect(rendered.getByText('data: data')).toBeInTheDocument()
71β
72 rendered.rerender(
73 <QueryClientProvider client={queryClient}>
74 <Suspense fallback="loading">
75 <TestComponent />
76 </Suspense>
77 </QueryClientProvider>,
78 )
79β
80 await vi.advanceTimersByTimeAsync(10)
81β
82 expect(fetchCount.count).toBe(1)
83 })
84β
85 it('should enforce minimum staleTime of 1000ms when using suspense with function', async () => {
86 const TestComponent = createTestQuery({
87 fetchCount,
88 queryKey: queryKey(),
89 staleTime: () => 10,
90 })
91β
92 const rendered = renderWithSuspense(queryClient, <TestComponent />)
93β
94 expect(rendered.getByText('loading')).toBeInTheDocument()
95 await vi.advanceTimersByTimeAsync(10)
96 expect(rendered.getByText('data: data')).toBeInTheDocument()
97β
98 rendered.rerender(
99 <QueryClientProvider client={queryClient}>
100 <Suspense fallback="loading">
101 <TestComponent />
102 </Suspense>
103 </QueryClientProvider>,
104 )
105β
106 await vi.advanceTimersByTimeAsync(10)
107β
108 expect(fetchCount.count).toBe(1)
109 })
110β
111 it('should respect staleTime when value is greater than 1000ms', async () => {
112 const TestComponent = createTestQuery({
113 fetchCount,
114 queryKey: queryKey(),
115 staleTime: 2000,
116 })
117β
118 const rendered = renderWithSuspense(queryClient, <TestComponent />)
119β
120 expect(rendered.getByText('loading')).toBeInTheDocument()
121 await vi.advanceTimersByTimeAsync(10)
122 expect(rendered.getByText('data: data')).toBeInTheDocument()
123β
124 rendered.rerender(
125 <QueryClientProvider client={queryClient}>
126 <Suspense fallback="loading">
127 <TestComponent />
128 </Suspense>
129 </QueryClientProvider>,
130 )
131β
132 await vi.advanceTimersByTimeAsync(1500)
133β
134 expect(fetchCount.count).toBe(1)
135 })
136β
137 it('should enforce minimum staleTime when undefined is provided', async () => {
138 const TestComponent = createTestQuery({
139 fetchCount,
140 queryKey: queryKey(),
141 staleTime: undefined,
142 })
143β
144 const rendered = renderWithSuspense(queryClient, <TestComponent />)
145β
146 expect(rendered.getByText('loading')).toBeInTheDocument()
147 await vi.advanceTimersByTimeAsync(10)
148 expect(rendered.getByText('data: data')).toBeInTheDocument()
149β
150 rendered.rerender(
151 <QueryClientProvider client={queryClient}>
152 <Suspense fallback="loading">
153 <TestComponent />
154 </Suspense>
155 </QueryClientProvider>,
156 )
157β
158 await vi.advanceTimersByTimeAsync(500)
159β
160 expect(fetchCount.count).toBe(1)
161 })
162β
163 it('should preserve staleTime when value is static', async () => {
164 const TestComponent = createTestQuery({
165 fetchCount,
166 queryKey: queryKey(),
167 staleTime: 'static',
168 })
169β
170 const rendered = renderWithSuspense(queryClient, <TestComponent />)
171β
172 expect(rendered.getByText('loading')).toBeInTheDocument()
173 await vi.advanceTimersByTimeAsync(10)
174 expect(rendered.getByText('data: data')).toBeInTheDocument()
175β
176 rendered.rerender(
177 <QueryClientProvider client={queryClient}>
178 <Suspense fallback="loading">
179 <TestComponent />
180 </Suspense>
181 </QueryClientProvider>,
182 )
183β
184 await vi.advanceTimersByTimeAsync(2000)
185β
186 expect(fetchCount.count).toBe(1)
187 })
188β
189 it('should preserve staleTime when function returns static', async () => {
190 const TestComponent = createTestQuery({
191 fetchCount,
192 queryKey: queryKey(),
193 staleTime: () => 'static',
194 })
195β
196 const rendered = renderWithSuspense(queryClient, <TestComponent />)
197β
198 expect(rendered.getByText('loading')).toBeInTheDocument()
199 await vi.advanceTimersByTimeAsync(10)
200 expect(rendered.getByText('data: data')).toBeInTheDocument()
201β
202 rendered.rerender(
203 <QueryClientProvider client={queryClient}>
204 <Suspense fallback="loading">
205 <TestComponent />
206 </Suspense>
207 </QueryClientProvider>,
208 )
209β
210 await vi.advanceTimersByTimeAsync(2000)
211β
212 expect(fetchCount.count).toBe(1)
213 })
214β
215 it('should respect staleTime when function returns value greater than 1000ms', async () => {
216 const TestComponent = createTestQuery({
217 fetchCount,
218 queryKey: queryKey(),
219 staleTime: () => 3000,
220 })
221β
222 const rendered = renderWithSuspense(queryClient, <TestComponent />)
223β
224 expect(rendered.getByText('loading')).toBeInTheDocument()
225 await vi.advanceTimersByTimeAsync(10)
226 expect(rendered.getByText('data: data')).toBeInTheDocument()
227β
228 rendered.rerender(
229 <QueryClientProvider client={queryClient}>
230 <Suspense fallback="loading">
231 <TestComponent />
232 </Suspense>
233 </QueryClientProvider>,
234 )
235β
236 await vi.advanceTimersByTimeAsync(2000)
237β
238 expect(fetchCount.count).toBe(1)
239 })
240})