@@ -3,19 +3,20 @@ import 'package:flutter_test/flutter_test.dart';
33import 'package:getwidget/getwidget.dart' ;
44
55void main () {
6+ final Key searchBarKey = UniqueKey ();
67 const List searchList = [
78 'Flutter' ,
89 'React' ,
910 'Ionic' ,
1011 'Xamarin' ,
1112 'Flutter2' ,
12- 'React2' ,
13- 'Ionic2' ,
14- 'Xamarin2' ,
13+ 'Angular'
1514 ];
1615
16+ const notFound = 'oopsy...no items found' ;
17+
1718 final noItemsFound = Container (
18- child: const Text ('no items found...' ),
19+ child: const Text (notFound ),
1920 );
2021
2122 final decoration = InputDecoration (
@@ -43,6 +44,7 @@ void main() {
4344
4445 testWidgets ('GF SearchBar can be constructed' , (tester) async {
4546 final GFSearchBar searchBar = GFSearchBar (
47+ key: searchBarKey,
4648 searchList: searchList,
4749 overlaySearchListItemBuilder: (item) => Container (
4850 padding: const EdgeInsets .all (8 ),
@@ -57,12 +59,31 @@ void main() {
5759 );
5860
5961 final TestApp app = TestApp (searchBar);
60-
6162 await tester.pumpWidget (app);
63+
64+ // find searchBar by key
65+ expect (find.byKey (searchBarKey), findsOneWidget);
66+ // tap the textField to display overlay search list items
67+ await tester.tap (find.byType (TextField ));
68+ // rebuild the widget
69+ await tester.pump ();
70+ // find overlay search list item
71+ expect (find.text ('${searchList [1 ]}' ), findsOneWidget);
72+ // enter 'flutter' to the textField
73+ await tester.enterText (find.byWidget (searchBar), 'flutter' );
74+ // find the text 'flutter' in textField
75+ expect (find.text ('flutter' ), findsOneWidget);
76+ // find the text 'flutter' in overlay search list items
77+ expect (find.widgetWithText (Container , 'flutter' ), findsOneWidget);
78+ // tap the close icon to close the overlay search list items
79+ await tester.tap (find.byIcon (Icons .close));
80+ // find the text 'flutter' in overlay search list items
81+ expect (find.widgetWithText (Container , 'flutter' ), findsNothing);
6282 });
6383
6484 testWidgets ('Can hide searchBox when item selected' , (tester) async {
6585 final GFSearchBar searchBar = GFSearchBar (
86+ key: searchBarKey,
6687 searchList: searchList,
6788 overlaySearchListItemBuilder: (item) => Container (
6889 padding: const EdgeInsets .all (8 ),
@@ -79,16 +100,33 @@ void main() {
79100 );
80101
81102 final TestApp app = TestApp (searchBar);
82-
83103 await tester.pumpWidget (app);
84104
105+ // find searchBar by key
106+ expect (find.byKey (searchBarKey), findsOneWidget);
107+ // set searchBar.hideSearchBoxWhenItemSelected = true state to hide searchBar when item selected
108+ expect (app.searchBar.hideSearchBoxWhenItemSelected, isTrue);
109+ // tap the textField to display overlay search list items
110+ await tester.tap (find.byType (TextField ));
111+ // rebuild the widget
112+ await tester.pump ();
113+ // find overlay search list item
114+ expect (find.text ('${searchList [1 ]}' ), findsOneWidget);
115+ // tap to select item from overlay search list items
116+ await tester.tap (find.text ('${searchList [1 ]}' ));
117+ // rebuild the widget
118+ await tester.pump ();
119+ // find searchBar
120+ expect (find.byType (TextField ), findsNothing);
121+
85122 expect (app.searchBar.hideSearchBoxWhenItemSelected, isTrue);
86123 expect (app.searchBar.overlaySearchListHeight, 115 );
87124 });
88125
89126 testWidgets ('On item selected and when item not found in GFSearchBar List' ,
90127 (tester) async {
91128 final GFSearchBar searchBar = GFSearchBar (
129+ key: searchBarKey,
92130 searchList: searchList,
93131 overlaySearchListItemBuilder: (item) => Container (
94132 padding: const EdgeInsets .all (8 ),
@@ -97,7 +135,6 @@ void main() {
97135 style: const TextStyle (fontSize: 18 ),
98136 ),
99137 ),
100- hideSearchBoxWhenItemSelected: true ,
101138 searchQueryBuilder: (query, list) => list
102139 .where ((item) => item.toLowerCase ().contains (query.toLowerCase ()))
103140 .toList (),
@@ -108,15 +145,39 @@ void main() {
108145 );
109146
110147 final TestApp app = TestApp (searchBar);
111-
112148 await tester.pumpWidget (app);
113149
114- expect (app.searchBar.hideSearchBoxWhenItemSelected, isTrue);
150+ // find searchBar by key
151+ expect (find.byKey (searchBarKey), findsOneWidget);
152+ // tap the textField to display overlay search list items
153+ await tester.tap (find.byType (TextField ));
154+ // rebuild the widget
155+ await tester.pump ();
156+ // find overlay search list item
157+ expect (find.text ('${searchList [1 ]}' ), findsOneWidget);
158+ // enter 'flutter' to the textField
159+ await tester.enterText (find.byWidget (searchBar), 'flu' );
160+ // find text 'flutter' in overlay search list item
161+ expect (find.text ('flu' ), findsOneWidget);
162+ // find text 'flutter' in overlay search list item
163+ expect (find.text ('${searchList [1 ]}' ), findsOneWidget);
164+ // tap to select 'flutter' item from overlay search list items
165+ await tester.tap (find.text ('${searchList [1 ]}' ));
166+ // rebuild the widget
167+ await tester.pump ();
168+ // find overlay search list
169+ expect (find.text ('${searchList [1 ]}' ), findsNothing);
170+ // enter 'dart' to the textField
171+ await tester.enterText (find.byWidget (searchBar), 'dart' );
172+ // find text 'oopsy...no items found' in overlay search list item
173+ // expect(find.text(notFound), findsOneWidget);
174+
115175 expect (app.searchBar.noItemsFoundWidget, noItemsFound);
116176 });
117177
118178 testWidgets ('GFSearchBar with search box input decoration' , (tester) async {
119179 final GFSearchBar searchBar = GFSearchBar (
180+ key: searchBarKey,
120181 searchList: searchList,
121182 overlaySearchListItemBuilder: (item) => Container (
122183 padding: const EdgeInsets .all (8 ),
@@ -125,23 +186,18 @@ void main() {
125186 style: const TextStyle (fontSize: 18 ),
126187 ),
127188 ),
128- hideSearchBoxWhenItemSelected: true ,
129189 searchQueryBuilder: (query, list) => list
130190 .where ((item) => item.toLowerCase ().contains (query.toLowerCase ()))
131191 .toList (),
132192 onItemSelected: (item) {
133193 print ('selected item $item ' );
134194 },
135- noItemsFoundWidget: noItemsFound,
136195 searchBoxInputDecoration: decoration,
137196 );
138197
139198 final TestApp app = TestApp (searchBar);
140-
141199 await tester.pumpWidget (app);
142200
143- expect (app.searchBar.hideSearchBoxWhenItemSelected, isTrue);
144- expect (app.searchBar.noItemsFoundWidget, noItemsFound);
145201 expect (app.searchBar.searchBoxInputDecoration, decoration);
146202 });
147203}
0 commit comments