Open History to see the last commit
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})