Skip to content

Commit 52b285f

Browse files
committed
Started adding tests for fields.
1 parent 013729c commit 52b285f

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:flutter_test/flutter_test.dart';
3+
import 'package:flutter_form_builder/flutter_form_builder.dart';
4+
import 'form_builder_tester.dart';
5+
6+
void main() {
7+
testWidgets('FormBuilderSwitch -- Off/On/Off', (WidgetTester tester) async {
8+
const switchName = 'switch1';
9+
final testWidget = FormBuilderSwitch(
10+
name: switchName,
11+
title: const Text('Switch 1'),
12+
initialValue: false,
13+
);
14+
final widgetFinder = find.byWidget(testWidget);
15+
16+
await tester.pumpWidget(buildTestableFieldWidget(testWidget));
17+
18+
expect(formSave(), isTrue);
19+
expect(formValue(switchName), isFalse);
20+
await tester.tap(widgetFinder);
21+
await tester.pumpAndSettle();
22+
expect(formSave(), isTrue);
23+
expect(formValue(switchName), isTrue);
24+
await tester.tap(widgetFinder);
25+
await tester.pumpAndSettle();
26+
expect(formSave(), isTrue);
27+
expect(formValue(switchName), isFalse);
28+
});
29+
}

test/fields/form_builder_tester.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:flutter_form_builder/flutter_form_builder.dart';
3+
4+
final _formKey = GlobalKey<FormBuilderState>();
5+
6+
Widget buildTestableFieldWidget(Widget widget) {
7+
return MaterialApp(
8+
home: Scaffold(
9+
body: FormBuilder(
10+
key: _formKey,
11+
child: widget,
12+
),
13+
),
14+
);
15+
}
16+
17+
bool formSave() => _formKey.currentState.saveAndValidate();
18+
dynamic formValue(String name) => _formKey.currentState.value[name];
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import 'package:flutter_test/flutter_test.dart';
2+
import 'package:flutter_form_builder/flutter_form_builder.dart';
3+
4+
import 'form_builder_tester.dart';
5+
6+
void main() {
7+
testWidgets('FormBuilderTextField -- Hello Planet',
8+
(WidgetTester tester) async {
9+
const newTextValue = 'Hello 🪐';
10+
const textFieldName = 'text1';
11+
final testWidget = FormBuilderTextField(
12+
name: textFieldName,
13+
);
14+
final widgetFinder = find.byWidget(testWidget);
15+
16+
await tester.pumpWidget(buildTestableFieldWidget(testWidget));
17+
expect(formSave(), isTrue);
18+
// TODO Confirm that this should not be isEmpty
19+
expect(formValue(textFieldName), isNull);
20+
await tester.enterText(widgetFinder, newTextValue);
21+
expect(formSave(), isTrue);
22+
expect(formValue(textFieldName), equals(newTextValue));
23+
await tester.enterText(widgetFinder, '');
24+
expect(formSave(), isTrue);
25+
expect(formValue(textFieldName), isEmpty);
26+
});
27+
}

0 commit comments

Comments
 (0)