test: add test for format_rich_text_style.dart

This commit is contained in:
Lucas.Xu 2022-10-25 11:13:14 +08:00
parent 5d2ff8e07c
commit 50814b2bee
2 changed files with 40 additions and 0 deletions

View File

@ -91,6 +91,10 @@ void formatBulletedList(EditorState editorState) {
});
}
/// Format the current selection with the given attributes.
///
/// If the selected nodes are not text nodes, this method will do nothing.
/// If the selected text nodes already contain the style in attributes, this method will remove the existing style.
bool formatTextNodes(EditorState editorState, Attributes attributes) {
final nodes = editorState.service.selectionService.currentSelectedNodes;
final textNodes = nodes.whereType<TextNode>().toList();

View File

@ -0,0 +1,36 @@
import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:appflowy_editor/src/render/rich_text/quoted_text.dart';
import 'package:appflowy_editor/src/service/default_text_operations/format_rich_text_style.dart';
import 'package:flutter_test/flutter_test.dart';
import '../../infra/test_editor.dart';
void main() async {
setUpAll(() {
TestWidgetsFlutterBinding.ensureInitialized();
});
group('format_rich_text_style.dart', () {
testWidgets('formatTextNodes', (tester) async {
const text = 'Welcome to Appflowy 😁';
final editor = tester.editor..insertTextNode(text);
await editor.startTesting();
await editor.updateSelection(
Selection.single(path: [0], startOffset: 0, endOffset: text.length),
);
// format the text to Quote
formatTextNodes(editor.editorState, {
BuiltInAttributeKey.subtype: BuiltInAttributeKey.quote,
});
await tester.pumpAndSettle(const Duration(seconds: 1));
expect(find.byType(QuotedTextNodeWidget), findsOneWidget);
// format the text to Quote again. The style should be removed.
formatTextNodes(editor.editorState, {
BuiltInAttributeKey.subtype: BuiltInAttributeKey.quote,
});
await tester.pumpAndSettle();
expect(find.byType(QuotedTextNodeWidget), findsNothing);
});
});
}