|
1 | 1 | import 'package:flutter/material.dart';
|
| 2 | +import 'package:flutter/services.dart'; |
2 | 3 | import 'package:flutter_form_builder/flutter_form_builder.dart';
|
3 | 4 | import 'package:flutter_test/flutter_test.dart';
|
4 | 5 |
|
@@ -249,6 +250,126 @@ void main() {
|
249 | 250 |
|
250 | 251 | expect(find.text(errorTextField), findsOneWidget);
|
251 | 252 | });
|
| 253 | + testWidgets( |
| 254 | + 'Should not show error when init form and AutovalidateMode is disabled', |
| 255 | + (tester) async { |
| 256 | + const textFieldName = 'text4'; |
| 257 | + const errorTextField = 'error text field'; |
| 258 | + final testWidget = FormBuilderTextField( |
| 259 | + name: textFieldName, |
| 260 | + validator: (value) => errorTextField, |
| 261 | + ); |
| 262 | + await tester.pumpWidget(buildTestableFieldWidget( |
| 263 | + testWidget, |
| 264 | + autovalidateMode: AutovalidateMode.disabled, |
| 265 | + )); |
| 266 | + await tester.pumpAndSettle(); |
| 267 | + |
| 268 | + expect(find.text(errorTextField), findsNothing); |
| 269 | + }); |
| 270 | + testWidgets( |
| 271 | + 'Should show error when init form and AutovalidateMode is onUserInteraction', |
| 272 | + (tester) async { |
| 273 | + const textFieldName = 'text4'; |
| 274 | + const errorTextField = 'error text field'; |
| 275 | + final testWidget = FormBuilderTextField( |
| 276 | + name: textFieldName, |
| 277 | + validator: (value) => errorTextField, |
| 278 | + ); |
| 279 | + await tester.pumpWidget(buildTestableFieldWidget( |
| 280 | + testWidget, |
| 281 | + autovalidateMode: AutovalidateMode.onUserInteraction, |
| 282 | + )); |
| 283 | + await tester.pumpAndSettle(); |
| 284 | + |
| 285 | + expect(find.text(errorTextField), findsNothing); |
| 286 | + |
| 287 | + final widgetFinder = find.byWidget(testWidget); |
| 288 | + await tester.enterText(widgetFinder, 'test'); |
| 289 | + await tester.pumpAndSettle(); |
| 290 | + |
| 291 | + expect(find.text(errorTextField), findsOneWidget); |
| 292 | + }); |
| 293 | + testWidgets( |
| 294 | + 'Should show error when init form and AutovalidateMode is onUnfocus', |
| 295 | + (tester) async { |
| 296 | + const textFieldName = 'text4'; |
| 297 | + const errorTextField = 'error text field'; |
| 298 | + final testWidget = FormBuilderTextField( |
| 299 | + name: textFieldName, |
| 300 | + validator: (value) => errorTextField, |
| 301 | + ); |
| 302 | + final widgetFinder = find.byWidget(testWidget); |
| 303 | + |
| 304 | + // Init form |
| 305 | + await tester.pumpWidget(buildTestableFieldWidget( |
| 306 | + Column( |
| 307 | + children: [ |
| 308 | + testWidget, |
| 309 | + ElevatedButton(onPressed: () {}, child: const Text('Submit')), |
| 310 | + ], |
| 311 | + ), |
| 312 | + autovalidateMode: AutovalidateMode.onUnfocus, |
| 313 | + )); |
| 314 | + await tester.pumpAndSettle(); |
| 315 | + final focusNode = |
| 316 | + formKey.currentState?.fields[textFieldName]?.effectiveFocusNode; |
| 317 | + expect(find.text(errorTextField), findsNothing); |
| 318 | + expect(Focus.of(tester.element(widgetFinder)).hasFocus, false); |
| 319 | + expect(focusNode?.hasFocus, false); |
| 320 | + |
| 321 | + // Focus input and write text |
| 322 | + await tester.enterText(widgetFinder, 'test'); |
| 323 | + await tester.pumpAndSettle(); |
| 324 | + expect(Focus.of(tester.element(widgetFinder)).hasFocus, true); |
| 325 | + expect(focusNode?.hasFocus, true); |
| 326 | + expect(find.text(errorTextField), findsNothing); |
| 327 | + |
| 328 | + // Unfocus input and show error |
| 329 | + await tester.sendKeyEvent(LogicalKeyboardKey.tab); |
| 330 | + await tester.pumpAndSettle(); |
| 331 | + expect(find.text(errorTextField), findsOneWidget); |
| 332 | + }); |
| 333 | + group('Interact with FormBuilderField -', () { |
| 334 | + testWidgets( |
| 335 | + 'Should show error when FormBuilder is disabled and FormBuilderField is always', |
| 336 | + (tester) async { |
| 337 | + const textFieldName = 'text4'; |
| 338 | + const errorTextField = 'error text field'; |
| 339 | + final testWidget = FormBuilderTextField( |
| 340 | + name: textFieldName, |
| 341 | + validator: (value) => errorTextField, |
| 342 | + autovalidateMode: AutovalidateMode.always, |
| 343 | + ); |
| 344 | + await tester.pumpWidget(buildTestableFieldWidget( |
| 345 | + testWidget, |
| 346 | + autovalidateMode: AutovalidateMode.disabled, |
| 347 | + )); |
| 348 | + await tester.pumpAndSettle(); |
| 349 | + |
| 350 | + expect(find.text(errorTextField), findsOneWidget); |
| 351 | + }); |
| 352 | + // TODO: Enable when solve issue |
| 353 | + // https://github.com/flutter/flutter/issues/125766 |
| 354 | + // testWidgets( |
| 355 | + // 'Should not show error when FormBuilder is always and FormBuilderField is disabled', |
| 356 | + // (tester) async { |
| 357 | + // const textFieldName = 'text4'; |
| 358 | + // const errorTextField = 'error text field'; |
| 359 | + // final testWidget = FormBuilderTextField( |
| 360 | + // name: textFieldName, |
| 361 | + // validator: (value) => errorTextField, |
| 362 | + // autovalidateMode: AutovalidateMode.disabled, |
| 363 | + // ); |
| 364 | + // await tester.pumpWidget(buildTestableFieldWidget( |
| 365 | + // testWidget, |
| 366 | + // autovalidateMode: AutovalidateMode.always, |
| 367 | + // )); |
| 368 | + // await tester.pumpAndSettle(); |
| 369 | + |
| 370 | + // expect(find.text(errorTextField), findsNothing); |
| 371 | + // }); |
| 372 | + }); |
252 | 373 | });
|
253 | 374 |
|
254 | 375 | group('isDirty - ', () {
|
|
0 commit comments