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

pending-tasks.test.ts

815 lines24.2 KB

1import { HttpClient, provideHttpClient } from '@angular/common/http'
2import {
3 HttpTestingController,
4 provideHttpClientTesting,
5} from '@angular/common/http/testing'
6import {
7 ApplicationRef,
8 ChangeDetectionStrategy,
9 Component,
10} from '@angular/core'
11import { TestBed } from '@angular/core/testing'
12import { sleep } from '@tanstack/query-test-utils'
13import { lastValueFrom } from 'rxjs'
14import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
15import {
16 QueryClient,
17 injectMutation,
18 injectQuery,
19 onlineManager,
20 skipToken,
21} from '..'
22import { flushQueryUpdates, setupTanStackQueryTestBed } from './test-utils'
23​
24describe('PendingTasks Integration', () => {
25 let queryClient: QueryClient
26​
27 beforeEach(() => {
28 vi.useFakeTimers()
29​
30 queryClient = new QueryClient({
31 defaultOptions: {
32 queries: {
33 retry: false,
34 },
35 mutations: {
36 retry: false,
37 },
38 },
39 })
40​
41 setupTanStackQueryTestBed(queryClient)
42 })
43​
44 afterEach(() => {
45 onlineManager.setOnline(true)
46 queryClient.clear()
47 vi.useRealTimers()
48 })
49​
50 it.each([false, true])(
51 'keeps all mutation invocations pending (reset=%s)',
52 async (reset) => {
53 vi.useRealTimers()
54 const resolve = new Map<string, (value: string) => void>()
55 const mutation = TestBed.runInInjectionContext(() =>
56 injectMutation(() => ({
57 mutationFn: (id: string) =>
58 new Promise<string>((done) => resolve.set(id, done)),
59 })),
60 )
61 const first = mutation.mutateAsync('first')
62 const second = mutation.mutateAsync('second')
63 await Promise.resolve()
64 // oxlint-disable-next-line vitest-js/no-conditional-in-test -- The parameter selects setup/actions; every case runs the assertions.
65 if (reset) mutation.reset()
66 let stable = false
67 const stability = TestBed.inject(ApplicationRef)
68 .whenStable()
69 .then(() => {
70 stable = true
71 })
72 resolve.get('second')!('second')
73 await second
74 await Promise.resolve()
75 expect(stable).toBe(false)
76 expect(queryClient.isMutating()).toBe(1)
77 resolve.get('first')!('first')
78 await first
79 await stability
80 expect(stable).toBe(true)
81 },
82 )
83​
84 describe('Synchronous Resolution', () => {
85 it('should handle synchronous queryFn with whenStable()', async () => {
86 const app = TestBed.inject(ApplicationRef)
87​
88 @Component({
89 selector: 'app-test',
90 template: '',
91 changeDetection: ChangeDetectionStrategy.OnPush,
92 })
93 class TestComponent {
94 query = injectQuery(() => ({
95 queryKey: ['sync'],
96 queryFn: () => 'instant-data',
97 }))
98 }
99​
100 const fixture = TestBed.createComponent(TestComponent)
101 fixture.autoDetectChanges()
102 const query = fixture.componentInstance.query
103​
104 // Should start as pending even with synchronous data
105 expect(query.status()).toBe('pending')
106 expect(query.data()).toBeUndefined()
107​
108 const stablePromise = app.whenStable()
109 // Flush microtasks to allow TanStack Query's scheduled notifications to process
110 await Promise.resolve()
111 await vi.advanceTimersByTimeAsync(10)
112 await stablePromise
113​
114 expect(query.status()).toBe('success')
115 expect(query.data()).toBe('instant-data')
116 })
117​
118 it('should handle synchronous error with whenStable()', async () => {
119 const app = TestBed.inject(ApplicationRef)
120​
121 const query = TestBed.runInInjectionContext(() =>
122 injectQuery(() => ({
123 queryKey: ['sync-error'],
124 queryFn: () => {
125 throw new Error('instant-error')
126 },
127 })),
128 )
129​
130 const stablePromise = app.whenStable()
131 // Flush microtasks to allow TanStack Query's scheduled notifications to process
132 await Promise.resolve()
133 await vi.advanceTimersByTimeAsync(10)
134 await stablePromise
135 expect(query.status()).toBe('error')
136 expect(query.error()).toStrictEqual(new Error('instant-error'))
137 })
138​
139 it('should handle synchronous mutationFn with whenStable()', async () => {
140 const app = TestBed.inject(ApplicationRef)
141 let mutationFnCalled = false
142​
143 const mutation = TestBed.runInInjectionContext(() =>
144 injectMutation(() => ({
145 mutationFn: async (data: string) => {
146 mutationFnCalled = true
147 await Promise.resolve()
148 return `processed: ${data}`
149 },
150 })),
151 )
152​
153 mutation.mutate('test')
154 TestBed.tick()
155​
156 const stablePromise = app.whenStable()
157 await Promise.resolve()
158 await vi.advanceTimersByTimeAsync(10)
159 await stablePromise
160 expect(mutationFnCalled).toBe(true)
161 expect(mutation.isSuccess()).toBe(true)
162 expect(mutation.data()).toBe('processed: test')
163 })
164​
165 it('should handle synchronous mutation error with whenStable()', async () => {
166 const app = TestBed.inject(ApplicationRef)
167​
168 const mutation = TestBed.runInInjectionContext(() =>
169 injectMutation(() => ({
170 mutationFn: async () => {
171 await Promise.resolve()
172 throw new Error('sync-mutation-error')
173 },
174 })),
175 )
176​
177 mutation.mutate()
178 TestBed.tick()
179​
180 const stablePromise = app.whenStable()
181 await Promise.resolve()
182 await vi.advanceTimersByTimeAsync(10)
183 await stablePromise
184 expect(mutation.isError()).toBe(true)
185 expect(mutation.error()).toStrictEqual(new Error('sync-mutation-error'))
186 })
187​
188 it('should keep whenStable pending after mutate starts', async () => {
189 const app = TestBed.inject(ApplicationRef)
190​
191 const mutation = TestBed.runInInjectionContext(() =>
192 injectMutation(() => ({
193 mutationFn: async () => {
194 await sleep(20)
195 return 'done'
196 },
197 })),
198 )
199​
200 mutation.mutate()
201​
202 let stableResolved = false
203 const stablePromise = app.whenStable().then(() => {
204 stableResolved = true
205 })
206​
207 await Promise.resolve()
208 expect(stableResolved).toBe(false)
209​
210 await vi.advanceTimersByTimeAsync(21)
211 await vi.advanceTimersByTimeAsync(0)
212 await stablePromise
213 expect(mutation.isSuccess()).toBe(true)
214 expect(mutation.data()).toBe('done')
215 })
216 })
217​
218 describe('Race Conditions', () => {
219 it('should not register a pending task while query is disabled by enabled', async () => {
220 const queryFn = vi.fn<() => Promise<string>>(async () => {
221 await sleep(10)
222 return 'disabled-data'
223 })
224​
225 @Component({
226 template: '',
227 changeDetection: ChangeDetectionStrategy.OnPush,
228 })
229 class TestComponent {
230 query = injectQuery(() => ({
231 queryKey: ['disabled-enabled'],
232 queryFn,
233 enabled: false,
234 }))
235 }
236​
237 const fixture = TestBed.createComponent(TestComponent)
238 fixture.detectChanges()
239 const query = fixture.componentInstance.query
240​
241 await Promise.resolve()
242​
243 expect(query.status()).toBe('pending')
244 expect(query.fetchStatus()).toBe('idle')
245 expect(query.isPending()).toBe(true)
246 expect(queryFn).not.toHaveBeenCalled()
247 expect(fixture.isStable()).toBe(true)
248 })
249​
250 it('should not register a pending task while query is disabled by skipToken', async () => {
251 @Component({
252 template: '',
253 changeDetection: ChangeDetectionStrategy.OnPush,
254 })
255 class TestComponent {
256 query = injectQuery(() => ({
257 queryKey: ['disabled-skip-token'],
258 queryFn: skipToken,
259 }))
260 }
261​
262 const fixture = TestBed.createComponent(TestComponent)
263 fixture.detectChanges()
264 const query = fixture.componentInstance.query
265​
266 await Promise.resolve()
267​
268 expect(query.status()).toBe('pending')
269 expect(query.fetchStatus()).toBe('idle')
270 expect(query.isPending()).toBe(true)
271 expect(query.isEnabled()).toBe(false)
272 expect(fixture.isStable()).toBe(true)
273 })
274​
275 it('should handle query that completes during initial subscription', async () => {
276 const app = TestBed.inject(ApplicationRef)
277 let resolveQuery: (value: string) => void
278​
279 const queryPromise = new Promise<string>((resolve) => {
280 resolveQuery = resolve
281 })
282​
283 const query = TestBed.runInInjectionContext(() =>
284 injectQuery(() => ({
285 queryKey: ['race-condition'],
286 queryFn: () => queryPromise,
287 })),
288 )
289​
290 // Resolve immediately to create potential race condition
291 resolveQuery!('race-data')
292​
293 const stablePromise = app.whenStable()
294 await Promise.resolve()
295 await vi.advanceTimersByTimeAsync(10)
296 await stablePromise
297 expect(query.status()).toBe('success')
298 expect(query.data()).toBe('race-data')
299 })
300​
301 it('should handle rapid refetches without task leaks', async () => {
302 const app = TestBed.inject(ApplicationRef)
303​
304 @Component({
305 selector: 'app-test',
306 template: '',
307 changeDetection: ChangeDetectionStrategy.OnPush,
308 })
309 class TestComponent {
310 callCount = 0
311 query = injectQuery(() => ({
312 queryKey: ['rapid-refetch'],
313 queryFn: async () => {
314 this.callCount++
315 await sleep(10)
316 return `data-${this.callCount}`
317 },
318 }))
319 }
320​
321 const fixture = TestBed.createComponent(TestComponent)
322 fixture.autoDetectChanges()
323 const query = fixture.componentInstance.query
324​
325 // Trigger multiple rapid refetches
326 query.refetch()
327 query.refetch()
328 query.refetch()
329​
330 const stablePromise = app.whenStable()
331 await vi.advanceTimersByTimeAsync(20)
332 await stablePromise
333 expect(query.status()).toBe('success')
334 expect(query.data()).toMatch(/^data-\d+$/)
335 })
336​
337 it('should keep PendingTasks active when query starts offline (never reaches fetching)', async () => {
338 const app = TestBed.inject(ApplicationRef)
339​
340 onlineManager.setOnline(false)
341​
342 const query = TestBed.runInInjectionContext(() =>
343 injectQuery(() => ({
344 queryKey: ['start-offline'],
345 networkMode: 'online', // Default: won't fetch while offline
346 queryFn: async () => {
347 await sleep(10)
348 return 'online-data'
349 },
350 })),
351 )
352​
353 // Allow query to initialize
354 await Promise.resolve()
355 await flushQueryUpdates()
356​
357 // Query should initialize directly to 'paused' (never goes through 'fetching')
358 expect(query.status()).toBe('pending')
359 expect(query.fetchStatus()).toBe('paused')
360​
361 const stablePromise = app.whenStable()
362 let stableResolved = false
363 void stablePromise.then(() => {
364 stableResolved = true
365 })
366​
367 await Promise.resolve()
368​
369 // PendingTasks should block stability even though we never hit 'fetching'
370 expect(stableResolved).toBe(false)
371​
372 // Bring the app back online so the query can fetch
373 onlineManager.setOnline(true)
374​
375 await vi.advanceTimersByTimeAsync(20)
376 await Promise.resolve()
377​
378 await stablePromise
379​
380 expect(stableResolved).toBe(true)
381 expect(query.status()).toBe('success')
382 expect(query.data()).toBe('online-data')
383 })
384​
385 it('should keep PendingTasks active while query retry is paused offline', async () => {
386 const app = TestBed.inject(ApplicationRef)
387 let attempt = 0
388​
389 const query = TestBed.runInInjectionContext(() =>
390 injectQuery(() => ({
391 queryKey: ['paused-offline'],
392 retry: 1,
393 retryDelay: 50, // Longer delay to ensure we can go offline before retry
394 queryFn: async () => {
395 attempt++
396 if (attempt === 1) {
397 throw new Error('offline-fail')
398 }
399 await sleep(10)
400 return 'final-data'
401 },
402 })),
403 )
404​
405 // Allow the initial attempt to start and fail
406 await flushQueryUpdates()
407 await Promise.resolve()
408​
409 // Wait for the first attempt to complete and start retry delay
410 await vi.advanceTimersByTimeAsync(10)
411 await Promise.resolve()
412 expect(query.status()).toBe('pending')
413 expect(query.fetchStatus()).toBe('fetching')
414​
415 // Simulate the app going offline during retry delay
416 onlineManager.setOnline(false)
417​
418 // Advance past the retry delay to trigger the pause
419 await vi.advanceTimersByTimeAsync(50)
420 await Promise.resolve()
421 expect(query.fetchStatus()).toBe('paused')
422​
423 const stablePromise = app.whenStable()
424 let stableResolved = false
425 void stablePromise.then(() => {
426 stableResolved = true
427 })
428​
429 await Promise.resolve()
430​
431 // PendingTasks should continue blocking stability while the fetch is paused
432 expect(stableResolved).toBe(false)
433 expect(query.status()).toBe('pending')
434​
435 // Bring the app back online so the retry can continue
436 onlineManager.setOnline(true)
437​
438 // Give time for the retry to resume and complete
439 await vi.advanceTimersByTimeAsync(20)
440 await Promise.resolve()
441 await stablePromise
442 expect(stableResolved).toBe(true)
443 expect(query.status()).toBe('success')
444 expect(query.data()).toBe('final-data')
445 })
446 })
447​
448 describe('Component Destruction', () => {
449 it('becomes stable after destruction before an unresolved query completes', async () => {
450 vi.useRealTimers()
451 let resolveQuery!: (value: string) => void
452 const queryPromise = new Promise<string>((resolve) => {
453 resolveQuery = resolve
454 })
455​
456 @Component({ template: '' })
457 class UnresolvedQueryComponent {
458 query = injectQuery(() => ({
459 queryKey: ['unresolved-component-query'],
460 queryFn: () => queryPromise,
461 }))
462 }
463​
464 const fixture = TestBed.createComponent(UnresolvedQueryComponent)
465 fixture.detectChanges()
466 expect(fixture.isStable()).toBe(false)
467​
468 fixture.destroy()
469 await fixture.whenStable()
470 expect(fixture.isStable()).toBe(true)
471​
472 resolveQuery('eventual data')
473 await Promise.resolve()
474 })
475​
476 it('becomes stable after destruction before an unresolved mutation completes', async () => {
477 vi.useRealTimers()
478 let resolveMutation!: (value: string) => void
479 const mutationPromise = new Promise<string>((resolve) => {
480 resolveMutation = resolve
481 })
482​
483 @Component({ template: '' })
484 class UnresolvedMutationComponent {
485 mutation = injectMutation(() => ({
486 mutationFn: () => mutationPromise,
487 }))
488 }
489​
490 const fixture = TestBed.createComponent(UnresolvedMutationComponent)
491 fixture.detectChanges()
492 fixture.componentInstance.mutation.mutate()
493 expect(fixture.isStable()).toBe(false)
494​
495 fixture.destroy()
496 await fixture.whenStable()
497 expect(fixture.isStable()).toBe(true)
498​
499 resolveMutation('eventual data')
500 await Promise.resolve()
501 })
502 })
503​
504 describe('Concurrent Operations', () => {
505 it('should handle multiple queries running simultaneously', async () => {
506 const app = TestBed.inject(ApplicationRef)
507​
508 @Component({
509 selector: 'app-test',
510 template: '',
511 changeDetection: ChangeDetectionStrategy.OnPush,
512 })
513 class TestComponent {
514 query1 = injectQuery(() => ({
515 queryKey: ['concurrent-1'],
516 queryFn: async () => {
517 await sleep(30)
518 return 'data-1'
519 },
520 }))
521​
522 query2 = injectQuery(() => ({
523 queryKey: ['concurrent-2'],
524 queryFn: async () => {
525 await sleep(50)
526 return 'data-2'
527 },
528 }))
529​
530 query3 = injectQuery(() => ({
531 queryKey: ['concurrent-3'],
532 queryFn: () => 'instant-data',
533 }))
534 }
535​
536 const fixture = TestBed.createComponent(TestComponent)
537 fixture.autoDetectChanges()
538 const { query1, query2, query3 } = fixture.componentInstance
539​
540 expect(query1.status()).toBe('pending')
541 expect(query2.status()).toBe('pending')
542 expect(query3.status()).toBe('pending')
543​
544 const stablePromise = app.whenStable()
545 await vi.advanceTimersByTimeAsync(60)
546 await stablePromise
547​
548 expect(query1.status()).toBe('success')
549 expect(query1.data()).toBe('data-1')
550 expect(query2.status()).toBe('success')
551 expect(query2.data()).toBe('data-2')
552 expect(query3.status()).toBe('success')
553 expect(query3.data()).toBe('instant-data')
554 })
555​
556 it('should handle multiple mutations running simultaneously', async () => {
557 const app = TestBed.inject(ApplicationRef)
558​
559 const mutation1 = TestBed.runInInjectionContext(() =>
560 injectMutation(() => ({
561 mutationFn: async (data: string) => {
562 await sleep(30)
563 return `processed-1: ${data}`
564 },
565 })),
566 )
567​
568 const mutation2 = TestBed.runInInjectionContext(() =>
569 injectMutation(() => ({
570 mutationFn: async (data: string) => {
571 await sleep(50)
572 return `processed-2: ${data}`
573 },
574 })),
575 )
576​
577 const mutation3 = TestBed.runInInjectionContext(() =>
578 injectMutation(() => ({
579 mutationFn: async (data: string) => {
580 await Promise.resolve()
581 return `processed-3: ${data}`
582 },
583 })),
584 )
585​
586 mutation1.mutate('test1')
587 mutation2.mutate('test2')
588 mutation3.mutate('test3')
589 TestBed.tick()
590​
591 const stablePromise = app.whenStable()
592 await vi.advanceTimersByTimeAsync(60)
593 await stablePromise
594​
595 expect(mutation1.isSuccess()).toBe(true)
596 expect(mutation1.data()).toBe('processed-1: test1')
597 expect(mutation2.isSuccess()).toBe(true)
598 expect(mutation2.data()).toBe('processed-2: test2')
599 expect(mutation3.isSuccess()).toBe(true)
600 expect(mutation3.data()).toBe('processed-3: test3')
601 })
602​
603 it('should handle mixed queries and mutations', async () => {
604 const app = TestBed.inject(ApplicationRef)
605​
606 const query = TestBed.runInInjectionContext(() =>
607 injectQuery(() => ({
608 queryKey: ['mixed-query'],
609 queryFn: async () => {
610 await sleep(40)
611 return 'query-data'
612 },
613 })),
614 )
615​
616 const mutation = TestBed.runInInjectionContext(() =>
617 injectMutation(() => ({
618 mutationFn: async (data: string) => {
619 await sleep(60)
620 return `mutation: ${data}`
621 },
622 })),
623 )
624​
625 mutation.mutate('test')
626​
627 const stablePromise = app.whenStable()
628 await vi.advanceTimersByTimeAsync(70)
629 await stablePromise
630​
631 expect(query.status()).toBe('success')
632 expect(query.data()).toBe('query-data')
633 expect(mutation.isSuccess()).toBe(true)
634 expect(mutation.data()).toBe('mutation: test')
635 })
636 })
637​
638 describe('HttpClient Integration', () => {
639 beforeEach(() => {
640 setupTanStackQueryTestBed(queryClient, {
641 providers: [provideHttpClient(), provideHttpClientTesting()],
642 })
643 })
644​
645 it('should handle multiple HttpClient requests with lastValueFrom', async () => {
646 const app = TestBed.inject(ApplicationRef)
647 const httpClient = TestBed.inject(HttpClient)
648 const httpTestingController = TestBed.inject(HttpTestingController)
649​
650 const query1 = TestBed.runInInjectionContext(() =>
651 injectQuery(() => ({
652 queryKey: ['http-1'],
653 queryFn: () =>
654 lastValueFrom(httpClient.get<{ id: number }>('/api/1')),
655 })),
656 )
657​
658 const query2 = TestBed.runInInjectionContext(() =>
659 injectQuery(() => ({
660 queryKey: ['http-2'],
661 queryFn: () =>
662 lastValueFrom(httpClient.get<{ id: number }>('/api/2')),
663 })),
664 )
665​
666 setTimeout(() => {
667 const req1 = httpTestingController.expectOne('/api/1')
668 req1.flush({ id: 1 })
669​
670 const req2 = httpTestingController.expectOne('/api/2')
671 req2.flush({ id: 2 })
672 }, 10)
673​
674 const stablePromise = app.whenStable()
675 await vi.advanceTimersByTimeAsync(20)
676 await stablePromise
677 expect(query1.status()).toBe('success')
678 expect(query1.data()).toStrictEqual({ id: 1 })
679 expect(query2.status()).toBe('success')
680 expect(query2.data()).toStrictEqual({ id: 2 })
681​
682 httpTestingController.verify()
683 })
684​
685 it('should handle HttpClient request cancellation', async () => {
686 const app = TestBed.inject(ApplicationRef)
687 const httpClient = TestBed.inject(HttpClient)
688 const httpTestingController = TestBed.inject(HttpTestingController)
689​
690 const query = TestBed.runInInjectionContext(() =>
691 injectQuery(() => ({
692 queryKey: ['http-cancel'],
693 queryFn: () =>
694 lastValueFrom(httpClient.get<{ data: string }>('/api/cancel')),
695 })),
696 )
697​
698 // Cancel the request before it completes
699 setTimeout(() => {
700 const req = httpTestingController.expectOne('/api/cancel')
701 req.error(new ProgressEvent('error'), {
702 status: 0,
703 statusText: 'Unknown Error',
704 })
705 }, 10)
706​
707 const stablePromise = app.whenStable()
708 await vi.advanceTimersByTimeAsync(20)
709 await stablePromise
710 expect(query.status()).toBe('error')
711​
712 httpTestingController.verify()
713 })
714 })
715​
716 describe('Edge Cases', () => {
717 it('should handle query cancellation mid-flight', async () => {
718 const app = TestBed.inject(ApplicationRef)
719​
720 const query = TestBed.runInInjectionContext(() =>
721 injectQuery(() => ({
722 queryKey: ['cancel-test'],
723 queryFn: async () => {
724 await sleep(100)
725 return 'data'
726 },
727 })),
728 )
729​
730 // Cancel the query after a short delay
731 setTimeout(() => {
732 queryClient.cancelQueries({ queryKey: ['cancel-test'] })
733 }, 20)
734​
735 // Advance to the cancellation point
736 await vi.advanceTimersByTimeAsync(20)
737​
738 const stablePromise = app.whenStable()
739 await vi.advanceTimersByTimeAsync(130)
740 await stablePromise
741​
742 // Cancellation should restore the pre-fetch state
743 expect(query.status()).toBe('pending')
744 expect(query.fetchStatus()).toBe('idle')
745 })
746​
747 it('should handle query retry and pending task tracking', async () => {
748 const app = TestBed.inject(ApplicationRef)
749 let attemptCount = 0
750​
751 const query = TestBed.runInInjectionContext(() =>
752 injectQuery(() => ({
753 queryKey: ['retry-test'],
754 retry: 2,
755 retryDelay: 10,
756 queryFn: async () => {
757 attemptCount++
758 if (attemptCount <= 2) {
759 throw new Error(`Attempt ${attemptCount} failed`)
760 }
761 return 'success-data'
762 },
763 })),
764 )
765​
766 const stablePromise = app.whenStable()
767 await vi.advanceTimersByTimeAsync(50)
768 await stablePromise
769 expect(query.status()).toBe('success')
770 expect(query.data()).toBe('success-data')
771 expect(attemptCount).toBe(3) // Initial + 2 retries
772 })
773​
774 it('should handle mutation with optimistic updates', async () => {
775 const app = TestBed.inject(ApplicationRef)
776 const testQueryKey = ['optimistic-test']
777​
778 queryClient.setQueryData(testQueryKey, 'initial-data')
779​
780 const mutation = TestBed.runInInjectionContext(() =>
781 injectMutation(() => ({
782 mutationFn: async (newData: string) => {
783 await sleep(50)
784 return newData
785 },
786 onMutate: async (newData) => {
787 // Optimistic update
788 const previousData = queryClient.getQueryData(testQueryKey)
789 queryClient.setQueryData(testQueryKey, newData)
790 return { previousData }
791 },
792 onError: (_err, _newData, context) => {
793 // Rollback on error
794 if (context?.previousData) {
795 queryClient.setQueryData(testQueryKey, context.previousData)
796 }
797 },
798 })),
799 )
800​
801 mutation.mutate('optimistic-data')
802 await Promise.resolve()
803​
804 // Data should be optimistically updated immediately
805 expect(queryClient.getQueryData(testQueryKey)).toBe('optimistic-data')
806​
807 const stablePromise = app.whenStable()
808 await vi.advanceTimersByTimeAsync(60)
809 await stablePromise
810 expect(mutation.isSuccess()).toBe(true)
811 expect(mutation.data()).toBe('optimistic-data')
812 expect(queryClient.getQueryData(testQueryKey)).toBe('optimistic-data')
813 })
814 })
815})