Skip to content

Bring projects, wikis, and teams together with AI. AppFlowy is the AI collaborative workspace where you achieve more without losing control of your data. The leading open source Notion alternative.

test: add test for format_rich_text_style.dart

Lucas.Xu50814b2bee4b
diff --git a/frontend/app_flowy/packages/appflowy_editor/lib/src/service/default_text_operations/format_rich_text_style.dart b/frontend/app_flowy/packages/appflowy_editor/lib/src/service/default_text_operations/format_rich_text_style.dart
index 93ef3ebea9..f0312bdeff 100644
--- a/frontend/app_flowy/packages/appflowy_editor/lib/src/service/default_text_operations/format_rich_text_style.dart
+++ b/frontend/app_flowy/packages/appflowy_editor/lib/src/service/default_text_operations/format_rich_text_style.dart
@@ -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();
diff --git a/frontend/app_flowy/packages/appflowy_editor/test/service/default_text_operations/format_rich_text_style_test.dart b/frontend/app_flowy/packages/appflowy_editor/test/service/default_text_operations/format_rich_text_style_test.dart
new file mode 100644
index 0000000000..ccece41022
--- /dev/null
+++ b/frontend/app_flowy/packages/appflowy_editor/test/service/default_text_operations/format_rich_text_style_test.dart
@@ -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);
+ });
+ });
+}