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