|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:flutter_test/flutter_test.dart'; |
| 3 | +import 'package:getwidget/getwidget.dart'; |
| 4 | + |
| 5 | +void main() { |
| 6 | + const List searchList = [ |
| 7 | + 'Flutter', |
| 8 | + 'React', |
| 9 | + 'Ionic', |
| 10 | + 'Xamarin', |
| 11 | + 'Flutter2', |
| 12 | + 'React2', |
| 13 | + 'Ionic2', |
| 14 | + 'Xamarin2', |
| 15 | + ]; |
| 16 | + |
| 17 | + final noItemsFound = Container( |
| 18 | + child: const Text('no items found...'), |
| 19 | + ); |
| 20 | + |
| 21 | + final decoration = InputDecoration( |
| 22 | + enabledBorder: OutlineInputBorder( |
| 23 | + borderSide: const BorderSide( |
| 24 | + color: Colors.teal, |
| 25 | + ), |
| 26 | + borderRadius: BorderRadius.circular(50)), |
| 27 | + ); |
| 28 | + |
| 29 | + testWidgets( |
| 30 | + 'GF SearchBar without searchList, overlaySearchListItemBuilder, searchQueryBuilder', |
| 31 | + (tester) async { |
| 32 | + // `GFSearchBar.searchList` null or `GFSearchBar.overlaySearchListItemBuilder` null |
| 33 | + // or `GFSearchBar.searchQueryBuilder` null. |
| 34 | + expect( |
| 35 | + () => GFSearchBar( |
| 36 | + searchList: null, |
| 37 | + overlaySearchListItemBuilder: null, |
| 38 | + searchQueryBuilder: null, |
| 39 | + ), |
| 40 | + throwsAssertionError, |
| 41 | + ); |
| 42 | + }); |
| 43 | + |
| 44 | + testWidgets('GF SearchBar can be constructed', (tester) async { |
| 45 | + final GFSearchBar searchBar = GFSearchBar( |
| 46 | + searchList: searchList, |
| 47 | + overlaySearchListItemBuilder: (item) => Container( |
| 48 | + padding: const EdgeInsets.all(8), |
| 49 | + child: Text( |
| 50 | + item, |
| 51 | + style: const TextStyle(fontSize: 18), |
| 52 | + ), |
| 53 | + ), |
| 54 | + searchQueryBuilder: (query, list) => list |
| 55 | + .where((item) => item.toLowerCase().contains(query.toLowerCase())) |
| 56 | + .toList(), |
| 57 | + ); |
| 58 | + |
| 59 | + final TestApp app = TestApp(searchBar); |
| 60 | + |
| 61 | + await tester.pumpWidget(app); |
| 62 | + }); |
| 63 | + |
| 64 | + testWidgets('Can hide searchBox when item selected', (tester) async { |
| 65 | + final GFSearchBar searchBar = GFSearchBar( |
| 66 | + searchList: searchList, |
| 67 | + overlaySearchListItemBuilder: (item) => Container( |
| 68 | + padding: const EdgeInsets.all(8), |
| 69 | + child: Text( |
| 70 | + item, |
| 71 | + style: const TextStyle(fontSize: 18), |
| 72 | + ), |
| 73 | + ), |
| 74 | + hideSearchBoxWhenItemSelected: true, |
| 75 | + searchQueryBuilder: (query, list) => list |
| 76 | + .where((item) => item.toLowerCase().contains(query.toLowerCase())) |
| 77 | + .toList(), |
| 78 | + overlaySearchListHeight: 115, |
| 79 | + ); |
| 80 | + |
| 81 | + final TestApp app = TestApp(searchBar); |
| 82 | + |
| 83 | + await tester.pumpWidget(app); |
| 84 | + |
| 85 | + expect(app.searchBar.hideSearchBoxWhenItemSelected, isTrue); |
| 86 | + expect(app.searchBar.overlaySearchListHeight, 115); |
| 87 | + }); |
| 88 | + |
| 89 | + testWidgets('On item selected and when item not found in GFSearchBar List', |
| 90 | + (tester) async { |
| 91 | + final GFSearchBar searchBar = GFSearchBar( |
| 92 | + searchList: searchList, |
| 93 | + overlaySearchListItemBuilder: (item) => Container( |
| 94 | + padding: const EdgeInsets.all(8), |
| 95 | + child: Text( |
| 96 | + item, |
| 97 | + style: const TextStyle(fontSize: 18), |
| 98 | + ), |
| 99 | + ), |
| 100 | + hideSearchBoxWhenItemSelected: true, |
| 101 | + searchQueryBuilder: (query, list) => list |
| 102 | + .where((item) => item.toLowerCase().contains(query.toLowerCase())) |
| 103 | + .toList(), |
| 104 | + onItemSelected: (item) { |
| 105 | + print('selected item $item'); |
| 106 | + }, |
| 107 | + noItemsFoundWidget: noItemsFound, |
| 108 | + ); |
| 109 | + |
| 110 | + final TestApp app = TestApp(searchBar); |
| 111 | + |
| 112 | + await tester.pumpWidget(app); |
| 113 | + |
| 114 | + expect(app.searchBar.hideSearchBoxWhenItemSelected, isTrue); |
| 115 | + expect(app.searchBar.noItemsFoundWidget, noItemsFound); |
| 116 | + }); |
| 117 | + |
| 118 | + testWidgets('GFSearchBar with search box input decoration', (tester) async { |
| 119 | + final GFSearchBar searchBar = GFSearchBar( |
| 120 | + searchList: searchList, |
| 121 | + overlaySearchListItemBuilder: (item) => Container( |
| 122 | + padding: const EdgeInsets.all(8), |
| 123 | + child: Text( |
| 124 | + item, |
| 125 | + style: const TextStyle(fontSize: 18), |
| 126 | + ), |
| 127 | + ), |
| 128 | + hideSearchBoxWhenItemSelected: true, |
| 129 | + searchQueryBuilder: (query, list) => list |
| 130 | + .where((item) => item.toLowerCase().contains(query.toLowerCase())) |
| 131 | + .toList(), |
| 132 | + onItemSelected: (item) { |
| 133 | + print('selected item $item'); |
| 134 | + }, |
| 135 | + noItemsFoundWidget: noItemsFound, |
| 136 | + searchBoxInputDecoration: decoration, |
| 137 | + ); |
| 138 | + |
| 139 | + final TestApp app = TestApp(searchBar); |
| 140 | + |
| 141 | + await tester.pumpWidget(app); |
| 142 | + |
| 143 | + expect(app.searchBar.hideSearchBoxWhenItemSelected, isTrue); |
| 144 | + expect(app.searchBar.noItemsFoundWidget, noItemsFound); |
| 145 | + expect(app.searchBar.searchBoxInputDecoration, decoration); |
| 146 | + }); |
| 147 | +} |
| 148 | + |
| 149 | +class TestApp extends StatefulWidget { |
| 150 | + const TestApp(this.searchBar); |
| 151 | + |
| 152 | + final GFSearchBar searchBar; |
| 153 | + |
| 154 | + @override |
| 155 | + _TestAppState createState() => _TestAppState(); |
| 156 | +} |
| 157 | + |
| 158 | +class _TestAppState extends State<TestApp> { |
| 159 | + @override |
| 160 | + Widget build(BuildContext context) => MaterialApp( |
| 161 | + home: Scaffold( |
| 162 | + body: Column( |
| 163 | + children: [ |
| 164 | + widget.searchBar, |
| 165 | + ], |
| 166 | + ), |
| 167 | + ), |
| 168 | + ); |
| 169 | +} |
0 commit comments