Open History to see the last commit
1import { onScopeDispose, reactive, ref } from 'vue-demi'
2β
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'
12β
13jest.mock('../useQueryClient')
14β
15describe('useQueries', () => {
16 test('should return result for each query', () => {
17 const queries = [
18 {
19 queryKey: ['key1'],
20 queryFn: simpleFetcher,
21 },
22 {
23 queryKey: ['key2'],
24 queryFn: simpleFetcher,
25 },
26 ]
27 const queriesState = useQueries({ queries })
28β
29 expect(queriesState).toMatchObject([
30 {
31 status: 'loading',
32 isLoading: true,
33 isFetching: true,
34 isStale: true,
35 },
36 {
37 status: 'loading',
38 isLoading: 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: simpleFetcher,
50 },
51 {
52 queryKey: ['key12'],
53 queryFn: simpleFetcher,
54 },
55 ]
56 const queriesState = useQueries({ queries })
57β
58 await flushPromises()
59β
60 expect(queriesState).toMatchObject([
61 {
62 status: 'success',
63 isLoading: false,
64 isFetching: false,
65 isStale: true,
66 },
67 {
68 status: 'success',
69 isLoading: 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: rejectFetcher,
81 },
82 {
83 queryKey: ['key22'],
84 queryFn: simpleFetcher,
85 },
86 ]
87 const queriesState = useQueries({ queries })
88β
89 await flushPromises()
90β
91 expect(queriesState).toMatchObject([
92 {
93 status: 'error',
94 isLoading: false,
95 isFetching: false,
96 isStale: true,
97 },
98 {
99 status: 'success',
100 isLoading: false,
101 isFetching: false,
102 isStale: true,
103 },
104 ])
105 })
106β
107 test('should return state for new queries', async () => {
108 const queries = reactive([
109 {
110 queryKey: ['key31'],
111 queryFn: getSimpleFetcherWithReturnData('value31'),
112 },
113 {
114 queryKey: ['key32'],
115 queryFn: getSimpleFetcherWithReturnData('value32'),
116 },
117 {
118 queryKey: ['key33'],
119 queryFn: getSimpleFetcherWithReturnData('value33'),
120 },
121 ])
122 const queriesState = useQueries({ queries })
123β
124 await flushPromises()
125β
126 queries.splice(
127 0,
128 queries.length,
129 {
130 queryKey: ['key31'],
131 queryFn: getSimpleFetcherWithReturnData('value31'),
132 },
133 {
134 queryKey: ['key34'],
135 queryFn: getSimpleFetcherWithReturnData('value34'),
136 },
137 )
138β
139 await flushPromises()
140 await flushPromises()
141β
142 expect(queriesState.length).toEqual(2)
143 expect(queriesState).toMatchObject([
144 {
145 data: 'value31',
146 status: 'success',
147 isLoading: false,
148 isFetching: false,
149 isStale: true,
150 },
151 {
152 data: 'value34',
153 status: 'success',
154 isLoading: false,
155 isFetching: false,
156 isStale: true,
157 },
158 ])
159 })
160β
161 test('should stop listening to changes on onScopeDispose', async () => {
162 const onScopeDisposeMock = onScopeDispose as jest.MockedFunction<
163 typeof onScopeDispose
164 >
165 onScopeDisposeMock.mockImplementationOnce((fn) => fn())
166β
167 const queries = [
168 {
169 queryKey: ['key41'],
170 queryFn: simpleFetcher,
171 },
172 {
173 queryKey: ['key42'],
174 queryFn: simpleFetcher,
175 },
176 ]
177 const queriesState = useQueries({ queries })
178 await flushPromises()
179β
180 expect(queriesState).toMatchObject([
181 {
182 status: 'loading',
183 isLoading: true,
184 isFetching: true,
185 isStale: true,
186 },
187 {
188 status: 'loading',
189 isLoading: true,
190 isFetching: true,
191 isStale: true,
192 },
193 ])
194 })
195β
196 test('should use queryClient provided via options', async () => {
197 const queryClient = new QueryClient()
198 const queries = [
199 {
200 queryKey: ['key41'],
201 queryFn: simpleFetcher,
202 },
203 {
204 queryKey: ['key42'],
205 queryFn: simpleFetcher,
206 },
207 ]
208β
209 useQueries({ queries, queryClient })
210 await flushPromises()
211β
212 expect(useQueryClient).toHaveBeenCalledTimes(0)
213 })
214β
215 test('should use queryClient provided via query options', async () => {
216 const queryClient = new QueryClient()
217 const queries = [
218 {
219 queryKey: ['key41'],
220 queryFn: simpleFetcher,
221 queryClient,
222 },
223 {
224 queryKey: ['key42'],
225 queryFn: simpleFetcher,
226 },
227 ]
228β
229 useQueries({ queries })
230 await flushPromises()
231β
232 expect(useQueryClient).toHaveBeenCalledTimes(0)
233 })
234β
235 test('should be `enabled` to accept getter function', async () => {
236 const fetchFn = jest.fn()
237β
238 const checked = ref(false)
239β
240 useQueries({
241 queries: [
242 {
243 queryKey: ['enabled'],
244 queryFn: fetchFn,
245 enabled: () => checked.value,
246 },
247 ],
248 })
249β
250 expect(fetchFn).not.toHaveBeenCalled()
251β
252 checked.value = true
253β
254 await flushPromises()
255β
256 expect(fetchFn).toHaveBeenCalled()
257 })
258})