Skip to content

Commit

Permalink
test: add autovalidate mode test cases on form builder
Browse files Browse the repository at this point in the history
  • Loading branch information
deandreamatias committed Jan 6, 2025
1 parent 81b5f5e commit 97be5e6
Showing 1 changed file with 81 additions and 0 deletions.
81 changes: 81 additions & 0 deletions test/src/form_builder_test.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_form_builder/flutter_form_builder.dart';
import 'package:flutter_test/flutter_test.dart';

Expand Down Expand Up @@ -249,6 +250,86 @@ void main() {

expect(find.text(errorTextField), findsOneWidget);
});
testWidgets(
'Should not show error when init form and AutovalidateMode is disabled',
(tester) async {
const textFieldName = 'text4';
const errorTextField = 'error text field';
final testWidget = FormBuilderTextField(
name: textFieldName,
validator: (value) => errorTextField,
);
await tester.pumpWidget(buildTestableFieldWidget(
testWidget,
autovalidateMode: AutovalidateMode.disabled,
));
await tester.pumpAndSettle();

expect(find.text(errorTextField), findsNothing);
});
testWidgets(
'Should show error when init form and AutovalidateMode is onUserInteraction',
(tester) async {
const textFieldName = 'text4';
const errorTextField = 'error text field';
final testWidget = FormBuilderTextField(
name: textFieldName,
validator: (value) => errorTextField,
);
await tester.pumpWidget(buildTestableFieldWidget(
testWidget,
autovalidateMode: AutovalidateMode.onUserInteraction,
));
await tester.pumpAndSettle();

expect(find.text(errorTextField), findsNothing);

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

expect(find.text(errorTextField), findsOneWidget);
});
testWidgets(
'Should show error when init form and AutovalidateMode is onUnfocus',
(tester) async {
const textFieldName = 'text4';
const errorTextField = 'error text field';
final testWidget = FormBuilderTextField(
name: textFieldName,
validator: (value) => errorTextField,
);
final widgetFinder = find.byWidget(testWidget);

// Init form
await tester.pumpWidget(buildTestableFieldWidget(
Column(
children: [
testWidget,
ElevatedButton(onPressed: () {}, child: const Text('Submit')),
],
),
autovalidateMode: AutovalidateMode.onUnfocus,
));
await tester.pumpAndSettle();
final focusNode =
formKey.currentState?.fields[textFieldName]?.effectiveFocusNode;
expect(find.text(errorTextField), findsNothing);
expect(Focus.of(tester.element(widgetFinder)).hasFocus, false);
expect(focusNode?.hasFocus, false);

// Focus input and write text
await tester.enterText(widgetFinder, 'test');
await tester.pumpAndSettle();
expect(Focus.of(tester.element(widgetFinder)).hasFocus, true);
expect(focusNode?.hasFocus, true);
expect(find.text(errorTextField), findsNothing);

// Unfocus input and show error
await tester.sendKeyEvent(LogicalKeyboardKey.tab);
await tester.pumpAndSettle();
expect(find.text(errorTextField), findsOneWidget);
});
});

group('isDirty - ', () {
Expand Down

0 comments on commit 97be5e6

Please sign in to comment.