Skip to content

Commit fe391e4

Browse files
test: add focus test to form field
1 parent c6a0091 commit fe391e4

File tree

1 file changed

+51
-4
lines changed

1 file changed

+51
-4
lines changed

test/src/form_builder_field_test.dart

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,6 @@ void main() {
164164
expect(textFieldKey.currentState?.valueHasError, isTrue);
165165
});
166166
});
167-
168167
group('autovalidateMode -', () {
169168
testWidgets(
170169
'Should show error when init form and AutovalidateMode is always',
@@ -201,7 +200,6 @@ void main() {
201200
expect(find.text(errorTextField), findsOneWidget);
202201
});
203202
});
204-
205203
group('isDirty - ', () {
206204
testWidgets('Should not dirty by default', (tester) async {
207205
const textFieldName = 'text';
@@ -301,7 +299,6 @@ void main() {
301299
expect(textFieldKey.currentState?.isDirty, false);
302300
});
303301
});
304-
305302
group('isTouched - ', () {
306303
testWidgets('Should not touched by default', (tester) async {
307304
const textFieldName = 'text';
@@ -325,7 +322,6 @@ void main() {
325322
expect(textFieldKey.currentState?.isTouched, true);
326323
});
327324
});
328-
329325
group('reset -', () {
330326
testWidgets('Should reset to null when call reset', (tester) async {
331327
const textFieldName = 'text';
@@ -378,5 +374,56 @@ void main() {
378374
expect(find.text(errorTextField), findsNothing);
379375
});
380376
});
377+
group('focus -', () {
378+
testWidgets('Should focus on field when invalidate it', (tester) async {
379+
final textFieldKey = GlobalKey<FormBuilderFieldState>();
380+
const widgetName = 'text';
381+
const errorTextField = 'error text field';
382+
final testWidget = FormBuilderTextField(
383+
name: widgetName,
384+
key: textFieldKey,
385+
);
386+
final widgetFinder = find.byWidget(testWidget);
387+
388+
await tester.pumpWidget(buildTestableFieldWidget(testWidget));
389+
final focusNode =
390+
formKey.currentState?.fields[widgetName]?.effectiveFocusNode;
391+
392+
expect(Focus.of(tester.element(widgetFinder)).hasFocus, false);
393+
expect(focusNode?.hasFocus, false);
394+
395+
textFieldKey.currentState?.invalidate(errorTextField);
396+
await tester.pumpAndSettle();
397+
398+
expect(Focus.of(tester.element(widgetFinder)).hasFocus, true);
399+
expect(focusNode?.hasFocus, true);
400+
});
401+
testWidgets(
402+
'Should not focus on field when invalidate field and is disabled',
403+
(tester) async {
404+
final textFieldKey = GlobalKey<FormBuilderFieldState>();
405+
const widgetName = 'text';
406+
const errorTextField = 'error text field';
407+
final testWidget = FormBuilderTextField(
408+
name: widgetName,
409+
key: textFieldKey,
410+
enabled: false,
411+
);
412+
final widgetFinder = find.byWidget(testWidget);
413+
414+
await tester.pumpWidget(buildTestableFieldWidget(testWidget));
415+
final focusNode =
416+
formKey.currentState?.fields[widgetName]?.effectiveFocusNode;
417+
418+
expect(Focus.of(tester.element(widgetFinder)).hasFocus, false);
419+
expect(focusNode?.hasFocus, false);
420+
421+
textFieldKey.currentState?.invalidate(errorTextField);
422+
await tester.pumpAndSettle();
423+
424+
expect(Focus.of(tester.element(widgetFinder)).hasFocus, false);
425+
expect(focusNode?.hasFocus, false);
426+
});
427+
});
381428
});
382429
}

0 commit comments

Comments
 (0)