Open History to see the last commit
1import {
2 afterEach,
3 beforeEach,
4 describe,
5 expect,
6 expectTypeOf,
7 test,
8 vi,
9} from 'vitest'
10import { QueryObserver, focusManager } from '..'
11import { createQueryClient, queryKey, sleep } from './utils'
12import type { QueryClient, QueryObserverResult } from '..'
13β
14describe('queryObserver', () => {
15 let queryClient: QueryClient
16β
17 beforeEach(() => {
18 queryClient = createQueryClient()
19 queryClient.mount()
20 })
21β
22 afterEach(() => {
23 queryClient.clear()
24 })
25β
26 test('should trigger a fetch when subscribed', async () => {
27 const key = queryKey()
28 const queryFn = vi.fn<Array<unknown>, string>().mockReturnValue('data')
29 const observer = new QueryObserver(queryClient, { queryKey: key, queryFn })
30 const unsubscribe = observer.subscribe(() => undefined)
31 await sleep(1)
32 unsubscribe()
33 expect(queryFn).toHaveBeenCalledTimes(1)
34 })
35β
36 test('should notify when switching query', async () => {
37 const key1 = queryKey()
38 const key2 = queryKey()
39 const results: Array<QueryObserverResult> = []
40 const observer = new QueryObserver(queryClient, {
41 queryKey: key1,
42 queryFn: () => 1,
43 })
44 const unsubscribe = observer.subscribe((result) => {
45 results.push(result)
46 })
47 await sleep(1)
48 observer.setOptions({ queryKey: key2, queryFn: () => 2 })
49 await sleep(1)
50 unsubscribe()
51 expect(results.length).toBe(4)
52 expect(results[0]).toMatchObject({ data: undefined, status: 'pending' })
53 expect(results[1]).toMatchObject({ data: 1, status: 'success' })
54 expect(results[2]).toMatchObject({ data: undefined, status: 'pending' })
55 expect(results[3]).toMatchObject({ data: 2, status: 'success' })
56 })
57β
58 test('should be able to fetch with a selector', async () => {
59 const key = queryKey()
60 const observer = new QueryObserver(queryClient, {
61 queryKey: key,
62 queryFn: () => ({ count: 1 }),
63 select: (data) => ({ myCount: data.count }),
64 })
65 let observerResult
66 const unsubscribe = observer.subscribe((result) => {
67 expectTypeOf<QueryObserverResult<{ myCount: number }>>(result)
68 observerResult = result
69 })
70 await sleep(1)
71 unsubscribe()
72 expect(observerResult).toMatchObject({ data: { myCount: 1 } })
73 })
74β
75 test('should be able to fetch with a selector using the fetch method', async () => {
76 const key = queryKey()
77 const observer = new QueryObserver(queryClient, {
78 queryKey: key,
79 queryFn: () => ({ count: 1 }),
80 select: (data) => ({ myCount: data.count }),
81 })
82 const observerResult = await observer.refetch()
83 expectTypeOf<{ myCount: number } | undefined>(observerResult.data)
84 expect(observerResult.data).toMatchObject({ myCount: 1 })
85 })
86β
87 test('should be able to fetch with a selector and object syntax', async () => {
88 const key = queryKey()
89 const observer = new QueryObserver(queryClient, {
90 queryKey: key,
91 queryFn: () => ({ count: 1 }),
92 select: (data) => ({ myCount: data.count }),
93 })
94 let observerResult
95 const unsubscribe = observer.subscribe((result) => {
96 observerResult = result
97 })
98 await sleep(1)
99 unsubscribe()
100 expect(observerResult).toMatchObject({ data: { myCount: 1 } })
101 })
102β
103 test('should run the selector again if the data changed', async () => {
104 const key = queryKey()
105 let count = 0
106 const observer = new QueryObserver(queryClient, {
107 queryKey: key,
108 queryFn: () => ({ count }),
109 select: (data) => {
110 count++
111 return { myCount: data.count }
112 },
113 })
114 const observerResult1 = await observer.refetch()
115 const observerResult2 = await observer.refetch()
116 expect(count).toBe(2)
117 expect(observerResult1.data).toMatchObject({ myCount: 0 })
118 expect(observerResult2.data).toMatchObject({ myCount: 1 })
119 })
120β
121 test('should run the selector again if the selector changed', async () => {
122 const key = queryKey()
123 let count = 0
124 const results: Array<QueryObserverResult> = []
125 const queryFn = () => ({ count: 1 })
126 const select1 = (data: ReturnType<typeof queryFn>) => {
127 count++
128 return { myCount: data.count }
129 }
130 const select2 = (_data: ReturnType<typeof queryFn>) => {
131 count++
132 return { myCount: 99 }
133 }
134 const observer = new QueryObserver(queryClient, {
135 queryKey: key,
136 queryFn,
137 select: select1,
138 })
139 const unsubscribe = observer.subscribe((result) => {
140 results.push(result)
141 })
142 await sleep(1)
143 observer.setOptions({
144 queryKey: key,
145 queryFn,
146 select: select2,
147 })
148 await sleep(1)
149 await observer.refetch()
150 unsubscribe()
151 expect(count).toBe(2)
152 expect(results.length).toBe(5)
153 expect(results[0]).toMatchObject({
154 status: 'pending',
155 isFetching: true,
156 data: undefined,
157 })
158 expect(results[1]).toMatchObject({
159 status: 'success',
160 isFetching: false,
161 data: { myCount: 1 },
162 })
163 expect(results[2]).toMatchObject({
164 status: 'success',
165 isFetching: false,
166 data: { myCount: 99 },
167 })
168 expect(results[3]).toMatchObject({
169 status: 'success',
170 isFetching: true,
171 data: { myCount: 99 },
172 })
173 expect(results[4]).toMatchObject({
174 status: 'success',
175 isFetching: false,
176 data: { myCount: 99 },
177 })
178 })
179β
180 test('should not run the selector again if the data and selector did not change', async () => {
181 const key = queryKey()
182 let count = 0
183 const results: Array<QueryObserverResult> = []
184 const queryFn = () => ({ count: 1 })
185 const select = (data: ReturnType<typeof queryFn>) => {
186 count++
187 return { myCount: data.count }
188 }
189 const observer = new QueryObserver(queryClient, {
190 queryKey: key,
191 queryFn,
192 select,
193 })
194 const unsubscribe = observer.subscribe((result) => {
195 results.push(result)
196 })
197 await sleep(1)
198 observer.setOptions({
199 queryKey: key,
200 queryFn,
201 select,
202 })
203 await sleep(1)
204 await observer.refetch()
205 unsubscribe()
206 expect(count).toBe(1)
207 expect(results.length).toBe(4)
208 expect(results[0]).toMatchObject({
209 status: 'pending',
210 isFetching: true,
211 data: undefined,
212 })
213 expect(results[1]).toMatchObject({
214 status: 'success',
215 isFetching: false,
216 data: { myCount: 1 },
217 })
218 expect(results[2]).toMatchObject({
219 status: 'success',
220 isFetching: true,
221 data: { myCount: 1 },
222 })
223 expect(results[3]).toMatchObject({
224 status: 'success',
225 isFetching: false,
226 data: { myCount: 1 },
227 })
228 })
229β
230 test('should not run the selector again if the data did not change', async () => {
231 const key = queryKey()
232 let count = 0
233 const observer = new QueryObserver(queryClient, {
234 queryKey: key,
235 queryFn: () => ({ count: 1 }),
236 select: (data) => {
237 count++
238 return { myCount: data.count }
239 },
240 })
241 const observerResult1 = await observer.refetch()
242 const observerResult2 = await observer.refetch()
243 expect(count).toBe(1)
244 expect(observerResult1.data).toMatchObject({ myCount: 1 })
245 expect(observerResult2.data).toMatchObject({ myCount: 1 })
246 })
247β
248 test('should always run the selector again if selector throws an error and selector is not referentially stable', async () => {
249 const key = queryKey()
250 const results: Array<QueryObserverResult> = []
251 const queryFn = async () => {
252 await sleep(10)
253 return { count: 1 }
254 }
255 const observer = new QueryObserver(queryClient, {
256 queryKey: key,
257 queryFn,
258 select: () => {
259 throw new Error('selector error')
260 },
261 })
262 const unsubscribe = observer.subscribe((result) => {
263 results.push(result)
264 })
265 await sleep(50)
266 await observer.refetch()
267 unsubscribe()
268 expect(results[0]).toMatchObject({
269 status: 'pending',
270 isFetching: true,
271 data: undefined,
272 })
273 expect(results[1]).toMatchObject({
274 status: 'error',
275 isFetching: false,
276 data: undefined,
277 })
278 expect(results[2]).toMatchObject({
279 status: 'error',
280 isFetching: true,
281 data: undefined,
282 })
283 expect(results[3]).toMatchObject({
284 status: 'error',
285 isFetching: false,
286 data: undefined,
287 })
288 })
289β
290 test('should return stale data if selector throws an error', async () => {
291 const key = queryKey()
292 const results: Array<QueryObserverResult> = []
293 let shouldError = false
294 const error = new Error('select error')
295 const observer = new QueryObserver(queryClient, {
296 queryKey: key,
297 retry: 0,
298 queryFn: async () => {
299 await sleep(10)
300 return shouldError ? 2 : 1
301 },
302 select: (num) => {
303 if (shouldError) {
304 throw error
305 }
306 shouldError = true
307 return String(num)
308 },
309 })
310β
311 const unsubscribe = observer.subscribe((result) => {
312 results.push(result)
313 })
314 await sleep(50)
315 await observer.refetch()
316 unsubscribe()
317β
318 expect(results[0]).toMatchObject({
319 status: 'pending',
320 isFetching: true,
321 data: undefined,
322 error: null,
323 })
324 expect(results[1]).toMatchObject({
325 status: 'success',
326 isFetching: false,
327 data: '1',
328 error: null,
329 })
330 expect(results[2]).toMatchObject({
331 status: 'success',
332 isFetching: true,
333 data: '1',
334 error: null,
335 })
336 expect(results[3]).toMatchObject({
337 status: 'error',
338 isFetching: false,
339 data: '1',
340 error,
341 })
342 })
343β
344 test('should structurally share the selector', async () => {
345 const key = queryKey()
346 let count = 0
347 const observer = new QueryObserver(queryClient, {
348 queryKey: key,
349 queryFn: () => ({ count: ++count }),
350 select: () => ({ myCount: 1 }),
351 })
352 const observerResult1 = await observer.refetch()
353 const observerResult2 = await observer.refetch()
354 expect(count).toBe(2)
355 expect(observerResult1.data).toBe(observerResult2.data)
356 })
357β
358 test('should not trigger a fetch when subscribed and disabled', async () => {
359 const key = queryKey()
360 const queryFn = vi.fn<Array<unknown>, string>().mockReturnValue('data')
361 const observer = new QueryObserver(queryClient, {
362 queryKey: key,
363 queryFn,
364 enabled: false,
365 })
366 const unsubscribe = observer.subscribe(() => undefined)
367 await sleep(1)
368 unsubscribe()
369 expect(queryFn).toHaveBeenCalledTimes(0)
370 })
371β
372 test('should not trigger a fetch when not subscribed', async () => {
373 const key = queryKey()
374 const queryFn = vi.fn<Array<unknown>, string>().mockReturnValue('data')
375 new QueryObserver(queryClient, { queryKey: key, queryFn })
376 await sleep(1)
377 expect(queryFn).toHaveBeenCalledTimes(0)
378 })
379β
380 test('should be able to watch a query without defining a query function', async () => {
381 const key = queryKey()
382 const queryFn = vi.fn<Array<unknown>, string>().mockReturnValue('data')
383 const callback = vi.fn()
384 const observer = new QueryObserver(queryClient, {
385 queryKey: key,
386 enabled: false,
387 })
388 const unsubscribe = observer.subscribe(callback)
389 await queryClient.fetchQuery({ queryKey: key, queryFn })
390 unsubscribe()
391 expect(queryFn).toHaveBeenCalledTimes(1)
392 expect(callback).toHaveBeenCalledTimes(2)
393 })
394β
395 test('should accept unresolved query config in update function', async () => {
396 const key = queryKey()
397 const queryFn = vi.fn<Array<unknown>, string>().mockReturnValue('data')
398 const observer = new QueryObserver(queryClient, {
399 queryKey: key,
400 enabled: false,
401 })
402 const results: Array<QueryObserverResult<unknown>> = []
403 const unsubscribe = observer.subscribe((x) => {
404 results.push(x)
405 })
406 observer.setOptions({ enabled: false, staleTime: 10 })
407 await queryClient.fetchQuery({ queryKey: key, queryFn })
408 await sleep(100)
409 unsubscribe()
410 expect(queryFn).toHaveBeenCalledTimes(1)
411 expect(results.length).toBe(3)
412 expect(results[0]).toMatchObject({ isStale: true })
413 expect(results[1]).toMatchObject({ isStale: false })
414 expect(results[2]).toMatchObject({ isStale: true })
415 })
416β
417 test('should be able to handle multiple subscribers', async () => {
418 const key = queryKey()
419 const queryFn = vi.fn<Array<unknown>, string>().mockReturnValue('data')
420 const observer = new QueryObserver<string>(queryClient, {
421 queryKey: key,
422 enabled: false,
423 })
424 const results1: Array<QueryObserverResult<string>> = []
425 const results2: Array<QueryObserverResult<string>> = []
426 const unsubscribe1 = observer.subscribe((x) => {
427 results1.push(x)
428 })
429 const unsubscribe2 = observer.subscribe((x) => {
430 results2.push(x)
431 })
432 await queryClient.fetchQuery({ queryKey: key, queryFn })
433 await sleep(50)
434 unsubscribe1()
435 unsubscribe2()
436 expect(queryFn).toHaveBeenCalledTimes(1)
437 expect(results1.length).toBe(2)
438 expect(results2.length).toBe(2)
439 expect(results1[0]).toMatchObject({ data: undefined })
440 expect(results1[1]).toMatchObject({ data: 'data' })
441 expect(results2[0]).toMatchObject({ data: undefined })
442 expect(results2[1]).toMatchObject({ data: 'data' })
443 })
444β
445 test('should stop retry when unsubscribing', async () => {
446 const key = queryKey()
447 let count = 0
448 const observer = new QueryObserver(queryClient, {
449 queryKey: key,
450 queryFn: () => {
451 count++
452 return Promise.reject<unknown>('reject')
453 },
454 retry: 10,
455 retryDelay: 50,
456 })
457 const unsubscribe = observer.subscribe(() => undefined)
458 await sleep(70)
459 unsubscribe()
460 await sleep(200)
461 expect(count).toBe(2)
462 })
463β
464 test('should clear interval when unsubscribing to a refetchInterval query', async () => {
465 const key = queryKey()
466 let count = 0
467β
468 const fetchData = () => {
469 count++
470 return Promise.resolve('data')
471 }
472 const observer = new QueryObserver(queryClient, {
473 queryKey: key,
474 queryFn: fetchData,
475 gcTime: 0,
476 refetchInterval: 10,
477 })
478 const unsubscribe = observer.subscribe(() => undefined)
479 expect(count).toBe(1)
480 await sleep(15)
481 expect(count).toBe(2)
482 unsubscribe()
483 await sleep(10)
484 expect(queryClient.getQueryCache().find({ queryKey: key })).toBeUndefined()
485 expect(count).toBe(2)
486 })
487β
488 test('uses placeholderData as non-cache data when pending a query with no data', async () => {
489 const key = queryKey()
490 const observer = new QueryObserver(queryClient, {
491 queryKey: key,
492 queryFn: () => 'data',
493 placeholderData: 'placeholder',
494 })
495β
496 expect(observer.getCurrentResult()).toMatchObject({
497 status: 'success',
498 data: 'placeholder',
499 })
500β
501 const results: Array<QueryObserverResult<unknown>> = []
502β
503 const unsubscribe = observer.subscribe((x) => {
504 results.push(x)
505 })
506β
507 await sleep(10)
508 unsubscribe()
509β
510 expect(results.length).toBe(2)
511 expect(results[0]).toMatchObject({ status: 'success', data: 'placeholder' })
512 expect(results[1]).toMatchObject({ status: 'success', data: 'data' })
513 })
514β
515 test('should structurally share placeholder data', async () => {
516 const key = queryKey()
517 const observer = new QueryObserver(queryClient, {
518 queryKey: key,
519 enabled: false,
520 queryFn: () => 'data',
521 placeholderData: {},
522 })
523β
524 const firstData = observer.getCurrentResult().data
525β
526 observer.setOptions({ placeholderData: {} })
527β
528 const secondData = observer.getCurrentResult().data
529β
530 expect(firstData).toBe(secondData)
531 })
532β
533 test('should throw an error if enabled option type is not valid', async () => {
534 const key = queryKey()
535β
536 expect(
537 () =>
538 new QueryObserver(queryClient, {
539 queryKey: key,
540 queryFn: () => 'data',
541 //@ts-expect-error
542 enabled: null,
543 }),
544 ).toThrowError('Expected enabled to be a boolean')
545 })
546β
547 test('getCurrentQuery should return the current query', async () => {
548 const key = queryKey()
549β
550 const observer = new QueryObserver(queryClient, {
551 queryKey: key,
552 queryFn: () => 'data',
553 })
554β
555 expect(observer.getCurrentQuery().queryKey).toEqual(key)
556 })
557β
558 test('should throw an error if throwOnError option is true', async () => {
559 const key = queryKey()
560β
561 const observer = new QueryObserver(queryClient, {
562 queryKey: key,
563 queryFn: () => Promise.reject<unknown>('error'),
564 retry: false,
565 })
566β
567 let error: string | null = null
568 try {
569 await observer.refetch({ throwOnError: true })
570 } catch (err) {
571 error = err as string
572 }
573β
574 expect(error).toEqual('error')
575 })
576β
577 test('should not refetch in background if refetchIntervalInBackground is false', async () => {
578 const key = queryKey()
579 const queryFn = vi.fn<Array<unknown>, string>().mockReturnValue('data')
580β
581 focusManager.setFocused(false)
582 const observer = new QueryObserver(queryClient, {
583 queryKey: key,
584 queryFn,
585 refetchIntervalInBackground: false,
586 refetchInterval: 10,
587 })
588β
589 const unsubscribe = observer.subscribe(() => undefined)
590 await sleep(30)
591β
592 expect(queryFn).toHaveBeenCalledTimes(1)
593β
594 // Clean-up
595 unsubscribe()
596 focusManager.setFocused(true)
597 })
598β
599 test('should not use replaceEqualDeep for select value when structuralSharing option is true', async () => {
600 const key = queryKey()
601β
602 const data = { value: 'data' }
603 const selectedData = { value: 'data' }
604β
605 const observer = new QueryObserver(queryClient, {
606 queryKey: key,
607 queryFn: () => data,
608 select: () => data,
609 })
610β
611 const unsubscribe = observer.subscribe(() => undefined)
612β
613 await sleep(10)
614 expect(observer.getCurrentResult().data).toBe(data)
615β
616 observer.setOptions({
617 queryKey: key,
618 queryFn: () => data,
619 structuralSharing: false,
620 select: () => selectedData,
621 })
622β
623 await observer.refetch()
624 expect(observer.getCurrentResult().data).toBe(selectedData)
625β
626 unsubscribe()
627 })
628β
629 test('should not use replaceEqualDeep for select value when structuralSharing option is true and placeholderdata is defined', () => {
630 const key = queryKey()
631β
632 const data = { value: 'data' }
633 const selectedData1 = { value: 'data' }
634 const selectedData2 = { value: 'data' }
635 const placeholderData1 = { value: 'data' }
636 const placeholderData2 = { value: 'data' }
637β
638 const observer = new QueryObserver(queryClient, {
639 queryKey: key,
640 queryFn: () => data,
641 select: () => data,
642 })
643β
644 observer.setOptions({
645 queryKey: key,
646 queryFn: () => data,
647 select: () => {
648 return selectedData1
649 },
650 placeholderData: placeholderData1,
651 })
652β
653 observer.setOptions({
654 queryKey: key,
655 queryFn: () => data,
656 select: () => {
657 return selectedData2
658 },
659 placeholderData: placeholderData2,
660 structuralSharing: false,
661 })
662β
663 expect(observer.getCurrentResult().data).toBe(selectedData2)
664 })
665β
666 test('should not use an undefined value returned by select as placeholderdata', () => {
667 const key = queryKey()
668β
669 const data = { value: 'data' }
670 const selectedData = { value: 'data' }
671 const placeholderData1 = { value: 'data' }
672 const placeholderData2 = { value: 'data' }
673β
674 const observer = new QueryObserver(queryClient, {
675 queryKey: key,
676 queryFn: () => data,
677 select: () => data,
678 })
679β
680 observer.setOptions({
681 queryKey: key,
682 queryFn: () => data,
683 select: () => {
684 return selectedData
685 },
686 placeholderData: placeholderData1,
687 })
688β
689 expect(observer.getCurrentResult().isPlaceholderData).toBe(true)
690β
691 observer.setOptions({
692 queryKey: key,
693 queryFn: () => data,
694 //@ts-expect-error
695 select: () => undefined,
696 placeholderData: placeholderData2,
697 })
698β
699 expect(observer.getCurrentResult().isPlaceholderData).toBe(false)
700 })
701β
702 test('should pass the correct previous queryKey (from prevQuery) to placeholderData function params with select', async () => {
703 const results: Array<QueryObserverResult> = []
704 const keys: Array<ReadonlyArray<unknown> | null> = []
705β
706 const key1 = queryKey()
707 const key2 = queryKey()
708β
709 const data1 = { value: 'data1' }
710 const data2 = { value: 'data2' }
711β
712 const observer = new QueryObserver(queryClient, {
713 queryKey: key1,
714 queryFn: () => data1,
715 placeholderData: (prev, prevQuery) => {
716 keys.push(prevQuery?.queryKey || null)
717 return prev
718 },
719 select: (data) => data.value,
720 })
721β
722 const unsubscribe = observer.subscribe((result) => {
723 results.push(result)
724 })
725β
726 await sleep(1)
727β
728 observer.setOptions({
729 queryKey: key2,
730 queryFn: () => data2,
731 placeholderData: (prev, prevQuery) => {
732 keys.push(prevQuery?.queryKey || null)
733 return prev
734 },
735 select: (data) => data.value,
736 })
737β
738 await sleep(1)
739 unsubscribe()
740 expect(results.length).toBe(4)
741 expect(keys.length).toBe(3)
742 expect(keys[0]).toBe(null) // First Query - status: 'pending', fetchStatus: 'idle'
743 expect(keys[1]).toBe(null) // First Query - status: 'pending', fetchStatus: 'fetching'
744 expect(keys[2]).toBe(key1) // Second Query - status: 'pending', fetchStatus: 'fetching'
745β
746 expect(results[0]).toMatchObject({
747 data: undefined,
748 status: 'pending',
749 fetchStatus: 'fetching',
750 }) // Initial fetch
751 expect(results[1]).toMatchObject({
752 data: 'data1',
753 status: 'success',
754 fetchStatus: 'idle',
755 }) // Successful fetch
756 expect(results[2]).toMatchObject({
757 data: 'data1',
758 status: 'success',
759 fetchStatus: 'fetching',
760 }) // Fetch for new key, but using previous data as placeholder
761 expect(results[3]).toMatchObject({
762 data: 'data2',
763 status: 'success',
764 fetchStatus: 'idle',
765 }) // Successful fetch for new key
766 })
767β
768 test('should pass the correct previous data to placeholderData function params when select function is used in conjunction', async () => {
769 const results: Array<QueryObserverResult> = []
770β
771 const key1 = queryKey()
772 const key2 = queryKey()
773β
774 const data1 = { value: 'data1' }
775 const data2 = { value: 'data2' }
776β
777 const observer = new QueryObserver(queryClient, {
778 queryKey: key1,
779 queryFn: () => data1,
780 placeholderData: (prev) => prev,
781 select: (data) => data.value,
782 })
783β
784 const unsubscribe = observer.subscribe((result) => {
785 results.push(result)
786 })
787β
788 await sleep(1)
789β
790 observer.setOptions({
791 queryKey: key2,
792 queryFn: () => data2,
793 placeholderData: (prev) => prev,
794 select: (data) => data.value,
795 })
796β
797 await sleep(1)
798 unsubscribe()
799β
800 expect(results.length).toBe(4)
801 expect(results[0]).toMatchObject({
802 data: undefined,
803 status: 'pending',
804 fetchStatus: 'fetching',
805 }) // Initial fetch
806 expect(results[1]).toMatchObject({
807 data: 'data1',
808 status: 'success',
809 fetchStatus: 'idle',
810 }) // Successful fetch
811 expect(results[2]).toMatchObject({
812 data: 'data1',
813 status: 'success',
814 fetchStatus: 'fetching',
815 }) // Fetch for new key, but using previous data as placeholder
816 expect(results[3]).toMatchObject({
817 data: 'data2',
818 status: 'success',
819 fetchStatus: 'idle',
820 }) // Successful fetch for new key
821 })
822β
823 test('setOptions should notify cache listeners', async () => {
824 const key = queryKey()
825β
826 const observer = new QueryObserver(queryClient, {
827 queryKey: key,
828 })
829β
830 const spy = vi.fn()
831 const unsubscribe = queryClient.getQueryCache().subscribe(spy)
832 observer.setOptions({ enabled: false })
833β
834 expect(spy).toHaveBeenCalledTimes(1)
835 expect(spy).toHaveBeenCalledWith(
836 expect.objectContaining({ type: 'observerOptionsUpdated' }),
837 )
838β
839 unsubscribe()
840 })
841})