Skip to content

Commit

Permalink
Applied new perfectionist rules
Browse files Browse the repository at this point in the history
  • Loading branch information
marekdedic committed Nov 25, 2024
1 parent f956760 commit 5e169a1
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 185 deletions.
136 changes: 68 additions & 68 deletions __tests__/utils/MarkExtensionTester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
);
});
});
}
}
122 changes: 61 additions & 61 deletions __tests__/utils/NodeExtensionTester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,67 @@ export class NodeExtensionTester<
this.inputRuleMatches = [];
}

public shouldMatchInputRule(
editorInput: string,
proseMirrorNodes: (
schema: Schema<string, string>,
) => Array<ProseMirrorNode>,
markdownOutput: string,
): this {
this.inputRuleMatches.push({
editorInput,
markdownOutput,
proseMirrorNodes: proseMirrorNodes(this.pmu.schema()),
});
return this;
}

public shouldMatchProseMirrorNode(
node: (schema: Schema<string, string>) => ProseMirrorNode,
): this {
this.proseMirrorNodeMatches.push({
node: node(this.pmu.schema()),
shouldMatch: true,
});
return this;
}

public shouldNotMatchInputRule(
editorInput: string,
markdownOutput: string,
proseMirrorNodes?: (
schema: Schema<string, string>,
) => Array<ProseMirrorNode>,
): 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<string, string>) => 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();

Expand Down Expand Up @@ -113,65 +174,4 @@ export class NodeExtensionTester<
});
});
}

public shouldMatchInputRule(
editorInput: string,
proseMirrorNodes: (
schema: Schema<string, string>,
) => Array<ProseMirrorNode>,
markdownOutput: string,
): this {
this.inputRuleMatches.push({
editorInput,
markdownOutput,
proseMirrorNodes: proseMirrorNodes(this.pmu.schema()),
});
return this;
}

public shouldMatchProseMirrorNode(
node: (schema: Schema<string, string>) => ProseMirrorNode,
): this {
this.proseMirrorNodeMatches.push({
node: node(this.pmu.schema()),
shouldMatch: true,
});
return this;
}

public shouldNotMatchInputRule(
editorInput: string,
markdownOutput: string,
proseMirrorNodes?: (
schema: Schema<string, string>,
) => Array<ProseMirrorNode>,
): 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<string, string>) => 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();
});
}
}
Loading

0 comments on commit 5e169a1

Please sign in to comment.