Skip to content

Commit 96d202b

Browse files
committed
Made test functions pluggable for test cases
1 parent 0e418d5 commit 96d202b

File tree

141 files changed

+3875
-7398
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

141 files changed

+3875
-7398
lines changed

packages/core/src/api/tests/clipboard/clipboardTestUtil.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
import { Node, Slice } from "@tiptap/pm/model";
2+
import { Selection } from "@tiptap/pm/state";
23
import { EditorView } from "@tiptap/pm/view";
34
import * as pmView from "@tiptap/pm/view";
45

6+
import { PartialBlock } from "../../../blocks/defaultBlocks.js";
7+
import { BlockNoteEditor } from "../../../editor/BlockNoteEditor.js";
8+
import {
9+
BlockSchema,
10+
InlineContentSchema,
11+
StyleSchema,
12+
} from "../../../schema/index.js";
13+
514
// Helper function to get the position of a text node with given text content.
615
// By default, returns the position just before the node, but can be just after
716
// instead if `after` is set to true.
@@ -49,6 +58,30 @@ export const getPosOfTableCellNode = (doc: Node, textContent: string) => {
4958
return ret;
5059
};
5160

61+
export const setupClipboardTest = <
62+
B extends BlockSchema,
63+
I extends InlineContentSchema,
64+
S extends StyleSchema
65+
>(
66+
editor: BlockNoteEditor<B, I, S>,
67+
document: PartialBlock<B, I, S>[],
68+
getSelection: (pmDoc: Node) => Selection
69+
) => {
70+
if (!editor.prosemirrorView) {
71+
throw new Error("Editor view not initialized.");
72+
}
73+
74+
(window as any).__TEST_OPTIONS.mockID = 0;
75+
76+
editor.replaceBlocks(editor.document, document);
77+
78+
editor.dispatch(
79+
editor._tiptapEditor.state.tr.setSelection(
80+
getSelection(editor.prosemirrorView.state.doc)
81+
)
82+
);
83+
};
84+
5285
function sliceSingleNode(slice: Slice) {
5386
return slice.openStart === 0 &&
5487
slice.openEnd === 0 &&
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { Node } from "@tiptap/pm/model";
2+
import { Selection } from "@tiptap/pm/state";
3+
4+
import { PartialBlock } from "../../../../blocks/defaultBlocks.js";
5+
import {
6+
BlockSchema,
7+
InlineContentSchema,
8+
StyleSchema,
9+
} from "../../../../schema/index.js";
10+
11+
export type CopyTestCase<
12+
B extends BlockSchema,
13+
I extends InlineContentSchema,
14+
S extends StyleSchema
15+
> = {
16+
name: string;
17+
document: PartialBlock<B, I, S>[];
18+
getCopySelection: (pmDoc: Node) => Selection;
19+
};
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { prettify } from "htmlfy";
2+
import { expect } from "vitest";
3+
4+
import { BlockNoteEditor } from "../../../../editor/BlockNoteEditor.js";
5+
import {
6+
BlockSchema,
7+
InlineContentSchema,
8+
StyleSchema,
9+
} from "../../../../schema/index.js";
10+
import { selectedFragmentToHTML } from "../../../clipboard/toClipboard/copyExtension.js";
11+
import { setupClipboardTest } from "../clipboardTestUtil.js";
12+
import { CopyTestCase } from "./copyTestCase.js";
13+
14+
export const testCopyBlockNoteHTML = async <
15+
B extends BlockSchema,
16+
I extends InlineContentSchema,
17+
S extends StyleSchema
18+
>(
19+
editor: BlockNoteEditor<B, I, S>,
20+
testCase: CopyTestCase<B, I, S>
21+
) => {
22+
setupClipboardTest(editor, testCase.document, testCase.getCopySelection);
23+
24+
const { clipboardHTML } = selectedFragmentToHTML(
25+
editor.prosemirrorView!,
26+
editor
27+
);
28+
29+
await expect(prettify(clipboardHTML, { tag_wrap: true })).toMatchFileSnapshot(
30+
`./__snapshots__/blocknote/html/${testCase.name}.html`
31+
);
32+
};
33+
34+
export const testCopyHTML = async <
35+
B extends BlockSchema,
36+
I extends InlineContentSchema,
37+
S extends StyleSchema
38+
>(
39+
editor: BlockNoteEditor<B, I, S>,
40+
testCase: CopyTestCase<B, I, S>
41+
) => {
42+
setupClipboardTest(editor, testCase.document, testCase.getCopySelection);
43+
44+
const { externalHTML } = selectedFragmentToHTML(
45+
editor.prosemirrorView!,
46+
editor
47+
);
48+
49+
await expect(prettify(externalHTML, { tag_wrap: true })).toMatchFileSnapshot(
50+
`./__snapshots__/text/html/${testCase.name}.html`
51+
);
52+
};
53+
54+
export const testCopyMarkdown = async <
55+
B extends BlockSchema,
56+
I extends InlineContentSchema,
57+
S extends StyleSchema
58+
>(
59+
editor: BlockNoteEditor<B, I, S>,
60+
testCase: CopyTestCase<B, I, S>
61+
) => {
62+
setupClipboardTest(editor, testCase.document, testCase.getCopySelection);
63+
64+
const { markdown } = selectedFragmentToHTML(editor.prosemirrorView!, editor);
65+
66+
await expect(markdown).toMatchFileSnapshot(
67+
`./__snapshots__/text/plain/${testCase.name}.md`
68+
);
69+
};

0 commit comments

Comments
 (0)