I'm trying to create widget tests for a flutter application using GraphQL. What I want to do is to test the behaviour of the app which depends on the result of a GraphQL Mutation on a user action.
This is a very simple example of the app:
class FirstScreen extends StatelessWidget {
@override
Widget return Container(
child: Mutation(
options: myMutationOptions,
onCompleted: (dynamic result) {
final bool myBool = result['bool'] as bool;
if (myBool) {
Navigator.of(context).push(MaterialPageRoute(builder: (context) => SecondScreen()));
} else {
Navigator.of(context).push(MaterialPageRoute(builder: (context) => ThirdScreen()));
}
},
builder: (RunMutation runMutation, QueryResult queryResult) {
return FlatButton(
child: Text('Button'),
onPressed: () async {
await runMutation(myParameters).networkResult;
},
);
},
),
);
}
What I would like to do is to mock the result of the mutation so in my widget tests, I can test that the button redirects to the SecondScreen
or ThirdScreen
depending of the result myBool
.
How can I do that ?
I finally managed to successfully mock a GraphQL Mutation. Here is how I did it, it is inspired from @Gpack's comment but I had to add some modifications and details to it.
To make it easy to use I created a wrapper widget
GraphQLMutationMocker
:Then it was pretty easy to write the tests