Skip to content

Commit 2e7f803

Browse files
committed
test: markdown encoder test
1 parent 9eaa79b commit 2e7f803

File tree

4 files changed

+249
-0
lines changed

4 files changed

+249
-0
lines changed
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
import 'dart:convert';
2+
3+
import 'package:appflowy_editor/appflowy_editor.dart';
4+
import 'package:flutter_test/flutter_test.dart';
5+
6+
void main() async {
7+
group('document_markdown_encoder.dart', () {
8+
const example = '''
9+
{
10+
"document": {
11+
"type": "editor",
12+
"children": [
13+
{
14+
"type": "text",
15+
"attributes": {
16+
"subtype": "heading",
17+
"heading": "h2"
18+
},
19+
"delta": [
20+
{ "insert": "👋 " },
21+
{ "insert": "Welcome to", "attributes": { "bold": true } },
22+
{ "insert": " " },
23+
{
24+
"insert": "AppFlowy Editor",
25+
"attributes": {
26+
"href": "appflowy.io",
27+
"italic": true,
28+
"bold": true
29+
}
30+
}
31+
]
32+
},
33+
{ "type": "text", "delta": [] },
34+
{
35+
"type": "text",
36+
"delta": [
37+
{ "insert": "AppFlowy Editor is a " },
38+
{ "insert": "highly customizable", "attributes": { "bold": true } },
39+
{ "insert": " " },
40+
{ "insert": "rich-text editor", "attributes": { "italic": true } },
41+
{ "insert": " for " },
42+
{ "insert": "Flutter", "attributes": { "underline": true } }
43+
]
44+
},
45+
{
46+
"type": "text",
47+
"attributes": { "checkbox": true, "subtype": "checkbox" },
48+
"delta": [{ "insert": "Customizable" }]
49+
},
50+
{
51+
"type": "text",
52+
"attributes": { "checkbox": true, "subtype": "checkbox" },
53+
"delta": [{ "insert": "Test-covered" }]
54+
},
55+
{
56+
"type": "text",
57+
"attributes": { "checkbox": false, "subtype": "checkbox" },
58+
"delta": [{ "insert": "more to come!" }]
59+
},
60+
{ "type": "text", "delta": [] },
61+
{
62+
"type": "text",
63+
"attributes": { "subtype": "quote" },
64+
"delta": [{ "insert": "Here is an example you can give a try" }]
65+
},
66+
{ "type": "text", "delta": [] },
67+
{
68+
"type": "text",
69+
"delta": [
70+
{ "insert": "You can also use " },
71+
{
72+
"insert": "AppFlowy Editor",
73+
"attributes": {
74+
"italic": true,
75+
"bold": true,
76+
"backgroundColor": "0x6000BCF0"
77+
}
78+
},
79+
{ "insert": " as a component to build your own app." }
80+
]
81+
},
82+
{ "type": "text", "delta": [] },
83+
{
84+
"type": "text",
85+
"attributes": { "subtype": "bulleted-list" },
86+
"delta": [{ "insert": "Use / to insert blocks" }]
87+
},
88+
{
89+
"type": "text",
90+
"attributes": { "subtype": "bulleted-list" },
91+
"delta": [
92+
{
93+
"insert": "Select text to trigger to the toolbar to format your notes."
94+
}
95+
]
96+
},
97+
{ "type": "text", "delta": [] },
98+
{
99+
"type": "text",
100+
"delta": [
101+
{
102+
"insert": "If you have questions or feedback, please submit an issue on Github or join the community along with 1000+ builders!"
103+
}
104+
]
105+
}
106+
]
107+
}
108+
}
109+
''';
110+
setUpAll(() {
111+
TestWidgetsFlutterBinding.ensureInitialized();
112+
});
113+
114+
test('parser document', () async {
115+
final data = Map<String, Object>.from(json.decode(example));
116+
final document = Document.fromJson(data);
117+
final result = DocumentMarkdownEncoder().convert(document);
118+
expect(result, '''
119+
## 👋 **Welcome to** ***[AppFlowy Editor](appflowy.io)***
120+
121+
AppFlowy Editor is a **highly customizable** _rich-text editor_ for <u>Flutter</u>
122+
- [x] Customizable
123+
- [x] Test-covered
124+
- [ ] more to come!
125+
126+
> Here is an example you can give a try
127+
128+
You can also use ***AppFlowy Editor*** as a component to build your own app.
129+
130+
* Use / to insert blocks
131+
* Select text to trigger to the toolbar to format your notes.
132+
133+
If you have questions or feedback, please submit an issue on Github or join the community along with 1000+ builders!
134+
''');
135+
});
136+
});
137+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import 'package:appflowy_editor/appflowy_editor.dart';
2+
import 'package:flutter_test/flutter_test.dart';
3+
4+
void main() async {
5+
group('image_node_parser.dart', () {
6+
test('parser image node', () {
7+
final node = Node(
8+
type: 'image',
9+
attributes: {
10+
'image_src': 'https://appflowy.io',
11+
},
12+
);
13+
final result = const ImageNodeParser().transform(node);
14+
expect(result, '![](https://appflowy.io)');
15+
});
16+
});
17+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import 'package:appflowy_editor/appflowy_editor.dart';
2+
import 'package:flutter_test/flutter_test.dart';
3+
4+
void main() async {
5+
group('text_node_parser.dart', () {
6+
const text = 'Welcome to AppFlowy';
7+
8+
test('heading style', () {
9+
final h1 = TextNode(
10+
delta: Delta(operations: [TextInsert(text)]),
11+
attributes: {
12+
BuiltInAttributeKey.subtype: BuiltInAttributeKey.heading,
13+
BuiltInAttributeKey.heading: BuiltInAttributeKey.h1,
14+
},
15+
);
16+
final h2 = TextNode(
17+
delta: Delta(operations: [TextInsert(text)]),
18+
attributes: {
19+
BuiltInAttributeKey.subtype: BuiltInAttributeKey.heading,
20+
BuiltInAttributeKey.heading: BuiltInAttributeKey.h2,
21+
},
22+
);
23+
final h3 = TextNode(
24+
delta: Delta(operations: [TextInsert(text)]),
25+
attributes: {
26+
BuiltInAttributeKey.subtype: BuiltInAttributeKey.heading,
27+
BuiltInAttributeKey.heading: BuiltInAttributeKey.h3,
28+
},
29+
);
30+
expect(const TextNodeParser().transform(h1), '# $text');
31+
expect(const TextNodeParser().transform(h2), '## $text');
32+
expect(const TextNodeParser().transform(h3), '### $text');
33+
});
34+
35+
test('bulleted list style', () {
36+
final node = TextNode(
37+
delta: Delta(operations: [TextInsert(text)]),
38+
attributes: {
39+
BuiltInAttributeKey.subtype: BuiltInAttributeKey.bulletedList,
40+
},
41+
);
42+
expect(const TextNodeParser().transform(node), '* $text');
43+
});
44+
45+
test('number list style', () {
46+
final node = TextNode(
47+
delta: Delta(operations: [TextInsert(text)]),
48+
attributes: {
49+
BuiltInAttributeKey.subtype: BuiltInAttributeKey.numberList,
50+
BuiltInAttributeKey.number: 1,
51+
},
52+
);
53+
expect(const TextNodeParser().transform(node), '1. $text');
54+
});
55+
56+
test('checkbox style', () {
57+
final checkbox = TextNode(
58+
delta: Delta(operations: [TextInsert(text)]),
59+
attributes: {
60+
BuiltInAttributeKey.subtype: BuiltInAttributeKey.checkbox,
61+
BuiltInAttributeKey.checkbox: true,
62+
},
63+
);
64+
final unCheckbox = TextNode(
65+
delta: Delta(operations: [TextInsert(text)]),
66+
attributes: {
67+
BuiltInAttributeKey.subtype: BuiltInAttributeKey.checkbox,
68+
BuiltInAttributeKey.checkbox: false,
69+
},
70+
);
71+
expect(const TextNodeParser().transform(checkbox), '- [x] $text');
72+
expect(const TextNodeParser().transform(unCheckbox), '- [ ] $text');
73+
});
74+
75+
test('quote style', () {
76+
final node = TextNode(
77+
delta: Delta(operations: [TextInsert(text)]),
78+
attributes: {
79+
BuiltInAttributeKey.subtype: BuiltInAttributeKey.quote,
80+
},
81+
);
82+
expect(const TextNodeParser().transform(node), '> $text');
83+
});
84+
85+
test('code block style', () {
86+
final node = TextNode(
87+
delta: Delta(operations: [TextInsert(text)]),
88+
attributes: {
89+
BuiltInAttributeKey.subtype: 'code-block',
90+
},
91+
);
92+
expect(const TextNodeParser().transform(node), '```\n$text\n```');
93+
});
94+
});
95+
}

0 commit comments

Comments
 (0)