|
| 1 | +import 'package:exui/material.dart'; |
| 2 | +import 'package:flutter/material.dart'; |
| 3 | +import 'package:flutter_test/flutter_test.dart'; |
| 4 | + |
| 5 | +List<BottomNavigationBarItem> extensionTestItems() => |
| 6 | + const <BottomNavigationBarItem>[ |
| 7 | + BottomNavigationBarItem( |
| 8 | + icon: Icon(Icons.home), |
| 9 | + label: 'Home', |
| 10 | + ), |
| 11 | + BottomNavigationBarItem( |
| 12 | + icon: Icon(Icons.search), |
| 13 | + label: 'Search', |
| 14 | + ), |
| 15 | + ]; |
| 16 | + |
| 17 | +void main() { |
| 18 | + late List<BottomNavigationBarItem> items; |
| 19 | + |
| 20 | + setUp(() { |
| 21 | + items = extensionTestItems(); |
| 22 | + }); |
| 23 | + |
| 24 | + testWidgets('creates BottomNavigationBar with default values', |
| 25 | + (tester) async { |
| 26 | + final widget = MaterialApp( |
| 27 | + home: Scaffold( |
| 28 | + bottomNavigationBar: items.bottomNavigationBar(), |
| 29 | + ), |
| 30 | + ); |
| 31 | + |
| 32 | + await tester.pumpWidget(widget); |
| 33 | + |
| 34 | + expect(find.byType(BottomNavigationBar), findsOneWidget); |
| 35 | + final bottomNavBar = tester.widget<BottomNavigationBar>( |
| 36 | + find.byType(BottomNavigationBar), |
| 37 | + ); |
| 38 | + |
| 39 | + expect(bottomNavBar.items.length, 2); |
| 40 | + expect(bottomNavBar.currentIndex, 0); |
| 41 | + expect(bottomNavBar.iconSize, 24.0); |
| 42 | + expect(bottomNavBar.selectedFontSize, 14.0); |
| 43 | + expect(bottomNavBar.unselectedFontSize, 12.0); |
| 44 | + expect(bottomNavBar.useLegacyColorScheme, isTrue); |
| 45 | + }); |
| 46 | + |
| 47 | + testWidgets('passes currentIndex and onTap correctly', (tester) async { |
| 48 | + int? tappedIndex; |
| 49 | + |
| 50 | + final widget = MaterialApp( |
| 51 | + home: Scaffold( |
| 52 | + bottomNavigationBar: items.bottomNavigationBar( |
| 53 | + currentIndex: 1, |
| 54 | + onTap: (index) => tappedIndex = index, |
| 55 | + ), |
| 56 | + ), |
| 57 | + ); |
| 58 | + |
| 59 | + await tester.pumpWidget(widget); |
| 60 | + final bottomNavBar = tester.widget<BottomNavigationBar>( |
| 61 | + find.byType(BottomNavigationBar), |
| 62 | + ); |
| 63 | + |
| 64 | + expect(bottomNavBar.currentIndex, 1); |
| 65 | + |
| 66 | + await tester.tap(find.text('Home')); |
| 67 | + expect(tappedIndex, 0); |
| 68 | + |
| 69 | + await tester.tap(find.text('Search')); |
| 70 | + expect(tappedIndex, 1); |
| 71 | + }); |
| 72 | + |
| 73 | + testWidgets('applies color and type configurations', (tester) async { |
| 74 | + final widget = MaterialApp( |
| 75 | + home: Scaffold( |
| 76 | + bottomNavigationBar: items.bottomNavigationBar( |
| 77 | + selectedItemColor: Colors.red, |
| 78 | + unselectedItemColor: Colors.grey, |
| 79 | + backgroundColor: Colors.black, |
| 80 | + type: BottomNavigationBarType.fixed, |
| 81 | + ), |
| 82 | + ), |
| 83 | + ); |
| 84 | + |
| 85 | + await tester.pumpWidget(widget); |
| 86 | + final bottomNavBar = tester.widget<BottomNavigationBar>( |
| 87 | + find.byType(BottomNavigationBar), |
| 88 | + ); |
| 89 | + |
| 90 | + expect(bottomNavBar.selectedItemColor, Colors.red); |
| 91 | + expect(bottomNavBar.unselectedItemColor, Colors.grey); |
| 92 | + expect(bottomNavBar.backgroundColor, Colors.black); |
| 93 | + expect(bottomNavBar.type, BottomNavigationBarType.fixed); |
| 94 | + }); |
| 95 | + |
| 96 | + testWidgets('uses custom icon theme and label styles', (tester) async { |
| 97 | + final selectedIconTheme = IconThemeData(size: 30, color: Colors.green); |
| 98 | + final unselectedIconTheme = IconThemeData(size: 20, color: Colors.blue); |
| 99 | + final selectedLabelStyle = TextStyle(fontWeight: FontWeight.bold); |
| 100 | + final unselectedLabelStyle = TextStyle(fontWeight: FontWeight.normal); |
| 101 | + |
| 102 | + final widget = MaterialApp( |
| 103 | + home: Scaffold( |
| 104 | + bottomNavigationBar: items.bottomNavigationBar( |
| 105 | + selectedIconTheme: selectedIconTheme, |
| 106 | + unselectedIconTheme: unselectedIconTheme, |
| 107 | + selectedLabelStyle: selectedLabelStyle, |
| 108 | + unselectedLabelStyle: unselectedLabelStyle, |
| 109 | + ), |
| 110 | + ), |
| 111 | + ); |
| 112 | + |
| 113 | + await tester.pumpWidget(widget); |
| 114 | + final bottomNavBar = tester.widget<BottomNavigationBar>( |
| 115 | + find.byType(BottomNavigationBar), |
| 116 | + ); |
| 117 | + |
| 118 | + expect(bottomNavBar.selectedIconTheme, selectedIconTheme); |
| 119 | + expect(bottomNavBar.unselectedIconTheme, unselectedIconTheme); |
| 120 | + expect(bottomNavBar.selectedLabelStyle, selectedLabelStyle); |
| 121 | + expect(bottomNavBar.unselectedLabelStyle, unselectedLabelStyle); |
| 122 | + }); |
| 123 | + |
| 124 | + testWidgets('respects feedback and landscape layout', (tester) async { |
| 125 | + final widget = MaterialApp( |
| 126 | + home: Scaffold( |
| 127 | + bottomNavigationBar: items.bottomNavigationBar( |
| 128 | + enableFeedback: false, |
| 129 | + landscapeLayout: BottomNavigationBarLandscapeLayout.centered, |
| 130 | + useLegacyColorScheme: false, |
| 131 | + ), |
| 132 | + ), |
| 133 | + ); |
| 134 | + |
| 135 | + await tester.pumpWidget(widget); |
| 136 | + final bottomNavBar = tester.widget<BottomNavigationBar>( |
| 137 | + find.byType(BottomNavigationBar), |
| 138 | + ); |
| 139 | + |
| 140 | + expect(bottomNavBar.enableFeedback, false); |
| 141 | + expect(bottomNavBar.landscapeLayout, |
| 142 | + BottomNavigationBarLandscapeLayout.centered); |
| 143 | + expect(bottomNavBar.useLegacyColorScheme, false); |
| 144 | + }); |
| 145 | + |
| 146 | + testWidgets('respects label visibility settings', (tester) async { |
| 147 | + final widget = MaterialApp( |
| 148 | + home: Scaffold( |
| 149 | + bottomNavigationBar: items.bottomNavigationBar( |
| 150 | + showSelectedLabels: false, |
| 151 | + showUnselectedLabels: false, |
| 152 | + ), |
| 153 | + ), |
| 154 | + ); |
| 155 | + |
| 156 | + await tester.pumpWidget(widget); |
| 157 | + final bottomNavBar = tester.widget<BottomNavigationBar>( |
| 158 | + find.byType(BottomNavigationBar), |
| 159 | + ); |
| 160 | + |
| 161 | + expect(bottomNavBar.showSelectedLabels, false); |
| 162 | + expect(bottomNavBar.showUnselectedLabels, false); |
| 163 | + }); |
| 164 | +} |
0 commit comments