Skip to content

Commit c77b578

Browse files
committed
test: add a test case showing the inital block id behavior
1 parent 6ce83dd commit c77b578

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

packages/core/src/editor/BlockNoteEditor.test.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
} from "../api/getBlockInfoFromPos.js";
66
import { BlockNoteEditor } from "./BlockNoteEditor.js";
77
import { BlockNoteExtension } from "./BlockNoteExtension.js";
8+
import * as Y from "yjs";
89

910
/**
1011
* @vitest-environment jsdom
@@ -146,3 +147,67 @@ it("onCreate event", () => {
146147
});
147148
expect(created).toBe(true);
148149
});
150+
151+
it("sets an initial block id when using Y.js", async () => {
152+
const doc = new Y.Doc();
153+
const fragment = doc.getXmlFragment("doc");
154+
let transactionCount = 0;
155+
const editor = BlockNoteEditor.create({
156+
collaboration: {
157+
fragment,
158+
user: { name: "Hello", color: "#FFFFFF" },
159+
provider: null,
160+
},
161+
_tiptapOptions: {
162+
onTransaction: () => {
163+
transactionCount++;
164+
},
165+
},
166+
});
167+
168+
editor.mount(document.createElement("div"));
169+
170+
expect(editor.prosemirrorState.doc.toJSON()).toMatchInlineSnapshot(`
171+
{
172+
"content": [
173+
{
174+
"content": [
175+
{
176+
"attrs": {
177+
"id": "initialBlockId",
178+
},
179+
"content": [
180+
{
181+
"attrs": {
182+
"backgroundColor": "default",
183+
"textAlignment": "left",
184+
"textColor": "default",
185+
},
186+
"type": "paragraph",
187+
},
188+
],
189+
"type": "blockContainer",
190+
},
191+
],
192+
"type": "blockGroup",
193+
},
194+
],
195+
"type": "doc",
196+
}
197+
`);
198+
expect(transactionCount).toBe(1);
199+
// The fragment should not be modified yet, since the editor's content is only the initial content
200+
expect(fragment.toJSON()).toMatchInlineSnapshot(`""`);
201+
202+
editor.replaceBlocks(editor.document, [
203+
{
204+
type: "paragraph",
205+
content: [{ text: "Hello", styles: {}, type: "text" }],
206+
},
207+
]);
208+
expect(transactionCount).toBe(2);
209+
// Only after a real modification is made, will the fragment be updated
210+
expect(fragment.toJSON()).toMatchInlineSnapshot(
211+
`"<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>"`,
212+
);
213+
});

0 commit comments

Comments
 (0)