Skip to content

Commit 2ac3ef8

Browse files
committed
Added tests
1 parent c048296 commit 2ac3ef8

21 files changed

Lines changed: 638 additions & 5 deletions

File tree

packages/math-block/src/block/vanilla/createMathBlockSpec.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,4 +254,29 @@ describe("Math block source popup keyboard handling", () => {
254254
expect(editor.prosemirrorView.state.selection.$from.parentOffset).toBe(3);
255255
});
256256
});
257+
258+
describe("clicking the OK button", () => {
259+
beforeEach(() => {
260+
setup([
261+
{ id: "before", type: "paragraph", content: "before" },
262+
{ id: "math", type: "math", content: "a^2" },
263+
]);
264+
editor.setTextCursorPosition("math", "start");
265+
// Open the popup so the OK button has something to close.
266+
pressKey("math", "Enter");
267+
expect(isPopupOpen("math")).toBe(true);
268+
});
269+
270+
it("closes the popup", () => {
271+
const okButton = div.querySelector(
272+
`.bn-block[data-id="math"] .bn-code-block-source-popup-ok-button`,
273+
) as HTMLElement;
274+
275+
okButton.dispatchEvent(
276+
new MouseEvent("mousedown", { bubbles: true, cancelable: true }),
277+
);
278+
279+
expect(isPopupOpen("math")).toBe(false);
280+
});
281+
});
257282
});

