forked from joanpablo/reactive_forms
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve required validator joanpablo#377
- Loading branch information
1 parent
59fe334
commit 330a17b
Showing
2 changed files
with
116 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:reactive_forms/reactive_forms.dart'; | ||
|
||
void main() { | ||
group('Required validator tests', () { | ||
test('FormControl is invalid if value is null', () { | ||
final control = FormControl<String>( | ||
value: null, | ||
validators: [Validators.required], | ||
); | ||
|
||
expect(control.valid, false); | ||
expect(control.errors, { | ||
ValidationMessage.required: true, | ||
}); | ||
}); | ||
|
||
test('FormControl is invalid if value is empty string', () { | ||
final control = FormControl<String>( | ||
value: '', | ||
validators: [Validators.required], | ||
); | ||
|
||
expect(control.valid, false); | ||
expect(control.errors, { | ||
ValidationMessage.required: true, | ||
}); | ||
}); | ||
|
||
test('FormControl is valid if value is non empty string', () { | ||
final control = FormControl<String>( | ||
value: 'non empty string', | ||
validators: [Validators.required], | ||
); | ||
|
||
expect(control.valid, true); | ||
expect(control.errors, <String, dynamic>{}); | ||
}); | ||
|
||
test('FormControl is invalid if value is empty list', () { | ||
final control = FormControl<List<String>>( | ||
value: [], | ||
validators: [Validators.required], | ||
); | ||
|
||
expect(control.valid, false); | ||
expect(control.errors, { | ||
ValidationMessage.required: true, | ||
}); | ||
}); | ||
|
||
test('FormControl is valid if value is non empty list', () { | ||
final control = FormControl<List<String>>( | ||
value: ['non empty string'], | ||
validators: [Validators.required], | ||
); | ||
|
||
expect(control.valid, true); | ||
expect(control.errors, <String, dynamic>{}); | ||
}); | ||
|
||
test('FormControl is invalid if value is empty map', () { | ||
final control = FormControl<Map<String, dynamic>>( | ||
value: <String, dynamic>{}, | ||
validators: [Validators.required], | ||
); | ||
|
||
expect(control.valid, false); | ||
expect(control.errors, { | ||
ValidationMessage.required: true, | ||
}); | ||
}); | ||
|
||
test('FormControl is valid if value is non empty map', () { | ||
final control = FormControl<Map<String, String>>( | ||
value: {'key': 'value'}, | ||
validators: [Validators.required], | ||
); | ||
|
||
expect(control.valid, true); | ||
expect(control.errors, <String, dynamic>{}); | ||
}); | ||
|
||
test('FormControl is invalid if value is empty set', () { | ||
final control = FormControl<Set<String>>( | ||
value: <String>{}, | ||
validators: [Validators.required], | ||
); | ||
|
||
expect(control.valid, false); | ||
expect(control.errors, { | ||
ValidationMessage.required: true, | ||
}); | ||
}); | ||
|
||
test('FormControl is valid if value is non empty set', () { | ||
final control = FormControl<Set<String>>( | ||
value: {'value'}, | ||
validators: [Validators.required], | ||
); | ||
|
||
expect(control.valid, true); | ||
expect(control.errors, <String, dynamic>{}); | ||
}); | ||
}); | ||
} |