@@ -7,8 +7,7 @@ import '../form_builder_tester.dart';
7
7
void main () {
8
8
group ('FormBuilderField -' , () {
9
9
group ('custom error -' , () {
10
- testWidgets ('Should show custom error when invalidate field' ,
11
- (tester) async {
10
+ testWidgets ('Should show custom error when invalidate field' , (tester) async {
12
11
final textFieldKey = GlobalKey <FormBuilderFieldState >();
13
12
const textFieldName = 'text2' ;
14
13
const errorTextField = 'error text field' ;
@@ -42,18 +41,15 @@ void main() {
42
41
43
42
expect (textFieldKey.currentState? .isValid, isFalse);
44
43
});
45
- testWidgets (
46
- 'Should valid when no has error and autovalidateMode is always' ,
47
- (tester) async {
44
+ testWidgets ('Should valid when no has error and autovalidateMode is always' , (tester) async {
48
45
final textFieldKey = GlobalKey <FormBuilderFieldState >();
49
46
const textFieldName = 'text' ;
50
47
const errorTextField = 'error text field' ;
51
48
final testWidget = FormBuilderTextField (
52
49
name: textFieldName,
53
50
key: textFieldKey,
54
51
autovalidateMode: AutovalidateMode .always,
55
- validator: (value) =>
56
- value == null || value.isEmpty ? errorTextField : null ,
52
+ validator: (value) => value == null || value.isEmpty ? errorTextField : null ,
57
53
);
58
54
await tester.pumpWidget (buildTestableFieldWidget (testWidget));
59
55
@@ -65,18 +61,15 @@ void main() {
65
61
66
62
expect (textFieldKey.currentState? .isValid, isTrue);
67
63
});
68
- testWidgets (
69
- 'Should invalid when has error and autovalidateMode is always' ,
70
- (tester) async {
64
+ testWidgets ('Should invalid when has error and autovalidateMode is always' , (tester) async {
71
65
final textFieldKey = GlobalKey <FormBuilderFieldState >();
72
66
const textFieldName = 'text' ;
73
67
const errorTextField = 'error text field' ;
74
68
final testWidget = FormBuilderTextField (
75
69
name: textFieldName,
76
70
key: textFieldKey,
77
71
autovalidateMode: AutovalidateMode .always,
78
- validator: (value) =>
79
- value == null || value.length < 10 ? errorTextField : null ,
72
+ validator: (value) => value == null || value.length < 10 ? errorTextField : null ,
80
73
);
81
74
await tester.pumpWidget (buildTestableFieldWidget (testWidget));
82
75
@@ -107,8 +100,7 @@ void main() {
107
100
108
101
expect (textFieldKey.currentState? .hasError, isTrue);
109
102
});
110
- testWidgets ('Should no has errors when is empty and no has validators' ,
111
- (tester) async {
103
+ testWidgets ('Should no has errors when is empty and no has validators' , (tester) async {
112
104
final textFieldKey = GlobalKey <FormBuilderFieldState >();
113
105
const textFieldName = 'text' ;
114
106
final testWidget = FormBuilderTextField (
@@ -125,26 +117,58 @@ void main() {
125
117
});
126
118
});
127
119
120
+ group ('valueIsValid -' , () {
121
+ testWidgets ('Should value is valid when validator passes, despite set custom error' , (tester) async {
122
+ final textFieldKey = GlobalKey <FormBuilderFieldState >();
123
+ const textFieldName = 'text' ;
124
+ const errorTextField = 'error text field' ;
125
+ final testWidget = FormBuilderTextField (
126
+ name: textFieldName,
127
+ key: textFieldKey,
128
+ );
129
+ await tester.pumpWidget (buildTestableFieldWidget (testWidget));
130
+
131
+ // Set custom error
132
+ textFieldKey.currentState? .invalidate (errorTextField);
133
+ await tester.pumpAndSettle ();
134
+
135
+ expect (textFieldKey.currentState? .valueIsValid, isTrue);
136
+ });
137
+ });
138
+
139
+ group ('valueHasError -' , () {
140
+ testWidgets ('Should value is invalid when validator passes' , (tester) async {
141
+ final textFieldKey = GlobalKey <FormBuilderFieldState >();
142
+ const textFieldName = 'text' ;
143
+ const invalidValue = 'invalid' ;
144
+ final testWidget = FormBuilderTextField (
145
+ name: textFieldName,
146
+ key: textFieldKey,
147
+ initialValue: invalidValue,
148
+ validator: (value) => (value == invalidValue) ? 'error' : null ,
149
+ autovalidateMode: AutovalidateMode .always,
150
+ );
151
+ await tester.pumpWidget (buildTestableFieldWidget (testWidget));
152
+
153
+ expect (textFieldKey.currentState? .valueHasError, isTrue);
154
+ });
155
+ });
156
+
128
157
group ('autovalidateMode -' , () {
129
- testWidgets (
130
- 'Should show error when init form and AutovalidateMode is always' ,
131
- (tester) async {
158
+ testWidgets ('Should show error when init form and AutovalidateMode is always' , (tester) async {
132
159
const textFieldName = 'text4' ;
133
160
const errorTextField = 'error text field' ;
134
161
final testWidget = FormBuilderTextField (
135
162
name: textFieldName,
136
- validator: (value) =>
137
- value == null || value.isEmpty ? errorTextField : null ,
163
+ validator: (value) => value == null || value.isEmpty ? errorTextField : null ,
138
164
autovalidateMode: AutovalidateMode .always,
139
165
);
140
166
await tester.pumpWidget (buildTestableFieldWidget (testWidget));
141
167
await tester.pumpAndSettle ();
142
168
143
169
expect (find.text (errorTextField), findsOneWidget);
144
170
});
145
- testWidgets (
146
- 'Should show error when AutovalidateMode is onUserInteraction and change field' ,
147
- (tester) async {
171
+ testWidgets ('Should show error when AutovalidateMode is onUserInteraction and change field' , (tester) async {
148
172
const textFieldName = 'text4' ;
149
173
const errorTextField = 'error text field' ;
150
174
final testWidget = FormBuilderTextField (
@@ -166,40 +190,34 @@ void main() {
166
190
testWidgets ('Should not dirty by default' , (tester) async {
167
191
const textFieldName = 'text' ;
168
192
final textFieldKey = GlobalKey <FormBuilderFieldState >();
169
- final testWidget =
170
- FormBuilderTextField (name: textFieldName, key: textFieldKey);
193
+ final testWidget = FormBuilderTextField (name: textFieldName, key: textFieldKey);
171
194
await tester.pumpWidget (buildTestableFieldWidget (testWidget));
172
195
173
196
expect (textFieldKey.currentState? .isDirty, false );
174
197
});
175
- testWidgets ('Should dirty when update field value by user' ,
176
- (tester) async {
198
+ testWidgets ('Should dirty when update field value by user' , (tester) async {
177
199
const textFieldName = 'text' ;
178
200
final textFieldKey = GlobalKey <FormBuilderFieldState >();
179
- final testWidget =
180
- FormBuilderTextField (name: textFieldName, key: textFieldKey);
201
+ final testWidget = FormBuilderTextField (name: textFieldName, key: textFieldKey);
181
202
await tester.pumpWidget (buildTestableFieldWidget (testWidget));
182
203
183
204
final widgetFinder = find.byWidget (testWidget);
184
205
await tester.enterText (widgetFinder, 'test' );
185
206
186
207
expect (textFieldKey.currentState? .isDirty, true );
187
208
});
188
- testWidgets ('Should dirty when update field value by method' ,
189
- (tester) async {
209
+ testWidgets ('Should dirty when update field value by method' , (tester) async {
190
210
const textFieldName = 'text' ;
191
211
final textFieldKey = GlobalKey <FormBuilderFieldState >();
192
- final testWidget =
193
- FormBuilderTextField (name: textFieldName, key: textFieldKey);
212
+ final testWidget = FormBuilderTextField (name: textFieldName, key: textFieldKey);
194
213
await tester.pumpWidget (buildTestableFieldWidget (testWidget));
195
214
196
215
textFieldKey.currentState? .setValue ('test' );
197
216
await tester.pumpAndSettle ();
198
217
199
218
expect (textFieldKey.currentState? .isDirty, true );
200
219
});
201
- testWidgets ('Should dirty when update field with initial value by user' ,
202
- (tester) async {
220
+ testWidgets ('Should dirty when update field with initial value by user' , (tester) async {
203
221
const textFieldName = 'text' ;
204
222
final textFieldKey = GlobalKey <FormBuilderFieldState >();
205
223
final testWidget = FormBuilderTextField (
@@ -214,8 +232,7 @@ void main() {
214
232
215
233
expect (textFieldKey.currentState? .isDirty, true );
216
234
});
217
- testWidgets ('Should dirty when update field with initial value by method' ,
218
- (tester) async {
235
+ testWidgets ('Should dirty when update field with initial value by method' , (tester) async {
219
236
const textFieldName = 'text' ;
220
237
final textFieldKey = GlobalKey <FormBuilderFieldState >();
221
238
final testWidget = FormBuilderTextField (
@@ -233,8 +250,7 @@ void main() {
233
250
testWidgets ('Should not dirty when reset field value' , (tester) async {
234
251
const textFieldName = 'text' ;
235
252
final textFieldKey = GlobalKey <FormBuilderFieldState >();
236
- final testWidget =
237
- FormBuilderTextField (name: textFieldName, key: textFieldKey);
253
+ final testWidget = FormBuilderTextField (name: textFieldName, key: textFieldKey);
238
254
await tester.pumpWidget (buildTestableFieldWidget (testWidget));
239
255
240
256
textFieldKey.currentState? .setValue ('test' );
@@ -243,8 +259,7 @@ void main() {
243
259
244
260
expect (textFieldKey.currentState? .isDirty, false );
245
261
});
246
- testWidgets ('Should not dirty when reset field with initial value' ,
247
- (tester) async {
262
+ testWidgets ('Should not dirty when reset field with initial value' , (tester) async {
248
263
const textFieldName = 'text' ;
249
264
final textFieldKey = GlobalKey <FormBuilderFieldState >();
250
265
final testWidget = FormBuilderTextField (
@@ -266,17 +281,15 @@ void main() {
266
281
testWidgets ('Should not touched by default' , (tester) async {
267
282
const textFieldName = 'text' ;
268
283
final textFieldKey = GlobalKey <FormBuilderFieldState >();
269
- final testWidget =
270
- FormBuilderTextField (name: textFieldName, key: textFieldKey);
284
+ final testWidget = FormBuilderTextField (name: textFieldName, key: textFieldKey);
271
285
await tester.pumpWidget (buildTestableFieldWidget (testWidget));
272
286
273
287
expect (textFieldKey.currentState? .isTouched, false );
274
288
});
275
289
testWidgets ('Should touched when focus input' , (tester) async {
276
290
const textFieldName = 'text' ;
277
291
final textFieldKey = GlobalKey <FormBuilderFieldState >();
278
- final testWidget =
279
- FormBuilderTextField (name: textFieldName, key: textFieldKey);
292
+ final testWidget = FormBuilderTextField (name: textFieldName, key: textFieldKey);
280
293
await tester.pumpWidget (buildTestableFieldWidget (testWidget));
281
294
282
295
final widgetFinder = find.byWidget (testWidget);
@@ -290,8 +303,7 @@ void main() {
290
303
testWidgets ('Should reset to null when call reset' , (tester) async {
291
304
const textFieldName = 'text' ;
292
305
final textFieldKey = GlobalKey <FormBuilderFieldState >();
293
- final testWidget =
294
- FormBuilderTextField (name: textFieldName, key: textFieldKey);
306
+ final testWidget = FormBuilderTextField (name: textFieldName, key: textFieldKey);
295
307
await tester.pumpWidget (buildTestableFieldWidget (testWidget));
296
308
297
309
textFieldKey.currentState? .setValue ('test' );
@@ -317,9 +329,7 @@ void main() {
317
329
318
330
expect (textFieldKey.currentState? .value, equals (initialValue));
319
331
});
320
- testWidgets (
321
- 'Should reset custom error when invalidate field and then reset' ,
322
- (tester) async {
332
+ testWidgets ('Should reset custom error when invalidate field and then reset' , (tester) async {
323
333
final textFieldKey = GlobalKey <FormBuilderFieldState >();
324
334
const textFieldName = 'text' ;
325
335
const errorTextField = 'error text field' ;
0 commit comments