vendredi 21 septembre 2018

Flutter: testing widgets

what I'm trying to test is: the user taps a button and the snackbar is shown for five second.

Here is the scaffold with its key:

final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>(debugLabel: "scaffoldState");

...

builder: (context, viewModel) => Scaffold(
              key: _scaffoldKey,

Then the code for the button and the snackBar:

child: FlatButton(key: Key('loginButton'), onPressed: () { 
 validate();

...

  validate() {
    // FormState object can be used to save, reset, and validate
    // every FormField that is a descendant of the associated Form.

    final FormState form = _formKey.currentState;

    if (form.validate()) {
      form.save();
      form.reset();

      //implementation of the snackBar:
      _scaffoldKey.currentState.showSnackBar(new SnackBar(
        duration: new Duration(seconds: 5)

...

And I am trying to test this behaviour in this way (I'm using also Flutter redux):

void main(){

  final store = Store<AppState>(reducer, initialState: AppState.initialState());
  testWidgets("MaterialApp local", (WidgetTester tester) async{
    await tester.pumpWidget(new StoreProvider(
        store: store,
        child: MaterialApp(
          home: LoginView(),
        )));
    await tester.pumpAndSettle();
    await tester.press(find.byKey(Key("loginButton")));
    expect();

I don't know what to put inside expect. I was trying to take the GlobalKey using find.ByKey(Key)) and to search for the debugLabel, but it doesn't seem to work. I'm trying since 2 days, but I can't find a way out.

Aucun commentaire:

Enregistrer un commentaire