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 expect(fetchCount.count).toBe(1)
82 })
83β
84 it('should enforce minimum staleTime of 1000ms when using suspense with function', async () => {
85 const TestComponent = createTestQuery({
86 fetchCount,
87 queryKey: queryKey(),
88 staleTime: () => 10,
89 })
90β
91 const rendered = renderWithSuspense(queryClient, <TestComponent />)
92β
93 expect(rendered.getByText('loading')).toBeInTheDocument()
94 await vi.advanceTimersByTimeAsync(10)
95 expect(rendered.getByText('data: data')).toBeInTheDocument()
96β
97 rendered.rerender(
98 <QueryClientProvider client={queryClient}>
99 <Suspense fallback="loading">
100 <TestComponent />
101 </Suspense>
102 </QueryClientProvider>,
103 )
104β
105 await vi.advanceTimersByTimeAsync(10)
106 expect(fetchCount.count).toBe(1)
107 })
108β
109 it('should respect staleTime when value is greater than 1000ms', async () => {
110 const TestComponent = createTestQuery({
111 fetchCount,
112 queryKey: queryKey(),
113 staleTime: 2000,
114 })
115β
116 const rendered = renderWithSuspense(queryClient, <TestComponent />)
117β
118 expect(rendered.getByText('loading')).toBeInTheDocument()
119 await vi.advanceTimersByTimeAsync(10)
120 expect(rendered.getByText('data: data')).toBeInTheDocument()
121β
122 rendered.rerender(
123 <QueryClientProvider client={queryClient}>
124 <Suspense fallback="loading">
125 <TestComponent />
126 </Suspense>
127 </QueryClientProvider>,
128 )
129β
130 await vi.advanceTimersByTimeAsync(1500)
131 expect(fetchCount.count).toBe(1)
132 })
133β
134 it('should enforce minimum staleTime when undefined is provided', async () => {
135 const TestComponent = createTestQuery({
136 fetchCount,
137 queryKey: queryKey(),
138 staleTime: undefined,
139 })
140β
141 const rendered = renderWithSuspense(queryClient, <TestComponent />)
142β
143 expect(rendered.getByText('loading')).toBeInTheDocument()
144 await vi.advanceTimersByTimeAsync(10)
145 expect(rendered.getByText('data: data')).toBeInTheDocument()
146β
147 rendered.rerender(
148 <QueryClientProvider client={queryClient}>
149 <Suspense fallback="loading">
150 <TestComponent />
151 </Suspense>
152 </QueryClientProvider>,
153 )
154β
155 await vi.advanceTimersByTimeAsync(500)
156 expect(fetchCount.count).toBe(1)
157 })
158β
159 it('should preserve staleTime when value is static', async () => {
160 const TestComponent = createTestQuery({
161 fetchCount,
162 queryKey: queryKey(),
163 staleTime: 'static',
164 })
165β
166 const rendered = renderWithSuspense(queryClient, <TestComponent />)
167β
168 expect(rendered.getByText('loading')).toBeInTheDocument()
169 await vi.advanceTimersByTimeAsync(10)
170 expect(rendered.getByText('data: data')).toBeInTheDocument()
171β
172 rendered.rerender(
173 <QueryClientProvider client={queryClient}>
174 <Suspense fallback="loading">
175 <TestComponent />
176 </Suspense>
177 </QueryClientProvider>,
178 )
179β
180 await vi.advanceTimersByTimeAsync(2000)
181 expect(fetchCount.count).toBe(1)
182 })
183β
184 it('should preserve staleTime when function returns static', async () => {
185 const TestComponent = createTestQuery({
186 fetchCount,
187 queryKey: queryKey(),
188 staleTime: () => 'static',
189 })
190β
191 const rendered = renderWithSuspense(queryClient, <TestComponent />)
192β
193 expect(rendered.getByText('loading')).toBeInTheDocument()
194 await vi.advanceTimersByTimeAsync(10)
195 expect(rendered.getByText('data: data')).toBeInTheDocument()
196β
197 rendered.rerender(
198 <QueryClientProvider client={queryClient}>
199 <Suspense fallback="loading">
200 <TestComponent />
201 </Suspense>
202 </QueryClientProvider>,
203 )
204β
205 await vi.advanceTimersByTimeAsync(2000)
206 expect(fetchCount.count).toBe(1)
207 })
208β
209 it('should respect staleTime when function returns value greater than 1000ms', async () => {
210 const TestComponent = createTestQuery({
211 fetchCount,
212 queryKey: queryKey(),
213 staleTime: () => 3000,
214 })
215β
216 const rendered = renderWithSuspense(queryClient, <TestComponent />)
217β
218 expect(rendered.getByText('loading')).toBeInTheDocument()
219 await vi.advanceTimersByTimeAsync(10)
220 expect(rendered.getByText('data: data')).toBeInTheDocument()
221β
222 rendered.rerender(
223 <QueryClientProvider client={queryClient}>
224 <Suspense fallback="loading">
225 <TestComponent />
226 </Suspense>
227 </QueryClientProvider>,
228 )
229β
230 await vi.advanceTimersByTimeAsync(2000)
231 expect(fetchCount.count).toBe(1)
232 })
233})