|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:flutter_form_builder/flutter_form_builder.dart'; |
| 3 | +import 'package:flutter_test/flutter_test.dart'; |
| 4 | + |
| 5 | +import 'form_builder_tester.dart'; |
| 6 | + |
| 7 | +void main() { |
| 8 | + testWidgets('FormBuilderTypeahead -- Two', (WidgetTester tester) async { |
| 9 | + const options = ['One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven']; |
| 10 | + const initialTextValue = 'One'; |
| 11 | + const newTextValue = 'Two'; |
| 12 | + const textFieldName = 'typeahead1'; |
| 13 | + final textEditingController = TextEditingController(); |
| 14 | + final testWidgetKey = GlobalKey<FormBuilderFieldState>(); |
| 15 | + |
| 16 | + final testWidget = FormBuilderTypeAhead<String>( |
| 17 | + key: testWidgetKey, |
| 18 | + name: textFieldName, |
| 19 | + initialValue: initialTextValue, |
| 20 | + controller: textEditingController, |
| 21 | + itemBuilder: (context, country) { |
| 22 | + return ListTile(title: Text(country)); |
| 23 | + }, |
| 24 | + suggestionsCallback: (query) { |
| 25 | + if (query.isNotEmpty) { |
| 26 | + var lowercaseQuery = query.toLowerCase(); |
| 27 | + return options.where((country) { |
| 28 | + return country.toLowerCase().contains(lowercaseQuery); |
| 29 | + }).toList(growable: false) |
| 30 | + ..sort((a, b) => a |
| 31 | + .toLowerCase() |
| 32 | + .indexOf(lowercaseQuery) |
| 33 | + .compareTo(b.toLowerCase().indexOf(lowercaseQuery))); |
| 34 | + } else { |
| 35 | + return options; |
| 36 | + } |
| 37 | + }, |
| 38 | + ); |
| 39 | + final widgetFinder = find.byWidget(testWidget); |
| 40 | + |
| 41 | + await tester.pumpWidget(buildTestableFieldWidget(testWidget)); |
| 42 | + expect(formSave(), isTrue); |
| 43 | + expect(formValue(textFieldName), initialTextValue); |
| 44 | + |
| 45 | + // await tester.enterText(widgetFinder, newTextValue); |
| 46 | + textEditingController.text = newTextValue; |
| 47 | + expect(formSave(), isTrue); |
| 48 | + expect(formValue(textFieldName), equals(newTextValue)); |
| 49 | + |
| 50 | + // await tester.enterText(widgetFinder, newTextValue); |
| 51 | + testWidgetKey.currentState.didChange(initialTextValue); |
| 52 | + expect(textEditingController.text, initialTextValue); |
| 53 | + expect(formSave(), isTrue); |
| 54 | + expect(formValue(textFieldName), equals(initialTextValue)); |
| 55 | + |
| 56 | + // await tester.enterText(widgetFinder, ''); |
| 57 | + textEditingController.text = ''; |
| 58 | + expect(formSave(), isTrue); |
| 59 | + expect(formValue(textFieldName), isEmpty); |
| 60 | + }); |
| 61 | +} |
0 commit comments