-
-
Notifications
You must be signed in to change notification settings - Fork 543
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
2 changed files
with
197 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,187 @@ | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_form_builder/flutter_form_builder.dart'; | ||
|
||
import '../form_builder_tester.dart'; | ||
|
||
void main() { | ||
group('FormBuilderFieldDecoration -', () { | ||
testWidgets('when change the error text then the field should be invalid', | ||
(WidgetTester tester) async { | ||
final decorationFieldKey = GlobalKey<FormBuilderFieldDecorationState>(); | ||
const name = 'testField'; | ||
const errorTextField = 'error text field'; | ||
final widget = FormBuilderFieldDecoration<String>( | ||
key: decorationFieldKey, | ||
name: name, | ||
builder: (FormFieldState<String?> field) { | ||
return InputDecorator( | ||
decoration: (field as FormBuilderFieldDecorationState).decoration, | ||
child: TextField( | ||
onChanged: (value) { | ||
field.didChange(value); | ||
}, | ||
), | ||
); | ||
}, | ||
); | ||
|
||
await tester.pumpWidget(buildTestableFieldWidget(widget)); | ||
|
||
// Initially, the field should be valid | ||
expect(decorationFieldKey.currentState?.isValid, isTrue); | ||
|
||
decorationFieldKey.currentState?.invalidate(errorTextField); | ||
|
||
// The field should be invalid | ||
expect(decorationFieldKey.currentState?.isValid, isFalse); | ||
|
||
// Clear the error | ||
decorationFieldKey.currentState?.reset(); | ||
|
||
// The field should be valid again | ||
expect(decorationFieldKey.currentState?.isValid, isTrue); | ||
}); | ||
group('decoration enabled -', () { | ||
testWidgets('when change the error text then the field should be invalid', | ||
(WidgetTester tester) async { | ||
final decorationFieldKey = GlobalKey<FormBuilderFieldDecorationState>(); | ||
const name = 'testField'; | ||
const errorTextField = 'error text field'; | ||
final widget = FormBuilderFieldDecoration<String>( | ||
key: decorationFieldKey, | ||
name: name, | ||
builder: (FormFieldState<String?> field) { | ||
return InputDecorator( | ||
decoration: (field as FormBuilderFieldDecorationState).decoration, | ||
child: TextField( | ||
onChanged: (value) { | ||
field.didChange(value); | ||
}, | ||
), | ||
); | ||
}, | ||
); | ||
|
||
await tester.pumpWidget(buildTestableFieldWidget(widget)); | ||
|
||
// Initially, the field should be valid | ||
expect(decorationFieldKey.currentState?.isValid, isTrue); | ||
|
||
decorationFieldKey.currentState?.invalidate(errorTextField); | ||
|
||
// The field should be invalid | ||
expect(decorationFieldKey.currentState?.isValid, isFalse); | ||
|
||
// Clear the error | ||
decorationFieldKey.currentState?.reset(); | ||
|
||
// The field should be valid again | ||
expect(decorationFieldKey.currentState?.isValid, isTrue); | ||
}); | ||
test( | ||
'when enable property on decoration is false and enabled true, then throw an assert', | ||
() async { | ||
final decorationFieldKey = GlobalKey<FormBuilderFieldDecorationState>(); | ||
const name = 'testField'; | ||
|
||
expect( | ||
() => FormBuilderFieldDecoration<String>( | ||
key: decorationFieldKey, | ||
name: name, | ||
decoration: const InputDecoration(enabled: false), | ||
builder: (FormFieldState<String?> field) { | ||
return InputDecorator( | ||
decoration: | ||
(field as FormBuilderFieldDecorationState).decoration, | ||
child: TextField( | ||
onChanged: (value) { | ||
field.didChange(value); | ||
}, | ||
), | ||
); | ||
}, | ||
), | ||
throwsAssertionError, | ||
); | ||
}); | ||
test( | ||
'when enable property on decoration is false and enable is false, then build normally', | ||
() async { | ||
final decorationFieldKey = GlobalKey<FormBuilderFieldDecorationState>(); | ||
const name = 'testField'; | ||
|
||
expect( | ||
() => FormBuilderFieldDecoration<String>( | ||
key: decorationFieldKey, | ||
name: name, | ||
decoration: const InputDecoration(enabled: false), | ||
enabled: false, | ||
builder: (FormFieldState<String?> field) { | ||
return InputDecorator( | ||
decoration: | ||
(field as FormBuilderFieldDecorationState).decoration, | ||
child: TextField( | ||
onChanged: (value) { | ||
field.didChange(value); | ||
}, | ||
), | ||
); | ||
}, | ||
), | ||
returnsNormally, | ||
); | ||
}); | ||
test('when decoration is default (enabled: true), then build normally', | ||
() async { | ||
final decorationFieldKey = GlobalKey<FormBuilderFieldDecorationState>(); | ||
const name = 'testField'; | ||
|
||
expect( | ||
() => FormBuilderFieldDecoration<String>( | ||
key: decorationFieldKey, | ||
name: name, | ||
builder: (FormFieldState<String?> field) { | ||
return InputDecorator( | ||
decoration: | ||
(field as FormBuilderFieldDecorationState).decoration, | ||
child: TextField( | ||
onChanged: (value) { | ||
field.didChange(value); | ||
}, | ||
), | ||
); | ||
}, | ||
), | ||
returnsNormally, | ||
); | ||
}); | ||
test( | ||
'when decoration is default (enabled: true) and enable false, then build normally', | ||
() async { | ||
final decorationFieldKey = GlobalKey<FormBuilderFieldDecorationState>(); | ||
const name = 'testField'; | ||
|
||
expect( | ||
() => FormBuilderFieldDecoration<String>( | ||
key: decorationFieldKey, | ||
name: name, | ||
enabled: false, | ||
builder: (FormFieldState<String?> field) { | ||
return InputDecorator( | ||
decoration: | ||
(field as FormBuilderFieldDecorationState).decoration, | ||
child: TextField( | ||
onChanged: (value) { | ||
field.didChange(value); | ||
}, | ||
), | ||
); | ||
}, | ||
), | ||
returnsNormally, | ||
); | ||
}); | ||
}); | ||
}); | ||
} |