Skip to content

Commit ac77d13

Browse files
committed
tests for new valueIsValid, valueHasError
1 parent 294044a commit ac77d13

File tree

2 files changed

+60
-50
lines changed

2 files changed

+60
-50
lines changed

lib/src/form_builder_field.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class FormBuilderFieldState<F extends FormBuilderField<T>, T> extends FormFieldS
9494
@override
9595
bool get isValid => super.isValid && errorText == null;
9696

97-
bool get isValueValid => super.isValid;
97+
bool get valueIsValid => super.isValid;
9898
bool get valueHasError => super.hasError;
9999

100100
bool get enabled => widget.enabled && (_formBuilderState?.enabled ?? true);

test/src/form_builder_field_test.dart

Lines changed: 59 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ import '../form_builder_tester.dart';
77
void main() {
88
group('FormBuilderField -', () {
99
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 {
1211
final textFieldKey = GlobalKey<FormBuilderFieldState>();
1312
const textFieldName = 'text2';
1413
const errorTextField = 'error text field';
@@ -42,18 +41,15 @@ void main() {
4241

4342
expect(textFieldKey.currentState?.isValid, isFalse);
4443
});
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 {
4845
final textFieldKey = GlobalKey<FormBuilderFieldState>();
4946
const textFieldName = 'text';
5047
const errorTextField = 'error text field';
5148
final testWidget = FormBuilderTextField(
5249
name: textFieldName,
5350
key: textFieldKey,
5451
autovalidateMode: AutovalidateMode.always,
55-
validator: (value) =>
56-
value == null || value.isEmpty ? errorTextField : null,
52+
validator: (value) => value == null || value.isEmpty ? errorTextField : null,
5753
);
5854
await tester.pumpWidget(buildTestableFieldWidget(testWidget));
5955

@@ -65,18 +61,15 @@ void main() {
6561

6662
expect(textFieldKey.currentState?.isValid, isTrue);
6763
});
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 {
7165
final textFieldKey = GlobalKey<FormBuilderFieldState>();
7266
const textFieldName = 'text';
7367
const errorTextField = 'error text field';
7468
final testWidget = FormBuilderTextField(
7569
name: textFieldName,
7670
key: textFieldKey,
7771
autovalidateMode: AutovalidateMode.always,
78-
validator: (value) =>
79-
value == null || value.length < 10 ? errorTextField : null,
72+
validator: (value) => value == null || value.length < 10 ? errorTextField : null,
8073
);
8174
await tester.pumpWidget(buildTestableFieldWidget(testWidget));
8275

@@ -107,8 +100,7 @@ void main() {
107100

108101
expect(textFieldKey.currentState?.hasError, isTrue);
109102
});
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 {
112104
final textFieldKey = GlobalKey<FormBuilderFieldState>();
113105
const textFieldName = 'text';
114106
final testWidget = FormBuilderTextField(
@@ -125,26 +117,58 @@ void main() {
125117
});
126118
});
127119

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+
128157
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 {
132159
const textFieldName = 'text4';
133160
const errorTextField = 'error text field';
134161
final testWidget = FormBuilderTextField(
135162
name: textFieldName,
136-
validator: (value) =>
137-
value == null || value.isEmpty ? errorTextField : null,
163+
validator: (value) => value == null || value.isEmpty ? errorTextField : null,
138164
autovalidateMode: AutovalidateMode.always,
139165
);
140166
await tester.pumpWidget(buildTestableFieldWidget(testWidget));
141167
await tester.pumpAndSettle();
142168

143169
expect(find.text(errorTextField), findsOneWidget);
144170
});
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 {
148172
const textFieldName = 'text4';
149173
const errorTextField = 'error text field';
150174
final testWidget = FormBuilderTextField(
@@ -166,40 +190,34 @@ void main() {
166190
testWidgets('Should not dirty by default', (tester) async {
167191
const textFieldName = 'text';
168192
final textFieldKey = GlobalKey<FormBuilderFieldState>();
169-
final testWidget =
170-
FormBuilderTextField(name: textFieldName, key: textFieldKey);
193+
final testWidget = FormBuilderTextField(name: textFieldName, key: textFieldKey);
171194
await tester.pumpWidget(buildTestableFieldWidget(testWidget));
172195

173196
expect(textFieldKey.currentState?.isDirty, false);
174197
});
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 {
177199
const textFieldName = 'text';
178200
final textFieldKey = GlobalKey<FormBuilderFieldState>();
179-
final testWidget =
180-
FormBuilderTextField(name: textFieldName, key: textFieldKey);
201+
final testWidget = FormBuilderTextField(name: textFieldName, key: textFieldKey);
181202
await tester.pumpWidget(buildTestableFieldWidget(testWidget));
182203

183204
final widgetFinder = find.byWidget(testWidget);
184205
await tester.enterText(widgetFinder, 'test');
185206

186207
expect(textFieldKey.currentState?.isDirty, true);
187208
});
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 {
190210
const textFieldName = 'text';
191211
final textFieldKey = GlobalKey<FormBuilderFieldState>();
192-
final testWidget =
193-
FormBuilderTextField(name: textFieldName, key: textFieldKey);
212+
final testWidget = FormBuilderTextField(name: textFieldName, key: textFieldKey);
194213
await tester.pumpWidget(buildTestableFieldWidget(testWidget));
195214

196215
textFieldKey.currentState?.setValue('test');
197216
await tester.pumpAndSettle();
198217

199218
expect(textFieldKey.currentState?.isDirty, true);
200219
});
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 {
203221
const textFieldName = 'text';
204222
final textFieldKey = GlobalKey<FormBuilderFieldState>();
205223
final testWidget = FormBuilderTextField(
@@ -214,8 +232,7 @@ void main() {
214232

215233
expect(textFieldKey.currentState?.isDirty, true);
216234
});
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 {
219236
const textFieldName = 'text';
220237
final textFieldKey = GlobalKey<FormBuilderFieldState>();
221238
final testWidget = FormBuilderTextField(
@@ -233,8 +250,7 @@ void main() {
233250
testWidgets('Should not dirty when reset field value', (tester) async {
234251
const textFieldName = 'text';
235252
final textFieldKey = GlobalKey<FormBuilderFieldState>();
236-
final testWidget =
237-
FormBuilderTextField(name: textFieldName, key: textFieldKey);
253+
final testWidget = FormBuilderTextField(name: textFieldName, key: textFieldKey);
238254
await tester.pumpWidget(buildTestableFieldWidget(testWidget));
239255

240256
textFieldKey.currentState?.setValue('test');
@@ -243,8 +259,7 @@ void main() {
243259

244260
expect(textFieldKey.currentState?.isDirty, false);
245261
});
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 {
248263
const textFieldName = 'text';
249264
final textFieldKey = GlobalKey<FormBuilderFieldState>();
250265
final testWidget = FormBuilderTextField(
@@ -266,17 +281,15 @@ void main() {
266281
testWidgets('Should not touched by default', (tester) async {
267282
const textFieldName = 'text';
268283
final textFieldKey = GlobalKey<FormBuilderFieldState>();
269-
final testWidget =
270-
FormBuilderTextField(name: textFieldName, key: textFieldKey);
284+
final testWidget = FormBuilderTextField(name: textFieldName, key: textFieldKey);
271285
await tester.pumpWidget(buildTestableFieldWidget(testWidget));
272286

273287
expect(textFieldKey.currentState?.isTouched, false);
274288
});
275289
testWidgets('Should touched when focus input', (tester) async {
276290
const textFieldName = 'text';
277291
final textFieldKey = GlobalKey<FormBuilderFieldState>();
278-
final testWidget =
279-
FormBuilderTextField(name: textFieldName, key: textFieldKey);
292+
final testWidget = FormBuilderTextField(name: textFieldName, key: textFieldKey);
280293
await tester.pumpWidget(buildTestableFieldWidget(testWidget));
281294

282295
final widgetFinder = find.byWidget(testWidget);
@@ -290,8 +303,7 @@ void main() {
290303
testWidgets('Should reset to null when call reset', (tester) async {
291304
const textFieldName = 'text';
292305
final textFieldKey = GlobalKey<FormBuilderFieldState>();
293-
final testWidget =
294-
FormBuilderTextField(name: textFieldName, key: textFieldKey);
306+
final testWidget = FormBuilderTextField(name: textFieldName, key: textFieldKey);
295307
await tester.pumpWidget(buildTestableFieldWidget(testWidget));
296308

297309
textFieldKey.currentState?.setValue('test');
@@ -317,9 +329,7 @@ void main() {
317329

318330
expect(textFieldKey.currentState?.value, equals(initialValue));
319331
});
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 {
323333
final textFieldKey = GlobalKey<FormBuilderFieldState>();
324334
const textFieldName = 'text';
325335
const errorTextField = 'error text field';

0 commit comments

Comments
 (0)