Open History to see the last commit
1import { onScopeDispose, ref } from 'vue-demi'
2β
3import {
4 flushPromises,
5 rejectFetcher,
6 simpleFetcher,
7 getSimpleFetcherWithReturnData,
8} from './test-utils'
9import { useQueries } from '../useQueries'
10import { useQueryClient } from '../useQueryClient'
11import { QueryClient } from '../queryClient'
12import { vi } from 'vitest'
13import type { MockedFunction } from 'vitest'
14β
15vi.mock('../useQueryClient')
16β
17describe('useQueries', () => {
18 beforeEach(() => {
19 vi.restoreAllMocks()
20 })
21β
22 test('should return result for each query', () => {
23 const queries = [
24 {
25 queryKey: ['key1'],
26 queryFn: simpleFetcher,
27 },
28 {
29 queryKey: ['key2'],
30 queryFn: simpleFetcher,
31 },
32 ]
33 const queriesState = useQueries({ queries })
34β
35 expect(queriesState.value).toMatchObject([
36 {
37 status: 'pending',
38 isPending: true,
39 isFetching: true,
40 isStale: true,
41 },
42 {
43 status: 'pending',
44 isPending: true,
45 isFetching: true,
46 isStale: true,
47 },
48 ])
49 })
50β
51 test('should resolve to success and update reactive state', async () => {
52 const queries = [
53 {
54 queryKey: ['key11'],
55 queryFn: simpleFetcher,
56 },
57 {
58 queryKey: ['key12'],
59 queryFn: simpleFetcher,
60 },
61 ]
62 const queriesState = useQueries({ queries })
63β
64 await flushPromises()
65β
66 expect(queriesState.value).toMatchObject([
67 {
68 status: 'success',
69 isPending: false,
70 isFetching: false,
71 isStale: true,
72 },
73 {
74 status: 'success',
75 isPending: false,
76 isFetching: false,
77 isStale: true,
78 },
79 ])
80 })
81β
82 test('should reject one of the queries and update reactive state', async () => {
83 const queries = [
84 {
85 queryKey: ['key21'],
86 queryFn: rejectFetcher,
87 },
88 {
89 queryKey: ['key22'],
90 queryFn: simpleFetcher,
91 },
92 ]
93 const queriesState = useQueries({ queries })
94β
95 await flushPromises()
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: getSimpleFetcherWithReturnData('value31'),
118 },
119 {
120 queryKey: ['key32'],
121 queryFn: getSimpleFetcherWithReturnData('value32'),
122 },
123 {
124 queryKey: ['key33'],
125 queryFn: getSimpleFetcherWithReturnData('value33'),
126 },
127 ])
128 const queriesState = useQueries({ queries })
129β
130 await flushPromises()
131β
132 queries.value.splice(
133 0,
134 queries.value.length,
135 {
136 queryKey: ['key31'],
137 queryFn: getSimpleFetcherWithReturnData('value31'),
138 },
139 {
140 queryKey: ['key34'],
141 queryFn: getSimpleFetcherWithReturnData('value34'),
142 },
143 )
144β
145 await flushPromises()
146 await flushPromises()
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: simpleFetcher,
177 },
178 {
179 queryKey: ['key42'],
180 queryFn: simpleFetcher,
181 },
182 ]
183 const queriesState = useQueries({ queries })
184 await flushPromises()
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: simpleFetcher,
208 },
209 {
210 queryKey: ['key42'],
211 queryFn: simpleFetcher,
212 },
213 ]
214β
215 useQueries({ queries }, queryClient)
216 await flushPromises()
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: getSimpleFetcherWithReturnData(firstResult),
230 },
231 {
232 queryKey: ['key42'],
233 queryFn: getSimpleFetcherWithReturnData(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 flushPromises()
250β
251 expect(queriesResult.value).toMatchObject({
252 combined: true,
253 res: [firstResult, secondResult],
254 })
255 })
256})