forked from TypeCellOS/BlockNote
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlockNoteViewRemount.test.tsx
More file actions
72 lines (60 loc) · 2.05 KB
/
BlockNoteViewRemount.test.tsx
File metadata and controls
72 lines (60 loc) · 2.05 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
import { BlockNoteEditor } from "@blocknote/core";
import { BlockNoteView } from "@blocknote/mantine";
import "@blocknote/mantine/style.css";
import { FormattingToolbar } from "@blocknote/react";
import { flushSync } from "react-dom";
import { createRoot } from "react-dom/client";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
describe("FormattingToolbar unmount", () => {
let div: HTMLDivElement;
beforeEach(() => {
div = document.createElement("div");
document.body.appendChild(div);
});
afterEach(() => {
document.body.removeChild(div);
});
it("should not throw error when unmounting editor with FormattingToolbar", () => {
const editor = BlockNoteEditor.create();
const root = createRoot(div);
// Mount the editor with FormattingToolbar
// This will cause CreateLinkButton to mount and register its event listeners
flushSync(() => {
root.render(
<BlockNoteView editor={editor}>
<FormattingToolbar />
</BlockNoteView>,
);
});
// Unmount should not throw error
// Before the fix in commit 17bff6362, this would throw:
// "Cannot read properties of undefined (reading 'removeEventListener')"
// because CreateLinkButton's useEffect cleanup tries to access
// editor.prosemirrorView.dom.removeEventListener()
// but prosemirrorView.dom is undefined when the editor is being destroyed
expect(() => {
root.unmount();
}).not.toThrow();
// Cleanup
editor._tiptapEditor.destroy();
});
it("should handle rapid mount/unmount cycles", () => {
// Test multiple mount/unmount cycles
for (let i = 0; i < 3; i++) {
const editor = BlockNoteEditor.create();
const root = createRoot(div);
flushSync(() => {
root.render(
<BlockNoteView editor={editor}>
<FormattingToolbar />
</BlockNoteView>,
);
});
// Should not throw on unmount
expect(() => {
root.unmount();
}).not.toThrow();
editor._tiptapEditor.destroy();
}
});
});