Skip to content

Commit 2998d6b

Browse files
committed
Unit tests
1 parent ae2a677 commit 2998d6b

7 files changed

Lines changed: 385 additions & 2 deletions

File tree

test/alerts_test.dart

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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('ThemedAlert widget', () {
7+
testWidgets('renders info alert', (WidgetTester tester) async {
8+
await tester.pumpWidget(
9+
MaterialApp(
10+
home: Scaffold(
11+
body: ThemedAlert(
12+
type: ThemedAlertType.info,
13+
title: 'Info Title',
14+
description: 'Info Description',
15+
),
16+
),
17+
),
18+
);
19+
expect(find.text('Info Title'), findsOneWidget);
20+
expect(find.text('Info Description'), findsOneWidget);
21+
expect(find.byType(ThemedAlert), findsOneWidget);
22+
});
23+
24+
testWidgets('renders success alert', (WidgetTester tester) async {
25+
await tester.pumpWidget(
26+
MaterialApp(
27+
home: Scaffold(
28+
body: ThemedAlert(
29+
type: ThemedAlertType.success,
30+
title: 'Success Title',
31+
description: 'Success Description',
32+
),
33+
),
34+
),
35+
);
36+
expect(find.text('Success Title'), findsOneWidget);
37+
expect(find.text('Success Description'), findsOneWidget);
38+
expect(find.byType(ThemedAlert), findsOneWidget);
39+
});
40+
41+
testWidgets('renders custom alert with icon and color', (WidgetTester tester) async {
42+
await tester.pumpWidget(
43+
MaterialApp(
44+
home: Scaffold(
45+
body: ThemedAlert(
46+
type: ThemedAlertType.custom,
47+
title: 'Custom Title',
48+
description: 'Custom Description',
49+
color: Colors.purple,
50+
icon: Icons.star,
51+
),
52+
),
53+
),
54+
);
55+
expect(find.text('Custom Title'), findsOneWidget);
56+
expect(find.text('Custom Description'), findsOneWidget);
57+
expect(find.byType(ThemedAlert), findsOneWidget);
58+
expect(find.byIcon(Icons.star), findsOneWidget);
59+
});
60+
});
61+
}

test/datetime_extension_test.dart

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import 'package:flutter_test/flutter_test.dart';
2+
import 'package:layrz_theme/layrz_theme.dart';
3+
4+
void main() {
5+
group('DateTimeExtension', () {
6+
test('secondsSinceEpoch returns correct value', () {
7+
final date = DateTime(2023, 1, 1);
8+
expect(date.secondsSinceEpoch, date.millisecondsSinceEpoch / 1000);
9+
});
10+
11+
test('thisWeek and lastWeek return correct dates', () {
12+
final date = DateTime(2023, 8, 1); // Tuesday
13+
final thisWeek = date.thisWeek;
14+
expect(thisWeek.first, DateTime(2023, 7, 31)); // Monday
15+
expect(thisWeek.last, DateTime(2023, 8, 6)); // Sunday
16+
17+
final lastWeek = date.lastWeek;
18+
expect(lastWeek.first, DateTime(2023, 7, 24)); // Monday
19+
expect(lastWeek.last, DateTime(2023, 7, 30)); // Sunday
20+
});
21+
22+
test('thisMonth and lastMonth return correct dates', () {
23+
final date = DateTime(2023, 8, 15);
24+
final thisMonth = date.thisMonth;
25+
expect(thisMonth.first, DateTime(2023, 8, 1));
26+
expect(thisMonth.last, DateTime(2023, 8, 31));
27+
28+
final lastMonth = date.lastMonth;
29+
expect(lastMonth.first, DateTime(2023, 7, 1));
30+
expect(lastMonth.last, DateTime(2023, 7, 31));
31+
});
32+
});
33+
}

