Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

queriesObserver.test.tsx

335 lines11.7 KB

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