Skip to content

Commit bccc31a

Browse files
committed
feat: Add tests for Material. #1
1 parent 0d70017 commit bccc31a

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

lib/material.dart

+7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import 'package:flutter/material.dart';
22
import 'package:intl/intl.dart';
33

4+
final dateButtonKey = UniqueKey();
5+
final timeButtonKey = UniqueKey();
6+
final dateTimeButtonKey = UniqueKey();
7+
48
/// Material example page.
59
/// Showcases the usage of `DatePicker` and `TimePicker` to change date and time.
610
class MaterialExamplePage extends StatefulWidget {
@@ -69,6 +73,7 @@ class _MaterialExamplePageState extends State<MaterialExamplePage> {
6973
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
7074
children: [
7175
ElevatedButton(
76+
key: dateButtonKey,
7277
style: ElevatedButton.styleFrom(backgroundColor: Colors.blue.shade300),
7378
child: const Text(
7479
"Date",
@@ -85,6 +90,7 @@ class _MaterialExamplePageState extends State<MaterialExamplePage> {
8590
);
8691
}),
8792
ElevatedButton(
93+
key: timeButtonKey,
8894
style: ElevatedButton.styleFrom(backgroundColor: Colors.blue.shade300),
8995
child: const Text(
9096
"Time",
@@ -109,6 +115,7 @@ class _MaterialExamplePageState extends State<MaterialExamplePage> {
109115
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
110116
children: [
111117
ElevatedButton(
118+
key: dateTimeButtonKey,
112119
style: ElevatedButton.styleFrom(backgroundColor: Colors.orange.shade400),
113120
onPressed: pickDateTime,
114121
child: const Text(

test/widget_test.dart

+55-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// gestures. You can also use WidgetTester to find child widgets in the widget
66
// tree, read text, and verify that the values of widget properties are correct.
77

8+
import 'package:app/material.dart';
89
import 'package:flutter/material.dart';
910
import 'package:flutter_test/flutter_test.dart';
1011

@@ -15,7 +16,6 @@ void main() {
1516
// Build our app and trigger a frame.
1617
await tester.pumpWidget(const App());
1718

18-
// Verify that our counter starts at 0.
1919
expect(find.byKey(materialPageKey), findsOneWidget);
2020
expect(find.byKey(cupertinoPageKey), findsNothing);
2121

@@ -26,4 +26,58 @@ void main() {
2626
expect(find.byKey(cupertinoPageKey), findsOneWidget);
2727
expect(find.byKey(materialPageKey), findsNothing);
2828
});
29+
30+
testWidgets('Test Material page pickers', (WidgetTester tester) async {
31+
// Build our app and trigger a frame.
32+
await tester.pumpWidget(const App());
33+
34+
expect(find.byKey(materialPageKey), findsOneWidget);
35+
expect(find.byKey(cupertinoPageKey), findsNothing);
36+
37+
// TEST DATE BUTTON ONLY -----------
38+
final dateButton = find.byKey(dateButtonKey);
39+
40+
// Tap on `Cupertino` key and page should change
41+
await tester.tap(dateButton);
42+
await tester.pumpAndSettle();
43+
44+
// Tap on "OK"
45+
await tester.tap(find.text('OK'));
46+
await tester.pumpAndSettle();
47+
48+
// Should show page again
49+
expect(find.byKey(materialPageKey), findsOneWidget);
50+
51+
// TEST TIME BUTTON ONLY -----------
52+
final timeButton = find.byKey(timeButtonKey);
53+
54+
// Tap on `Cupertino` key and page should change
55+
await tester.tap(timeButton);
56+
await tester.pumpAndSettle();
57+
58+
// Tap on "OK"
59+
await tester.tap(find.text('OK'));
60+
await tester.pumpAndSettle();
61+
62+
// Should show page again
63+
expect(find.byKey(materialPageKey), findsOneWidget);
64+
65+
// TEST DATE AND TIME BUTTON -----------
66+
final dateTimeButton = find.byKey(dateTimeButtonKey);
67+
68+
// Tap on `Cupertino` key and page should change
69+
await tester.tap(dateTimeButton);
70+
await tester.pumpAndSettle();
71+
72+
// Tap on "OK" for date
73+
await tester.tap(find.text('OK'));
74+
await tester.pumpAndSettle();
75+
76+
// Tap on "OK" for time
77+
await tester.tap(find.text('OK'));
78+
await tester.pumpAndSettle();
79+
80+
// Should show page again
81+
expect(find.byKey(materialPageKey), findsOneWidget);
82+
});
2983
}

0 commit comments

Comments
 (0)