diff --git a/__tests__/utils/MarkExtensionTester.ts b/__tests__/utils/MarkExtensionTester.ts index 0fd1c5b7..731c0a0d 100644 --- a/__tests__/utils/MarkExtensionTester.ts +++ b/__tests__/utils/MarkExtensionTester.ts @@ -51,74 +51,6 @@ export class MarkExtensionTester< this.inputRuleMatches = []; } - protected override enqueueTests(): void { - super.enqueueTests(); - - test("Provides the correct ProseMirror mark", () => { - expect(this.extension.proseMirrorMarkName()).toBe( - this.proseMirrorMarkName, - ); - }); - - this.enqueueProseMirrorNodeMatchTests(); - this.enqueueInputRuleTests(); - } - - private enqueueInputRuleTests(): void { - if (this.inputRuleMatches.length === 0) { - return; - } - - describe("Matches input rules correctly", () => { - test.each(this.inputRuleMatches)( - "%p", - ({ editorInput, markdownOutput, proseMirrorNodes }) => { - expect.assertions(3); - - const source = "BEGIN"; - const proseMirrorRoot = this.pmu.parse(source); - const proseMirrorTree = this.pmu - .schema() - .nodes[ - "doc" - ].create({}, [this.pmu.schema().nodes["paragraph"].create({}, [this.pmu.schema().text("BEGIN"), ...proseMirrorNodes, this.pmu.schema().text("END")])]); - - jest.spyOn(console, "warn").mockImplementation(); - createEditor(proseMirrorRoot, { - plugins: [this.pmu.inputRulesPlugin()], - }) - .selectText("end") - .insertText(editorInput) - .insertText("END") - .callback((content) => { - expect(content.doc).toEqualProsemirrorNode(proseMirrorTree); - expect(this.pmu.serialize(content.doc)).toBe( - `BEGIN${markdownOutput}END\n`, - ); - }); - - // eslint-disable-next-line no-console -- Testing for console - expect(console.warn).not.toHaveBeenCalled(); - }, - ); - }); - } - - private enqueueProseMirrorNodeMatchTests(): void { - if (this.proseMirrorMarkMatches.length === 0) { - return; - } - - describe("Matches correct ProseMirror nodes", () => { - test.each(this.proseMirrorMarkMatches)("%p", ({ mark, shouldMatch }) => { - expect.assertions(1); - expect(mark.type.name === this.extension.proseMirrorMarkName()).toBe( - shouldMatch, - ); - }); - }); - } - public shouldMatchInputRule( editorInput: string, markdownOutput: string, @@ -189,4 +121,72 @@ export class MarkExtensionTester< this.enqueueTests(); }); } + + protected override enqueueTests(): void { + super.enqueueTests(); + + test("Provides the correct ProseMirror mark", () => { + expect(this.extension.proseMirrorMarkName()).toBe( + this.proseMirrorMarkName, + ); + }); + + this.enqueueProseMirrorNodeMatchTests(); + this.enqueueInputRuleTests(); + } + + private enqueueInputRuleTests(): void { + if (this.inputRuleMatches.length === 0) { + return; + } + + describe("Matches input rules correctly", () => { + test.each(this.inputRuleMatches)( + "%p", + ({ editorInput, markdownOutput, proseMirrorNodes }) => { + expect.assertions(3); + + const source = "BEGIN"; + const proseMirrorRoot = this.pmu.parse(source); + const proseMirrorTree = this.pmu + .schema() + .nodes[ + "doc" + ].create({}, [this.pmu.schema().nodes["paragraph"].create({}, [this.pmu.schema().text("BEGIN"), ...proseMirrorNodes, this.pmu.schema().text("END")])]); + + jest.spyOn(console, "warn").mockImplementation(); + createEditor(proseMirrorRoot, { + plugins: [this.pmu.inputRulesPlugin()], + }) + .selectText("end") + .insertText(editorInput) + .insertText("END") + .callback((content) => { + expect(content.doc).toEqualProsemirrorNode(proseMirrorTree); + expect(this.pmu.serialize(content.doc)).toBe( + `BEGIN${markdownOutput}END\n`, + ); + }); + + // eslint-disable-next-line no-console -- Testing for console + expect(console.warn).not.toHaveBeenCalled(); + }, + ); + }); + } + + private enqueueProseMirrorNodeMatchTests(): void { + if (this.proseMirrorMarkMatches.length === 0) { + return; + } + + describe("Matches correct ProseMirror nodes", () => { + test.each(this.proseMirrorMarkMatches)("%p", ({ mark, shouldMatch }) => { + expect.assertions(1); + expect(mark.type.name === this.extension.proseMirrorMarkName()).toBe( + shouldMatch, + ); + }); + }); + } } diff --git a/__tests__/utils/NodeExtensionTester.ts b/__tests__/utils/NodeExtensionTester.ts index e4f8a222..381ac514 100644 --- a/__tests__/utils/NodeExtensionTester.ts +++ b/__tests__/utils/NodeExtensionTester.ts @@ -51,6 +51,67 @@ export class NodeExtensionTester< this.inputRuleMatches = []; } + public shouldMatchInputRule( + editorInput: string, + proseMirrorNodes: ( + schema: Schema, + ) => Array, + markdownOutput: string, + ): this { + this.inputRuleMatches.push({ + editorInput, + markdownOutput, + proseMirrorNodes: proseMirrorNodes(this.pmu.schema()), + }); + return this; + } + + public shouldMatchProseMirrorNode( + node: (schema: Schema) => ProseMirrorNode, + ): this { + this.proseMirrorNodeMatches.push({ + node: node(this.pmu.schema()), + shouldMatch: true, + }); + return this; + } + + public shouldNotMatchInputRule( + editorInput: string, + markdownOutput: string, + proseMirrorNodes?: ( + schema: Schema, + ) => Array, + ): this { + this.inputRuleMatches.push({ + editorInput, + markdownOutput, + proseMirrorNodes: proseMirrorNodes?.(this.pmu.schema()) ?? [ + this.pmu + .schema() + .nodes["paragraph"].create({}, [this.pmu.schema().text(editorInput)]), + ], + }); + return this; + } + + public shouldNotMatchProseMirrorNode( + node: (schema: Schema) => ProseMirrorNode, + ): this { + this.proseMirrorNodeMatches.push({ + node: node(this.pmu.schema()), + shouldMatch: false, + }); + return this; + } + + public test(): void { + // eslint-disable-next-line jest/valid-title -- The rule can't parse that this is a string + describe(this.extension.constructor.name, () => { + this.enqueueTests(); + }); + } + protected override enqueueTests(): void { super.enqueueTests(); @@ -113,65 +174,4 @@ export class NodeExtensionTester< }); }); } - - public shouldMatchInputRule( - editorInput: string, - proseMirrorNodes: ( - schema: Schema, - ) => Array, - markdownOutput: string, - ): this { - this.inputRuleMatches.push({ - editorInput, - markdownOutput, - proseMirrorNodes: proseMirrorNodes(this.pmu.schema()), - }); - return this; - } - - public shouldMatchProseMirrorNode( - node: (schema: Schema) => ProseMirrorNode, - ): this { - this.proseMirrorNodeMatches.push({ - node: node(this.pmu.schema()), - shouldMatch: true, - }); - return this; - } - - public shouldNotMatchInputRule( - editorInput: string, - markdownOutput: string, - proseMirrorNodes?: ( - schema: Schema, - ) => Array, - ): this { - this.inputRuleMatches.push({ - editorInput, - markdownOutput, - proseMirrorNodes: proseMirrorNodes?.(this.pmu.schema()) ?? [ - this.pmu - .schema() - .nodes["paragraph"].create({}, [this.pmu.schema().text(editorInput)]), - ], - }); - return this; - } - - public shouldNotMatchProseMirrorNode( - node: (schema: Schema) => ProseMirrorNode, - ): this { - this.proseMirrorNodeMatches.push({ - node: node(this.pmu.schema()), - shouldMatch: false, - }); - return this; - } - - public test(): void { - // eslint-disable-next-line jest/valid-title -- The rule can't parse that this is a string - describe(this.extension.constructor.name, () => { - this.enqueueTests(); - }); - } } diff --git a/__tests__/utils/SyntaxExtensionTester.ts b/__tests__/utils/SyntaxExtensionTester.ts index ffd8ee2c..42a6b6fd 100644 --- a/__tests__/utils/SyntaxExtensionTester.ts +++ b/__tests__/utils/SyntaxExtensionTester.ts @@ -83,6 +83,61 @@ export class SyntaxExtensionTester< ]); } + public shouldConvertProseMirrorNode( + source: (schema: Schema) => ProseMirrorNode, + target: Array, + ): this { + this.proseMirrorNodeConversions.push({ + source: source(this.pmu.schema()), + target, + }); + return this; + } + + public shouldConvertUnistNode( + source: UnistNode | UNode, + target: (schema: Schema) => Array, + injectNodes: Array = [], + ): this { + this.unistNodeConversions.push({ + injectNodes, + source, + target: target(this.pmu.schema()), + }); + return this; + } + + public shouldMatchUnistNode(node: UNode): this { + this.unistNodeMatches.push({ node, shouldMatch: true }); + return this; + } + + public shouldNotMatchUnistNode(node: UnistNode): this { + this.unistNodeMatches.push({ node, shouldMatch: false }); + return this; + } + + public shouldSupportKeymap( + proseMirrorBefore: ( + schema: Schema, + ) => Array, + selection: PrimitiveSelection, + key: string, + proseMirrorAfter: ( + schema: Schema, + ) => Array, + markdownOutput: string, + ): this { + this.keymapMatches.push({ + key, + markdownOutput, + proseMirrorAfter: proseMirrorAfter(this.pmu.schema()), + proseMirrorBefore: proseMirrorBefore(this.pmu.schema()), + selection, + }); + return this; + } + protected enqueueTests(): void { test("Handles the correct unist node", () => { expect(this.extension.unistNodeName()).toBe(this.unistNodeName); @@ -206,59 +261,4 @@ export class SyntaxExtensionTester< }); }); } - - public shouldConvertProseMirrorNode( - source: (schema: Schema) => ProseMirrorNode, - target: Array, - ): this { - this.proseMirrorNodeConversions.push({ - source: source(this.pmu.schema()), - target, - }); - return this; - } - - public shouldConvertUnistNode( - source: UnistNode | UNode, - target: (schema: Schema) => Array, - injectNodes: Array = [], - ): this { - this.unistNodeConversions.push({ - injectNodes, - source, - target: target(this.pmu.schema()), - }); - return this; - } - - public shouldMatchUnistNode(node: UNode): this { - this.unistNodeMatches.push({ node, shouldMatch: true }); - return this; - } - - public shouldNotMatchUnistNode(node: UnistNode): this { - this.unistNodeMatches.push({ node, shouldMatch: false }); - return this; - } - - public shouldSupportKeymap( - proseMirrorBefore: ( - schema: Schema, - ) => Array, - selection: PrimitiveSelection, - key: string, - proseMirrorAfter: ( - schema: Schema, - ) => Array, - markdownOutput: string, - ): this { - this.keymapMatches.push({ - key, - markdownOutput, - proseMirrorAfter: proseMirrorAfter(this.pmu.schema()), - proseMirrorBefore: proseMirrorBefore(this.pmu.schema()), - selection, - }); - return this; - } } diff --git a/eslint.config.js b/eslint.config.js index 9343a318..75b1cb46 100644 --- a/eslint.config.js +++ b/eslint.config.js @@ -1,6 +1,6 @@ -import js from "@eslint/js"; import eslintComments from "@eslint-community/eslint-plugin-eslint-comments"; import commentsConfig from "@eslint-community/eslint-plugin-eslint-comments/configs"; +import js from "@eslint/js"; import jest from "eslint-plugin-jest"; import perfectionist from "eslint-plugin-perfectionist"; import preferArrowFunctions from "eslint-plugin-prefer-arrow-functions";