@@ -659,6 +659,54 @@ describe('useMutation Hook', () => {
659659 expect ( onError ) . toHaveBeenCalledTimes ( 0 ) ;
660660 } ) ;
661661
662+ it ( 'prefers the onCompleted handler passed to the execution function rather than the hook' , async ( ) => {
663+ const CREATE_TODO_DATA = {
664+ createTodo : {
665+ id : 1 ,
666+ priority : 'Low' ,
667+ description : 'Get milk!' ,
668+ __typename : 'Todo' ,
669+ } ,
670+ } ;
671+ const variables = {
672+ priority : 'Low' ,
673+ description : 'Get milk.' ,
674+ }
675+ const mocks = [
676+ {
677+ request : {
678+ query : CREATE_TODO_MUTATION ,
679+ variables,
680+ } ,
681+ result : {
682+ data : CREATE_TODO_DATA
683+ } ,
684+ }
685+ ] ;
686+
687+ const hookOnCompleted = jest . fn ( ) ;
688+
689+ const { result } = renderHook (
690+ ( ) => useMutation ( CREATE_TODO_MUTATION , { onCompleted : hookOnCompleted } ) ,
691+ {
692+ wrapper : ( { children } ) => (
693+ < MockedProvider mocks = { mocks } >
694+ { children }
695+ </ MockedProvider >
696+ )
697+ } ,
698+ ) ;
699+
700+ const [ createTodo ] = result . current ;
701+ const onCompleted = jest . fn ( ) ;
702+ await act ( async ( ) => {
703+ await createTodo ( { variables, onCompleted } ) ;
704+ } ) ;
705+
706+ expect ( onCompleted ) . toHaveBeenCalledTimes ( 1 ) ;
707+ expect ( hookOnCompleted ) . not . toHaveBeenCalled ( ) ;
708+ } ) ;
709+
662710 it ( 'should allow passing an onError handler to the execution function' , async ( ) => {
663711 const errors = [ new GraphQLError ( CREATE_TODO_ERROR ) ] ;
664712 const variables = {
@@ -712,6 +760,46 @@ describe('useMutation Hook', () => {
712760 expect ( onError ) . toHaveBeenCalledWith ( errors [ 0 ] , expect . objectContaining ( { variables} ) ) ;
713761 } ) ;
714762
763+ it ( 'prefers the onError handler passed to the execution function instead of the hook' , async ( ) => {
764+ const variables = {
765+ priority : 'Low' ,
766+ description : 'Get milk.' ,
767+ }
768+ const mocks = [
769+ {
770+ request : {
771+ query : CREATE_TODO_MUTATION ,
772+ variables,
773+ } ,
774+ result : {
775+ errors : [ new GraphQLError ( CREATE_TODO_ERROR ) ] ,
776+ } ,
777+ }
778+ ] ;
779+
780+ const hookOnError = jest . fn ( ) ;
781+
782+ const { result } = renderHook (
783+ ( ) => useMutation ( CREATE_TODO_MUTATION , { onError : hookOnError } ) ,
784+ {
785+ wrapper : ( { children } ) => (
786+ < MockedProvider mocks = { mocks } >
787+ { children }
788+ </ MockedProvider >
789+ )
790+ } ,
791+ ) ;
792+
793+ const [ createTodo ] = result . current ;
794+ const onError = jest . fn ( ) ;
795+ await act ( async ( ) => {
796+ await createTodo ( { variables, onError } ) ;
797+ } ) ;
798+
799+ expect ( onError ) . toHaveBeenCalledTimes ( 1 ) ;
800+ expect ( hookOnError ) . not . toHaveBeenCalled ( ) ;
801+ } ) ;
802+
715803 it ( 'should allow updating onError while mutation is executing' , async ( ) => {
716804 const errors = [ new GraphQLError ( CREATE_TODO_ERROR ) ] ;
717805 const variables = {
0 commit comments