test/widgets/alerts_test.dart

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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('ThemedAlert widget', () {
7+
testWidgets('renders info alert', (WidgetTester tester) async {
8+
await tester.pumpWidget(
9+
MaterialApp(
10+
home: Scaffold(
11+
body: ThemedAlert(
12+
type: ThemedAlertType.info,
13+
title: 'Info Title',
14+
description: 'Info Description',
15+
),
16+
),
17+
),
18+
);
19+
expect(find.text('Info Title'), findsOneWidget);
20+
expect(find.text('Info Description'), findsOneWidget);
21+
expect(find.byType(ThemedAlert), findsOneWidget);
22+
});
23+
24+
testWidgets('renders success alert', (WidgetTester tester) async {
25+
await tester.pumpWidget(
26+
MaterialApp(
27+
home: Scaffold(
28+
body: ThemedAlert(
29+
type: ThemedAlertType.success,
30+
title: 'Success Title',
31+
description: 'Success Description',
32+
),
33+
),
34+
),
35+
);
36+
expect(find.text('Success Title'), findsOneWidget);
37+
expect(find.text('Success Description'), findsOneWidget);
38+
expect(find.byType(ThemedAlert), findsOneWidget);
39+
});
40+
41+
testWidgets('renders custom alert with icon and color', (WidgetTester tester) async {
42+
await tester.pumpWidget(
43+
MaterialApp(
44+
home: Scaffold(
45+
body: ThemedAlert(
46+
type: ThemedAlertType.custom,
47+
title: 'Custom Title',
48+
description: 'Custom Description',
49+
color: Colors.purple,
50+
icon: Icons.star,
51+
),
52+
),
53+
),
54+
);
55+
expect(find.text('Custom Title'), findsOneWidget);
56+
expect(find.text('Custom Description'), findsOneWidget);
57+
expect(find.byType(ThemedAlert), findsOneWidget);
58+
expect(find.byIcon(Icons.star), findsOneWidget);
59+
});
60+
});
61+
}

test/widgets/button_test.dart

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
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+
}

test/widgets/chip_test.dart

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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('ThemedChip widget', () {
7+
testWidgets('renders with label', (WidgetTester tester) async {
8+
await tester.pumpWidget(
9+
MaterialApp(
10+
home: Scaffold(
11+
body: ThemedChip(labelText: 'Test Chip'),
12+
),
13+
),
14+
);
15+
// The label is rendered as a TextSpan inside RichText, not a Text widget
16+
final richText = tester.widget<RichText>(find.byType(RichText));
17+
final textSpan = richText.text as TextSpan;
18+
expect(textSpan.toPlainText(), contains('Test Chip'));
19+
expect(find.byType(ThemedChip), findsOneWidget);
20+
});
21+
22+
testWidgets('renders with custom color', (WidgetTester tester) async {
23+
await tester.pumpWidget(
24+
MaterialApp(
25+
home: Scaffold(
26+
body: ThemedChip(labelText: 'Colored Chip', color: Colors.red),
27+
),
28+
),
29+
);
30+
final richText = tester.widget<RichText>(find.byType(RichText));
31+
final textSpan = richText.text as TextSpan;
32+
expect(textSpan.toPlainText(), contains('Colored Chip'));
33+
expect(find.byType(ThemedChip), findsOneWidget);
34+
});
35+
});
36+
}

test/widgets/layo_test.dart

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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('Layo widget', () {
7+
testWidgets('renders with default values', (WidgetTester tester) async {
8+
await tester.pumpWidget(
9+
MaterialApp(
10+
home: Scaffold(
11+
body: Layo(size: 50),
12+
),
13+
),
14+
);
15+
expect(find.byType(Layo), findsOneWidget);
16+
});
17+
18+
testWidgets('renders with custom emotion', (WidgetTester tester) async {
19+
await tester.pumpWidget(
20+
MaterialApp(
21+
home: Scaffold(
22+
body: Layo(size: 50, emotion: LayoEmotions.happy),
23+
),
24+
),
25+
);
26+
expect(find.byType(Layo), findsOneWidget);
27+
});
28+
29+
test('throws assertion error for invalid elevation', () {
30+
expect(() => Layo(size: 50, elevation: 6), throwsAssertionError);
31+
expect(() => Layo(size: 50, elevation: -1), throwsAssertionError);
32+
});
33+
});
34+
}

0 commit comments

Comments
 (0)