main
1import 'package:blood_pressure_app/components/input_dialoge.dart';
2import 'package:flutter/material.dart';
3import 'package:blood_pressure_app/l10n/app_localizations.dart';
4import 'package:flutter_test/flutter_test.dart';
5
6import '../util.dart';
7
8void main() {
9 group('InputDialoge', () {
10 testWidgets('should initialize without errors', (tester) async {
11 await tester.pumpWidget(materialApp(const InputDialoge()));
12 expect(tester.takeException(), isNull);
13 await tester.pumpWidget(const MaterialApp(
14 localizationsDelegates: [AppLocalizations.delegate,], locale: Locale('en'),
15 home: InputDialoge(
16 hintText: 'test hint',
17 initialValue: 'initial text',
18 ),
19 ),);
20 expect(tester.takeException(), isNull);
21 expect(find.byType(InputDialoge), findsOneWidget);
22 });
23 testWidgets('should show prefilled text', (tester) async {
24 await tester.pumpWidget(materialApp(const InputDialoge(
25 hintText: 'test hint',
26 initialValue: 'initial text',
27 ),),);
28 expect(find.text('initial text'), findsOneWidget);
29 expect(find.text('test hint'), findsNWidgets(2));
30 });
31 testWidgets('should show validator errors', (tester) async {
32 await tester.pumpWidget(materialApp(InputDialoge(
33 initialValue: 'initial text',
34 validator: (_) => 'test error',
35 ),),);
36 final localizations = await AppLocalizations.delegate.load(const Locale('en'));
37
38 expect(find.text(localizations.btnConfirm), findsOneWidget);
39 await tester.tap(find.text(localizations.btnConfirm));
40 await tester.pumpAndSettle();
41
42 expect(find.byType(InputDialoge), findsOneWidget);
43 expect(find.text('test error'), findsOneWidget);
44 });
45 testWidgets('should send current text to validator', (tester) async {
46 int validatorCalls = 0;
47 await tester.pumpWidget(materialApp(InputDialoge(
48 initialValue: 'initial text',
49 validator: (value) {
50 expect(value, 'initial text');
51 validatorCalls += 1;
52 return null;
53 },
54 ),),);
55 final localizations = await AppLocalizations.delegate.load(const Locale('en'));
56
57 expect(validatorCalls, 0);
58
59 expect(find.text(localizations.btnConfirm), findsOneWidget);
60 await tester.tap(find.text(localizations.btnConfirm));
61 await tester.pumpAndSettle();
62
63 expect(validatorCalls, 1);
64
65 expect(find.byType(InputDialoge), findsNothing);
66 });
67 });
68 group('showInputDialoge', () {
69 testWidgets('should start with input focused', (tester) async {
70 await loadDialoge(tester, (context) => showInputDialoge(context, initialValue: 'testval'));
71
72 expect(find.byType(InputDialoge), findsOneWidget);
73 final primaryFocus = FocusManager.instance.primaryFocus;
74 expect(primaryFocus?.context?.widget, isNotNull);
75 final focusedTextField = find.ancestor(
76 of: find.byWidget(primaryFocus!.context!.widget),
77 matching: find.byType(TextField),
78 );
79 expect(find.descendant(of: focusedTextField, matching: find.text('testval')), findsOneWidget);
80 });
81 testWidgets('should allow entering a value', (tester) async {
82 String? result = 'init';
83 await loadDialoge(tester, (context) async => result = await showInputDialoge(context));
84 final localizations = await AppLocalizations.delegate.load(const Locale('en'));
85
86 expect(find.byType(InputDialoge), findsOneWidget);
87 expect(find.byType(TextField), findsOneWidget);
88
89 await tester.enterText(find.byType(TextField), 'inputted text');
90 expect(find.text(localizations.btnConfirm), findsOneWidget);
91 await tester.tap(find.text(localizations.btnConfirm));
92 await tester.pumpAndSettle();
93
94 expect(result, 'inputted text');
95 });
96 testWidgets('should not return value on cancel', (tester) async {
97 String? result = 'init';
98 await loadDialoge(tester, (context) async => result = await showInputDialoge(context, initialValue: 'test'));
99 final localizations = await AppLocalizations.delegate.load(const Locale('en'));
100
101 expect(find.byType(InputDialoge), findsOneWidget);
102 expect(find.byType(TextField), findsOneWidget);
103
104 await tester.enterText(find.byType(TextField), 'inputted text');
105 expect(find.text(localizations.btnCancel), findsOneWidget);
106 await tester.tap(find.text(localizations.btnCancel));
107 await tester.pumpAndSettle();
108
109 expect(result, null);
110 });
111 });
112 group('showNumberInputDialoge', () {
113 testWidgets('should start with input focused', (tester) async {
114 await loadDialoge(tester, (context) => showNumberInputDialoge(context, initialValue: 123));
115
116 expect(find.byType(InputDialoge), findsOneWidget);
117 expect(find.text('123'), findsOneWidget);
118
119 final primaryFocus = FocusManager.instance.primaryFocus;
120 expect(primaryFocus?.context?.widget, isNotNull);
121 final focusedTextField = find.ancestor(
122 of: find.byWidget(primaryFocus!.context!.widget),
123 matching: find.byType(TextField),
124 );
125 expect(find.descendant(of: focusedTextField, matching: find.text('123')), findsOneWidget);
126 });
127 testWidgets('should allow entering a number', (tester) async {
128 double? result = -1;
129 await loadDialoge(tester, (context) async => result = await showNumberInputDialoge(context));
130 final localizations = await AppLocalizations.delegate.load(const Locale('en'));
131
132 expect(find.byType(InputDialoge), findsOneWidget);
133 expect(find.byType(TextField), findsOneWidget);
134
135 await tester.enterText(find.byType(TextField), '123.76');
136 expect(find.text(localizations.btnConfirm), findsOneWidget);
137 await tester.tap(find.text(localizations.btnConfirm));
138 await tester.pumpAndSettle();
139
140 expect(result, 123.76);
141 });
142 testWidgets('should not allow entering text', (tester) async {
143 double? result = -1;
144 await loadDialoge(tester, (context) async => result = await showNumberInputDialoge(context));
145 final localizations = await AppLocalizations.delegate.load(const Locale('en'));
146
147 expect(find.byType(InputDialoge), findsOneWidget);
148
149 await tester.enterText(find.byType(TextField), 'test');
150 expect(find.text(localizations.btnConfirm), findsOneWidget);
151 await tester.tap(find.text(localizations.btnConfirm));
152 await tester.pumpAndSettle();
153
154 expect(find.byType(InputDialoge), findsOneWidget); // unclosable through confirm
155 expect(find.text(localizations.errNaN), findsOneWidget);
156
157 expect(find.text(localizations.btnCancel), findsOneWidget);
158 await tester.tap(find.text(localizations.btnCancel));
159 await tester.pumpAndSettle();
160
161 expect(result, null);
162 });
163 });
164}