packages/math-block/src/inlineContent/react/createReactMathInlineContentSpec.test.tsx

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,4 +226,31 @@ describe("Inline math source popup", () => {
226226
expect(editor.prosemirrorState.selection.$from.parentOffset).toBe(3);
227227
});
228228
});
229+
230+
describe("clicking the OK button", () => {
231+
beforeEach(async () => {
232+
// Open the popup so the OK button has something to close.
233+
selectSource();
234+
await flush();
235+
expect(isPopupOpen()).toBe(true);
236+
});
237+
238+
it("closes the popup and moves the selection just after the inline content", async () => {
239+
const { node, pos } = inlineMath();
240+
241+
const okButton = div.querySelector(
242+
".bn-code-block-source-popup-ok-button",
243+
) as HTMLElement;
244+
245+
okButton.dispatchEvent(
246+
new MouseEvent("mousedown", { bubbles: true, cancelable: true }),
247+
);
248+
await flush();
249+
250+
expect(isPopupOpen()).toBe(false);
251+
expect(selectedNodeType()).not.toBe("inlineMath");
252+
// The caret lands just after the inline math.
253+
expect(editor.prosemirrorState.selection.from).toBe(pos + node.nodeSize);
254+
});
255+
});
229256
});

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"@blocknote/ariakit": "workspace:^",
1313
"@blocknote/core": "workspace:^",
1414
"@blocknote/mantine": "workspace:^",
15+
"@blocknote/math-block": "workspace:^",
1516
"@blocknote/react": "workspace:^",
1617
"@blocknote/shadcn": "workspace:^",
1718
"@playwright/test": "1.60.0",
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
The identity
2+
<span
3+
as="span"
4+
class="bn-inline-content-section"
5+
data-inline-content-type="inlineMath"
6+
data-node-view-wrapper=""
7+
style="white-space: normal;"
8+
>
9+
<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline">
10+
<semantics>
11+
<mrow>
12+
<msup>
13+
<mi>e</mi>
14+
<mrow>
15+
<mi>i</mi>
16+
<mi>π</mi>
17+
</mrow>
18+
</msup>
19+
<mo>+</mo>
20+
<mn>1</mn>
21+
<mo>=</mo>
22+
<mn>0</mn>
23+
</mrow>
24+
<annotation encoding="application/x-tex">e^{i\pi} + 1 = 0</annotation>
25+
</semantics>
26+
</math>
27+
</span>
28+
is elegant.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block">
2+
<semantics>
3+
<mrow>
4+
<msup>
5+
<mi>a</mi>
6+
<mn>2</mn>
7+
</msup>
8+
<mo>+</mo>
9+
<msup>
10+
<mi>b</mi>
11+
<mn>2</mn>
12+
</msup>
13+
<mo>=</mo>
14+
<msup>
15+
<mi>c</mi>
16+
<mn>2</mn>
17+
</msup>
18+
</mrow>
19+
<annotation encoding="application/x-tex">a^2 + b^2 = c^2</annotation>
20+
</semantics>
21+
</math>
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { NodeSelection, TextSelection } from "@tiptap/pm/state";
2+
3+
import { CopyTestCase } from "../../../shared/clipboard/copy/copyTestCase.js";
4+
import { testCopyHTML } from "../../../shared/clipboard/copy/copyTestExecutors.js";
5+
import { getPosOfTextNode } from "../../../shared/testUtil.js";
6+
import { TestInstance } from "../../../types.js";
7+
import {
8+
TestBlockSchema,
9+
TestInlineContentSchema,
10+
TestStyleSchema,
11+
} from "../../testSchema.js";
12+
13+
export const copyTestInstancesHTML: TestInstance<
14+
CopyTestCase<TestBlockSchema, TestInlineContentSchema, TestStyleSchema>,
15+
TestBlockSchema,
16+
TestInlineContentSchema,
17+
TestStyleSchema
18+
>[] = [
19+
{
20+
testCase: {
21+
name: "mathBlock",
22+
document: [
23+
{
24+
type: "math",
25+
content: "a^2 + b^2 = c^2",
26+
},
27+
],
28+
getCopySelection: (doc) => {
29+
let startPos: number | undefined = undefined;
30+
31+
doc.descendants((node, pos) => {
32+
if (node.type.name === "math") {
33+
startPos = pos;
34+
}
35+
});
36+
37+
if (startPos === undefined) {
38+
throw new Error("Math node not found.");
39+
}
40+
41+
return NodeSelection.create(doc, startPos);
42+
},
43+
},
44+
executeTest: testCopyHTML,
45+
},
46+
{
47+
testCase: {
48+
name: "inlineMath",
49+
document: [
50+
{
51+
type: "paragraph",
52+
content: [
53+
"The identity ",
54+
{
55+
type: "inlineMath",
56+
content: "e^{i\\pi} + 1 = 0",
57+
} as const,
58+
" is elegant.",
59+
],
60+
},
61+
],
62+
getCopySelection: (doc) => {
63+
const startPos = getPosOfTextNode(doc, "The identity ");
64+
const endPos = getPosOfTextNode(doc, " is elegant.", true);
65+
66+
return TextSelection.create(doc, startPos, endPos);
67+
},
68+
},
69+
executeTest: testCopyHTML,
70+
},
71+
];
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { describe, it } from "vite-plus/test";
2+
3+
import { setupTestEditor } from "../../setupTestEditor.js";
4+
import { testSchema } from "../../testSchema.js";
5+
import { copyTestInstancesHTML } from "./copyTestInstances.js";
6+
7+
describe("React copy tests (HTML)", () => {
8+
const getEditor = setupTestEditor(testSchema);
9+
10+
for (const { testCase, executeTest } of copyTestInstancesHTML) {
11+
it(`${testCase.name}`, async () => {
12+
await executeTest(getEditor(), testCase);
13+
});
14+
}
15+
});
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { CopyPasteEqualityTestCase } from "../../../shared/clipboard/copyPasteEquality/copyPasteEqualityTestCase.js";
2+
import { testCopyPasteEquality } from "../../../shared/clipboard/copyPasteEquality/copyPasteEqualityTestExecutors.js";
3+
import { TestInstance } from "../../../types.js";
4+
import {
5+
TestBlockSchema,
6+
TestInlineContentSchema,
7+
TestStyleSchema,
8+
} from "../../testSchema.js";
9+
import { copyTestInstancesHTML } from "../copy/copyTestInstances.js";
10+
11+
// NOTE: Only the block is covered in these tests. Inline `<math>` is foreign (non-HTML) content,
12+
// and ProseMirror's clipboard parser in the jsdom unit environment doesn't run the inline
13+
// content's `parseContent` for it, so pasting the copied inline math doubles its source here. It
14+
// round-trips correctly in a real browser and should be covered by the browser-mode e2e suite
15+
// instead.
16+
export const copyPasteEqualityTestInstances: TestInstance<
17+
CopyPasteEqualityTestCase<
18+
TestBlockSchema,
19+
TestInlineContentSchema,
20+
TestStyleSchema
21+
>,
22+
TestBlockSchema,
23+
TestInlineContentSchema,
24+
TestStyleSchema
25+
>[] = copyTestInstancesHTML
26+
.filter(({ testCase }) => testCase.name === "mathBlock")
27+
.map(({ testCase }) => ({
28+
testCase: {
29+
name: testCase.name,
30+
document: testCase.document,
31+
getCopyAndPasteSelection: testCase.getCopySelection,
32+
},
33+
executeTest: testCopyPasteEquality,
34+
}));
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { describe, it } from "vite-plus/test";
2+
3+
import { setupTestEditor } from "../../setupTestEditor.js";
4+
import { testSchema } from "../../testSchema.js";
5+
import { copyPasteEqualityTestInstances } from "./copyPasteEqualityTestInstances.js";
6+
7+
describe("React copy/paste equality tests", () => {
8+
const getEditor = setupTestEditor(testSchema);
9+
10+
for (const { testCase, executeTest } of copyPasteEqualityTestInstances) {
11+
it(`${testCase.name}`, async () => {
12+
await executeTest(getEditor(), testCase);
13+
});
14+
}
15+
});

0 commit comments

Comments
 (0)