Skip to content

Commit 9ad7185

Browse files
committed
test: add more tests
1 parent 1e9dfb3 commit 9ad7185

File tree

1 file changed

+55
-1
lines changed

1 file changed

+55
-1
lines changed

packages/core/src/extensions/Collaboration/schemaMigration/migrationRules/moveColorAttributes.test.ts

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ it("does not move color attributes on newer documents", async () => {
5353
props: {
5454
backgroundColor: "red",
5555
textColor: "blue",
56+
// Set to non-default value to ensure it is not overridden by the migration rule.
57+
textAlignment: "right",
5658
},
5759
},
5860
],
@@ -66,11 +68,63 @@ it("does not move color attributes on newer documents", async () => {
6668

6769
expect(fragment.toJSON()).toMatchInlineSnapshot(
6870
// The color attributes are on the paragraph, not the blockContainer.
69-
`"<blockgroup><blockcontainer id="0"><paragraph backgroundColor="red" textAlignment="left" textColor="blue">Welcome to this demo!</paragraph></blockcontainer></blockgroup>"`,
71+
`"<blockgroup><blockcontainer id="0"><paragraph backgroundColor="red" textAlignment="right" textColor="blue">Welcome to this demo!</paragraph></blockcontainer></blockgroup>"`,
7072
);
7173

7274
const tr = editor.prosemirrorState.tr;
7375
moveColorAttributes(fragment, tr);
7476
// The document will be unchanged because the color attributes are already on the paragraph.
7577
expect(tr.docChanged).toBe(false);
7678
});
79+
80+
it("can move color attributes on older documents multiple times", async () => {
81+
const doc = new Y.Doc();
82+
const fragment = doc.getXmlFragment("doc");
83+
const editor = BlockNoteEditor.create({
84+
initialContent: [
85+
{
86+
type: "paragraph",
87+
content: "Welcome to this demo!",
88+
},
89+
],
90+
});
91+
92+
// Because this was a previous schema, we are creating the YFragment manually
93+
const blockGroup = new Y.XmlElement("blockGroup");
94+
const el = new Y.XmlElement("blockContainer");
95+
el.setAttribute("id", "0");
96+
el.setAttribute("backgroundColor", "red");
97+
el.setAttribute("textColor", "blue");
98+
const para = new Y.XmlElement("paragraph");
99+
para.setAttribute("textAlignment", "left");
100+
para.insert(0, [new Y.XmlText("Welcome to this demo!")]);
101+
el.insert(0, [para]);
102+
blockGroup.insert(0, [el]);
103+
fragment.insert(0, [blockGroup]);
104+
105+
// Note that the blockContainer has the color attributes, but the paragraph does not.
106+
expect(fragment.toJSON()).toMatchInlineSnapshot(
107+
`"<blockgroup><blockcontainer backgroundColor="red" id="0" textColor="blue"><paragraph textAlignment="left">Welcome to this demo!</paragraph></blockcontainer></blockgroup>"`,
108+
);
109+
110+
const tr = editor.prosemirrorState.tr;
111+
moveColorAttributes(fragment, tr);
112+
// Note that the color attributes have been moved to the paragraph.
113+
expect(JSON.stringify(tr.doc.toJSON())).toMatchInlineSnapshot(
114+
`"{"type":"doc","content":[{"type":"blockGroup","content":[{"type":"blockContainer","attrs":{"id":"0"},"content":[{"type":"paragraph","attrs":{"backgroundColor":"red","textColor":"blue","textAlignment":"left"},"content":[{"type":"text","text":"Welcome to this demo!"}]}]}]}]}"`,
115+
);
116+
117+
el.setAttribute("backgroundColor", "green");
118+
el.setAttribute("textColor", "yellow");
119+
120+
expect(fragment.toJSON()).toMatchInlineSnapshot(
121+
`"<blockgroup><blockcontainer backgroundColor="green" id="0" textColor="yellow"><paragraph textAlignment="left">Welcome to this demo!</paragraph></blockcontainer></blockgroup>"`,
122+
);
123+
124+
const nextTr = editor.prosemirrorState.tr;
125+
moveColorAttributes(fragment, nextTr);
126+
// Note that the color attributes have been moved to the paragraph.
127+
expect(JSON.stringify(nextTr.doc.toJSON())).toMatchInlineSnapshot(
128+
`"{"type":"doc","content":[{"type":"blockGroup","content":[{"type":"blockContainer","attrs":{"id":"0"},"content":[{"type":"paragraph","attrs":{"backgroundColor":"green","textColor":"yellow","textAlignment":"left"},"content":[{"type":"text","text":"Welcome to this demo!"}]}]}]}]}"`,
129+
);
130+
});

0 commit comments

Comments
 (0)