diff --git a/test/src/form_builder_test.dart b/test/src/form_builder_test.dart index 93ec7257a..1b6f87f1d 100644 --- a/test/src/form_builder_test.dart +++ b/test/src/form_builder_test.dart @@ -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'; @@ -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 - ', () {