mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
test: split test into widget and unit test (#1432)
This commit is contained in:
@ -136,31 +136,12 @@ class _SelectOptionTextFieldState extends State<SelectOptionTextField> {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
final trimmedText = text.trimLeft();
|
final result = splitInput(text.trimLeft(), widget.textSeparators);
|
||||||
List<String> splits = [];
|
|
||||||
String currentString = '';
|
|
||||||
|
|
||||||
// split the string into tokens
|
editingController.text = result[1];
|
||||||
for (final char in trimmedText.split('')) {
|
|
||||||
if (widget.textSeparators.contains(char)) {
|
|
||||||
if (currentString.isNotEmpty) {
|
|
||||||
splits.add(currentString.trim());
|
|
||||||
}
|
|
||||||
currentString = '';
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
currentString += char;
|
|
||||||
}
|
|
||||||
// add the remainder (might be '')
|
|
||||||
splits.add(currentString);
|
|
||||||
|
|
||||||
final submittedOptions = splits.sublist(0, splits.length - 1).toList();
|
|
||||||
|
|
||||||
final remainder = splits.elementAt(splits.length - 1).trimLeft();
|
|
||||||
editingController.text = remainder;
|
|
||||||
editingController.selection =
|
editingController.selection =
|
||||||
TextSelection.collapsed(offset: controller.text.length);
|
TextSelection.collapsed(offset: controller.text.length);
|
||||||
widget.onPaste(submittedOptions, remainder);
|
widget.onPaste(result[0], result[1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
Widget? _renderTags(BuildContext context, ScrollController sc) {
|
Widget? _renderTags(BuildContext context, ScrollController sc) {
|
||||||
@ -193,3 +174,28 @@ class _SelectOptionTextFieldState extends State<SelectOptionTextField> {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@visibleForTesting
|
||||||
|
List splitInput(String input, List<String> textSeparators) {
|
||||||
|
List<String> splits = [];
|
||||||
|
String currentString = '';
|
||||||
|
|
||||||
|
// split the string into tokens
|
||||||
|
for (final char in input.split('')) {
|
||||||
|
if (textSeparators.contains(char)) {
|
||||||
|
if (currentString.trim().isNotEmpty) {
|
||||||
|
splits.add(currentString.trim());
|
||||||
|
}
|
||||||
|
currentString = '';
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
currentString += char;
|
||||||
|
}
|
||||||
|
// add the remainder (might be '')
|
||||||
|
splits.add(currentString);
|
||||||
|
|
||||||
|
final submittedOptions = splits.sublist(0, splits.length - 1).toList();
|
||||||
|
final remainder = splits.elementAt(splits.length - 1).trimLeft();
|
||||||
|
|
||||||
|
return [submittedOptions, remainder];
|
||||||
|
}
|
||||||
|
@ -0,0 +1,46 @@
|
|||||||
|
import 'package:app_flowy/plugins/grid/presentation/widgets/cell/select_option_cell/text_field.dart';
|
||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
const textSeparators = [','];
|
||||||
|
|
||||||
|
group('split input unit test', () {
|
||||||
|
test('empty input', () {
|
||||||
|
List result = splitInput(' ', textSeparators);
|
||||||
|
expect(result[0], []);
|
||||||
|
expect(result[1], '');
|
||||||
|
|
||||||
|
result = splitInput(', , , ', textSeparators);
|
||||||
|
expect(result[0], []);
|
||||||
|
expect(result[1], '');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('simple input', () {
|
||||||
|
List result = splitInput('exampleTag', textSeparators);
|
||||||
|
expect(result[0], []);
|
||||||
|
expect(result[1], 'exampleTag');
|
||||||
|
|
||||||
|
result = splitInput('tag with longer name', textSeparators);
|
||||||
|
expect(result[0], []);
|
||||||
|
expect(result[1], 'tag with longer name');
|
||||||
|
|
||||||
|
result = splitInput('trailing space ', textSeparators);
|
||||||
|
expect(result[0], []);
|
||||||
|
expect(result[1], 'trailing space ');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('input with commas', () {
|
||||||
|
List result = splitInput('a, b, c', textSeparators);
|
||||||
|
expect(result[0], ['a', 'b']);
|
||||||
|
expect(result[1], 'c');
|
||||||
|
|
||||||
|
result = splitInput('a, b, c, ', textSeparators);
|
||||||
|
expect(result[0], ['a', 'b', 'c']);
|
||||||
|
expect(result[1], '');
|
||||||
|
|
||||||
|
result = splitInput(',tag 1 ,2nd tag, third tag ', textSeparators);
|
||||||
|
expect(result[0], ['tag 1', '2nd tag']);
|
||||||
|
expect(result[1], 'third tag ');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
@ -1,11 +1,9 @@
|
|||||||
import 'dart:collection';
|
import 'dart:collection';
|
||||||
|
|
||||||
import 'package:app_flowy/plugins/grid/presentation/widgets/cell/select_option_cell/text_field.dart';
|
import 'package:app_flowy/plugins/grid/presentation/widgets/cell/select_option_cell/text_field.dart';
|
||||||
import 'package:flowy_infra/theme.dart';
|
|
||||||
import 'package:flowy_sdk/protobuf/flowy-grid/protobuf.dart';
|
import 'package:flowy_sdk/protobuf/flowy-grid/protobuf.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_test/flutter_test.dart';
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
import 'package:provider/provider.dart';
|
|
||||||
import 'package:textfield_tags/textfield_tags.dart';
|
import 'package:textfield_tags/textfield_tags.dart';
|
||||||
|
|
||||||
import '../bloc_test/grid_test/util.dart';
|
import '../bloc_test/grid_test/util.dart';
|
||||||
@ -30,7 +28,7 @@ void main() {
|
|||||||
remainder = remaining;
|
remainder = remaining;
|
||||||
select = options;
|
select = options;
|
||||||
},
|
},
|
||||||
newText: (_) {},
|
newText: (text) => remainder = text,
|
||||||
textSeparators: const [','],
|
textSeparators: const [','],
|
||||||
textController: TextEditingController(),
|
textController: TextEditingController(),
|
||||||
);
|
);
|
||||||
@ -40,10 +38,7 @@ void main() {
|
|||||||
await tester.pumpWidget(
|
await tester.pumpWidget(
|
||||||
MaterialApp(
|
MaterialApp(
|
||||||
home: Material(
|
home: Material(
|
||||||
child: Provider<AppTheme>.value(
|
child: textField,
|
||||||
value: AppTheme.fromType(Brightness.light),
|
|
||||||
child: textField,
|
|
||||||
),
|
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
@ -63,28 +58,15 @@ void main() {
|
|||||||
await tester.testTextInput.receiveAction(TextInputAction.done);
|
await tester.testTextInput.receiveAction(TextInputAction.done);
|
||||||
expect(submit, 'an option');
|
expect(submit, 'an option');
|
||||||
|
|
||||||
await tester.enterText(find.byType(TextField), ' another one ');
|
submit = '';
|
||||||
|
await tester.enterText(find.byType(TextField), ' ');
|
||||||
await tester.testTextInput.receiveAction(TextInputAction.done);
|
await tester.testTextInput.receiveAction(TextInputAction.done);
|
||||||
expect(submit, 'another one');
|
expect(submit, '');
|
||||||
|
|
||||||
// test inputs containing commas
|
// test inputs containing commas
|
||||||
await tester.enterText(find.byType(TextField), ' abcd,');
|
await tester.enterText(find.byType(TextField), 'a a, bbbb , c');
|
||||||
expect(remainder, '');
|
expect(remainder, 'c');
|
||||||
expect(select, ['abcd']);
|
|
||||||
|
|
||||||
await tester.enterText(find.byType(TextField), ',acd, aaaa ');
|
|
||||||
expect(remainder, 'aaaa ');
|
|
||||||
expect(select, ['acd']);
|
|
||||||
|
|
||||||
await tester.enterText(find.byType(TextField), 'a a, bbbb , ');
|
|
||||||
expect(remainder, '');
|
|
||||||
expect(select, ['a a', 'bbbb']);
|
expect(select, ['a a', 'bbbb']);
|
||||||
|
|
||||||
// test paste followed by submit
|
|
||||||
await tester.enterText(find.byType(TextField), 'aaa, bbb, c');
|
|
||||||
await tester.testTextInput.receiveAction(TextInputAction.done);
|
|
||||||
expect(select, ['aaa', 'bbb']);
|
|
||||||
expect(submit, 'c');
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user