jeudi 11 avril 2019

Flutter Typeahead test

I'm using: https://pub.dartlang.org/packages/flutter_typeahead. This is my code:

  @override
  Widget build(BuildContext context) {
    return BlocBuilder(bloc: _bloc, builder: (_, state) {
      if (state is TypeHeadSearchStateInitialized) {
        suggestions = [];
        state.value.forEach((element) => suggestions.add(element.name));
      }
      return TypeAheadFormField(
        textFieldConfiguration: TextFieldConfiguration(
          controller: _typeAheadController,
          decoration: InputDecoration(
              prefixIcon: _icon,
              hintText: _hintText),
        ),
        suggestionsCallback: (String pattern) async {
          return suggestions;
        },
        itemBuilder: (context, suggestion) {
          if (suggestions.isNotEmpty)
            return ListTile(
              title: Text(suggestion),
            );
        },
        onSuggestionSelected: (suggestion) {
          this._typeAheadController.text = suggestion;
        },
        hideOnEmpty: true,
      );
    });
  }

And this is the test that I'm doing to check if the suggestions will be displayed after that the user has insert something in the textField:

  testWidgets('Should return a ListTile for suggestions', (WidgetTester tester) async {
    await _prepare(tester);

    when(_mockService.getAllSuggestions("Name")).thenAnswer((_) async => generateTestResults(1));

    await tester.enterText(find.byWidgetPredicate((Widget widget) => widget is TextField), "Name");
    await tester.pump();
    expect(find.byWidgetPredicate((Widget widget) => widget is ListTile), findsOneWidget);
  });

The bloc works fine, I debug it and it enters in the TypeHeadSearchStateInitialized, fulfilling the suggestions with

state.value.forEach((element) => suggestions.add(element.name));

but then, the build just returns TypeAheadFormField, not building the suggestions and the test fails.

Aucun commentaire:

Enregistrer un commentaire