forked from TypeCellOS/BlockNote
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetupTestEditor.tsx
More file actions
63 lines (53 loc) · 1.74 KB
/
setupTestEditor.tsx
File metadata and controls
63 lines (53 loc) · 1.74 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
import {
BlockNoteEditor,
BlockNoteSchema,
BlockSchema,
InlineContentSchema,
StyleSchema,
} from "@blocknote/core";
import { BlockNoteViewRaw } from "@blocknote/react";
import { flushSync } from "react-dom";
import { createRoot, Root } from "react-dom/client";
import { afterAll, beforeAll } from "vitest";
import { TestContext } from "./testSchema.js";
export const setupTestEditor = <
B extends BlockSchema,
I extends InlineContentSchema,
S extends StyleSchema,
>(
schema: BlockNoteSchema<B, I, S>,
): (() => BlockNoteEditor<B, I, S>) => {
let editor: BlockNoteEditor<B, I, S>;
const div = document.createElement("div");
// Note that we don't necessarily need to mount a root (unless we need a React Context)
// Currently, we do mount to a root so that it reflects the "production" use-case more closely.
// However, it would be nice to increased converage and share the same set of tests for these cases:
// - does render to a root
// - does not render to a root
// - runs in server (jsdom) environment using server-util
let root: Root;
beforeAll(async () => {
(window as any).__TEST_OPTIONS = (window as any).__TEST_OPTIONS || {};
editor = BlockNoteEditor.create({
schema,
trailingBlock: false,
});
const el = (
<TestContext.Provider value={true}>
<BlockNoteViewRaw editor={editor} />
</TestContext.Provider>
);
root = createRoot(div);
flushSync(() => {
// eslint-disable-next-line testing-library/no-render-in-setup
root.render(el);
});
});
afterAll(() => {
root.unmount();
editor._tiptapEditor.destroy();
editor = undefined as any;
delete (window as Window & { __TEST_OPTIONS?: any }).__TEST_OPTIONS;
});
return () => editor;
};