forked from TypeCellOS/BlockNote
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlockNoteEditor.test.ts
More file actions
251 lines (236 loc) · 7.31 KB
/
Copy pathBlockNoteEditor.test.ts
File metadata and controls
251 lines (236 loc) · 7.31 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import { expect, it } from "vitest";
import * as Y from "yjs";
import {
getBlockInfo,
getNearestBlockPos,
} from "../api/getBlockInfoFromPos.js";
import { BlockNoteEditor } from "./BlockNoteEditor.js";
import { BlocksChanged } from "../api/getBlocksChangedByTransaction.js";
/**
* @vitest-environment jsdom
*/
it("creates an editor", () => {
const editor = BlockNoteEditor.create();
const posInfo = editor.transact((tr) => getNearestBlockPos(tr.doc, 2));
const info = getBlockInfo(posInfo);
expect(info.blockNoteType).toEqual("paragraph");
});
it("immediately replaces doc", async () => {
const editor = BlockNoteEditor.create();
const blocks = await editor.tryParseMarkdownToBlocks(
"This is a normal text\n\n# And this is a large heading",
);
editor.replaceBlocks(editor.document, blocks);
expect(editor.document).toMatchInlineSnapshot(`
[
{
"children": [],
"content": [
{
"styles": {},
"text": "This is a normal text",
"type": "text",
},
],
"id": "1",
"props": {
"backgroundColor": "default",
"textAlignment": "left",
"textColor": "default",
},
"type": "paragraph",
},
{
"children": [],
"content": [
{
"styles": {},
"text": "And this is a large heading",
"type": "text",
},
],
"id": "2",
"props": {
"backgroundColor": "default",
"isToggleable": false,
"level": 1,
"textAlignment": "left",
"textColor": "default",
},
"type": "heading",
},
]
`);
});
it("adds id attribute when requested", async () => {
const editor = BlockNoteEditor.create({
setIdAttribute: true,
});
const blocks = await editor.tryParseMarkdownToBlocks(
"This is a normal text\n\n# And this is a large heading",
);
editor.replaceBlocks(editor.document, blocks);
expect(await editor.blocksToFullHTML(editor.document)).toMatchInlineSnapshot(
`"<div class="bn-block-group" data-node-type="blockGroup"><div class="bn-block-outer" data-node-type="blockOuter" data-id="1" id="1"><div class="bn-block" data-node-type="blockContainer" data-id="1" id="1"><div class="bn-block-content" data-content-type="paragraph"><p class="bn-inline-content">This is a normal text</p></div></div></div><div class="bn-block-outer" data-node-type="blockOuter" data-id="2" id="2"><div class="bn-block" data-node-type="blockContainer" data-id="2" id="2"><div class="bn-block-content" data-content-type="heading"><h1 class="bn-inline-content">And this is a large heading</h1></div></div></div></div>"`,
);
});
it("updates block", () => {
const editor = BlockNoteEditor.create();
editor.updateBlock(editor.document[0], {
content: "hello",
});
});
it("block prop types", () => {
// this test checks whether the block props are correctly typed in typescript
const editor = BlockNoteEditor.create();
const block = editor.document[0];
if (block.type === "paragraph") {
// @ts-expect-error
const level = block.props.level; // doesn't have level prop
// eslint-disable-next-line
expect(level).toBe(undefined);
}
if (block.type === "heading") {
const level = block.props.level; // does have level prop
// eslint-disable-next-line
expect(level).toBe(1);
}
});
it("onMount and onUnmount", async () => {
const editor = BlockNoteEditor.create();
let mounted = false;
let unmounted = false;
editor.onMount(() => {
mounted = true;
});
editor.onUnmount(() => {
unmounted = true;
});
editor.mount(document.createElement("div"));
expect(mounted).toBe(true);
expect(unmounted).toBe(false);
editor.unmount();
// expect the unmount event to not have been triggered yet, since it waits 2 ticks
// expect(unmounted).toBe(false);
// wait 3 ticks to ensure the unmount event is triggered
await new Promise((resolve) => setTimeout(resolve, 3));
expect(mounted).toBe(true);
expect(unmounted).toBe(true);
});
it("sets an initial block id when using Y.js", async () => {
const doc = new Y.Doc();
const fragment = doc.getXmlFragment("doc");
let transactionCount = 0;
const editor = BlockNoteEditor.create({
collaboration: {
fragment,
user: { name: "Hello", color: "#FFFFFF" },
},
_tiptapOptions: {
onTransaction: () => {
transactionCount++;
},
},
});
editor.mount(document.createElement("div"));
expect(editor.prosemirrorState.doc.toJSON()).toMatchInlineSnapshot(`
{
"content": [
{
"content": [
{
"attrs": {
"id": "initialBlockId",
},
"content": [
{
"attrs": {
"backgroundColor": "default",
"textAlignment": "left",
"textColor": "default",
},
"type": "paragraph",
},
],
"type": "blockContainer",
},
],
"type": "blockGroup",
},
],
"type": "doc",
}
`);
expect(transactionCount).toBe(1);
// The fragment should not be modified yet, since the editor's content is only the initial content
expect(fragment.toJSON()).toMatchInlineSnapshot(`""`);
editor.replaceBlocks(editor.document, [
{
type: "paragraph",
content: [{ text: "Hello", styles: {}, type: "text" }],
},
]);
expect(transactionCount).toBe(2);
// Only after a real modification is made, will the fragment be updated
expect(fragment.toJSON()).toMatchInlineSnapshot(
`"<blockgroup><blockcontainer id="0"><paragraph backgroundColor="default" textAlignment="left" textColor="default">Hello</paragraph></blockcontainer><blockcontainer id="1"><paragraph backgroundColor="default" textAlignment="left" textColor="default"></paragraph></blockcontainer></blockgroup>"`,
);
});
it("onBeforeChange", () => {
const editor = BlockNoteEditor.create();
let beforeChangeCalled = false;
let changes: BlocksChanged<any, any, any> = [];
editor.onBeforeChange(({ getChanges }) => {
beforeChangeCalled = true;
changes = getChanges();
return true;
});
editor.mount(document.createElement("div"));
editor.replaceBlocks(editor.document, [
{
type: "paragraph",
content: [{ text: "Hello", styles: {}, type: "text" }],
},
]);
expect(beforeChangeCalled).toBe(true);
expect(changes).toMatchInlineSnapshot(`
[
{
"block": {
"children": [],
"content": [],
"id": "3",
"props": {
"backgroundColor": "default",
"textAlignment": "left",
"textColor": "default",
},
"type": "paragraph",
},
"prevBlock": undefined,
"source": {
"type": "local",
},
"type": "insert",
},
{
"block": {
"children": [],
"content": [],
"id": "2",
"props": {
"backgroundColor": "default",
"textAlignment": "left",
"textColor": "default",
},
"type": "paragraph",
},
"prevBlock": undefined,
"source": {
"type": "local",
},
"type": "delete",
},
]
`);
});