@@ -350,6 +350,135 @@ describe('mutationOptions', () => {
350350 unsubscribe ( )
351351 } )
352352
353+ it ( 'should return the number of fetching mutations when used with queryClient.isMutating (getter with mutationKey in mutationOptions)' , async ( ) => {
354+ const isMutatingArray : Array < number > = [ ]
355+ const queryClient = useQueryClient ( )
356+ const mutationOpts = mutationOptions ( ( ) => ( {
357+ mutationKey : [ 'mutation' ] ,
358+ mutationFn : ( ) => sleep ( 500 ) . then ( ( ) => 'data' ) ,
359+ } ) )
360+
361+ const { mutate } = useMutation ( mutationOpts )
362+
363+ const resolvedOpts = mutationOpts ( )
364+
365+ const mutationCache = queryClient . getMutationCache ( )
366+ const unsubscribe = mutationCache . subscribe ( ( ) => {
367+ isMutatingArray . push ( queryClient . isMutating ( resolvedOpts ) )
368+ } )
369+
370+ isMutatingArray . push ( queryClient . isMutating ( resolvedOpts ) )
371+
372+ mutate ( )
373+ await vi . advanceTimersByTimeAsync ( 0 )
374+ // Use Math.max because subscribe callback count is implementation-dependent
375+ expect ( Math . max ( ...isMutatingArray ) ) . toEqual ( 1 )
376+ await vi . advanceTimersByTimeAsync ( 500 )
377+ expect ( isMutatingArray [ isMutatingArray . length - 1 ] ) . toEqual ( 0 )
378+
379+ unsubscribe ( )
380+ } )
381+
382+ it ( 'should return the number of fetching mutations when used with queryClient.isMutating (getter without mutationKey in mutationOptions)' , async ( ) => {
383+ const isMutatingArray : Array < number > = [ ]
384+ const queryClient = useQueryClient ( )
385+ const mutationOpts = mutationOptions ( ( ) => ( {
386+ mutationFn : ( ) => sleep ( 500 ) . then ( ( ) => 'data' ) ,
387+ } ) )
388+
389+ const { mutate } = useMutation ( mutationOpts )
390+
391+ const mutationCache = queryClient . getMutationCache ( )
392+ const unsubscribe = mutationCache . subscribe ( ( ) => {
393+ isMutatingArray . push ( queryClient . isMutating ( ) )
394+ } )
395+
396+ isMutatingArray . push ( queryClient . isMutating ( ) )
397+
398+ mutate ( )
399+ await vi . advanceTimersByTimeAsync ( 0 )
400+ // Use Math.max because subscribe callback count is implementation-dependent
401+ expect ( Math . max ( ...isMutatingArray ) ) . toEqual ( 1 )
402+ await vi . advanceTimersByTimeAsync ( 500 )
403+ expect ( isMutatingArray [ isMutatingArray . length - 1 ] ) . toEqual ( 0 )
404+
405+ unsubscribe ( )
406+ } )
407+
408+ it ( 'should return the number of fetching mutations when used with queryClient.isMutating (getter)' , async ( ) => {
409+ const isMutatingArray : Array < number > = [ ]
410+ const queryClient = useQueryClient ( )
411+ const mutationOpts1 = mutationOptions ( ( ) => ( {
412+ mutationKey : [ 'mutation' ] ,
413+ mutationFn : ( ) => sleep ( 500 ) . then ( ( ) => 'data1' ) ,
414+ } ) )
415+ const mutationOpts2 = mutationOptions ( ( ) => ( {
416+ mutationFn : ( ) => sleep ( 500 ) . then ( ( ) => 'data2' ) ,
417+ } ) )
418+
419+ const { mutate : mutate1 } = useMutation ( mutationOpts1 )
420+ const { mutate : mutate2 } = useMutation ( mutationOpts2 )
421+
422+ const mutationCache = queryClient . getMutationCache ( )
423+ const unsubscribe = mutationCache . subscribe ( ( ) => {
424+ isMutatingArray . push ( queryClient . isMutating ( ) )
425+ } )
426+
427+ isMutatingArray . push ( queryClient . isMutating ( ) )
428+
429+ mutate1 ( )
430+ mutate2 ( )
431+ await vi . advanceTimersByTimeAsync ( 0 )
432+ // Use Math.max because subscribe callback count is implementation-dependent
433+ expect ( Math . max ( ...isMutatingArray ) ) . toEqual ( 2 )
434+ await vi . advanceTimersByTimeAsync ( 500 )
435+ expect ( isMutatingArray [ isMutatingArray . length - 1 ] ) . toEqual ( 0 )
436+
437+ unsubscribe ( )
438+ } )
439+
440+ it ( 'should return the number of fetching mutations when used with queryClient.isMutating (getter, filter mutationOpts1.mutationKey)' , async ( ) => {
441+ const isMutatingArray : Array < number > = [ ]
442+ const queryClient = useQueryClient ( )
443+ const mutationOpts1 = mutationOptions ( ( ) => ( {
444+ mutationKey : [ 'mutation' ] ,
445+ mutationFn : ( ) => sleep ( 500 ) . then ( ( ) => 'data1' ) ,
446+ } ) )
447+ const mutationOpts2 = mutationOptions ( ( ) => ( {
448+ mutationFn : ( ) => sleep ( 500 ) . then ( ( ) => 'data2' ) ,
449+ } ) )
450+
451+ const resolvedOpts1 = mutationOpts1 ( )
452+
453+ const { mutate : mutate1 } = useMutation ( mutationOpts1 )
454+ const { mutate : mutate2 } = useMutation ( mutationOpts2 )
455+
456+ const mutationCache = queryClient . getMutationCache ( )
457+ const unsubscribe = mutationCache . subscribe ( ( ) => {
458+ isMutatingArray . push (
459+ queryClient . isMutating ( {
460+ mutationKey : resolvedOpts1 . mutationKey ,
461+ } ) ,
462+ )
463+ } )
464+
465+ isMutatingArray . push (
466+ queryClient . isMutating ( {
467+ mutationKey : resolvedOpts1 . mutationKey ,
468+ } ) ,
469+ )
470+
471+ mutate1 ( )
472+ mutate2 ( )
473+ await vi . advanceTimersByTimeAsync ( 0 )
474+ // Use Math.max because subscribe callback count is implementation-dependent
475+ expect ( Math . max ( ...isMutatingArray ) ) . toEqual ( 1 )
476+ await vi . advanceTimersByTimeAsync ( 500 )
477+ expect ( isMutatingArray [ isMutatingArray . length - 1 ] ) . toEqual ( 0 )
478+
479+ unsubscribe ( )
480+ } )
481+
353482 it ( 'should return mutation states when used with useMutationState (with mutationKey in mutationOptions)' , async ( ) => {
354483 const mutationOpts = mutationOptions ( {
355484 mutationKey : [ 'mutation' ] ,
0 commit comments