Open History to see the last commit
1import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'
2import { QueriesObserver } from '..'
3import { createQueryClient, queryKey, sleep } from './utils'
4import type { QueryClient, QueryObserverResult } from '..'
5β
6describe('queriesObserver', () => {
7 let queryClient: QueryClient
8β
9 beforeEach(() => {
10 vi.useFakeTimers()
11 queryClient = createQueryClient()
12 queryClient.mount()
13 })
14β
15 afterEach(() => {
16 queryClient.clear()
17 vi.useRealTimers()
18 })
19β
20 test('should return an array with all query results', async () => {
21 const key1 = queryKey()
22 const key2 = queryKey()
23 const queryFn1 = vi.fn().mockReturnValue(1)
24 const queryFn2 = vi.fn().mockReturnValue(2)
25 const observer = new QueriesObserver(queryClient, [
26 { queryKey: key1, queryFn: queryFn1 },
27 { queryKey: key2, queryFn: queryFn2 },
28 ])
29 let observerResult
30 const unsubscribe = observer.subscribe((result) => {
31 observerResult = result
32 })
33 await vi.advanceTimersByTimeAsync(1)
34 unsubscribe()
35 expect(observerResult).toMatchObject([{ data: 1 }, { data: 2 }])
36 })
37β
38 test('should update when a query updates', async () => {
39 const key1 = queryKey()
40 const key2 = queryKey()
41 const queryFn1 = vi.fn().mockReturnValue(1)
42 const queryFn2 = vi.fn().mockReturnValue(2)
43 const observer = new QueriesObserver(queryClient, [
44 { queryKey: key1, queryFn: queryFn1 },
45 { queryKey: key2, queryFn: queryFn2 },
46 ])
47 const results: Array<Array<QueryObserverResult>> = []
48 results.push(observer.getCurrentResult())
49 const unsubscribe = observer.subscribe((result) => {
50 results.push(result)
51 })
52 await vi.advanceTimersByTimeAsync(1)
53 queryClient.setQueryData(key2, 3)
54 await vi.advanceTimersByTimeAsync(1)
55 unsubscribe()
56 expect(results.length).toBe(6)
57 expect(results[0]).toMatchObject([
58 { status: 'pending', fetchStatus: 'idle', data: undefined },
59 { status: 'pending', fetchStatus: 'idle', data: undefined },
60 ])
61 expect(results[1]).toMatchObject([
62 { status: 'pending', fetchStatus: 'fetching', data: undefined },
63 { status: 'pending', fetchStatus: 'idle', data: undefined },
64 ])
65 expect(results[2]).toMatchObject([
66 { status: 'pending', fetchStatus: 'fetching', data: undefined },
67 { status: 'pending', fetchStatus: 'fetching', data: undefined },
68 ])
69 expect(results[3]).toMatchObject([
70 { status: 'success', data: 1 },
71 { status: 'pending', fetchStatus: 'fetching', data: undefined },
72 ])
73 expect(results[4]).toMatchObject([
74 { status: 'success', data: 1 },
75 { status: 'success', data: 2 },
76 ])
77 expect(results[5]).toMatchObject([
78 { status: 'success', data: 1 },
79 { status: 'success', data: 3 },
80 ])
81 })
82β
83 test('should update when a query is removed', async () => {
84 const key1 = queryKey()
85 const key2 = queryKey()
86 const queryFn1 = vi.fn().mockReturnValue(1)
87 const queryFn2 = vi.fn().mockReturnValue(2)
88 const observer = new QueriesObserver(queryClient, [
89 { queryKey: key1, queryFn: queryFn1 },
90 { queryKey: key2, queryFn: queryFn2 },
91 ])
92 const results: Array<Array<QueryObserverResult>> = []
93 results.push(observer.getCurrentResult())
94 const unsubscribe = observer.subscribe((result) => {
95 results.push(result)
96 })
97 await vi.advanceTimersByTimeAsync(1)
98 observer.setQueries([{ queryKey: key2, queryFn: queryFn2 }])
99 await vi.advanceTimersByTimeAsync(1)
100 const queryCache = queryClient.getQueryCache()
101 expect(queryCache.find({ queryKey: key1, type: 'active' })).toBeUndefined()
102 expect(queryCache.find({ queryKey: key2, type: 'active' })).toBeDefined()
103 unsubscribe()
104 expect(queryCache.find({ queryKey: key1, type: 'active' })).toBeUndefined()
105 expect(queryCache.find({ queryKey: key2, type: 'active' })).toBeUndefined()
106 expect(results.length).toBe(6)
107 expect(results[0]).toMatchObject([
108 { status: 'pending', fetchStatus: 'idle', data: undefined },
109 { status: 'pending', fetchStatus: 'idle', data: undefined },
110 ])
111 expect(results[1]).toMatchObject([
112 { status: 'pending', fetchStatus: 'fetching', data: undefined },
113 { status: 'pending', fetchStatus: 'idle', data: undefined },
114 ])
115 expect(results[2]).toMatchObject([
116 { status: 'pending', fetchStatus: 'fetching', data: undefined },
117 { status: 'pending', fetchStatus: 'fetching', data: undefined },
118 ])
119 expect(results[3]).toMatchObject([
120 { status: 'success', data: 1 },
121 { status: 'pending', fetchStatus: 'fetching', data: undefined },
122 ])
123 expect(results[4]).toMatchObject([
124 { status: 'success', data: 1 },
125 { status: 'success', data: 2 },
126 ])
127 expect(results[5]).toMatchObject([{ status: 'success', data: 2 }])
128 })
129β
130 test('should update when a query changed position', async () => {
131 const key1 = queryKey()
132 const key2 = queryKey()
133 const queryFn1 = vi.fn().mockReturnValue(1)
134 const queryFn2 = vi.fn().mockReturnValue(2)
135 const observer = new QueriesObserver(queryClient, [
136 { queryKey: key1, queryFn: queryFn1 },
137 { queryKey: key2, queryFn: queryFn2 },
138 ])
139 const results: Array<Array<QueryObserverResult>> = []
140 results.push(observer.getCurrentResult())
141 const unsubscribe = observer.subscribe((result) => {
142 results.push(result)
143 })
144 await vi.advanceTimersByTimeAsync(1)
145 observer.setQueries([
146 { queryKey: key2, queryFn: queryFn2 },
147 { queryKey: key1, queryFn: queryFn1 },
148 ])
149 await vi.advanceTimersByTimeAsync(1)
150 unsubscribe()
151 expect(results.length).toBe(6)
152 expect(results[0]).toMatchObject([
153 { status: 'pending', fetchStatus: 'idle', data: undefined },
154 { status: 'pending', fetchStatus: 'idle', data: undefined },
155 ])
156 expect(results[1]).toMatchObject([
157 { status: 'pending', fetchStatus: 'fetching', data: undefined },
158 { status: 'pending', fetchStatus: 'idle', data: undefined },
159 ])
160 expect(results[2]).toMatchObject([
161 { status: 'pending', fetchStatus: 'fetching', data: undefined },
162 { status: 'pending', fetchStatus: 'fetching', data: undefined },
163 ])
164 expect(results[3]).toMatchObject([
165 { status: 'success', data: 1 },
166 { status: 'pending', fetchStatus: 'fetching', data: undefined },
167 ])
168 expect(results[4]).toMatchObject([
169 { status: 'success', data: 1 },
170 { status: 'success', data: 2 },
171 ])
172 expect(results[5]).toMatchObject([
173 { status: 'success', data: 2 },
174 { status: 'success', data: 1 },
175 ])
176 })
177β
178 test('should not update when nothing has changed', async () => {
179 const key1 = queryKey()
180 const key2 = queryKey()
181 const queryFn1 = vi.fn().mockReturnValue(1)
182 const queryFn2 = vi.fn().mockReturnValue(2)
183 const observer = new QueriesObserver(queryClient, [
184 { queryKey: key1, queryFn: queryFn1 },
185 { queryKey: key2, queryFn: queryFn2 },
186 ])
187 const results: Array<Array<QueryObserverResult>> = []
188 results.push(observer.getCurrentResult())
189 const unsubscribe = observer.subscribe((result) => {
190 results.push(result)
191 })
192 await vi.advanceTimersByTimeAsync(1)
193 observer.setQueries([
194 { queryKey: key1, queryFn: queryFn1 },
195 { queryKey: key2, queryFn: queryFn2 },
196 ])
197 await vi.advanceTimersByTimeAsync(1)
198 unsubscribe()
199 expect(results.length).toBe(5)
200 expect(results[0]).toMatchObject([
201 { status: 'pending', fetchStatus: 'idle', data: undefined },
202 { status: 'pending', fetchStatus: 'idle', data: undefined },
203 ])
204 expect(results[1]).toMatchObject([
205 { status: 'pending', fetchStatus: 'fetching', data: undefined },
206 { status: 'pending', fetchStatus: 'idle', data: undefined },
207 ])
208 expect(results[2]).toMatchObject([
209 { status: 'pending', fetchStatus: 'fetching', data: undefined },
210 { status: 'pending', fetchStatus: 'fetching', data: undefined },
211 ])
212 expect(results[3]).toMatchObject([
213 { status: 'success', data: 1 },
214 { status: 'pending', fetchStatus: 'fetching', data: undefined },
215 ])
216 expect(results[4]).toMatchObject([
217 { status: 'success', data: 1 },
218 { status: 'success', data: 2 },
219 ])
220 })
221β
222 test('should trigger all fetches when subscribed', async () => {
223 const key1 = queryKey()
224 const key2 = queryKey()
225 const queryFn1 = vi.fn().mockReturnValue(1)
226 const queryFn2 = vi.fn().mockReturnValue(2)
227 const observer = new QueriesObserver(queryClient, [
228 { queryKey: key1, queryFn: queryFn1 },
229 { queryKey: key2, queryFn: queryFn2 },
230 ])
231 const unsubscribe = observer.subscribe(() => undefined)
232 await vi.advanceTimersByTimeAsync(1)
233 unsubscribe()
234 expect(queryFn1).toHaveBeenCalledTimes(1)
235 expect(queryFn2).toHaveBeenCalledTimes(1)
236 })
237β
238 test('should not destroy the observer if there is still a subscription', async () => {
239 const key1 = queryKey()
240 const observer = new QueriesObserver(queryClient, [
241 {
242 queryKey: key1,
243 queryFn: async () => {
244 await sleep(20)
245 return 1
246 },
247 },
248 ])
249β
250 const subscription1Handler = vi.fn()
251 const subscription2Handler = vi.fn()
252β
253 const unsubscribe1 = observer.subscribe(subscription1Handler)
254 const unsubscribe2 = observer.subscribe(subscription2Handler)
255β
256 unsubscribe1()
257β
258 await vi.waitFor(() => {
259 // 1 call: pending
260 expect(subscription1Handler).toBeCalledTimes(1)
261 // 1 call: success
262 expect(subscription2Handler).toBeCalledTimes(1)
263 })
264β
265 // Clean-up
266 unsubscribe2()
267 })
268β
269 test('should handle duplicate query keys in different positions', async () => {
270 const key1 = queryKey()
271 const key2 = queryKey()
272 const queryFn1 = vi.fn().mockReturnValue(1)
273 const queryFn2 = vi.fn().mockReturnValue(2)
274β
275 const observer = new QueriesObserver(queryClient, [
276 { queryKey: key1, queryFn: queryFn1 },
277 { queryKey: key2, queryFn: queryFn2 },
278 { queryKey: key1, queryFn: queryFn1 },
279 ])
280β
281 const results: Array<Array<QueryObserverResult>> = []
282 results.push(
283 observer.getOptimisticResult(
284 [
285 { queryKey: key1, queryFn: queryFn1 },
286 { queryKey: key2, queryFn: queryFn2 },
287 { queryKey: key1, queryFn: queryFn1 },
288 ],
289 undefined,
290 )[0],
291 )
292β
293 const unsubscribe = observer.subscribe((result) => {
294 results.push(result)
295 })
296β
297 await vi.advanceTimersByTimeAsync(1)
298 unsubscribe()
299β
300 expect(results.length).toBe(6)
301 expect(results[0]).toMatchObject([
302 { status: 'pending', fetchStatus: 'idle', data: undefined },
303 { status: 'pending', fetchStatus: 'idle', data: undefined },
304 { status: 'pending', fetchStatus: 'idle', data: undefined },
305 ])
306 expect(results[1]).toMatchObject([
307 { status: 'pending', fetchStatus: 'fetching', data: undefined },
308 { status: 'pending', fetchStatus: 'idle', data: undefined },
309 { status: 'pending', fetchStatus: 'idle', data: undefined },
310 ])
311 expect(results[2]).toMatchObject([
312 { status: 'pending', fetchStatus: 'fetching', data: undefined },
313 { status: 'pending', fetchStatus: 'fetching', data: undefined },
314 { status: 'pending', fetchStatus: 'idle', data: undefined },
315 ])
316 expect(results[3]).toMatchObject([
317 { status: 'success', fetchStatus: 'idle', data: 1 },
318 { status: 'pending', fetchStatus: 'fetching', data: undefined },
319 { status: 'pending', fetchStatus: 'idle', data: undefined },
320 ])
321 expect(results[4]).toMatchObject([
322 { status: 'success', fetchStatus: 'idle', data: 1 },
323 { status: 'pending', fetchStatus: 'fetching', data: undefined },
324 { status: 'success', fetchStatus: 'idle', data: 1 },
325 ])
326 expect(results[5]).toMatchObject([
327 { status: 'success', fetchStatus: 'idle', data: 1 },
328 { status: 'success', fetchStatus: 'idle', data: 2 },
329 { status: 'success', fetchStatus: 'idle', data: 1 },
330 ])
331β
332 // Verify that queryFn1 was only called once despite being used twice
333 expect(queryFn1).toHaveBeenCalledTimes(1)
334 expect(queryFn2).toHaveBeenCalledTimes(1)
335 })
336})