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