Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

useQueries.test.ts

371 lines8.2 KB

1import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'
2import { onScopeDispose, ref } from 'vue-demi'
3import { sleep } from '@tanstack/query-test-utils'
4import { useQueries } from '../useQueries'
5import { useQueryClient } from '../useQueryClient'
6import { QueryClient } from '../queryClient'
7import type { MockedFunction } from 'vitest'
8​
9vi.mock('../useQueryClient')
10​
11describe('useQueries', () => {
12 beforeEach(() => {
13 vi.restoreAllMocks()
14 vi.useFakeTimers()
15 })
16​
17 afterEach(() => {
18 vi.useRealTimers()
19 })
20​
21 test('should return result for each query', () => {
22 const queries = [
23 {
24 queryKey: ['key1'],
25 queryFn: () => sleep(0).then(() => 'Some data'),
26 },
27 {
28 queryKey: ['key2'],
29 queryFn: () => sleep(0).then(() => 'Some data'),
30 },
31 ]
32 const queriesState = useQueries({ queries })
33​
34 expect(queriesState.value).toMatchObject([
35 {
36 status: 'pending',
37 isPending: true,
38 isFetching: true,
39 isStale: true,
40 },
41 {
42 status: 'pending',
43 isPending: true,
44 isFetching: true,
45 isStale: true,
46 },
47 ])
48 })
49​
50 test('should resolve to success and update reactive state', async () => {
51 const queries = [
52 {
53 queryKey: ['key11'],
54 queryFn: () => sleep(0).then(() => 'Some data'),
55 },
56 {
57 queryKey: ['key12'],
58 queryFn: () => sleep(0).then(() => 'Some data'),
59 },
60 ]
61 const queriesState = useQueries({ queries })
62​
63 await vi.advanceTimersByTimeAsync(0)
64​
65 expect(queriesState.value).toMatchObject([
66 {
67 status: 'success',
68 isPending: false,
69 isFetching: false,
70 isStale: true,
71 },
72 {
73 status: 'success',
74 isPending: false,
75 isFetching: false,
76 isStale: true,
77 },
78 ])
79 })
80​
81 test('should reject one of the queries and update reactive state', async () => {
82 const queries = [
83 {
84 queryKey: ['key21'],
85 queryFn: () =>
86 sleep(0).then(() => Promise.reject(new Error('Some error'))),
87 },
88 {
89 queryKey: ['key22'],
90 queryFn: () => sleep(0).then(() => 'Some data'),
91 },
92 ]
93 const queriesState = useQueries({ queries })
94​
95 await vi.advanceTimersByTimeAsync(0)
96​
97 expect(queriesState.value).toMatchObject([
98 {
99 status: 'error',
100 isPending: false,
101 isFetching: false,
102 isStale: true,
103 },
104 {
105 status: 'success',
106 isPending: false,
107 isFetching: false,
108 isStale: true,
109 },
110 ])
111 })
112​
113 test('should return state for new queries', async () => {
114 const queries = ref([
115 {
116 queryKey: ['key31'],
117 queryFn: () => sleep(0).then(() => 'value31'),
118 },
119 {
120 queryKey: ['key32'],
121 queryFn: () => sleep(0).then(() => 'value32'),
122 },
123 {
124 queryKey: ['key33'],
125 queryFn: () => sleep(0).then(() => 'value33'),
126 },
127 ])
128 const queriesState = useQueries({ queries })
129​
130 await vi.advanceTimersByTimeAsync(0)
131​
132 queries.value.splice(
133 0,
134 queries.value.length,
135 {
136 queryKey: ['key31'],
137 queryFn: () => sleep(0).then(() => 'value31'),
138 },
139 {
140 queryKey: ['key34'],
141 queryFn: () => sleep(0).then(() => 'value34'),
142 },
143 )
144​
145 await vi.advanceTimersByTimeAsync(0)
146 await vi.advanceTimersByTimeAsync(0)
147​
148 expect(queriesState.value.length).toEqual(2)
149 expect(queriesState.value).toMatchObject([
150 {
151 data: 'value31',
152 status: 'success',
153 isPending: false,
154 isFetching: false,
155 isStale: true,
156 },
157 {
158 data: 'value34',
159 status: 'success',
160 isPending: false,
161 isFetching: false,
162 isStale: true,
163 },
164 ])
165 })
166​
167 test('should stop listening to changes on onScopeDispose', async () => {
168 const onScopeDisposeMock = onScopeDispose as MockedFunction<
169 typeof onScopeDispose
170 >
171 onScopeDisposeMock.mockImplementationOnce((fn) => fn())
172​
173 const queries = [
174 {
175 queryKey: ['key41'],
176 queryFn: () => sleep(0).then(() => 'Some data'),
177 },
178 {
179 queryKey: ['key42'],
180 queryFn: () => sleep(0).then(() => 'Some data'),
181 },
182 ]
183 const queriesState = useQueries({ queries })
184 await vi.advanceTimersByTimeAsync(0)
185​
186 expect(queriesState.value).toMatchObject([
187 {
188 status: 'pending',
189 isPending: true,
190 isFetching: true,
191 isStale: true,
192 },
193 {
194 status: 'pending',
195 isPending: true,
196 isFetching: true,
197 isStale: true,
198 },
199 ])
200 })
201​
202 test('should use queryClient provided via options', async () => {
203 const queryClient = new QueryClient()
204 const queries = [
205 {
206 queryKey: ['key41'],
207 queryFn: () => sleep(0).then(() => 'Some data'),
208 },
209 {
210 queryKey: ['key42'],
211 queryFn: () => sleep(0).then(() => 'Some data'),
212 },
213 ]
214​
215 useQueries({ queries }, queryClient)
216 await vi.advanceTimersByTimeAsync(0)
217​
218 expect(useQueryClient).toHaveBeenCalledTimes(0)
219 })
220​
221 test('should combine queries', async () => {
222 const firstResult = 'first result'
223 const secondResult = 'second result'
224​
225 const queryClient = new QueryClient()
226 const queries = [
227 {
228 queryKey: ['key41'],
229 queryFn: () => sleep(0).then(() => firstResult),
230 },
231 {
232 queryKey: ['key42'],
233 queryFn: () => sleep(0).then(() => secondResult),
234 },
235 ]
236​
237 const queriesResult = useQueries(
238 {
239 queries,
240 combine: (results) => {
241 return {
242 combined: true,
243 res: results.map((res) => res.data),
244 }
245 },
246 },
247 queryClient,
248 )
249 await vi.advanceTimersByTimeAsync(0)
250​
251 expect(queriesResult.value).toMatchObject({
252 combined: true,
253 res: [firstResult, secondResult],
254 })
255 })
256​
257 test('should be `enabled` to accept getter function', async () => {
258 const fetchFn = vi.fn()
259 const checked = ref(false)
260​
261 useQueries({
262 queries: [
263 {
264 queryKey: ['enabled'],
265 queryFn: fetchFn,
266 enabled: () => checked.value,
267 },
268 ],
269 })
270​
271 expect(fetchFn).not.toHaveBeenCalled()
272​
273 checked.value = true
274​
275 await vi.advanceTimersByTimeAsync(0)
276​
277 expect(fetchFn).toHaveBeenCalled()
278 })
279​
280 test('should allow getters for query keys', async () => {
281 const fetchFn = vi.fn()
282 const key1 = ref('key1')
283 const key2 = ref('key2')
284​
285 useQueries({
286 queries: [
287 {
288 queryKey: ['key', () => key1.value, () => key2.value],
289 queryFn: fetchFn,
290 },
291 ],
292 })
293​
294 expect(fetchFn).toHaveBeenCalledTimes(1)
295​
296 key1.value = 'key3'
297​
298 await vi.advanceTimersByTimeAsync(0)
299​
300 expect(fetchFn).toHaveBeenCalledTimes(2)
301​
302 key2.value = 'key4'
303​
304 await vi.advanceTimersByTimeAsync(0)
305​
306 expect(fetchFn).toHaveBeenCalledTimes(3)
307 })
308​
309 test('should allow arbitrarily nested getters for query keys', async () => {
310 const fetchFn = vi.fn()
311 const key1 = ref('key1')
312 const key2 = ref('key2')
313 const key3 = ref('key3')
314 const key4 = ref('key4')
315 const key5 = ref('key5')
316​
317 useQueries({
318 queries: [
319 {
320 queryKey: [
321 'key',
322 key1,
323 () => key2.value,
324 { key: () => key3.value },
325 [{ foo: { bar: () => key4.value } }],
326 () => ({
327 foo: {
328 bar: {
329 baz: () => key5.value,
330 },
331 },
332 }),
333 ],
334 queryFn: fetchFn,
335 },
336 ],
337 })
338​
339 expect(fetchFn).toHaveBeenCalledTimes(1)
340​
341 key1.value = 'key1-updated'
342​
343 await vi.advanceTimersByTimeAsync(0)
344​
345 expect(fetchFn).toHaveBeenCalledTimes(2)
346​
347 key2.value = 'key2-updated'
348​
349 await vi.advanceTimersByTimeAsync(0)
350​
351 expect(fetchFn).toHaveBeenCalledTimes(3)
352​
353 key3.value = 'key3-updated'
354​
355 await vi.advanceTimersByTimeAsync(0)
356​
357 expect(fetchFn).toHaveBeenCalledTimes(4)
358​
359 key4.value = 'key4-updated'
360​
361 await vi.advanceTimersByTimeAsync(0)
362​
363 expect(fetchFn).toHaveBeenCalledTimes(5)
364​
365 key5.value = 'key5-updated'
366​
367 await vi.advanceTimersByTimeAsync(0)
368​
369 expect(fetchFn).toHaveBeenCalledTimes(6)
370 })
371})