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})