@@ -54,6 +54,19 @@ describe('mutationOptions', () => {
5454 expect ( mutationOptions ( getter ) ) . toBe ( getter )
5555 } )
5656
57+ it ( 'should work when used with useMutation (getter without mutationKey in mutationOptions)' , async ( ) => {
58+ const mutationOpts = mutationOptions ( ( ) => ( {
59+ mutationFn : ( ) => sleep ( 10 ) . then ( ( ) => 'data' ) ,
60+ } ) )
61+
62+ const { mutate, data } = useMutation ( mutationOpts )
63+
64+ mutate ( )
65+ await vi . advanceTimersByTimeAsync ( 10 )
66+
67+ expect ( data . value ) . toEqual ( 'data' )
68+ } )
69+
5770 it ( 'should return the number of fetching mutations when used with useIsMutating (with mutationKey in mutationOptions)' , async ( ) => {
5871 const isMutatingArray : Array < number > = [ ]
5972 const mutationOpts = mutationOptions ( {
@@ -149,6 +162,27 @@ describe('mutationOptions', () => {
149162 expect ( isMutatingArray ) . toEqual ( [ 0 , 1 , 0 ] )
150163 } )
151164
165+ it ( 'should return the number of fetching mutations when used with useIsMutating (getter with mutationKey in mutationOptions)' , async ( ) => {
166+ const keyRef = ref ( 'isMutatingGetter' )
167+ const mutationOpts = mutationOptions ( ( ) => ( {
168+ mutationKey : [ keyRef . value ] ,
169+ mutationFn : ( ) => sleep ( 10 ) . then ( ( ) => 'data' ) ,
170+ } ) )
171+
172+ const { mutate } = useMutation ( mutationOpts )
173+ mutate ( )
174+
175+ const isMutating = useIsMutating ( {
176+ mutationKey : [ keyRef . value ] ,
177+ } )
178+
179+ await vi . advanceTimersByTimeAsync ( 0 )
180+ expect ( isMutating . value ) . toEqual ( 1 )
181+
182+ await vi . advanceTimersByTimeAsync ( 10 )
183+ expect ( isMutating . value ) . toEqual ( 0 )
184+ } )
185+
152186 it ( 'should return the number of fetching mutations when used with useIsMutating (getter without mutationKey in mutationOptions)' , async ( ) => {
153187 const isMutatingArray : Array < number > = [ ]
154188 const mutationOpts = mutationOptions ( ( ) => ( {
@@ -564,6 +598,25 @@ describe('mutationOptions', () => {
564598 expect ( states . value ) . toEqual ( [ 'data1' ] )
565599 } )
566600
601+ it ( 'should return mutation states when used with useMutationState (getter with mutationKey in mutationOptions)' , async ( ) => {
602+ const keyRef = ref ( 'useMutationStateGetter' )
603+ const mutationOpts = mutationOptions ( ( ) => ( {
604+ mutationKey : [ keyRef . value ] ,
605+ mutationFn : ( params : string ) => sleep ( 10 ) . then ( ( ) => params ) ,
606+ } ) )
607+
608+ const { mutate } = useMutation ( mutationOpts )
609+ mutate ( 'foo' )
610+
611+ const states = useMutationState ( {
612+ filters : { mutationKey : [ keyRef . value ] , status : 'pending' } ,
613+ select : ( mutation ) => mutation . state . variables ,
614+ } )
615+
616+ await vi . advanceTimersByTimeAsync ( 0 )
617+ expect ( states . value ) . toEqual ( [ 'foo' ] )
618+ } )
619+
567620 it ( 'should return mutation states when used with useMutationState (getter without mutationKey in mutationOptions)' , async ( ) => {
568621 const mutationOpts = mutationOptions ( ( ) => ( {
569622 mutationFn : ( ) => sleep ( 10 ) . then ( ( ) => 'data' ) ,
@@ -632,59 +685,6 @@ describe('mutationOptions', () => {
632685 expect ( states . value ) . toEqual ( [ 'data1' ] )
633686 } )
634687
635- it ( 'should work with getter passed to mutationOptions when used with useIsMutating' , async ( ) => {
636- const keyRef = ref ( 'isMutatingGetter' )
637- const mutationOpts = mutationOptions ( ( ) => ( {
638- mutationKey : [ keyRef . value ] ,
639- mutationFn : ( ) => sleep ( 10 ) . then ( ( ) => 'data' ) ,
640- } ) )
641-
642- const { mutate } = useMutation ( mutationOpts )
643- mutate ( )
644-
645- const isMutating = useIsMutating ( {
646- mutationKey : [ keyRef . value ] ,
647- } )
648-
649- await vi . advanceTimersByTimeAsync ( 0 )
650- expect ( isMutating . value ) . toEqual ( 1 )
651-
652- await vi . advanceTimersByTimeAsync ( 10 )
653- expect ( isMutating . value ) . toEqual ( 0 )
654- } )
655-
656- it ( 'should work with getter passed to mutationOptions when used with useMutationState' , async ( ) => {
657- const keyRef = ref ( 'useMutationStateGetter' )
658- const mutationOpts = mutationOptions ( ( ) => ( {
659- mutationKey : [ keyRef . value ] ,
660- mutationFn : ( params : string ) => sleep ( 10 ) . then ( ( ) => params ) ,
661- } ) )
662-
663- const { mutate } = useMutation ( mutationOpts )
664- mutate ( 'foo' )
665-
666- const states = useMutationState ( {
667- filters : { mutationKey : [ keyRef . value ] , status : 'pending' } ,
668- select : ( mutation ) => mutation . state . variables ,
669- } )
670-
671- await vi . advanceTimersByTimeAsync ( 0 )
672- expect ( states . value ) . toEqual ( [ 'foo' ] )
673- } )
674-
675- it ( 'should work with getter passed to mutationOptions without mutationKey' , async ( ) => {
676- const mutationOpts = mutationOptions ( ( ) => ( {
677- mutationFn : ( ) => sleep ( 10 ) . then ( ( ) => 'data' ) ,
678- } ) )
679-
680- const { mutate, data } = useMutation ( mutationOpts )
681-
682- mutate ( )
683- await vi . advanceTimersByTimeAsync ( 10 )
684-
685- expect ( data . value ) . toEqual ( 'data' )
686- } )
687-
688688 it ( 'should return data in a shallow ref when shallow is true' , async ( ) => {
689689 const mutationOpts = mutationOptions ( {
690690 mutationKey : [ 'key' ] ,
0 commit comments