Open History to see the last commit
1import {
2 sleep,
3 queryKey,
4 mockVisibilityState,
5 mockConsoleError,
6} from '../../react/tests/utils'
7import {
8 QueryCache,
9 QueryClient,
10 QueryObserver,
11 isCancelledError,
12 isError,
13 onlineManager,
14 QueryFunctionContext,
15} from '../..'
16β
17describe('query', () => {
18 let queryClient: QueryClient
19 let queryCache: QueryCache
20β
21 beforeEach(() => {
22 queryClient = new QueryClient()
23 queryCache = queryClient.getQueryCache()
24 queryClient.mount()
25 })
26β
27 afterEach(() => {
28 queryClient.clear()
29 })
30β
31 test('should use the longest cache time it has seen', async () => {
32 const key = queryKey()
33 await queryClient.prefetchQuery(key, () => 'data', {
34 cacheTime: 100,
35 })
36 await queryClient.prefetchQuery(key, () => 'data', {
37 cacheTime: 200,
38 })
39 await queryClient.prefetchQuery(key, () => 'data', {
40 cacheTime: 10,
41 })
42 const query = queryCache.find(key)!
43 expect(query.cacheTime).toBe(200)
44 })
45β
46 it('should continue retry after focus regain and resolve all promises', async () => {
47 const key = queryKey()
48β
49 const originalVisibilityState = document.visibilityState
50β
51 // make page unfocused
52 mockVisibilityState('hidden')
53β
54 let count = 0
55 let result
56β
57 const promise = queryClient.fetchQuery(
58 key,
59 async () => {
60 count++
61β
62 if (count === 3) {
63 return `data${count}`
64 }
65β
66 throw new Error(`error${count}`)
67 },
68 {
69 retry: 3,
70 retryDelay: 1,
71 }
72 )
73β
74 promise.then(data => {
75 result = data
76 })
77β
78 // Check if we do not have a result
79 expect(result).toBeUndefined()
80β
81 // Check if the query is really paused
82 await sleep(50)
83 expect(result).toBeUndefined()
84β
85 // Reset visibilityState to original value
86 mockVisibilityState(originalVisibilityState)
87 window.dispatchEvent(new FocusEvent('focus'))
88β
89 // There should not be a result yet
90 expect(result).toBeUndefined()
91β
92 // By now we should have a value
93 await sleep(50)
94 expect(result).toBe('data3')
95 })
96β
97 it('should continue retry after reconnect and resolve all promises', async () => {
98 const key = queryKey()
99β
100 onlineManager.setOnline(false)
101β
102 let count = 0
103 let result
104β
105 const promise = queryClient.fetchQuery(
106 key,
107 async () => {
108 count++
109β
110 if (count === 3) {
111 return `data${count}`
112 }
113β
114 throw new Error(`error${count}`)
115 },
116 {
117 retry: 3,
118 retryDelay: 1,
119 }
120 )
121β
122 promise.then(data => {
123 result = data
124 })
125β
126 // Check if we do not have a result
127 expect(result).toBeUndefined()
128β
129 // Check if the query is really paused
130 await sleep(50)
131 expect(result).toBeUndefined()
132β
133 // Reset navigator to original value
134 onlineManager.setOnline(true)
135β
136 // There should not be a result yet
137 expect(result).toBeUndefined()
138β
139 // By now we should have a value
140 await sleep(50)
141 expect(result).toBe('data3')
142 })
143β
144 it('should throw a CancelledError when a paused query is cancelled', async () => {
145 const key = queryKey()
146β
147 const originalVisibilityState = document.visibilityState
148β
149 // make page unfocused
150 mockVisibilityState('hidden')
151β
152 let count = 0
153 let result
154β
155 const promise = queryClient.fetchQuery(
156 key,
157 async () => {
158 count++
159 throw new Error(`error${count}`)
160 },
161 {
162 retry: 3,
163 retryDelay: 1,
164 }
165 )
166β
167 promise.catch(data => {
168 result = data
169 })
170β
171 const query = queryCache.find(key)!
172β
173 // Check if the query is really paused
174 await sleep(50)
175 expect(result).toBeUndefined()
176β
177 // Cancel query
178 query.cancel()
179β
180 // Check if the error is set to the cancelled error
181 await sleep(0)
182 expect(isCancelledError(result)).toBe(true)
183β
184 // Reset visibilityState to original value
185 mockVisibilityState(originalVisibilityState)
186 window.dispatchEvent(new FocusEvent('focus'))
187 })
188β
189 test('should provide context to queryFn', async () => {
190 const key = queryKey()
191β
192 const queryFn = jest
193 .fn<Promise<'data'>, [QueryFunctionContext<string>]>()
194 .mockResolvedValue('data')
195β
196 queryClient.prefetchQuery(key, queryFn)
197β
198 await sleep(10)
199β
200 expect(queryFn).toHaveBeenCalledTimes(1)
201 const args = queryFn.mock.calls[0]![0]
202 expect(args).toBeDefined()
203 expect(args.pageParam).toBeUndefined()
204 expect(args.queryKey).toEqual([key])
205 if (typeof AbortSignal === 'function') {
206 expect(args.signal).toBeInstanceOf(AbortSignal)
207 } else {
208 expect(args.signal).toBeUndefined()
209 }
210 })
211β
212 test('should continue if cancellation is not supported and signal is not consumed', async () => {
213 const key = queryKey()
214β
215 queryClient.prefetchQuery(key, async () => {
216 await sleep(100)
217 return 'data'
218 })
219β
220 await sleep(10)
221β
222 // Subscribe and unsubscribe to simulate cancellation because the last observer unsubscribed
223 const observer = new QueryObserver(queryClient, {
224 queryKey: key,
225 enabled: false,
226 })
227 const unsubscribe = observer.subscribe()
228 unsubscribe()
229β
230 await sleep(100)
231β
232 const query = queryCache.find(key)!
233β
234 expect(query.state).toMatchObject({
235 data: 'data',
236 status: 'success',
237 dataUpdateCount: 1,
238 })
239 })
240β
241 test('should not continue when last observer unsubscribed if the signal was consumed', async () => {
242 const key = queryKey()
243β
244 queryClient.prefetchQuery(key, async ({ signal }) => {
245 await sleep(100)
246 return signal?.aborted ? 'aborted' : 'data'
247 })
248β
249 await sleep(10)
250β
251 // Subscribe and unsubscribe to simulate cancellation because the last observer unsubscribed
252 const observer = new QueryObserver(queryClient, {
253 queryKey: key,
254 enabled: false,
255 })
256 const unsubscribe = observer.subscribe()
257 unsubscribe()
258β
259 await sleep(100)
260β
261 const query = queryCache.find(key)!
262β
263 if (typeof AbortSignal === 'function') {
264 expect(query.state).toMatchObject({
265 data: undefined,
266 status: 'idle',
267 })
268 } else {
269 expect(query.state).toMatchObject({
270 data: 'data',
271 status: 'success',
272 dataUpdateCount: 1,
273 })
274 }
275 })
276β
277 test('should provide an AbortSignal to the queryFn that provides info about the cancellation state', async () => {
278 const key = queryKey()
279β
280 const queryFn = jest.fn<Promise<void>, [QueryFunctionContext<string>]>()
281 const onAbort = jest.fn()
282 const abortListener = jest.fn()
283 let error
284β
285 queryFn.mockImplementation(async ({ signal }) => {
286 if (signal) {
287 signal.onabort = onAbort
288 signal.addEventListener('abort', abortListener)
289 }
290 await sleep(10)
291 if (signal) {
292 signal.onabort = null
293 signal.removeEventListener('abort', abortListener)
294 }
295 throw new Error()
296 })
297β
298 const promise = queryClient.fetchQuery(key, queryFn, {
299 retry: 3,
300 retryDelay: 10,
301 })
302β
303 promise.catch(e => {
304 error = e
305 })
306β
307 const query = queryCache.find(key)!
308β
309 expect(queryFn).toHaveBeenCalledTimes(1)
310β
311 let signal = queryFn.mock.calls[0]![0].signal
312β
313 if (typeof AbortSignal === 'function') {
314 signal = queryFn.mock.calls[0]![0].signal
315 expect(signal?.aborted).toBe(false)
316 }
317 expect(onAbort).not.toHaveBeenCalled()
318 expect(abortListener).not.toHaveBeenCalled()
319β
320 query.cancel()
321β
322 await sleep(100)
323β
324 if (typeof AbortSignal === 'function') {
325 expect(signal?.aborted).toBe(true)
326 expect(onAbort).toHaveBeenCalledTimes(1)
327 expect(abortListener).toHaveBeenCalledTimes(1)
328 }
329 expect(isCancelledError(error)).toBe(true)
330 })
331β
332 test('should call cancel() fn if it was provided and not continue when last observer unsubscribed', async () => {
333 const key = queryKey()
334β
335 const cancel = jest.fn()
336β
337 queryClient.prefetchQuery(key, async () => {
338 const promise = new Promise((resolve, reject) => {
339 sleep(100).then(() => resolve('data'))
340 cancel.mockImplementation(() => {
341 reject(new Error('Cancelled'))
342 })
343 }) as any
344 promise.cancel = cancel
345 return promise
346 })
347β
348 await sleep(10)
349β
350 // Subscribe and unsubscribe to simulate cancellation because the last observer unsubscribed
351 const observer = new QueryObserver(queryClient, {
352 queryKey: key,
353 enabled: false,
354 })
355 const unsubscribe = observer.subscribe()
356 unsubscribe()
357β
358 await sleep(100)
359β
360 const query = queryCache.find(key)!
361β
362 expect(cancel).toHaveBeenCalled()
363 expect(query.state).toMatchObject({
364 data: undefined,
365 status: 'idle',
366 })
367 })
368β
369 test('should not continue if explicitly cancelled', async () => {
370 const key = queryKey()
371β
372 const queryFn = jest.fn()
373β
374 queryFn.mockImplementation(async () => {
375 await sleep(10)
376 throw new Error()
377 })
378β
379 let error
380β
381 const promise = queryClient.fetchQuery(key, queryFn, {
382 retry: 3,
383 retryDelay: 10,
384 })
385β
386 promise.catch(e => {
387 error = e
388 })
389β
390 const query = queryCache.find(key)!
391 query.cancel()
392β
393 await sleep(100)
394β
395 expect(queryFn).toHaveBeenCalledTimes(1)
396 expect(isCancelledError(error)).toBe(true)
397 })
398β
399 test('should not error if reset while loading', async () => {
400 const key = queryKey()
401β
402 const queryFn = jest.fn()
403β
404 queryFn.mockImplementation(async () => {
405 await sleep(10)
406 throw new Error()
407 })
408β
409 queryClient.fetchQuery(key, queryFn, {
410 retry: 3,
411 retryDelay: 10,
412 })
413β
414 // Ensure the query is loading
415 const query = queryCache.find(key)!
416 expect(query.state.status).toBe('loading')
417β
418 // Reset the query while it is loading
419 query.reset()
420β
421 await sleep(100)
422β
423 // The query should
424 expect(queryFn).toHaveBeenCalledTimes(1) // have been called,
425 expect(query.state.error).toBe(null) // not have an error, and
426 expect(query.state.status).toBe('idle') // not be loading any longer
427 })
428β
429 test('should be able to refetch a cancelled query', async () => {
430 const key = queryKey()
431β
432 const queryFn = jest.fn()
433β
434 queryFn.mockImplementation(async () => {
435 await sleep(50)
436 return 'data'
437 })
438β
439 queryClient.prefetchQuery(key, queryFn)
440 const query = queryCache.find(key)!
441 await sleep(10)
442 query.cancel()
443 await sleep(100)
444β
445 expect(queryFn).toHaveBeenCalledTimes(1)
446 expect(isCancelledError(query.state.error)).toBe(true)
447 const result = await query.fetch()
448 expect(result).toBe('data')
449 expect(query.state.error).toBe(null)
450 expect(queryFn).toHaveBeenCalledTimes(2)
451 })
452β
453 test('cancelling a resolved query should not have any effect', async () => {
454 const key = queryKey()
455 await queryClient.prefetchQuery(key, async () => 'data')
456 const query = queryCache.find(key)!
457 query.cancel()
458 await sleep(10)
459 expect(query.state.data).toBe('data')
460 })
461β
462 test('cancelling a rejected query should not have any effect', async () => {
463 const consoleMock = mockConsoleError()
464β
465 const key = queryKey()
466β
467 await queryClient.prefetchQuery(key, async () => {
468 throw new Error('error')
469 })
470 const query = queryCache.find(key)!
471 query.cancel()
472 await sleep(10)
473β
474 expect(isError(query.state.error)).toBe(true)
475 expect(isCancelledError(query.state.error)).toBe(false)
476β
477 consoleMock.mockRestore()
478 })
479β
480 test('the previous query status should be kept when refetching', async () => {
481 const consoleMock = mockConsoleError()
482 const key = queryKey()
483β
484 await queryClient.prefetchQuery(key, () => 'data')
485 const query = queryCache.find(key)!
486 expect(query.state.status).toBe('success')
487β
488 await queryClient.prefetchQuery(key, () => Promise.reject('reject'), {
489 retry: false,
490 })
491 expect(query.state.status).toBe('error')
492β
493 queryClient.prefetchQuery(
494 key,
495 async () => {
496 await sleep(10)
497 return Promise.reject('reject')
498 },
499 { retry: false }
500 )
501 expect(query.state.status).toBe('error')
502β
503 await sleep(100)
504 expect(query.state.status).toBe('error')
505β
506 consoleMock.mockRestore()
507 })
508β
509 test('queries with cacheTime 0 should be removed immediately after unsubscribing', async () => {
510 const consoleMock = mockConsoleError()
511 const key = queryKey()
512 let count = 0
513 const observer = new QueryObserver(queryClient, {
514 queryKey: key,
515 queryFn: () => {
516 count++
517 return 'data'
518 },
519 cacheTime: 0,
520 staleTime: Infinity,
521 })
522 const unsubscribe1 = observer.subscribe()
523 unsubscribe1()
524 await sleep(10)
525 const unsubscribe2 = observer.subscribe()
526 unsubscribe2()
527 await sleep(10)
528 expect(count).toBe(2)
529 expect(queryCache.find(key)).toBeUndefined()
530 consoleMock.mockRestore()
531 })
532β
533 test('should be garbage collected when unsubscribed to', async () => {
534 const key = queryKey()
535 const observer = new QueryObserver(queryClient, {
536 queryKey: key,
537 queryFn: async () => 'data',
538 cacheTime: 0,
539 })
540 expect(queryCache.find(key)).toBeDefined()
541 const unsubscribe = observer.subscribe()
542 expect(queryCache.find(key)).toBeDefined()
543 unsubscribe()
544 expect(queryCache.find(key)).toBeUndefined()
545 })
546β
547 test('should be garbage collected later when unsubscribed and query is fetching', async () => {
548 const key = queryKey()
549 const observer = new QueryObserver(queryClient, {
550 queryKey: key,
551 queryFn: async () => {
552 await sleep(20)
553 return 'data'
554 },
555 cacheTime: 10,
556 })
557 const unsubscribe = observer.subscribe()
558 await sleep(20)
559 expect(queryCache.find(key)).toBeDefined()
560 observer.refetch()
561 unsubscribe()
562 await sleep(10)
563 // unsubscribe should not remove even though cacheTime has elapsed b/c query is still fetching
564 expect(queryCache.find(key)).toBeDefined()
565 await sleep(10)
566 // should be removed after an additional staleTime wait
567 expect(queryCache.find(key)).toBeUndefined()
568 })
569β
570 test('should not be garbage collected unless there are no subscribers', async () => {
571 const key = queryKey()
572 const observer = new QueryObserver(queryClient, {
573 queryKey: key,
574 queryFn: async () => 'data',
575 cacheTime: 0,
576 })
577 expect(queryCache.find(key)).toBeDefined()
578 const unsubscribe = observer.subscribe()
579 await sleep(100)
580 expect(queryCache.find(key)).toBeDefined()
581 unsubscribe()
582 await sleep(100)
583 expect(queryCache.find(key)).toBeUndefined()
584 queryClient.setQueryData(key, 'data')
585 await sleep(100)
586 expect(queryCache.find(key)).toBeDefined()
587 })
588β
589 test('should return proper count of observers', async () => {
590 const key = queryKey()
591 const options = { queryKey: key, queryFn: async () => 'data' }
592 const observer = new QueryObserver(queryClient, options)
593 const observer2 = new QueryObserver(queryClient, options)
594 const observer3 = new QueryObserver(queryClient, options)
595 const query = queryCache.find(key)
596β
597 expect(query?.getObserversCount()).toEqual(0)
598β
599 const unsubscribe1 = observer.subscribe()
600 const unsubscribe2 = observer2.subscribe()
601 const unsubscribe3 = observer3.subscribe()
602 expect(query?.getObserversCount()).toEqual(3)
603β
604 unsubscribe3()
605 expect(query?.getObserversCount()).toEqual(2)
606β
607 unsubscribe2()
608 expect(query?.getObserversCount()).toEqual(1)
609β
610 unsubscribe1()
611 expect(query?.getObserversCount()).toEqual(0)
612 })
613β
614 test('stores meta object in query', async () => {
615 const meta = {
616 it: 'works',
617 }
618β
619 const key = queryKey()
620β
621 await queryClient.prefetchQuery(key, () => 'data', {
622 meta,
623 })
624β
625 const query = queryCache.find(key)!
626β
627 expect(query.meta).toBe(meta)
628 expect(query.options.meta).toBe(meta)
629 })
630β
631 test('updates meta object on change', async () => {
632 const meta = {
633 it: 'works',
634 }
635β
636 const key = queryKey()
637 const queryFn = () => 'data'
638β
639 await queryClient.prefetchQuery(key, queryFn, {
640 meta,
641 })
642β
643 await queryClient.prefetchQuery(key, queryFn, {
644 meta: undefined,
645 })
646β
647 const query = queryCache.find(key)!
648β
649 expect(query.meta).toBeUndefined()
650 expect(query.options.meta).toBeUndefined()
651 })
652β
653 test('provides meta object inside query function', async () => {
654 const meta = {
655 it: 'works',
656 }
657β
658 const queryFn = jest.fn(() => 'data')
659β
660 const key = queryKey()
661β
662 await queryClient.prefetchQuery(key, queryFn, {
663 meta,
664 })
665β
666 expect(queryFn).toBeCalledWith(
667 expect.objectContaining({
668 meta,
669 })
670 )
671 })
672β
673 test('should set default options', async () => {
674 const key = queryKey()
675β
676 await queryClient.prefetchQuery(key, () => 'data')
677 const query = queryCache.find(key)!
678β
679 query.setDefaultOptions({ retryDelay: 20 })
680β
681 await queryClient.prefetchQuery(key, () => 'data', {
682 cacheTime: 100,
683 })
684β
685 expect(query.options).toMatchObject({ cacheTime: 100, retryDelay: 20 })
686 })
687β
688 test('should refetch the observer when online method is called', async () => {
689 const key = queryKey()
690β
691 const observer = new QueryObserver(queryClient, {
692 queryKey: key,
693 queryFn: () => 'data',
694 })
695β
696 const refetchSpy = jest.spyOn(observer, 'refetch')
697 const unsubscribe = observer.subscribe()
698 queryCache.onOnline()
699β
700 // Should refetch the observer
701 expect(refetchSpy).toHaveBeenCalledTimes(1)
702β
703 unsubscribe()
704 refetchSpy.mockRestore()
705 })
706β
707 test('should not add an existing observer', async () => {
708 const key = queryKey()
709β
710 await queryClient.prefetchQuery(key, () => 'data')
711 const query = queryCache.find(key)!
712 expect(query.getObserversCount()).toEqual(0)
713β
714 const observer = new QueryObserver(queryClient, {
715 queryKey: key,
716 })
717 expect(query.getObserversCount()).toEqual(0)
718β
719 query.addObserver(observer)
720 expect(query.getObserversCount()).toEqual(1)
721β
722 query.addObserver(observer)
723 expect(query.getObserversCount()).toEqual(1)
724 })
725β
726 test('should not try to remove an observer that does not exist', async () => {
727 const key = queryKey()
728β
729 await queryClient.prefetchQuery(key, () => 'data')
730 const query = queryCache.find(key)!
731 const observer = new QueryObserver(queryClient, {
732 queryKey: key,
733 })
734 expect(query.getObserversCount()).toEqual(0)
735β
736 const notifySpy = jest.spyOn(queryCache, 'notify')
737 expect(() => query.removeObserver(observer)).not.toThrow()
738 expect(notifySpy).not.toHaveBeenCalled()
739β
740 notifySpy.mockRestore()
741 })
742β
743 test('should not dispatch "invalidate" on invalidate() if already invalidated', async () => {
744 const key = queryKey()
745β
746 await queryClient.prefetchQuery(key, () => 'data')
747 const query = queryCache.find(key)!
748β
749 query.invalidate()
750 expect(query.state.isInvalidated).toBeTruthy()
751β
752 const dispatchOriginal = query['dispatch']
753 const dispatchSpy = jest.fn()
754 query['dispatch'] = dispatchSpy
755β
756 query.invalidate()
757β
758 expect(query.state.isInvalidated).toBeTruthy()
759 expect(dispatchSpy).not.toHaveBeenCalled()
760β
761 query['dispatch'] = dispatchOriginal
762 })
763β
764 test('reducer should return the state for an unknown action type', async () => {
765 const key = queryKey()
766β
767 await queryClient.prefetchQuery(key, () => 'data')
768 const query = queryCache.find(key)!
769β
770 // Force unknown action type
771 //@ts-expect-error
772 const reducedState = query['reducer'](query.state, { type: 'unknown' })
773 expect(reducedState).toEqual(query.state)
774 })
775β
776 test('fetch should not dispatch "fetch" if state meta and fetchOptions meta are the same object', async () => {
777 const key = queryKey()
778β
779 const queryFn = async () => {
780 await sleep(10)
781 return 'data'
782 }
783β
784 await queryClient.prefetchQuery(key, queryFn)
785 const query = queryCache.find(key)!
786β
787 const meta = { meta1: '1' }
788β
789 // This first fetch will set the state.meta value
790 query.fetch(
791 {
792 queryKey: key,
793 queryFn,
794 },
795 {
796 meta,
797 }
798 )
799β
800 // Spy on private dispatch method
801 const dispatchOriginal = query['dispatch']
802 const dispatchSpy = jest.fn()
803 query['dispatch'] = dispatchSpy
804β
805 // Second fetch in parallel with the same meta
806 query.fetch(
807 {
808 queryKey: key,
809 queryFn,
810 },
811 {
812 meta,
813 // cancelRefetch must be set to true to enter in the case to test
814 // where isFetching is true
815 cancelRefetch: true,
816 }
817 )
818β
819 // Should not call dispatch with type set to fetch
820 expect(dispatchSpy).not.toHaveBeenCalledWith({
821 meta,
822 type: 'fetch',
823 })
824β
825 // Clean-up
826 await sleep(20)
827 query['dispatch'] = dispatchOriginal
828 })
829β
830 test('fetch should not set the signal in the queryFnContext if AbortController is undefined', async () => {
831 const key = queryKey()
832β
833 // Mock the AbortController to be undefined
834 const AbortControllerOriginal = globalThis['AbortController']
835 //@ts-expect-error
836 globalThis['AbortController'] = undefined
837β
838 let signalTest: any
839 await queryClient.prefetchQuery(key, ({ signal }) => {
840 signalTest = signal
841 })
842β
843 expect(signalTest).toBeUndefined()
844β
845 // Clean-up
846 //@ts-ignore
847 globalThis['AbortController'] = AbortControllerOriginal
848 })
849β
850 test('fetch should throw an error if the queryFn is not defined', async () => {
851 const consoleMock = mockConsoleError()
852 const key = queryKey()
853β
854 const observer = new QueryObserver(queryClient, {
855 queryKey: key,
856 queryFn: undefined,
857 retry: false,
858 })
859β
860 const unsubscribe = observer.subscribe()
861 await sleep(10)
862 expect(consoleMock).toHaveBeenCalledWith('Missing queryFn')
863β
864 unsubscribe()
865 consoleMock.mockRestore()
866 })
867β
868 test('fetch should dispatch fetch if is fetching and current promise is undefined', async () => {
869 const key = queryKey()
870β
871 const queryFn = async () => {
872 await sleep(10)
873 return 'data'
874 }
875β
876 await queryClient.prefetchQuery(key, queryFn)
877 const query = queryCache.find(key)!
878β
879 query.fetch({
880 queryKey: key,
881 queryFn,
882 })
883β
884 // Force promise to undefined
885 // because no use case have been identified
886 query['promise'] = undefined
887β
888 // Spy on private dispatch method
889 const dispatchOriginal = query['dispatch']
890 const dispatchSpy = jest.fn()
891 query['dispatch'] = dispatchSpy
892β
893 query.fetch({
894 queryKey: key,
895 queryFn,
896 })
897β
898 // Should call dispatch with type set to fetch
899 expect(dispatchSpy).toHaveBeenCalledWith({
900 meta: undefined,
901 type: 'fetch',
902 })
903β
904 // Clean-up
905 await sleep(20)
906 query['dispatch'] = dispatchOriginal
907 })
908β
909 test('constructor should call initialDataUpdatedAt if defined as a function', async () => {
910 const key = queryKey()
911β
912 const initialDataUpdatedAtSpy = jest.fn()
913β
914 await queryClient.prefetchQuery({
915 queryKey: key,
916 queryFn: () => 'data',
917 initialData: 'initial',
918 initialDataUpdatedAt: initialDataUpdatedAtSpy,
919 })
920β
921 expect(initialDataUpdatedAtSpy).toHaveBeenCalled()
922 })
923})