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