-
Notifications
You must be signed in to change notification settings - Fork 603
/
stateTransformer.test.ts
148 lines (138 loc) · 6.31 KB
/
stateTransformer.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/**
* @jest-environment jsdom
*/
import { LexicalNode } from "lexical";
import {
$isHeadingNode,
$isListNode,
$isParagraphNode,
$isQuoteNode,
HeadingNode,
ListNode,
ParagraphNode,
QuoteNode
} from "@webiny/lexical-nodes";
import { stateMock } from "./mocks/stateMocks";
import { createLexicalStateTransformer } from "~/createLexicalStateTransformer";
describe("Lexical State Transformer", () => {
it("should flatten lexical editor state to an array of objects with HTML", () => {
const transformer = createLexicalStateTransformer();
const output = transformer.flatten(stateMock);
expect(output).toEqual([
{
node: expect.any(HeadingNode),
html: `<h1 dir="ltr"><span style=\"white-space: pre-wrap;\">Test CMS Title</span></h1>`
},
{
node: expect.any(ParagraphNode),
html: `<p dir="ltr"><br></p>`
},
{
node: expect.any(ParagraphNode),
html: `<p dir="ltr"><span style=\"white-space: pre-wrap;\">Testing a </span><a href="https://space.com" rel="noreferrer"><span style=\"white-space: pre-wrap;\">link</span></a><span style=\"white-space: pre-wrap;\"> for parsing</span></p>`
},
{
node: expect.any(ParagraphNode),
html: `<p dir="ltr"><span style=\"white-space: pre-wrap;\">Test CMS Paragraph</span></p>`
},
{
node: expect.any(ParagraphNode),
html: `<p dir="ltr"><br></p>`
},
{
node: expect.any(QuoteNode),
html: `<blockquote dir="ltr"><span style=\"white-space: pre-wrap;\">Test quote from lexical </span><b><strong style=\"white-space: pre-wrap;\">CMS</strong></b></blockquote>`
},
{
node: expect.any(ParagraphNode),
html: `<p dir="ltr"><br></p>`
},
{
node: expect.any(ListNode),
html: `<ul data-theme-list-style-id="list"><li value="1"><span style=\"white-space: pre-wrap;\">List item 1</span></li><li value="2"><span style=\"white-space: pre-wrap;\">List item 2</span></li><li value="3"><span style=\"white-space: pre-wrap;\">List item 3</span></li></ul>`
},
{
node: expect.any(ParagraphNode),
html: `<p><br></p>`
}
]);
});
test("example of flat state post-processing", () => {
const transformer = createLexicalStateTransformer();
const nodeToType = (node: LexicalNode) => {
switch (true) {
case $isHeadingNode(node):
return "heading";
case $isParagraphNode(node):
return "paragraph";
case $isQuoteNode(node):
return "quote";
case $isListNode(node):
return "list";
default:
return "unknown";
}
};
const output = transformer.flatten(stateMock).map((item, index) => {
return {
order: index + 1,
text: item.html,
type: nodeToType(item.node)
};
});
expect(output).toEqual([
{
order: 1,
text: `<h1 dir="ltr"><span style=\"white-space: pre-wrap;\">Test CMS Title</span></h1>`,
type: "heading"
},
{
order: 2,
text: `<p dir="ltr"><br></p>`,
type: "paragraph"
},
{
order: 3,
text: `<p dir="ltr"><span style=\"white-space: pre-wrap;\">Testing a </span><a href="https://space.com" rel="noreferrer"><span style=\"white-space: pre-wrap;\">link</span></a><span style=\"white-space: pre-wrap;\"> for parsing</span></p>`,
type: "paragraph"
},
{
order: 4,
text: `<p dir="ltr"><span style=\"white-space: pre-wrap;\">Test CMS Paragraph</span></p>`,
type: "paragraph"
},
{
order: 5,
text: `<p dir="ltr"><br></p>`,
type: "paragraph"
},
{
order: 6,
text: `<blockquote dir="ltr"><span style=\"white-space: pre-wrap;\">Test quote from lexical </span><b><strong style=\"white-space: pre-wrap;\">CMS</strong></b></blockquote>`,
type: "quote"
},
{
order: 7,
text: `<p dir="ltr"><br></p>`,
type: "paragraph"
},
{
order: 8,
text: `<ul data-theme-list-style-id="list"><li value="1"><span style=\"white-space: pre-wrap;\">List item 1</span></li><li value="2"><span style=\"white-space: pre-wrap;\">List item 2</span></li><li value="3"><span style=\"white-space: pre-wrap;\">List item 3</span></li></ul>`,
type: "list"
},
{
order: 9,
text: `<p><br></p>`,
type: "paragraph"
}
]);
});
it("should convert lexical state to HTML", () => {
const transformer = createLexicalStateTransformer();
const output = transformer.toHtml(stateMock);
expect(output).toEqual(
`<h1 dir="ltr"><span style=\"white-space: pre-wrap;\">Test CMS Title</span></h1><p dir="ltr"><br></p><p dir="ltr"><span style=\"white-space: pre-wrap;\">Testing a </span><a href="https://space.com" rel="noreferrer"><span style=\"white-space: pre-wrap;\">link</span></a><span style=\"white-space: pre-wrap;\"> for parsing</span></p><p dir="ltr"><span style=\"white-space: pre-wrap;\">Test CMS Paragraph</span></p><p dir="ltr"><br></p><blockquote dir="ltr"><span style=\"white-space: pre-wrap;\">Test quote from lexical </span><b><strong style=\"white-space: pre-wrap;\">CMS</strong></b></blockquote><p dir="ltr"><br></p><ul data-theme-list-style-id="list"><li value="1"><span style=\"white-space: pre-wrap;\">List item 1</span></li><li value="2"><span style=\"white-space: pre-wrap;\">List item 2</span></li><li value="3"><span style=\"white-space: pre-wrap;\">List item 3</span></li></ul><p><br></p>`
);
});
});