Skip to content

Commit 5b4a93e

Browse files
authored
test: split test into widget and unit test (#1432)
1 parent f426745 commit 5b4a93e

File tree

3 files changed

+81
-47
lines changed

3 files changed

+81
-47
lines changed

frontend/app_flowy/lib/plugins/grid/presentation/widgets/cell/select_option_cell/text_field.dart

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -136,31 +136,12 @@ class _SelectOptionTextFieldState extends State<SelectOptionTextField> {
136136
return;
137137
}
138138

139-
final trimmedText = text.trimLeft();
140-
List<String> splits = [];
141-
String currentString = '';
142-
143-
// split the string into tokens
144-
for (final char in trimmedText.split('')) {
145-
if (widget.textSeparators.contains(char)) {
146-
if (currentString.isNotEmpty) {
147-
splits.add(currentString.trim());
148-
}
149-
currentString = '';
150-
continue;
151-
}
152-
currentString += char;
153-
}
154-
// add the remainder (might be '')
155-
splits.add(currentString);
156-
157-
final submittedOptions = splits.sublist(0, splits.length - 1).toList();
139+
final result = splitInput(text.trimLeft(), widget.textSeparators);
158140

159-
final remainder = splits.elementAt(splits.length - 1).trimLeft();
160-
editingController.text = remainder;
141+
editingController.text = result[1];
161142
editingController.selection =
162143
TextSelection.collapsed(offset: controller.text.length);
163-
widget.onPaste(submittedOptions, remainder);
144+
widget.onPaste(result[0], result[1]);
164145
}
165146

166147
Widget? _renderTags(BuildContext context, ScrollController sc) {
@@ -193,3 +174,28 @@ class _SelectOptionTextFieldState extends State<SelectOptionTextField> {
193174
);
194175
}
195176
}
177+
178+
@visibleForTesting
179+
List splitInput(String input, List<String> textSeparators) {
180+
List<String> splits = [];
181+
String currentString = '';
182+
183+
// split the string into tokens
184+
for (final char in input.split('')) {
185+
if (textSeparators.contains(char)) {
186+
if (currentString.trim().isNotEmpty) {
187+
splits.add(currentString.trim());
188+
}
189+
currentString = '';
190+
continue;
191+
}
192+
currentString += char;
193+
}
194+
// add the remainder (might be '')
195+
splits.add(currentString);
196+
197+
final submittedOptions = splits.sublist(0, splits.length - 1).toList();
198+
final remainder = splits.elementAt(splits.length - 1).trimLeft();
199+
200+
return [submittedOptions, remainder];
201+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import 'package:app_flowy/plugins/grid/presentation/widgets/cell/select_option_cell/text_field.dart';
2+
import 'package:flutter_test/flutter_test.dart';
3+
4+
void main() {
5+
const textSeparators = [','];
6+
7+
group('split input unit test', () {
8+
test('empty input', () {
9+
List result = splitInput(' ', textSeparators);
10+
expect(result[0], []);
11+
expect(result[1], '');
12+
13+
result = splitInput(', , , ', textSeparators);
14+
expect(result[0], []);
15+
expect(result[1], '');
16+
});
17+
18+
test('simple input', () {
19+
List result = splitInput('exampleTag', textSeparators);
20+
expect(result[0], []);
21+
expect(result[1], 'exampleTag');
22+
23+
result = splitInput('tag with longer name', textSeparators);
24+
expect(result[0], []);
25+
expect(result[1], 'tag with longer name');
26+
27+
result = splitInput('trailing space ', textSeparators);
28+
expect(result[0], []);
29+
expect(result[1], 'trailing space ');
30+
});
31+
32+
test('input with commas', () {
33+
List result = splitInput('a, b, c', textSeparators);
34+
expect(result[0], ['a', 'b']);
35+
expect(result[1], 'c');
36+
37+
result = splitInput('a, b, c, ', textSeparators);
38+
expect(result[0], ['a', 'b', 'c']);
39+
expect(result[1], '');
40+
41+
result = splitInput(',tag 1 ,2nd tag, third tag ', textSeparators);
42+
expect(result[0], ['tag 1', '2nd tag']);
43+
expect(result[1], 'third tag ');
44+
});
45+
});
46+
}

frontend/app_flowy/test/widget_test/select_option_text_field_test.dart

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import 'dart:collection';
22

33
import 'package:app_flowy/plugins/grid/presentation/widgets/cell/select_option_cell/text_field.dart';
4-
import 'package:flowy_infra/theme.dart';
54
import 'package:flowy_sdk/protobuf/flowy-grid/protobuf.dart';
65
import 'package:flutter/material.dart';
76
import 'package:flutter_test/flutter_test.dart';
8-
import 'package:provider/provider.dart';
97
import 'package:textfield_tags/textfield_tags.dart';
108

119
import '../bloc_test/grid_test/util.dart';
@@ -30,7 +28,7 @@ void main() {
3028
remainder = remaining;
3129
select = options;
3230
},
33-
newText: (_) {},
31+
newText: (text) => remainder = text,
3432
textSeparators: const [','],
3533
textController: TextEditingController(),
3634
);
@@ -40,10 +38,7 @@ void main() {
4038
await tester.pumpWidget(
4139
MaterialApp(
4240
home: Material(
43-
child: Provider<AppTheme>.value(
44-
value: AppTheme.fromType(Brightness.light),
45-
child: textField,
46-
),
41+
child: textField,
4742
),
4843
),
4944
);
@@ -63,28 +58,15 @@ void main() {
6358
await tester.testTextInput.receiveAction(TextInputAction.done);
6459
expect(submit, 'an option');
6560

66-
await tester.enterText(find.byType(TextField), ' another one ');
61+
submit = '';
62+
await tester.enterText(find.byType(TextField), ' ');
6763
await tester.testTextInput.receiveAction(TextInputAction.done);
68-
expect(submit, 'another one');
64+
expect(submit, '');
6965

7066
// test inputs containing commas
71-
await tester.enterText(find.byType(TextField), ' abcd,');
72-
expect(remainder, '');
73-
expect(select, ['abcd']);
74-
75-
await tester.enterText(find.byType(TextField), ',acd, aaaa ');
76-
expect(remainder, 'aaaa ');
77-
expect(select, ['acd']);
78-
79-
await tester.enterText(find.byType(TextField), 'a a, bbbb , ');
80-
expect(remainder, '');
67+
await tester.enterText(find.byType(TextField), 'a a, bbbb , c');
68+
expect(remainder, 'c');
8169
expect(select, ['a a', 'bbbb']);
82-
83-
// test paste followed by submit
84-
await tester.enterText(find.byType(TextField), 'aaa, bbb, c');
85-
await tester.testTextInput.receiveAction(TextInputAction.done);
86-
expect(select, ['aaa', 'bbb']);
87-
expect(submit, 'c');
8870
});
8971
});
9072
}

0 commit comments

Comments
 (0)