|
| 1 | +import 'package:flutter_test/flutter_test.dart'; |
| 2 | +import 'package:flutter/material.dart'; |
| 3 | +import 'package:layrz_theme/layrz_theme.dart'; |
| 4 | + |
| 5 | +void main() { |
| 6 | + group('ThemedButton widget', () { |
| 7 | + testWidgets('renders with labelText', (WidgetTester tester) async { |
| 8 | + await tester.pumpWidget( |
| 9 | + MaterialApp( |
| 10 | + home: Scaffold( |
| 11 | + body: ThemedButton( |
| 12 | + labelText: 'Button Text', |
| 13 | + onTap: () {}, |
| 14 | + ), |
| 15 | + ), |
| 16 | + ), |
| 17 | + ); |
| 18 | + expect(find.byType(ThemedButton), findsOneWidget); |
| 19 | + final richTexts = tester.widgetList<RichText>(find.byType(RichText)); |
| 20 | + final allText = richTexts.map((rt) => (rt.text as TextSpan).toPlainText()).join(' '); |
| 21 | + expect(allText, contains('Button Text')); |
| 22 | + }); |
| 23 | + |
| 24 | + testWidgets('renders with icon', (WidgetTester tester) async { |
| 25 | + await tester.pumpWidget( |
| 26 | + MaterialApp( |
| 27 | + home: Scaffold( |
| 28 | + body: ThemedButton( |
| 29 | + labelText: 'Icon Button', |
| 30 | + icon: Icons.add, |
| 31 | + onTap: () {}, |
| 32 | + ), |
| 33 | + ), |
| 34 | + ), |
| 35 | + ); |
| 36 | + expect(find.byIcon(Icons.add), findsOneWidget); |
| 37 | + final richTexts = tester.widgetList<RichText>(find.byType(RichText)); |
| 38 | + final allText = richTexts.map((rt) => (rt.text as TextSpan).toPlainText()).join(' '); |
| 39 | + expect(allText, contains('Icon Button')); |
| 40 | + }); |
| 41 | + |
| 42 | + testWidgets('renders as disabled', (WidgetTester tester) async { |
| 43 | + await tester.pumpWidget( |
| 44 | + MaterialApp( |
| 45 | + home: Scaffold( |
| 46 | + body: ThemedButton( |
| 47 | + labelText: 'Disabled', |
| 48 | + isDisabled: true, |
| 49 | + onTap: () {}, |
| 50 | + ), |
| 51 | + ), |
| 52 | + ), |
| 53 | + ); |
| 54 | + final richTexts = tester.widgetList<RichText>(find.byType(RichText)); |
| 55 | + final allText = richTexts.map((rt) => (rt.text as TextSpan).toPlainText()).join(' '); |
| 56 | + expect(allText, contains('Disabled')); |
| 57 | + // Optionally check for disabled state visually |
| 58 | + }); |
| 59 | + |
| 60 | + testWidgets('renders loading state', (WidgetTester tester) async { |
| 61 | + await tester.pumpWidget( |
| 62 | + MaterialApp( |
| 63 | + home: Scaffold( |
| 64 | + body: ThemedButton( |
| 65 | + labelText: 'Loading', |
| 66 | + isLoading: true, |
| 67 | + onTap: () {}, |
| 68 | + ), |
| 69 | + ), |
| 70 | + ), |
| 71 | + ); |
| 72 | + // Loading state may not show label text, so check for LinearProgressIndicator |
| 73 | + expect(find.byType(LinearProgressIndicator), findsWidgets); |
| 74 | + }); |
| 75 | + |
| 76 | + testWidgets('renders with custom color', (WidgetTester tester) async { |
| 77 | + await tester.pumpWidget( |
| 78 | + MaterialApp( |
| 79 | + home: Scaffold( |
| 80 | + body: ThemedButton( |
| 81 | + labelText: 'Colored', |
| 82 | + color: Colors.purple, |
| 83 | + onTap: () {}, |
| 84 | + ), |
| 85 | + ), |
| 86 | + ), |
| 87 | + ); |
| 88 | + final richTexts = tester.widgetList<RichText>(find.byType(RichText)); |
| 89 | + final allText = richTexts.map((rt) => (rt.text as TextSpan).toPlainText()).join(' '); |
| 90 | + expect(allText, contains('Colored')); |
| 91 | + }); |
| 92 | + |
| 93 | + testWidgets('renders with different styles', (WidgetTester tester) async { |
| 94 | + for (final style in ThemedButtonStyle.values) { |
| 95 | + await tester.pumpWidget( |
| 96 | + MaterialApp( |
| 97 | + home: Scaffold( |
| 98 | + body: ThemedButton( |
| 99 | + labelText: style.toString(), |
| 100 | + style: style, |
| 101 | + icon: Icons.star, |
| 102 | + onTap: () {}, |
| 103 | + ), |
| 104 | + ), |
| 105 | + ), |
| 106 | + ); |
| 107 | + // FAB styles only show icon, not label text |
| 108 | + final isFab = [ |
| 109 | + ThemedButtonStyle.outlinedFab, |
| 110 | + ThemedButtonStyle.fab, |
| 111 | + ThemedButtonStyle.filledFab, |
| 112 | + ThemedButtonStyle.filledTonalFab, |
| 113 | + ThemedButtonStyle.elevatedFab, |
| 114 | + ThemedButtonStyle.outlinedTonalFab, |
| 115 | + ].contains(style); |
| 116 | + if (isFab) { |
| 117 | + expect(find.byIcon(Icons.star), findsOneWidget); |
| 118 | + } else { |
| 119 | + final richTexts = tester.widgetList<RichText>(find.byType(RichText)); |
| 120 | + final allText = richTexts.map((rt) => (rt.text as TextSpan).toPlainText()).join(' '); |
| 121 | + expect(allText, contains(style.toString())); |
| 122 | + } |
| 123 | + } |
| 124 | + }); |
| 125 | + |
| 126 | + testWidgets('renders with factory constructors', (WidgetTester tester) async { |
| 127 | + await tester.pumpWidget( |
| 128 | + MaterialApp( |
| 129 | + home: Column( |
| 130 | + children: [ |
| 131 | + ThemedButton.save(onTap: () {}, labelText: 'Save'), |
| 132 | + ThemedButton.cancel(onTap: () {}, labelText: 'Cancel'), |
| 133 | + ThemedButton.info(onTap: () {}, labelText: 'Info'), |
| 134 | + ThemedButton.show(onTap: () {}, labelText: 'Show'), |
| 135 | + ThemedButton.edit(onTap: () {}, labelText: 'Edit'), |
| 136 | + ThemedButton.delete(onTap: () {}, labelText: 'Delete'), |
| 137 | + ], |
| 138 | + ), |
| 139 | + ), |
| 140 | + ); |
| 141 | + // Only check for label text in non-FAB styles |
| 142 | + final richTexts = tester.widgetList<RichText>(find.byType(RichText)); |
| 143 | + final allText = richTexts.map((rt) => (rt.text as TextSpan).toPlainText()).join(' '); |
| 144 | + expect(allText, contains('Save')); |
| 145 | + expect(allText, contains('Cancel')); |
| 146 | + expect(allText, contains('Info')); |
| 147 | + expect(allText, contains('Show')); |
| 148 | + expect(allText, contains('Edit')); |
| 149 | + expect(allText, contains('Delete')); |
| 150 | + }); |
| 151 | + }); |
| 152 | +} |
0 commit comments