vitest-prosemirror 0.1.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -4,7 +4,11 @@ import { Plugin as Plugin_2 } from 'prosemirror-state';
|
|
|
4
4
|
import { Schema } from 'prosemirror-model';
|
|
5
5
|
import { Selection as Selection_2 } from 'prosemirror-state';
|
|
6
6
|
|
|
7
|
-
declare interface
|
|
7
|
+
export declare interface CustomMatchers<R = unknown> {
|
|
8
|
+
toEqualProseMirrorNode(expected: Node_2): R;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export declare interface Options {
|
|
8
12
|
plugins: Array<Plugin_2>;
|
|
9
13
|
}
|
|
10
14
|
|
|
@@ -19,11 +23,16 @@ export declare class ProseMirrorTester {
|
|
|
19
23
|
private getSelection;
|
|
20
24
|
}
|
|
21
25
|
|
|
22
|
-
declare type TesterSelection = "all" | "end" | "start" | {
|
|
26
|
+
export declare type TesterSelection = "all" | "end" | "start" | {
|
|
23
27
|
from: number;
|
|
24
28
|
to: number;
|
|
25
29
|
} | Selection_2 | number;
|
|
26
30
|
|
|
31
|
+
/**
|
|
32
|
+
* @public
|
|
33
|
+
*/
|
|
34
|
+
export declare function trimProseMirrorNode(node: Node_2): Node_2;
|
|
35
|
+
|
|
27
36
|
export { }
|
|
28
37
|
|
|
29
38
|
|
|
@@ -3,6 +3,7 @@ import stringifyObject from "stringify-object";
|
|
|
3
3
|
import { EditorState, AllSelection, TextSelection } from "prosemirror-state";
|
|
4
4
|
import { EditorView } from "prosemirror-view";
|
|
5
5
|
import { Keyboard } from "test-keyboard";
|
|
6
|
+
import { Fragment } from "prosemirror-model";
|
|
6
7
|
function getMarks(marks, origContent) {
|
|
7
8
|
let content = `'${origContent}'`;
|
|
8
9
|
for (const mark of [...marks].reverse()) {
|
|
@@ -79,16 +80,25 @@ class ProseMirrorTester {
|
|
|
79
80
|
const keys = Keyboard.create({
|
|
80
81
|
target: this.view.dom
|
|
81
82
|
}).start();
|
|
82
|
-
let pos = this.view.state.selection.from;
|
|
83
83
|
for (const character of text) {
|
|
84
84
|
keys.char({ text: character, typing: true });
|
|
85
85
|
if (this.view.someProp(
|
|
86
86
|
"handleTextInput",
|
|
87
|
-
(f) => f(
|
|
88
|
-
|
|
89
|
-
|
|
87
|
+
(f) => f(
|
|
88
|
+
this.view,
|
|
89
|
+
this.view.state.selection.from,
|
|
90
|
+
this.view.state.selection.from,
|
|
91
|
+
character
|
|
92
|
+
)
|
|
93
|
+
) !== true) {
|
|
94
|
+
this.view.dispatch(
|
|
95
|
+
this.view.state.tr.insertText(
|
|
96
|
+
character,
|
|
97
|
+
this.view.state.selection.from,
|
|
98
|
+
this.view.state.selection.from
|
|
99
|
+
)
|
|
100
|
+
);
|
|
90
101
|
}
|
|
91
|
-
pos = this.view.state.selection.anchor;
|
|
92
102
|
}
|
|
93
103
|
keys.end();
|
|
94
104
|
}
|
|
@@ -129,6 +139,25 @@ class ProseMirrorTester {
|
|
|
129
139
|
return TextSelection.near(this.doc.resolve(pos));
|
|
130
140
|
}
|
|
131
141
|
}
|
|
142
|
+
function trimProseMirrorNode(node) {
|
|
143
|
+
let start = 0;
|
|
144
|
+
let end = node.children.length;
|
|
145
|
+
for (const child of node.children) {
|
|
146
|
+
if (child.type.name === "paragraph" && ["", "\n"].includes(child.textContent)) {
|
|
147
|
+
start++;
|
|
148
|
+
} else {
|
|
149
|
+
break;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
for (const child of node.children.slice().reverse()) {
|
|
153
|
+
if (child.type.name === "paragraph" && ["", "\n"].includes(child.textContent)) {
|
|
154
|
+
end--;
|
|
155
|
+
} else {
|
|
156
|
+
break;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
return node.copy(Fragment.from(node.children.slice(start, end)));
|
|
160
|
+
}
|
|
132
161
|
expect.extend({
|
|
133
162
|
toEqualProseMirrorNode(received, expected) {
|
|
134
163
|
const receivedDoc = `
|
|
@@ -165,6 +194,7 @@ ${diffString}` : ""}`;
|
|
|
165
194
|
}
|
|
166
195
|
});
|
|
167
196
|
export {
|
|
168
|
-
ProseMirrorTester
|
|
197
|
+
ProseMirrorTester,
|
|
198
|
+
trimProseMirrorNode
|
|
169
199
|
};
|
|
170
200
|
//# sourceMappingURL=vitest-prosemirror.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"vitest-prosemirror.js","sources":["../src/stringifyProseMirrorNode.ts","../src/ProseMirrorTester.ts","../src/index.ts"],"sourcesContent":["import type { Mark, Node } from \"prosemirror-model\";\n\nimport stringifyObject from \"stringify-object\";\n\nfunction getMarks(marks: ReadonlyArray<Mark>, origContent: string): string {\n let content = `'${origContent}'`;\n\n for (const mark of [...marks].reverse()) {\n const hasAttrs = Object.keys(mark.attrs).length > 0;\n const items: Array<string> = [content];\n\n if (hasAttrs) {\n items.unshift(\n stringifyObject(mark.attrs, {\n indent: \" \",\n inlineCharacterLimit: 1000,\n }),\n );\n }\n\n content = `${mark.type.name}(${items.join(\", \")})`;\n }\n\n return content;\n}\n\nconst renamedTypes: Record<string, string> = {\n hardBreak: \"br\",\n heading: \"h\",\n horizontalRule: \"hr\",\n paragraph: \"p\",\n};\n\nexport function stringifyProseMirrorNode(node: Node, indentation = \"\"): string {\n const nextIndentation = `${indentation} `;\n\n if (node.type.name === \"text\") {\n return `${indentation}${getMarks(node.marks, node.text ?? \"\")}`;\n }\n\n const type = renamedTypes[node.type.name] ?? node.type.name;\n const content: Array<string> = [];\n const hasAttrs = Object.keys(node.attrs).length > 0;\n\n if (hasAttrs) {\n content.push(\n stringifyObject(node.attrs, { indent: \" \", inlineCharacterLimit: 1000 }),\n );\n }\n\n node.content.forEach((item) => {\n content.push(stringifyProseMirrorNode(item, nextIndentation));\n });\n\n if (\n !hasAttrs &&\n content.length === 1 &&\n node.content.firstChild?.type.name === \"text\"\n ) {\n return `${indentation}${type}(${stringifyProseMirrorNode(node.content.firstChild, \"\")})`;\n }\n\n const joiner = `,\\n`;\n const prefix = hasAttrs\n ? `\\n${nextIndentation}`\n : content.length > 0\n ? \"\\n\"\n : \"\";\n const postfix = content.length > 0 ? `\\n${indentation}` : \"\";\n\n return `${indentation}${type}(${prefix}${content.join(joiner)}${postfix})`;\n}\n","import type { Node, Schema } from \"prosemirror-model\";\n\nimport {\n AllSelection,\n EditorState,\n type Plugin,\n type Selection,\n TextSelection,\n} from \"prosemirror-state\";\nimport { EditorView } from \"prosemirror-view\";\nimport { Keyboard } from \"test-keyboard\";\n\ninterface Options {\n plugins: Array<Plugin>;\n}\n\ntype TesterSelection =\n | \"all\"\n | \"end\"\n | \"start\"\n | { from: number; to: number }\n | Selection\n | number;\n\nexport class ProseMirrorTester {\n public get doc(): Node {\n return this.view.state.doc;\n }\n\n public get schema(): Schema {\n return this.view.state.schema;\n }\n\n private readonly view: EditorView;\n\n public constructor(documentRoot: Node, options: Partial<Options> = {}) {\n if (typeof document === \"undefined\") {\n throw new Error(\"TODO\");\n }\n\n const element = document.createElement(\"div\");\n document.body.append(element);\n\n const state = EditorState.create({\n doc: documentRoot,\n plugins: options.plugins ?? [],\n });\n this.view = new EditorView(element, {\n state,\n });\n }\n\n public insertText(text: string): void {\n const keys = Keyboard.create({\n target: this.view.dom,\n }).start();\n\n let pos = this.view.state.selection.from;\n for (const character of text) {\n keys.char({ text: character, typing: true });\n\n if (\n this.view.someProp(\"handleTextInput\", (f) =>\n f(this.view, pos, pos, character),\n ) === undefined\n ) {\n this.view.dispatch(this.view.state.tr.insertText(character, pos, pos));\n }\n pos = this.view.state.selection.anchor;\n }\n keys.end();\n }\n\n public selectText(selection: TesterSelection): void {\n this.view.dispatch(\n this.view.state.tr.setSelection(this.getSelection(selection)),\n );\n }\n\n public shortcut(text: string): void {\n Keyboard.create({\n batch: true,\n target: this.view.dom,\n })\n .start()\n .mod({ text })\n .forEach(({ event }) => {\n this.view.dispatchEvent(event);\n })\n .end();\n }\n\n private getSelection(selection: TesterSelection): Selection {\n if (selection === \"all\") {\n return new AllSelection(this.doc);\n }\n\n if (\n typeof selection === \"object\" &&\n \"$anchor\" in selection &&\n \"$head\" in selection\n ) {\n return selection;\n }\n\n if (\n typeof selection === \"object\" &&\n \"from\" in selection &&\n \"to\" in selection\n ) {\n return TextSelection.between(\n this.doc.resolve(selection.from),\n this.doc.resolve(selection.to),\n );\n }\n\n let pos = 0;\n if (selection === \"start\") {\n pos = 0;\n } else if (selection === \"end\") {\n pos = this.doc.nodeSize - 2;\n } else {\n pos = selection;\n }\n\n return TextSelection.near(this.doc.resolve(pos));\n }\n}\n","import type { Node } from \"prosemirror-model\";\n\nimport { expect } from \"vitest\";\n\nimport { stringifyProseMirrorNode } from \"./stringifyProseMirrorNode\";\n\nexport { ProseMirrorTester } from \"./ProseMirrorTester\";\n\ninterface CustomMatchers<R = unknown> {\n toEqualProseMirrorNode(expected: Node): R;\n}\n\n/* eslint-disable @typescript-eslint/no-empty-object-type, @typescript-eslint/no-explicit-any -- These are overridest for vitest matchers */\n\ndeclare module \"vitest\" {\n interface Assertion<T = any> extends CustomMatchers<T> {}\n interface AsymmetricMatchersContaining extends CustomMatchers {}\n}\n\n/* eslint-enable */\n\nexpect.extend({\n toEqualProseMirrorNode(received: Node, expected: Node) {\n const receivedDoc = `\\n${stringifyProseMirrorNode(received)}\\n`;\n const expectedDoc = `\\n${stringifyProseMirrorNode(expected)}\\n`;\n const pass = this.equals(receivedDoc, expectedDoc);\n const message = pass\n ? (): string =>\n `${this.utils.matcherHint(\".not.toEqualProsemirrorNode\")}\\n\\n` +\n `Expected value of document to not equal:\\n ${this.utils.printExpected(expectedDoc)}\\n` +\n `Actual:\\n ${this.utils.printReceived(receivedDoc)}`\n : (): string => {\n const diffString = this.utils.diff(expectedDoc, receivedDoc, {\n expand: this.expand ?? false,\n });\n return `${this.utils.matcherHint(\".toEqualProsemirrorNode\")}\\n\\nExpected value of document to equal:\\n${this.utils.printExpected(expectedDoc)}\\nActual:\\n${this.utils.printReceived(receiv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UAAgB;AACrD,UAAM,cAAc;AAAA,EAAK,yBAAyB,QAAQ,CAAC;AAAA;AAC3D,UAAM,cAAc;AAAA,EAAK,yBAAyB,QAAQ,CAAC;AAAA;AAC3D,UAAM,OAAO,KAAK,OAAO,aAAa,WAAW;AAC3C,UAAA,UAAU,OACZ,MACE,GAAG,KAAK,MAAM,YAAY,6BAA6B,CAAC;AAAA;AAAA;AAAA,IACT,KAAK,MAAM,cAAc,WAAW,CAAC;AAAA;AAAA,IACtE,KAAK,MAAM,cAAc,WAAW,CAAC,KACrD,MAAc;AACZ,YAAM,aAAa,KAAK,MAAM,KAAK,aAAa,aAAa;AAAA,QAC3D,QAAQ,KAAK,UAAU;AAAA,MAAA,CACxB;AACD,aAAO,GAAG,KAAK,MAAM,YAAY,yBAAyB,CAAC;AAAA;AAAA;AAAA,EAA6C,KAAK,MAAM,cAAc,WAAW,CAAC;AAAA;AAAA,EAAc,KAAK,MAAM,cAAc,WAAW,CAAC,GAAG,eAAe,SAAY;AAAA;AAAA;AAAA;AAAA,EAAsB,UAAU,KAAK,EAAE;AAAA,IACvQ;AACG,WAAA;AAAA,MACL;AAAA,MACA;AAAA,IACF;AAAA,EAAA;AAEJ,CAAC;"}
|
|
1
|
+
{"version":3,"file":"vitest-prosemirror.js","sources":["../src/stringifyProseMirrorNode.ts","../src/ProseMirrorTester.ts","../src/trimProseMirrorNode.ts","../src/index.ts"],"sourcesContent":["import type { Mark, Node } from \"prosemirror-model\";\n\nimport stringifyObject from \"stringify-object\";\n\nfunction getMarks(marks: ReadonlyArray<Mark>, origContent: string): string {\n let content = `'${origContent}'`;\n\n for (const mark of [...marks].reverse()) {\n const hasAttrs = Object.keys(mark.attrs).length > 0;\n const items: Array<string> = [content];\n\n if (hasAttrs) {\n items.unshift(\n stringifyObject(mark.attrs, {\n indent: \" \",\n inlineCharacterLimit: 1000,\n }),\n );\n }\n\n content = `${mark.type.name}(${items.join(\", \")})`;\n }\n\n return content;\n}\n\nconst renamedTypes: Record<string, string> = {\n hardBreak: \"br\",\n heading: \"h\",\n horizontalRule: \"hr\",\n paragraph: \"p\",\n};\n\nexport function stringifyProseMirrorNode(node: Node, indentation = \"\"): string {\n const nextIndentation = `${indentation} `;\n\n if (node.type.name === \"text\") {\n return `${indentation}${getMarks(node.marks, node.text ?? \"\")}`;\n }\n\n const type = renamedTypes[node.type.name] ?? node.type.name;\n const content: Array<string> = [];\n const hasAttrs = Object.keys(node.attrs).length > 0;\n\n if (hasAttrs) {\n content.push(\n stringifyObject(node.attrs, { indent: \" \", inlineCharacterLimit: 1000 }),\n );\n }\n\n node.content.forEach((item) => {\n content.push(stringifyProseMirrorNode(item, nextIndentation));\n });\n\n if (\n !hasAttrs &&\n content.length === 1 &&\n node.content.firstChild?.type.name === \"text\"\n ) {\n return `${indentation}${type}(${stringifyProseMirrorNode(node.content.firstChild, \"\")})`;\n }\n\n const joiner = `,\\n`;\n const prefix = hasAttrs\n ? `\\n${nextIndentation}`\n : content.length > 0\n ? \"\\n\"\n : \"\";\n const postfix = content.length > 0 ? `\\n${indentation}` : \"\";\n\n return `${indentation}${type}(${prefix}${content.join(joiner)}${postfix})`;\n}\n","import type { Node, Schema } from \"prosemirror-model\";\n\nimport {\n AllSelection,\n EditorState,\n type Plugin,\n type Selection,\n TextSelection,\n} from \"prosemirror-state\";\nimport { EditorView } from \"prosemirror-view\";\nimport { Keyboard } from \"test-keyboard\";\n\nexport interface Options {\n plugins: Array<Plugin>;\n}\n\nexport type TesterSelection =\n | \"all\"\n | \"end\"\n | \"start\"\n | { from: number; to: number }\n | Selection\n | number;\n\nexport class ProseMirrorTester {\n public get doc(): Node {\n return this.view.state.doc;\n }\n\n public get schema(): Schema {\n return this.view.state.schema;\n }\n\n private readonly view: EditorView;\n\n public constructor(documentRoot: Node, options: Partial<Options> = {}) {\n if (typeof document === \"undefined\") {\n throw new Error(\"TODO\");\n }\n\n const element = document.createElement(\"div\");\n document.body.append(element);\n\n const state = EditorState.create({\n doc: documentRoot,\n plugins: options.plugins ?? [],\n });\n this.view = new EditorView(element, {\n state,\n });\n }\n\n public insertText(text: string): void {\n const keys = Keyboard.create({\n target: this.view.dom,\n }).start();\n\n for (const character of text) {\n keys.char({ text: character, typing: true });\n\n if (\n this.view.someProp(\"handleTextInput\", (f) =>\n f(\n this.view,\n this.view.state.selection.from,\n this.view.state.selection.from,\n character,\n ),\n ) !== true\n ) {\n this.view.dispatch(\n this.view.state.tr.insertText(\n character,\n this.view.state.selection.from,\n this.view.state.selection.from,\n ),\n );\n }\n }\n keys.end();\n }\n\n public selectText(selection: TesterSelection): void {\n this.view.dispatch(\n this.view.state.tr.setSelection(this.getSelection(selection)),\n );\n }\n\n public shortcut(text: string): void {\n Keyboard.create({\n batch: true,\n target: this.view.dom,\n })\n .start()\n .mod({ text })\n .forEach(({ event }) => {\n this.view.dispatchEvent(event);\n })\n .end();\n }\n\n private getSelection(selection: TesterSelection): Selection {\n if (selection === \"all\") {\n return new AllSelection(this.doc);\n }\n\n if (\n typeof selection === \"object\" &&\n \"$anchor\" in selection &&\n \"$head\" in selection\n ) {\n return selection;\n }\n\n if (\n typeof selection === \"object\" &&\n \"from\" in selection &&\n \"to\" in selection\n ) {\n return TextSelection.between(\n this.doc.resolve(selection.from),\n this.doc.resolve(selection.to),\n );\n }\n\n let pos = 0;\n if (selection === \"start\") {\n pos = 0;\n } else if (selection === \"end\") {\n pos = this.doc.nodeSize - 2;\n } else {\n pos = selection;\n }\n\n return TextSelection.near(this.doc.resolve(pos));\n }\n}\n","import { Fragment, type Node } from \"prosemirror-model\";\n\n/**\n * @public\n */\nexport function trimProseMirrorNode(node: Node): Node {\n let start = 0;\n let end = node.children.length;\n for (const child of node.children) {\n if (\n child.type.name === \"paragraph\" &&\n [\"\", \"\\n\"].includes(child.textContent)\n ) {\n start++;\n } else {\n break;\n }\n }\n for (const child of node.children.slice().reverse()) {\n if (\n child.type.name === \"paragraph\" &&\n [\"\", \"\\n\"].includes(child.textContent)\n ) {\n end--;\n } else {\n break;\n }\n }\n return node.copy(Fragment.from(node.children.slice(start, end)));\n}\n","import type { Node } from \"prosemirror-model\";\n\nimport { expect } from \"vitest\";\n\nimport { stringifyProseMirrorNode } from \"./stringifyProseMirrorNode\";\n\nexport {\n type Options,\n ProseMirrorTester,\n type TesterSelection,\n} from \"./ProseMirrorTester\";\nexport { trimProseMirrorNode } from \"./trimProseMirrorNode\";\n\nexport interface CustomMatchers<R = unknown> {\n toEqualProseMirrorNode(expected: Node): R;\n}\n\n/* eslint-disable @typescript-eslint/no-empty-object-type, @typescript-eslint/no-explicit-any -- These are overridest for vitest matchers */\n\ndeclare module \"vitest\" {\n interface Assertion<T = any> extends CustomMatchers<T> {}\n interface AsymmetricMatchersContaining extends CustomMatchers {}\n}\n\n/* eslint-enable */\n\nexpect.extend({\n toEqualProseMirrorNode(received: Node, expected: Node) {\n const receivedDoc = `\\n${stringifyProseMirrorNode(received)}\\n`;\n const expectedDoc = `\\n${stringifyProseMirrorNode(expected)}\\n`;\n const pass = this.equals(receivedDoc, expectedDoc);\n const message = pass\n ? (): string =>\n `${this.utils.matcherHint(\".not.toEqualProsemirrorNode\")}\\n\\n` +\n `Expected value of document to not equal:\\n ${this.utils.printExpected(expectedDoc)}\\n` +\n `Actual:\\n ${this.utils.printReceived(receivedDoc)}`\n : (): string => {\n const diffString = this.utils.diff(expectedDoc, receivedDoc, {\n expand: this.expand ?? false,\n });\n return `${this.utils.matcherHint(\".toEqualProsemirrorNode\")}\\n\\nExpected value of document to equal:\\n${this.utils.printExpected(expectedDoc)}\\nActual:\\n${this.utils.printReceived(receivedDoc)}${diffString !== undefined ? `\\n\\nDifference:\\n\\n${diffString}` : \"\"}`;\n };\n return {\n message,\n pass,\n };\n },\n});\n"],"names":[],"mappings":";;;;;;AAIA,SAAS,SAAS,OAA4B,aAA6B;AACrE,MAAA,UAAU,IAAI,WAAW;AAE7B,aAAW,QAAQ,CAAC,GAAG,KAAK,EAAE,WAAW;AACvC,UAAM,WAAW,OAAO,KAAK,KA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A plugin for Vitest that enables you to write tests using the ProseMirror editor",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"vitest",
|
|
@@ -35,6 +35,7 @@
|
|
|
35
35
|
"clean": "rimraf dist/*",
|
|
36
36
|
"prebuild": "npm run clean",
|
|
37
37
|
"build": "vite build",
|
|
38
|
+
"start": "vite build --watch",
|
|
38
39
|
"lint": "eslint --color 'src/**/*.ts' 'tests/**/*.ts' '*.config.{js,ts}'",
|
|
39
40
|
"test": "vitest",
|
|
40
41
|
"test-coverage": "vitest run --coverage"
|