@@ -55,8 +55,6 @@ describe('injectMutation', () => {
5555 } ) )
5656 } )
5757
58- TestBed . tick ( )
59-
6058 mutation . mutate ( result )
6159 await vi . advanceTimersByTimeAsync ( 0 )
6260
@@ -389,11 +387,42 @@ describe('injectMutation', () => {
389387 expect ( mutation2 ! . options . mutationKey ) . toEqual ( [ 'fake' , 'updatedValue' ] )
390388 } )
391389
390+ test ( 'should have pending state when mutating in constructor' , async ( ) => {
391+ @Component ( {
392+ selector : 'app-fake' ,
393+ template : `
394+ <span>{{ mutation.isPending() ? 'pending' : 'not pending' }}</span>
395+ ` ,
396+ } )
397+ class FakeComponent {
398+ mutation = injectMutation ( ( ) => ( {
399+ mutationKey : [ 'fake' ] ,
400+ mutationFn : ( ) => sleep ( 10 ) . then ( ( ) => 'fake' ) ,
401+ } ) )
402+
403+ constructor ( ) {
404+ this . mutation . mutate ( )
405+ }
406+ }
407+
408+ const fixture = TestBed . createComponent ( FakeComponent )
409+ const { debugElement } = fixture
410+ const span = debugElement . query ( By . css ( 'span' ) )
411+
412+ vi . advanceTimersByTime ( 1 )
413+ expect ( span . nativeElement . textContent ) . toEqual ( 'pending' )
414+
415+ await vi . advanceTimersByTimeAsync ( 11 )
416+ fixture . detectChanges ( )
417+
418+ expect ( span . nativeElement . textContent ) . toEqual ( 'not pending' )
419+ } )
420+
392421 describe ( 'throwOnError' , ( ) => {
393422 test ( 'should evaluate throwOnError when mutation is expected to throw' , async ( ) => {
394423 const err = new Error ( 'Expected mock error. All is well!' )
395424 const boundaryFn = vi . fn ( )
396- const { mutate } = TestBed . runInInjectionContext ( ( ) => {
425+ const { mutate, status , error } = TestBed . runInInjectionContext ( ( ) => {
397426 return injectMutation ( ( ) => ( {
398427 mutationKey : [ 'fake' ] ,
399428 mutationFn : ( ) => {
@@ -403,20 +432,20 @@ describe('injectMutation', () => {
403432 } ) )
404433 } )
405434
406- TestBed . tick ( )
407-
408435 mutate ( )
409436
410437 await vi . advanceTimersByTimeAsync ( 0 )
411438
412439 expect ( boundaryFn ) . toHaveBeenCalledTimes ( 1 )
413440 expect ( boundaryFn ) . toHaveBeenCalledWith ( err )
441+ expect ( status ( ) ) . toBe ( 'error' )
442+ expect ( error ) . toBe ( err )
414443 } )
415444 } )
416445
417446 test ( 'should throw when throwOnError is true' , async ( ) => {
418447 const err = new Error ( 'Expected mock error. All is well!' )
419- const { mutateAsync } = TestBed . runInInjectionContext ( ( ) => {
448+ const { mutateAsync, status , error } = TestBed . runInInjectionContext ( ( ) => {
420449 return injectMutation ( ( ) => ( {
421450 mutationKey : [ 'fake' ] ,
422451 mutationFn : ( ) => {
@@ -427,11 +456,13 @@ describe('injectMutation', () => {
427456 } )
428457
429458 await expect ( ( ) => mutateAsync ( ) ) . rejects . toThrowError ( err )
459+ expect ( status ( ) ) . toBe ( 'error' )
460+ expect ( error ) . toBe ( err )
430461 } )
431462
432463 test ( 'should throw when throwOnError function returns true' , async ( ) => {
433464 const err = new Error ( 'Expected mock error. All is well!' )
434- const { mutateAsync } = TestBed . runInInjectionContext ( ( ) => {
465+ const { mutateAsync, status , error } = TestBed . runInInjectionContext ( ( ) => {
435466 return injectMutation ( ( ) => ( {
436467 mutationKey : [ 'fake' ] ,
437468 mutationFn : ( ) => {
@@ -442,6 +473,8 @@ describe('injectMutation', () => {
442473 } )
443474
444475 await expect ( ( ) => mutateAsync ( ) ) . rejects . toThrowError ( err )
476+ expect ( status ( ) ) . toBe ( 'error' )
477+ expect ( error ) . toBe ( err )
445478 } )
446479
447480 describe ( 'injection context' , ( ) => {
@@ -533,21 +566,8 @@ describe('injectMutation', () => {
533566 // Start mutation
534567 mutation . mutate ( 'retry-test' )
535568
536- // Synchronize pending effects for each retry attempt
537- TestBed . tick ( )
538- await Promise . resolve ( )
539- await vi . advanceTimersByTimeAsync ( 10 )
540-
541- TestBed . tick ( )
542- await Promise . resolve ( )
543- await vi . advanceTimersByTimeAsync ( 10 )
544-
545- TestBed . tick ( )
546-
547- const stablePromise = app . whenStable ( )
548- await Promise . resolve ( )
549- await vi . advanceTimersByTimeAsync ( 10 )
550- await stablePromise
569+ await vi . advanceTimersByTimeAsync ( 30 )
570+ await app . whenStable ( )
551571
552572 expect ( mutation . isSuccess ( ) ) . toBe ( true )
553573 expect ( mutation . data ( ) ) . toBe ( 'processed: retry-test' )
@@ -590,14 +610,8 @@ describe('injectMutation', () => {
590610 mutation1 . mutate ( 'test1' )
591611 mutation2 . mutate ( 'test2' )
592612
593- // Synchronize pending effects
594- TestBed . tick ( )
595-
596- const stablePromise = app . whenStable ( )
597- // Flush microtasks to allow TanStack Query's scheduled notifications to process
598- await Promise . resolve ( )
599613 await vi . advanceTimersByTimeAsync ( 1 )
600- await stablePromise
614+ await app . whenStable ( )
601615
602616 expect ( mutation1 . isSuccess ( ) ) . toBe ( true )
603617 expect ( mutation1 . data ( ) ) . toBe ( 'mutation1: test1' )
@@ -642,14 +656,8 @@ describe('injectMutation', () => {
642656 // Start mutation
643657 mutation . mutate ( 'test' )
644658
645- // Synchronize pending effects
646- TestBed . tick ( )
647-
648- const stablePromise = app . whenStable ( )
649- // Flush microtasks to allow TanStack Query's scheduled notifications to process
650- await Promise . resolve ( )
651659 await vi . advanceTimersByTimeAsync ( 1 )
652- await stablePromise
660+ await app . whenStable ( )
653661
654662 expect ( onMutateCalled ) . toBe ( true )
655663 expect ( onSuccessCalled ) . toBe ( true )
@@ -679,14 +687,8 @@ describe('injectMutation', () => {
679687 // Start mutation
680688 mutation . mutate ( 'test' )
681689
682- // Synchronize pending effects
683- TestBed . tick ( )
684-
685- const stablePromise = app . whenStable ( )
686- // Flush microtasks to allow TanStack Query's scheduled notifications to process
687- await Promise . resolve ( )
688690 await vi . advanceTimersByTimeAsync ( 1 )
689- await stablePromise
691+ await app . whenStable ( )
690692
691693 // Synchronous mutations complete immediately
692694 expect ( mutation . isSuccess ( ) ) . toBe ( true )
0 commit comments