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

inject-mutation.test.ts

759 lines23.1 KB

1import {
2 ApplicationRef,
3 Component,
4 Injector,
5 input,
6 provideZonelessChangeDetection,
7 signal,
8} from '@angular/core'
9import { TestBed } from '@angular/core/testing'
10import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
11import { By } from '@angular/platform-browser'
12import { render } from '@testing-library/angular'
13import { queryKey, sleep } from '@tanstack/query-test-utils'
14import { QueryClient, injectMutation, provideTanStackQuery } from '..'
15import { expectSignals, setFixtureSignalInputs } from './test-utils'
16​
17describe('injectMutation', () => {
18 let queryClient: QueryClient
19​
20 beforeEach(() => {
21 queryClient = new QueryClient()
22 vi.useFakeTimers()
23 TestBed.configureTestingModule({
24 providers: [
25 provideZonelessChangeDetection(),
26 provideTanStackQuery(queryClient),
27 ],
28 })
29 })
30​
31 afterEach(() => {
32 vi.useRealTimers()
33 })
34​
35 it('should be in idle state initially', () => {
36 const mutation = TestBed.runInInjectionContext(() => {
37 return injectMutation(() => ({
38 mutationFn: (params) => sleep(0).then(() => params),
39 }))
40 })
41​
42 expectSignals(mutation, {
43 isIdle: true,
44 isPending: false,
45 isError: false,
46 isSuccess: false,
47 })
48 })
49​
50 it('should change state after invoking mutate', async () => {
51 const result = 'Mock data'
52​
53 @Component({
54 template: `
55 <div>isIdle: {{ mutation.isIdle() }}</div>
56 <div>isPending: {{ mutation.isPending() }}</div>
57 <div>isError: {{ mutation.isError() }}</div>
58 <div>isSuccess: {{ mutation.isSuccess() }}</div>
59 <div>data: {{ mutation.data() ?? 'none' }}</div>
60 <div>error: {{ mutation.error()?.message ?? 'none' }}</div>
61 `,
62 })
63 class Page {
64 readonly mutation = injectMutation(() => ({
65 mutationFn: (params: string) => sleep(10).then(() => params),
66 }))
67 }
68​
69 const rendered = await render(Page)
70​
71 rendered.fixture.componentInstance.mutation.mutate(result)
72 await vi.advanceTimersByTimeAsync(0)
73 rendered.fixture.detectChanges()
74​
75 expect(rendered.getByText('isIdle: false')).toBeInTheDocument()
76 expect(rendered.getByText('isPending: true')).toBeInTheDocument()
77 expect(rendered.getByText('isError: false')).toBeInTheDocument()
78 expect(rendered.getByText('isSuccess: false')).toBeInTheDocument()
79 expect(rendered.getByText('data: none')).toBeInTheDocument()
80 expect(rendered.getByText('error: none')).toBeInTheDocument()
81 })
82​
83 it('should return error when request fails', async () => {
84 @Component({
85 template: `
86 <div>isIdle: {{ mutation.isIdle() }}</div>
87 <div>isPending: {{ mutation.isPending() }}</div>
88 <div>isError: {{ mutation.isError() }}</div>
89 <div>isSuccess: {{ mutation.isSuccess() }}</div>
90 <div>data: {{ mutation.data() ?? 'none' }}</div>
91 <div>error: {{ mutation.error()?.message ?? 'none' }}</div>
92 `,
93 })
94 class Page {
95 readonly mutation = injectMutation(() => ({
96 mutationFn: () =>
97 sleep(10).then(() => Promise.reject(new Error('Some error'))),
98 }))
99 }
100​
101 const rendered = await render(Page)
102​
103 rendered.fixture.componentInstance.mutation.mutate()
104​
105 await vi.advanceTimersByTimeAsync(11)
106 rendered.fixture.detectChanges()
107​
108 expect(rendered.getByText('isIdle: false')).toBeInTheDocument()
109 expect(rendered.getByText('isPending: false')).toBeInTheDocument()
110 expect(rendered.getByText('isError: true')).toBeInTheDocument()
111 expect(rendered.getByText('isSuccess: false')).toBeInTheDocument()
112 expect(rendered.getByText('data: none')).toBeInTheDocument()
113 expect(rendered.getByText('error: Some error')).toBeInTheDocument()
114 })
115​
116 it('should return data when request succeeds', async () => {
117 const result = 'Mock data'
118​
119 @Component({
120 template: `
121 <div>isIdle: {{ mutation.isIdle() }}</div>
122 <div>isPending: {{ mutation.isPending() }}</div>
123 <div>isError: {{ mutation.isError() }}</div>
124 <div>isSuccess: {{ mutation.isSuccess() }}</div>
125 <div>data: {{ mutation.data() ?? 'none' }}</div>
126 <div>error: {{ mutation.error()?.message ?? 'none' }}</div>
127 `,
128 })
129 class Page {
130 readonly mutation = injectMutation(() => ({
131 mutationFn: (params: string) => sleep(10).then(() => params),
132 }))
133 }
134​
135 const rendered = await render(Page)
136​
137 rendered.fixture.componentInstance.mutation.mutate(result)
138​
139 await vi.advanceTimersByTimeAsync(11)
140 rendered.fixture.detectChanges()
141​
142 expect(rendered.getByText('isIdle: false')).toBeInTheDocument()
143 expect(rendered.getByText('isPending: false')).toBeInTheDocument()
144 expect(rendered.getByText('isError: false')).toBeInTheDocument()
145 expect(rendered.getByText('isSuccess: true')).toBeInTheDocument()
146 expect(rendered.getByText(`data: ${result}`)).toBeInTheDocument()
147 expect(rendered.getByText('error: none')).toBeInTheDocument()
148 })
149​
150 it('should update mutation when reactive options change', () => {
151 const mutationCache = queryClient.getMutationCache()
152 // Signal will be updated before the mutation is called
153 // this test confirms that the mutation uses the updated value
154 const key1 = queryKey()
155 const key2 = queryKey()
156 const mutationKey = signal(key1)
157 const mutation = TestBed.runInInjectionContext(() => {
158 return injectMutation(() => ({
159 mutationKey: mutationKey(),
160 mutationFn: (params: string) => sleep(0).then(() => params),
161 }))
162 })
163​
164 mutationKey.set(key2)
165​
166 mutation.mutate('xyz')
167​
168 const mutations = mutationCache.find({ mutationKey: key2 })
169​
170 expect(mutations?.options.mutationKey).toEqual(key2)
171 })
172​
173 it('should reset state after invoking mutation.reset', async () => {
174 @Component({
175 template: `
176 <div>isIdle: {{ mutation.isIdle() }}</div>
177 <div>isPending: {{ mutation.isPending() }}</div>
178 <div>isError: {{ mutation.isError() }}</div>
179 <div>isSuccess: {{ mutation.isSuccess() }}</div>
180 <div>data: {{ mutation.data() ?? 'none' }}</div>
181 <div>error: {{ mutation.error()?.message ?? 'none' }}</div>
182 `,
183 })
184 class Page {
185 readonly mutation = injectMutation(() => ({
186 mutationFn: () =>
187 sleep(10).then(() => Promise.reject(new Error('Some error'))),
188 }))
189 }
190​
191 const rendered = await render(Page)
192​
193 rendered.fixture.componentInstance.mutation.mutate()
194​
195 await vi.advanceTimersByTimeAsync(11)
196 rendered.fixture.detectChanges()
197​
198 expect(rendered.getByText('isError: true')).toBeInTheDocument()
199 expect(rendered.getByText('error: Some error')).toBeInTheDocument()
200​
201 rendered.fixture.componentInstance.mutation.reset()
202​
203 await vi.advanceTimersByTimeAsync(0)
204 rendered.fixture.detectChanges()
205​
206 expect(rendered.getByText('isIdle: true')).toBeInTheDocument()
207 expect(rendered.getByText('isPending: false')).toBeInTheDocument()
208 expect(rendered.getByText('isError: false')).toBeInTheDocument()
209 expect(rendered.getByText('isSuccess: false')).toBeInTheDocument()
210 expect(rendered.getByText('data: none')).toBeInTheDocument()
211 expect(rendered.getByText('error: none')).toBeInTheDocument()
212 })
213​
214 describe('side effects', () => {
215 beforeEach(() => {
216 vi.clearAllMocks()
217 })
218​
219 it('should call onMutate when passed as an option', async () => {
220 const onMutate = vi.fn()
221 const mutation = TestBed.runInInjectionContext(() => {
222 return injectMutation(() => ({
223 mutationFn: (params: string) => sleep(10).then(() => params),
224 onMutate,
225 }))
226 })
227​
228 mutation.mutate('')
229​
230 await vi.advanceTimersByTimeAsync(0)
231​
232 expect(onMutate).toHaveBeenCalledTimes(1)
233 })
234​
235 it('should call onError when passed as an option', async () => {
236 const onError = vi.fn()
237 const mutation = TestBed.runInInjectionContext(() => {
238 return injectMutation(() => ({
239 mutationFn: (_params: string) =>
240 sleep(10).then(() => Promise.reject(new Error('Some error'))),
241 onError,
242 }))
243 })
244​
245 mutation.mutate('')
246​
247 await vi.advanceTimersByTimeAsync(11)
248​
249 expect(onError).toHaveBeenCalledTimes(1)
250 })
251​
252 it('should call onSuccess when passed as an option', async () => {
253 const onSuccess = vi.fn()
254 const mutation = TestBed.runInInjectionContext(() => {
255 return injectMutation(() => ({
256 mutationFn: (params: string) => sleep(10).then(() => params),
257 onSuccess,
258 }))
259 })
260​
261 mutation.mutate('')
262​
263 await vi.advanceTimersByTimeAsync(11)
264​
265 expect(onSuccess).toHaveBeenCalledTimes(1)
266 })
267​
268 it('should call onSettled when passed as an option', async () => {
269 const onSettled = vi.fn()
270 const mutation = TestBed.runInInjectionContext(() => {
271 return injectMutation(() => ({
272 mutationFn: (params: string) => sleep(10).then(() => params),
273 onSettled,
274 }))
275 })
276​
277 mutation.mutate('')
278​
279 await vi.advanceTimersByTimeAsync(11)
280​
281 expect(onSettled).toHaveBeenCalledTimes(1)
282 })
283​
284 it('should call onError when passed as an argument of mutate function', async () => {
285 const onError = vi.fn()
286 const mutation = TestBed.runInInjectionContext(() => {
287 return injectMutation(() => ({
288 mutationFn: (_params: string) =>
289 sleep(10).then(() => Promise.reject(new Error('Some error'))),
290 }))
291 })
292​
293 mutation.mutate('', { onError })
294​
295 await vi.advanceTimersByTimeAsync(11)
296​
297 expect(onError).toHaveBeenCalledTimes(1)
298 })
299​
300 it('should call onSuccess when passed as an argument of mutate function', async () => {
301 const onSuccess = vi.fn()
302 const mutation = TestBed.runInInjectionContext(() => {
303 return injectMutation(() => ({
304 mutationFn: (params: string) => sleep(10).then(() => params),
305 }))
306 })
307​
308 mutation.mutate('', { onSuccess })
309​
310 await vi.advanceTimersByTimeAsync(11)
311​
312 expect(onSuccess).toHaveBeenCalledTimes(1)
313 })
314​
315 it('should call onSettled when passed as an argument of mutate function', async () => {
316 const onSettled = vi.fn()
317 const mutation = TestBed.runInInjectionContext(() => {
318 return injectMutation(() => ({
319 mutationFn: (params: string) => sleep(10).then(() => params),
320 }))
321 })
322​
323 mutation.mutate('', { onSettled })
324​
325 await vi.advanceTimersByTimeAsync(11)
326​
327 expect(onSettled).toHaveBeenCalledTimes(1)
328 })
329​
330 it('should fire both onSettled functions', async () => {
331 const onSettled = vi.fn()
332 const onSettledOnFunction = vi.fn()
333 const mutation = TestBed.runInInjectionContext(() => {
334 return injectMutation(() => ({
335 mutationFn: (params: string) => sleep(10).then(() => params),
336 onSettled,
337 }))
338 })
339​
340 mutation.mutate('', { onSettled: onSettledOnFunction })
341​
342 await vi.advanceTimersByTimeAsync(11)
343​
344 expect(onSettled).toHaveBeenCalledTimes(1)
345 expect(onSettledOnFunction).toHaveBeenCalledTimes(1)
346 })
347 })
348​
349 it('should support required signal inputs', async () => {
350 const mutationCache = queryClient.getMutationCache()
351​
352 @Component({
353 selector: 'app-fake',
354 template: `
355 <button (click)="mutate()"></button>
356 <span>{{ mutation.data() }}</span>
357 `,
358 })
359 class FakeComponent {
360 name = input.required<string>()
361​
362 mutation = injectMutation(() => ({
363 mutationKey: ['fake', this.name()],
364 mutationFn: () => sleep(10).then(() => this.name()),
365 }))
366​
367 mutate(): void {
368 this.mutation.mutate()
369 }
370 }
371​
372 const fixture = TestBed.createComponent(FakeComponent)
373 const { debugElement } = fixture
374 setFixtureSignalInputs(fixture, { name: 'value' })
375​
376 const button = debugElement.query(By.css('button'))
377 button.triggerEventHandler('click')
378​
379 await vi.advanceTimersByTimeAsync(11)
380 fixture.detectChanges()
381​
382 const text = debugElement.query(By.css('span')).nativeElement.textContent
383 expect(text).toEqual('value')
384 const mutation = mutationCache.find({ mutationKey: ['fake', 'value'] })
385 expect(mutation).toBeDefined()
386 expect(mutation!.options.mutationKey).toStrictEqual(['fake', 'value'])
387 })
388​
389 it('should update options on required signal input change', async () => {
390 const mutationCache = queryClient.getMutationCache()
391​
392 @Component({
393 selector: 'app-fake',
394 template: `
395 <button (click)="mutate()"></button>
396 <span>{{ mutation.data() }}</span>
397 `,
398 })
399 class FakeComponent {
400 name = input.required<string>()
401​
402 mutation = injectMutation(() => ({
403 mutationKey: ['fake', this.name()],
404 mutationFn: () => sleep(10).then(() => this.name()),
405 }))
406​
407 mutate(): void {
408 this.mutation.mutate()
409 }
410 }
411​
412 const fixture = TestBed.createComponent(FakeComponent)
413 const { debugElement } = fixture
414 setFixtureSignalInputs(fixture, { name: 'value' })
415​
416 const button = debugElement.query(By.css('button'))
417 const span = debugElement.query(By.css('span'))
418​
419 button.triggerEventHandler('click')
420 await vi.advanceTimersByTimeAsync(11)
421 fixture.detectChanges()
422​
423 expect(span.nativeElement.textContent).toEqual('value')
424​
425 setFixtureSignalInputs(fixture, { name: 'updatedValue' })
426​
427 button.triggerEventHandler('click')
428 await vi.advanceTimersByTimeAsync(11)
429 fixture.detectChanges()
430​
431 expect(span.nativeElement.textContent).toEqual('updatedValue')
432​
433 const mutations = mutationCache.findAll()
434 expect(mutations.length).toBe(2)
435 const [mutation1, mutation2] = mutations
436 expect(mutation1!.options.mutationKey).toEqual(['fake', 'value'])
437 expect(mutation2!.options.mutationKey).toEqual(['fake', 'updatedValue'])
438 })
439​
440 describe('throwOnError', () => {
441 it('should evaluate throwOnError when mutation is expected to throw', async () => {
442 const key = queryKey()
443 const err = new Error('Expected mock error. All is well!')
444 const boundaryFn = vi.fn()
445 const { mutate } = TestBed.runInInjectionContext(() => {
446 return injectMutation(() => ({
447 mutationKey: key,
448 mutationFn: () => {
449 return Promise.reject(err)
450 },
451 throwOnError: boundaryFn,
452 }))
453 })
454​
455 TestBed.tick()
456​
457 mutate()
458​
459 await vi.advanceTimersByTimeAsync(0)
460​
461 expect(boundaryFn).toHaveBeenCalledTimes(1)
462 expect(boundaryFn).toHaveBeenCalledWith(err)
463 })
464​
465 it('should throw when throwOnError is true and mutate is used', async () => {
466 const key = queryKey()
467 const { mutate } = TestBed.runInInjectionContext(() => {
468 return injectMutation(() => ({
469 mutationKey: key,
470 mutationFn: () => {
471 return Promise.reject(
472 new Error('Expected mock error. All is well!'),
473 )
474 },
475 throwOnError: true,
476 }))
477 })
478​
479 TestBed.tick()
480​
481 mutate()
482​
483 await expect(vi.advanceTimersByTimeAsync(0)).rejects.toThrow(
484 'Expected mock error. All is well!',
485 )
486 })
487 })
488​
489 it('should throw when throwOnError is true', async () => {
490 const key = queryKey()
491 const err = new Error('Expected mock error. All is well!')
492 const { mutateAsync } = TestBed.runInInjectionContext(() => {
493 return injectMutation(() => ({
494 mutationKey: key,
495 mutationFn: () => {
496 return Promise.reject(err)
497 },
498 throwOnError: true,
499 }))
500 })
501​
502 await expect(() => mutateAsync()).rejects.toThrow(err)
503 })
504​
505 it('should throw when throwOnError function returns true', async () => {
506 const key = queryKey()
507 const err = new Error('Expected mock error. All is well!')
508 const { mutateAsync } = TestBed.runInInjectionContext(() => {
509 return injectMutation(() => ({
510 mutationKey: key,
511 mutationFn: () => {
512 return Promise.reject(err)
513 },
514 throwOnError: () => true,
515 }))
516 })
517​
518 await expect(() => mutateAsync()).rejects.toThrow(err)
519 })
520​
521 it('should resolve mutateAsync with the value returned from mutationFn', async () => {
522 const key = queryKey()
523 const { mutateAsync } = TestBed.runInInjectionContext(() => {
524 return injectMutation(() => ({
525 mutationKey: key,
526 mutationFn: (params: string) => sleep(10).then(() => params),
527 }))
528 })
529​
530 const promise = mutateAsync('Mock data')
531 await vi.advanceTimersByTimeAsync(11)
532​
533 await expect(promise).resolves.toBe('Mock data')
534 })
535​
536 describe('injection context', () => {
537 it('should throw NG0203 with descriptive error outside injection context', () => {
538 const key = queryKey()
539 expect(() => {
540 injectMutation(() => ({
541 mutationKey: key,
542 mutationFn: () => Promise.resolve(),
543 }))
544 }).toThrow(/NG0203(.*?)injectMutation/)
545 })
546​
547 it('should be usable outside injection context when passing an injector', () => {
548 const key = queryKey()
549 expect(() => {
550 injectMutation(
551 () => ({
552 mutationKey: key,
553 mutationFn: () => Promise.resolve(),
554 }),
555 {
556 injector: TestBed.inject(Injector),
557 },
558 )
559 }).not.toThrow()
560 })
561​
562 it('should complete mutation before whenStable() resolves', async () => {
563 const app = TestBed.inject(ApplicationRef)
564 let mutationStarted = false
565 let mutationCompleted = false
566​
567 const key = queryKey()
568 const mutation = TestBed.runInInjectionContext(() =>
569 injectMutation(() => ({
570 mutationKey: key,
571 mutationFn: async (data: string) => {
572 mutationStarted = true
573 await sleep(50)
574 mutationCompleted = true
575 return `processed: ${data}`
576 },
577 })),
578 )
579​
580 // Initial state
581 expect(mutation.data()).toBeUndefined()
582 expect(mutationStarted).toBe(false)
583​
584 // Start mutation
585 mutation.mutate('test')
586​
587 // Wait for mutation to start and Angular to be "stable"
588 const stablePromise = app.whenStable()
589 await vi.advanceTimersByTimeAsync(60)
590 await stablePromise
591​
592 // After whenStable(), mutation should be complete
593 expect(mutationStarted).toBe(true)
594 expect(mutationCompleted).toBe(true)
595 expect(mutation.isSuccess()).toBe(true)
596 expect(mutation.data()).toBe('processed: test')
597 })
598​
599 it('should handle synchronous mutation with retry', async () => {
600 const app = TestBed.inject(ApplicationRef)
601 let attemptCount = 0
602​
603 const mutation = TestBed.runInInjectionContext(() =>
604 injectMutation(() => ({
605 retry: 2,
606 retryDelay: 0, // No delay for synchronous retry
607 mutationFn: async (data: string) => {
608 attemptCount++
609 if (attemptCount <= 2) {
610 throw new Error(`Sync attempt ${attemptCount} failed`)
611 }
612 return `processed: ${data}`
613 },
614 })),
615 )
616​
617 // Start mutation
618 mutation.mutate('retry-test')
619​
620 // Synchronize pending effects for each retry attempt
621 TestBed.tick()
622 await Promise.resolve()
623 await vi.advanceTimersByTimeAsync(10)
624​
625 TestBed.tick()
626 await Promise.resolve()
627 await vi.advanceTimersByTimeAsync(10)
628​
629 TestBed.tick()
630​
631 const stablePromise = app.whenStable()
632 await Promise.resolve()
633 await vi.advanceTimersByTimeAsync(10)
634 await stablePromise
635​
636 expect(mutation.isSuccess()).toBe(true)
637 expect(mutation.data()).toBe('processed: retry-test')
638 expect(attemptCount).toBe(3) // Initial + 2 retries
639 })
640​
641 it('should handle multiple synchronous mutations on same key', async () => {
642 const app = TestBed.inject(ApplicationRef)
643 let callCount = 0
644​
645 const key = queryKey()
646​
647 const mutation1 = TestBed.runInInjectionContext(() =>
648 injectMutation(() => ({
649 mutationKey: key,
650 mutationFn: async (data: string) => {
651 callCount++
652 return `mutation1: ${data}`
653 },
654 })),
655 )
656​
657 const mutation2 = TestBed.runInInjectionContext(() =>
658 injectMutation(() => ({
659 mutationKey: key,
660 mutationFn: async (data: string) => {
661 callCount++
662 return `mutation2: ${data}`
663 },
664 })),
665 )
666​
667 // Start both mutations
668 mutation1.mutate('test1')
669 mutation2.mutate('test2')
670​
671 // Synchronize pending effects
672 TestBed.tick()
673​
674 const stablePromise = app.whenStable()
675 // Flush microtasks to allow TanStack Query's scheduled notifications to process
676 await Promise.resolve()
677 await vi.advanceTimersByTimeAsync(1)
678 await stablePromise
679​
680 expect(mutation1.isSuccess()).toBe(true)
681 expect(mutation1.data()).toBe('mutation1: test1')
682 expect(mutation2.isSuccess()).toBe(true)
683 expect(mutation2.data()).toBe('mutation2: test2')
684 expect(callCount).toBe(2)
685 })
686​
687 it('should handle synchronous mutation with optimistic updates', async () => {
688 const app = TestBed.inject(ApplicationRef)
689 const testQueryKey = queryKey()
690 let onMutateCalled = false
691 let onSuccessCalled = false
692​
693 // Set initial data
694 queryClient.setQueryData(testQueryKey, 'initial')
695​
696 const mutation = TestBed.runInInjectionContext(() =>
697 injectMutation(() => ({
698 mutationFn: async (data: string) => `final: ${data}`, // Synchronous resolution
699 onMutate: async (variables) => {
700 onMutateCalled = true
701 const previousData = queryClient.getQueryData(testQueryKey)
702 queryClient.setQueryData(testQueryKey, `optimistic: ${variables}`)
703 return { previousData }
704 },
705 onSuccess: (data) => {
706 onSuccessCalled = true
707 queryClient.setQueryData(testQueryKey, data)
708 },
709 })),
710 )
711​
712 // Start mutation
713 mutation.mutate('test')
714​
715 // Synchronize pending effects
716 TestBed.tick()
717​
718 const stablePromise = app.whenStable()
719 // Flush microtasks to allow TanStack Query's scheduled notifications to process
720 await Promise.resolve()
721 await vi.advanceTimersByTimeAsync(1)
722 await stablePromise
723​
724 expect(onMutateCalled).toBe(true)
725 expect(onSuccessCalled).toBe(true)
726 expect(mutation.isSuccess()).toBe(true)
727 expect(mutation.data()).toBe('final: test')
728 expect(queryClient.getQueryData(testQueryKey)).toBe('final: test')
729 })
730​
731 it('should handle synchronous mutation cancellation', async () => {
732 const app = TestBed.inject(ApplicationRef)
733​
734 const key = queryKey()
735 const mutation = TestBed.runInInjectionContext(() =>
736 injectMutation(() => ({
737 mutationKey: key,
738 mutationFn: async (data: string) => `processed: ${data}`, // Synchronous resolution
739 })),
740 )
741​
742 // Start mutation
743 mutation.mutate('test')
744​
745 // Synchronize pending effects
746 TestBed.tick()
747​
748 const stablePromise = app.whenStable()
749 // Flush microtasks to allow TanStack Query's scheduled notifications to process
750 await Promise.resolve()
751 await vi.advanceTimersByTimeAsync(1)
752 await stablePromise
753​
754 // Synchronous mutations complete immediately
755 expect(mutation.isSuccess()).toBe(true)
756 expect(mutation.data()).toBe('processed: test')
757 })
758 })
759})