-
-
Notifications
You must be signed in to change notification settings - Fork 646
Expand file tree
/
Copy pathsearchbar_test.dart
More file actions
210 lines (188 loc) · 6.78 KB
/
searchbar_test.dart
File metadata and controls
210 lines (188 loc) · 6.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:getwidget/getwidget.dart';
void main() {
final Key searchBarKey = UniqueKey();
const List searchList = [
'Flutter',
'React',
'Ionic',
'Xamarin',
'Flutter2',
'Angular'
];
const notFound = 'oopsy...no items found';
final noItemsFound = Container(
child: const Text(notFound),
);
final decoration = InputDecoration(
enabledBorder: OutlineInputBorder(
borderSide: const BorderSide(
color: Colors.teal,
),
borderRadius: BorderRadius.circular(50)),
);
testWidgets('GF SearchBar can be constructed', (tester) async {
final GFSearchBar searchBar = GFSearchBar(
key: searchBarKey,
searchList: searchList,
overlaySearchListItemBuilder: (item) => Container(
padding: const EdgeInsets.all(8),
child: Text(
item,
style: const TextStyle(fontSize: 18),
),
),
searchQueryBuilder: (query, list) => list
.where((item) => item.toLowerCase().contains(query.toLowerCase()))
.toList(),
);
final TestApp app = TestApp(searchBar);
await tester.pumpWidget(app);
// find searchBar by key
expect(find.byKey(searchBarKey), findsOneWidget);
// tap the textField to display overlay search list items
await tester.tap(find.byType(TextField));
// rebuild the widget
await tester.pump();
// find overlay search list item
expect(find.text('${searchList[1]}'), findsOneWidget);
// enter 'flutter' to the textField
await tester.enterText(find.byWidget(searchBar), 'flutter');
// find the text 'flutter' in textField
expect(find.text('flutter'), findsOneWidget);
// find the text 'flutter' in overlay search list items
expect(find.widgetWithText(Container, 'flutter'), findsOneWidget);
// tap the close icon to close the overlay search list items
await tester.tap(find.byIcon(Icons.close));
// find the text 'flutter' in overlay search list items
expect(find.widgetWithText(Container, 'flutter'), findsNothing);
});
testWidgets('Can hide searchBox when item selected', (tester) async {
final GFSearchBar searchBar = GFSearchBar(
key: searchBarKey,
searchList: searchList,
overlaySearchListItemBuilder: (item) => Container(
padding: const EdgeInsets.all(8),
child: Text(
item,
style: const TextStyle(fontSize: 18),
),
),
hideSearchBoxWhenItemSelected: true,
searchQueryBuilder: (query, list) => list
.where((item) => item.toLowerCase().contains(query.toLowerCase()))
.toList(),
overlaySearchListHeight: 115,
);
final TestApp app = TestApp(searchBar);
await tester.pumpWidget(app);
// find searchBar by key
expect(find.byKey(searchBarKey), findsOneWidget);
// set searchBar.hideSearchBoxWhenItemSelected = true state to hide searchBar when item selected
expect(app.searchBar.hideSearchBoxWhenItemSelected, isTrue);
// tap the textField to display overlay search list items
await tester.tap(find.byType(TextField));
// rebuild the widget
await tester.pump();
// find overlay search list item
expect(find.text('${searchList[1]}'), findsOneWidget);
// tap to select item from overlay search list items
await tester.tap(find.text('${searchList[1]}'));
// rebuild the widget
await tester.pump();
// find searchBar
expect(find.byType(TextField), findsNothing);
expect(app.searchBar.hideSearchBoxWhenItemSelected, isTrue);
expect(app.searchBar.overlaySearchListHeight, 115);
});
testWidgets('On item selected and when item not found in GFSearchBar List',
(tester) async {
final GFSearchBar searchBar = GFSearchBar(
key: searchBarKey,
searchList: searchList,
overlaySearchListItemBuilder: (item) => Container(
padding: const EdgeInsets.all(8),
child: Text(
item,
style: const TextStyle(fontSize: 18),
),
),
searchQueryBuilder: (query, list) => list
.where((item) => item.toLowerCase().contains(query.toLowerCase()))
.toList(),
onItemSelected: (item) {
print('selected item $item');
},
noItemsFoundWidget: noItemsFound,
);
final TestApp app = TestApp(searchBar);
await tester.pumpWidget(app);
// find searchBar by key
expect(find.byKey(searchBarKey), findsOneWidget);
// tap the textField to display overlay search list items
await tester.tap(find.byType(TextField));
// rebuild the widget
await tester.pump();
// find overlay search list item
expect(find.text('${searchList[1]}'), findsOneWidget);
// enter 'flutter' to the textField
await tester.enterText(find.byWidget(searchBar), 'flu');
// find text 'flutter' in overlay search list item
expect(find.text('flu'), findsOneWidget);
// find text 'flutter' in overlay search list item
expect(find.text('${searchList[1]}'), findsOneWidget);
// tap to select 'flutter' item from overlay search list items
await tester.tap(find.text('${searchList[1]}'));
// rebuild the widget
await tester.pump();
// // find overlay search list
// expect(find.text('${searchList[1]}'), findsNothing);
// // enter 'dart' to the textField
// await tester.enterText(find.byWidget(searchBar), 'dart');
// // find text 'oopsy...no items found' in overlay search list item
// // expect(find.text(notFound), findsOneWidget);
expect(app.searchBar.noItemsFoundWidget, noItemsFound);
});
testWidgets('GFSearchBar with search box input decoration', (tester) async {
final GFSearchBar searchBar = GFSearchBar(
key: searchBarKey,
searchList: searchList,
overlaySearchListItemBuilder: (item) => Container(
padding: const EdgeInsets.all(8),
child: Text(
item,
style: const TextStyle(fontSize: 18),
),
),
searchQueryBuilder: (query, list) => list
.where((item) => item.toLowerCase().contains(query.toLowerCase()))
.toList(),
onItemSelected: (item) {
print('selected item $item');
},
searchBoxInputDecoration: decoration,
);
final TestApp app = TestApp(searchBar);
await tester.pumpWidget(app);
expect(app.searchBar.searchBoxInputDecoration, decoration);
});
}
class TestApp extends StatefulWidget {
const TestApp(this.searchBar);
final GFSearchBar searchBar;
@override
_TestAppState createState() => _TestAppState();
}
class _TestAppState extends State<TestApp> {
@override
Widget build(BuildContext context) => MaterialApp(
home: Scaffold(
body: Column(
children: [
widget.searchBar,
],
),
),
);
}