Skip to content

Build your personal knowledge base with Trilium Notes

test(ckeditor5): add shared editor test-kit and migrate specs to it

Elian Doran320fd9064171
diff --git a/packages/ckeditor5/src/plugins/admonition_toolbar.spec.ts b/packages/ckeditor5/src/plugins/admonition_toolbar.spec.ts
index 0a15be0ee9..394a662201 100644
--- a/packages/ckeditor5/src/plugins/admonition_toolbar.spec.ts
+++ b/packages/ckeditor5/src/plugins/admonition_toolbar.spec.ts
@@ -1,7 +1,8 @@
import { ClassicEditor, Essentials, Paragraph, _setModelData as setModelData, WidgetToolbarRepository } from "ckeditor5";
import { Admonition } from "@triliumnext/ckeditor5-admonition";
-import { afterEach, beforeEach, describe, expect, it } from "vitest";
+import { beforeEach, describe, expect, it } from "vitest";
+import { createTestEditor } from "../../test/editor-kit.js";
import AdmonitionToolbar from "./admonition_toolbar.js";
import AdmonitionTypeDropdown from "./admonition_type_dropdown.js";
@@ -25,22 +26,10 @@ function selectInsideFirstAdmonition(editor: ClassicEditor): void {
}
describe("AdmonitionToolbar", () => {
- let editorElement: HTMLDivElement;
let editor: ClassicEditor;
beforeEach(async () => {
- editorElement = document.createElement("div");
- document.body.appendChild(editorElement);
-
- editor = await ClassicEditor.create(editorElement, {
- licenseKey: "GPL",
- plugins: [Essentials, Paragraph, Admonition, AdmonitionTypeDropdown, AdmonitionToolbar]
- });
- });
-
- afterEach(async () => {
- editorElement.remove();
- await editor.destroy();
+ editor = await createTestEditor([Essentials, Paragraph, Admonition, AdmonitionTypeDropdown, AdmonitionToolbar]);
});
it("loads the plugin", () => {
diff --git a/packages/ckeditor5/src/plugins/admonition_type_dropdown.spec.ts b/packages/ckeditor5/src/plugins/admonition_type_dropdown.spec.ts
index 34a5177a5e..227e3d1cda 100644
--- a/packages/ckeditor5/src/plugins/admonition_type_dropdown.spec.ts
+++ b/packages/ckeditor5/src/plugins/admonition_type_dropdown.spec.ts
@@ -1,7 +1,8 @@
import { ClassicEditor, Essentials, Paragraph, _setModelData as setModelData } from "ckeditor5";
import { Admonition, ADMONITION_TYPES } from "@triliumnext/ckeditor5-admonition";
-import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+import { beforeEach, describe, expect, it, vi } from "vitest";
+import { createTestEditor } from "../../test/editor-kit.js";
import AdmonitionTypeDropdown from "./admonition_type_dropdown.js";
// ---- Typed interfaces for the dropdown internals ----
@@ -69,25 +70,14 @@ function selectInsideFirstAdmonition(editor: ClassicEditor): void {
}
describe("AdmonitionTypeDropdown", () => {
- let editorElement: HTMLDivElement;
let editor: ClassicEditor;
beforeEach(async () => {
- editorElement = document.createElement("div");
- document.body.appendChild(editorElement);
-
- editor = await ClassicEditor.create(editorElement, {
- licenseKey: "GPL",
- plugins: [Essentials, Paragraph, Admonition, AdmonitionTypeDropdown],
+ editor = await createTestEditor([Essentials, Paragraph, Admonition, AdmonitionTypeDropdown], {
toolbar: { items: ["admonitionTypeDropdown"] }
});
});
- afterEach(async () => {
- editorElement.remove();
- await editor.destroy();
- });
-
it("loads the plugin", () => {
expect(editor.plugins.get(AdmonitionTypeDropdown)).toBeInstanceOf(AdmonitionTypeDropdown);
});
diff --git a/packages/ckeditor5/src/plugins/code_block_language_dropdown.spec.ts b/packages/ckeditor5/src/plugins/code_block_language_dropdown.spec.ts
index de4cd3b377..777a63a255 100644
--- a/packages/ckeditor5/src/plugins/code_block_language_dropdown.spec.ts
+++ b/packages/ckeditor5/src/plugins/code_block_language_dropdown.spec.ts
@@ -1,6 +1,7 @@
import { ClassicEditor, CodeBlock, Essentials, Paragraph, _setModelData as setModelData } from "ckeditor5";
-import { afterEach, beforeEach, describe, expect, it } from "vitest";
+import { beforeEach, describe, expect, it } from "vitest";
+import { createTestEditor } from "../../test/editor-kit.js";
import CodeBlockLanguageDropdown from "./code_block_language_dropdown.js";
const LANGUAGES = [
@@ -54,16 +55,10 @@ function openDropdown(dropdown: DropdownView): ListView {
}
describe("CodeBlockLanguageDropdown", () => {
- let editorElement: HTMLDivElement;
let editor: ClassicEditor;
beforeEach(async () => {
- editorElement = document.createElement("div");
- document.body.appendChild(editorElement);
-
- editor = await ClassicEditor.create(editorElement, {
- licenseKey: "GPL",
- plugins: [Essentials, Paragraph, CodeBlock, CodeBlockLanguageDropdown],
+ editor = await createTestEditor([Essentials, Paragraph, CodeBlock, CodeBlockLanguageDropdown], {
codeBlock: {
languages: LANGUAGES.map(l => ({ ...l }))
},
@@ -71,11 +66,6 @@ describe("CodeBlockLanguageDropdown", () => {
});
});
- afterEach(async () => {
- editorElement.remove();
- await editor.destroy();
- });
-
it("registers the plugin", () => {
expect(editor.plugins.get(CodeBlockLanguageDropdown)).toBeInstanceOf(CodeBlockLanguageDropdown);
});
diff --git a/packages/ckeditor5/src/plugins/code_block_toolbar.spec.ts b/packages/ckeditor5/src/plugins/code_block_toolbar.spec.ts
index 57ed49a1b4..e945faccac 100644
--- a/packages/ckeditor5/src/plugins/code_block_toolbar.spec.ts
+++ b/packages/ckeditor5/src/plugins/code_block_toolbar.spec.ts
@@ -1,27 +1,16 @@
import { BalloonToolbar, ClassicEditor, CodeBlock, Essentials, Paragraph, WidgetToolbarRepository, _setModelData as setModelData } from "ckeditor5";
-import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+import { beforeEach, describe, expect, it, vi } from "vitest";
+import { createTestEditor } from "../../test/editor-kit.js";
import CodeBlockToolbar from "./code_block_toolbar.js";
import CodeBlockLanguageDropdown from "./code_block_language_dropdown.js";
import CopyToClipboardButton from "./copy_to_clipboard_button.js";
describe("CodeBlockToolbar", () => {
- let editorElement: HTMLDivElement;
let editor: ClassicEditor;
beforeEach(async () => {
- editorElement = document.createElement("div");
- document.body.appendChild(editorElement);
-
- editor = await ClassicEditor.create(editorElement, {
- licenseKey: "GPL",
- plugins: [Essentials, Paragraph, CodeBlock, CodeBlockToolbar]
- });
- });
-
- afterEach(async () => {
- editorElement.remove();
- await editor.destroy();
+ editor = await createTestEditor([Essentials, Paragraph, CodeBlock, CodeBlockToolbar]);
});
it("loads the plugin", () => {
@@ -103,25 +92,14 @@ describe("CodeBlockToolbar", () => {
});
describe("CodeBlockToolbar — with BalloonToolbar", () => {
- let editorElement: HTMLDivElement;
let editor: ClassicEditor;
beforeEach(async () => {
- editorElement = document.createElement("div");
- document.body.appendChild(editorElement);
-
- editor = await ClassicEditor.create(editorElement, {
- licenseKey: "GPL",
- plugins: [Essentials, Paragraph, CodeBlock, BalloonToolbar, CodeBlockToolbar],
+ editor = await createTestEditor([Essentials, Paragraph, CodeBlock, BalloonToolbar, CodeBlockToolbar], {
balloonToolbar: { items: [] }
});
});
- afterEach(async () => {
- editorElement.remove();
- await editor.destroy();
- });
-
it("registers a high-priority listener on BalloonToolbar show when BalloonToolbar is present", () => {
expect(editor.plugins.has("BalloonToolbar")).toBe(true);
});
diff --git a/packages/ckeditor5/src/plugins/collapsible_list_items.spec.ts b/packages/ckeditor5/src/plugins/collapsible_list_items.spec.ts
index 4397c68d97..7d5c704fd8 100644
--- a/packages/ckeditor5/src/plugins/collapsible_list_items.spec.ts
+++ b/packages/ckeditor5/src/plugins/collapsible_list_items.spec.ts
@@ -1,6 +1,7 @@
import { _setModelData as setModelData, ClassicEditor, keyCodes, List, Paragraph, TodoList, Typing, Undo, type ModelElement } from "ckeditor5";
-import { afterEach, beforeEach, describe, expect, it } from "vitest";
+import { beforeEach, describe, expect, it } from "vitest";
+import { createTestEditor, getEditorElement } from "../../test/editor-kit.js";
import CollapsibleListItems, { LIST_COLLAPSED_ATTRIBUTE, ToggleListCollapseCommand } from "./collapsible_list_items.js";
// Lists are flat in the model: sibling blocks related by listIndent/listItemId.
@@ -12,26 +13,14 @@ const LIST_FIXTURE =
"<paragraph>Outside of the list</paragraph>";
describe("CollapsibleListItems", () => {
- let editorElement: HTMLDivElement;
let editor: ClassicEditor;
beforeEach(async () => {
- editorElement = document.createElement("div");
- document.body.appendChild(editorElement);
-
- editor = await ClassicEditor.create(editorElement, {
- plugins: [CollapsibleListItems, List, TodoList, Paragraph, Typing, Undo],
- licenseKey: "GPL"
- });
+ editor = await createTestEditor([CollapsibleListItems, List, TodoList, Paragraph, Typing, Undo]);
setModelData(editor.model, LIST_FIXTURE);
});
- afterEach(async () => {
- editorElement.remove();
- await editor.destroy();
- });
-
it("collapses and expands via the command and persists the state into the data", () => {
const dataBefore = editor.getData();
const command = editor.commands.get("toggleListCollapse");
@@ -194,6 +183,7 @@ describe("CollapsibleListItems", () => {
// Shift the editor away from the viewport's left edge so the negative-inset gutter
// (where the arrow lives) is actually on-screen and hit-testable.
+ const editorElement = getEditorElement(editor);
editorElement.style.marginLeft = "160px";
editorElement.style.marginTop = "60px";
diff --git a/packages/ckeditor5/src/plugins/copy_anchor_link.spec.ts b/packages/ckeditor5/src/plugins/copy_anchor_link.spec.ts
index 52469b9555..320137f9e6 100644
--- a/packages/ckeditor5/src/plugins/copy_anchor_link.spec.ts
+++ b/packages/ckeditor5/src/plugins/copy_anchor_link.spec.ts
@@ -1,10 +1,10 @@
import { _setModelData as setModelData, Bold, Bookmark, ClassicEditor, Essentials, Paragraph } from "ckeditor5";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+import { createTestEditor } from "../../test/editor-kit.js";
import CopyAnchorLinkButton from "./copy_anchor_link.js";
describe("CopyAnchorLinkButton", () => {
- let editorElement: HTMLDivElement;
let editor: ClassicEditor;
let getActiveContextNote: ReturnType<typeof vi.fn>;
let getReferenceLinkTitleSync: ReturnType<typeof vi.fn>;
@@ -26,19 +26,11 @@ describe("CopyAnchorLinkButton", () => {
value: { write: clipboardWrite }
});
- editorElement = document.createElement("div");
- document.body.appendChild(editorElement);
-
- editor = await ClassicEditor.create(editorElement, {
- licenseKey: "GPL",
- plugins: [Essentials, Paragraph, Bold, Bookmark, CopyAnchorLinkButton]
- });
+ editor = await createTestEditor([Essentials, Paragraph, Bold, Bookmark, CopyAnchorLinkButton]);
});
- afterEach(async () => {
+ afterEach(() => {
delete (globalThis as { glob?: unknown }).glob;
- editorElement.remove();
- await editor.destroy();
Object.defineProperty(navigator, "clipboard", {
configurable: true,
value: originalClipboard
diff --git a/packages/ckeditor5/src/plugins/copy_link_url.spec.ts b/packages/ckeditor5/src/plugins/copy_link_url.spec.ts
index 777f78d11e..bf730f4415 100644
--- a/packages/ckeditor5/src/plugins/copy_link_url.spec.ts
+++ b/packages/ckeditor5/src/plugins/copy_link_url.spec.ts
@@ -1,28 +1,16 @@
import { _setModelData as setModelData, ClassicEditor, Essentials, Link, Paragraph } from "ckeditor5";
-import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+import { describe, expect, it, vi } from "vitest";
+import { createTestEditor } from "../../test/editor-kit.js";
import CopyLinkUrlButton from "./copy_link_url.js";
describe("CopyLinkUrlButton", () => {
- let editorElement: HTMLDivElement;
let editor: ClassicEditor;
async function createEditor(extraConfig: Record<string, unknown> = {}): Promise<ClassicEditor> {
- editorElement = document.createElement("div");
- document.body.appendChild(editorElement);
-
- return ClassicEditor.create(editorElement, {
- licenseKey: "GPL",
- plugins: [Essentials, Paragraph, Link, CopyLinkUrlButton],
- ...extraConfig
- });
+ return createTestEditor([Essentials, Paragraph, Link, CopyLinkUrlButton], extraConfig);
}
- afterEach(async () => {
- editorElement.remove();
- await editor.destroy();
- });
-
it("registers itself as a plugin", async () => {
editor = await createEditor();
expect(editor.plugins.get(CopyLinkUrlButton)).toBeInstanceOf(CopyLinkUrlButton);
diff --git a/packages/ckeditor5/src/plugins/copy_to_clipboard_button.spec.ts b/packages/ckeditor5/src/plugins/copy_to_clipboard_button.spec.ts
index ce95152857..523e671493 100644
--- a/packages/ckeditor5/src/plugins/copy_to_clipboard_button.spec.ts
+++ b/packages/ckeditor5/src/plugins/copy_to_clipboard_button.spec.ts
@@ -1,25 +1,14 @@
import { _setModelData as setModelData, ClassicEditor, Code, CodeBlock, Essentials, Paragraph } from "ckeditor5";
-import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+import { beforeEach, describe, expect, it, vi } from "vitest";
+import { createTestEditor } from "../../test/editor-kit.js";
import CopyToClipboardButton, { CopyToClipboardCommand } from "./copy_to_clipboard_button.js";
describe("CopyToClipboardButton", () => {
- let editorElement: HTMLDivElement;
let editor: ClassicEditor;
beforeEach(async () => {
- editorElement = document.createElement("div");
- document.body.appendChild(editorElement);
-
- editor = await ClassicEditor.create(editorElement, {
- licenseKey: "GPL",
- plugins: [Essentials, Paragraph, CodeBlock, Code, CopyToClipboardButton]
- });
- });
-
- afterEach(async () => {
- editorElement.remove();
- await editor.destroy();
+ editor = await createTestEditor([Essentials, Paragraph, CodeBlock, Code, CopyToClipboardButton]);
});
it("loads the plugin and registers the command and toolbar button", () => {
@@ -42,22 +31,10 @@ describe("CopyToClipboardButton", () => {
});
describe("CopyToClipboardCommand - code block", () => {
- let editorElement: HTMLDivElement;
let editor: ClassicEditor;
beforeEach(async () => {
- editorElement = document.createElement("div");
- document.body.appendChild(editorElement);
-
- editor = await ClassicEditor.create(editorElement, {
- licenseKey: "GPL",
- plugins: [Essentials, Paragraph, CodeBlock, Code, CopyToClipboardButton]
- });
- });
-
- afterEach(async () => {
- editorElement.remove();
- await editor.destroy();
+ editor = await createTestEditor([Essentials, Paragraph, CodeBlock, Code, CopyToClipboardButton]);
});
it("copies code block text using navigator.clipboard.writeText when no config callback", async () => {
@@ -120,19 +97,11 @@ describe("CopyToClipboardCommand - code block", () => {
// Rebuild editor with clipboard config
return (async () => {
- editorElement.remove();
- await editor.destroy();
-
- editorElement = document.createElement("div");
- document.body.appendChild(editorElement);
-
- editor = await ClassicEditor.create(editorElement, {
- licenseKey: "GPL",
- plugins: [Essentials, Paragraph, CodeBlock, Code, CopyToClipboardButton],
+ editor = await createTestEditor([Essentials, Paragraph, CodeBlock, Code, CopyToClipboardButton], {
clipboard: {
copy: copyCallback
- } as unknown as Record<string, unknown>
- });
+ }
+ } as unknown as Parameters<typeof createTestEditor>[1]);
setModelData(editor.model, "<codeBlock language=\"plaintext\">callback text[]</codeBlock>");
editor.execute("copyToClipboard");
@@ -177,22 +146,10 @@ describe("CopyToClipboardCommand - code block", () => {
});
describe("CopyToClipboardCommand - inline code", () => {
- let editorElement: HTMLDivElement;
let editor: ClassicEditor;
beforeEach(async () => {
- editorElement = document.createElement("div");
- document.body.appendChild(editorElement);
-
- editor = await ClassicEditor.create(editorElement, {
- licenseKey: "GPL",
- plugins: [Essentials, Paragraph, CodeBlock, Code, CopyToClipboardButton]
- });
- });
-
- afterEach(async () => {
- editorElement.remove();
- await editor.destroy();
+ editor = await createTestEditor([Essentials, Paragraph, CodeBlock, Code, CopyToClipboardButton]);
});
it("copies inline code text when cursor is inside a code-attributed text node", async () => {
@@ -227,19 +184,11 @@ describe("CopyToClipboardCommand - inline code", () => {
const copyCallback = vi.fn();
return (async () => {
- editorElement.remove();
- await editor.destroy();
-
- editorElement = document.createElement("div");
- document.body.appendChild(editorElement);
-
- editor = await ClassicEditor.create(editorElement, {
- licenseKey: "GPL",
- plugins: [Essentials, Paragraph, CodeBlock, Code, CopyToClipboardButton],
+ editor = await createTestEditor([Essentials, Paragraph, CodeBlock, Code, CopyToClipboardButton], {
clipboard: {
copy: copyCallback
- } as unknown as Record<string, unknown>
- });
+ }
+ } as unknown as Parameters<typeof createTestEditor>[1]);
setModelData(editor.model, "<paragraph><$text code=\"true\">inline[]code</$text></paragraph>");
editor.execute("copyToClipboard");
@@ -287,28 +236,17 @@ describe("CopyToClipboardCommand - inline code", () => {
});
describe("CopyToClipboardCommand - executeCallback caching", () => {
- let editorElement: HTMLDivElement;
let editor: ClassicEditor;
- afterEach(async () => {
- editorElement.remove();
- await editor.destroy();
- });
-
it("re-uses the executeCallback set on first execute for subsequent calls", () => {
const copyCallback = vi.fn();
return (async () => {
- editorElement = document.createElement("div");
- document.body.appendChild(editorElement);
-
- editor = await ClassicEditor.create(editorElement, {
- licenseKey: "GPL",
- plugins: [Essentials, Paragraph, CodeBlock, Code, CopyToClipboardButton],
+ editor = await createTestEditor([Essentials, Paragraph, CodeBlock, Code, CopyToClipboardButton], {
clipboard: {
copy: copyCallback
- } as unknown as Record<string, unknown>
- });
+ }
+ } as unknown as Parameters<typeof createTestEditor>[1]);
setModelData(editor.model, "<codeBlock language=\"plaintext\">first[]</codeBlock>");
editor.execute("copyToClipboard");
diff --git a/packages/ckeditor5/src/plugins/cuttonote.spec.ts b/packages/ckeditor5/src/plugins/cuttonote.spec.ts
index 2f11e76cf8..59122da412 100644
--- a/packages/ckeditor5/src/plugins/cuttonote.spec.ts
+++ b/packages/ckeditor5/src/plugins/cuttonote.spec.ts
@@ -1,10 +1,10 @@
import { _setModelData as setModelData, ClassicEditor, Essentials, Paragraph } from "ckeditor5";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+import { createTestEditor } from "../../test/editor-kit.js";
import CutToNotePlugin from "./cuttonote.js";
describe("CutToNotePlugin", () => {
- let editorElement: HTMLDivElement;
let editor: ClassicEditor;
let triggerCommand: ReturnType<typeof vi.fn>;
@@ -14,19 +14,11 @@ describe("CutToNotePlugin", () => {
getComponentByEl: () => ({ triggerCommand })
} as unknown as typeof glob;
- editorElement = document.createElement("div");
- document.body.appendChild(editorElement);
-
- editor = await ClassicEditor.create(editorElement, {
- licenseKey: "GPL",
- plugins: [Essentials, Paragraph, CutToNotePlugin]
- });
+ editor = await createTestEditor([Essentials, Paragraph, CutToNotePlugin]);
});
- afterEach(async () => {
+ afterEach(() => {
delete (globalThis as { glob?: unknown }).glob;
- editorElement.remove();
- await editor.destroy();
});
it("loads the plugin and registers the toolbar button", () => {
diff --git a/packages/ckeditor5/src/plugins/file_upload/fileuploadcommand.spec.ts b/packages/ckeditor5/src/plugins/file_upload/fileuploadcommand.spec.ts
index b929312965..2464babd92 100644
--- a/packages/ckeditor5/src/plugins/file_upload/fileuploadcommand.spec.ts
+++ b/packages/ckeditor5/src/plugins/file_upload/fileuploadcommand.spec.ts
@@ -10,8 +10,9 @@ import {
_getModelData as getModelData,
_setModelData as setModelData
} from "ckeditor5";
-import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+import { beforeEach, describe, expect, it, vi } from "vitest";
+import { createTestEditor } from "../../../test/editor-kit.js";
import FileUploadCommand from "./fileuploadcommand.js";
/**
@@ -85,17 +86,10 @@ function createUploadAdapterPlugin(editor: ClassicEditor) {
}
describe("FileUploadCommand", () => {
- let editorElement: HTMLDivElement;
let editor: ClassicEditor;
beforeEach(async () => {
- editorElement = document.createElement("div");
- document.body.appendChild(editorElement);
-
- editor = await ClassicEditor.create(editorElement, {
- licenseKey: "GPL",
- plugins: [Essentials, Paragraph, FileRepository, ReferenceSchema]
- });
+ editor = await createTestEditor([Essentials, Paragraph, FileRepository, ReferenceSchema]);
// Provide a minimal upload adapter so FileRepository.createLoader() succeeds.
createUploadAdapterPlugin(editor);
@@ -104,11 +98,6 @@ describe("FileUploadCommand", () => {
editor.commands.add("fileUpload", new FileUploadCommand(editor));
});
- afterEach(async () => {
- editorElement.remove();
- await editor.destroy();
- });
-
// -----------------------------------------------------------------
// refresh()
// -----------------------------------------------------------------
diff --git a/packages/ckeditor5/src/plugins/file_upload/uploadfileplugin.spec.ts b/packages/ckeditor5/src/plugins/file_upload/uploadfileplugin.spec.ts
index 740140af54..6499bb76dc 100644
--- a/packages/ckeditor5/src/plugins/file_upload/uploadfileplugin.spec.ts
+++ b/packages/ckeditor5/src/plugins/file_upload/uploadfileplugin.spec.ts
@@ -1,25 +1,14 @@
import { ClassicEditor, Essentials, Paragraph } from "ckeditor5";
-import { afterEach, beforeEach, describe, expect, it } from "vitest";
+import { beforeEach, describe, expect, it } from "vitest";
+import { createTestEditor } from "../../../test/editor-kit.js";
import Uploadfileplugin from "./uploadfileplugin.js";
describe("Uploadfileplugin", () => {
- let editorElement: HTMLDivElement;
let editor: ClassicEditor;
beforeEach(async () => {
- editorElement = document.createElement("div");
- document.body.appendChild(editorElement);
-
- editor = await ClassicEditor.create(editorElement, {
- licenseKey: "GPL",
- plugins: [Essentials, Paragraph, Uploadfileplugin]
- });
- });
-
- afterEach(async () => {
- editorElement.remove();
- await editor.destroy();
+ editor = await createTestEditor([Essentials, Paragraph, Uploadfileplugin]);
});
it("loads the plugin and its required FileUploadEditing dependency", () => {
diff --git a/packages/ckeditor5/src/plugins/image_actions.spec.ts b/packages/ckeditor5/src/plugins/image_actions.spec.ts
index 6887bcf19f..cbd9c91de1 100644
--- a/packages/ckeditor5/src/plugins/image_actions.spec.ts
+++ b/packages/ckeditor5/src/plugins/image_actions.spec.ts
@@ -1,25 +1,14 @@
import { ClassicEditor, Essentials, Image, ImageBlock, ImageInline, ImageUtils, Paragraph } from "ckeditor5";
-import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+import { beforeEach, describe, expect, it, vi } from "vitest";
+import { createTestEditor } from "../../test/editor-kit.js";
import ImageActions from "./image_actions.js";
describe("ImageActions", () => {
- let editorElement: HTMLDivElement;
let editor: ClassicEditor;
beforeEach(async () => {
- editorElement = document.createElement("div");
- document.body.appendChild(editorElement);
-
- editor = await ClassicEditor.create(editorElement, {
- licenseKey: "GPL",
- plugins: [Essentials, Paragraph, Image, ImageBlock, ImageInline, ImageActions]
- });
- });
-
- afterEach(async () => {
- editorElement.remove();
- await editor.destroy();
+ editor = await createTestEditor([Essentials, Paragraph, Image, ImageBlock, ImageInline, ImageActions]);
});
it("loads the plugin and registers commands and toolbar buttons", () => {
@@ -59,15 +48,7 @@ describe("ImageActions", () => {
const copyToClipboard = vi.fn();
return (async () => {
- editorElement.remove();
- await editor.destroy();
-
- editorElement = document.createElement("div");
- document.body.appendChild(editorElement);
-
- editor = await ClassicEditor.create(editorElement, {
- licenseKey: "GPL",
- plugins: [Essentials, Paragraph, Image, ImageBlock, ImageInline, ImageActions],
+ editor = await createTestEditor([Essentials, Paragraph, Image, ImageBlock, ImageInline, ImageActions], {
imageActions: {
copyToClipboard,
download: vi.fn()
@@ -94,15 +75,7 @@ describe("ImageActions", () => {
const download = vi.fn();
return (async () => {
- editorElement.remove();
- await editor.destroy();
-
- editorElement = document.createElement("div");
- document.body.appendChild(editorElement);
-
- editor = await ClassicEditor.create(editorElement, {
- licenseKey: "GPL",
- plugins: [Essentials, Paragraph, Image, ImageBlock, ImageInline, ImageActions],
+ editor = await createTestEditor([Essentials, Paragraph, Image, ImageBlock, ImageInline, ImageActions], {
imageActions: {
copyToClipboard: vi.fn(),
download
@@ -129,15 +102,7 @@ describe("ImageActions", () => {
const copyToClipboard = vi.fn();
return (async () => {
- editorElement.remove();
- await editor.destroy();
-
- editorElement = document.createElement("div");
- document.body.appendChild(editorElement);
-
- editor = await ClassicEditor.create(editorElement, {
- licenseKey: "GPL",
- plugins: [Essentials, Paragraph, Image, ImageBlock, ImageInline, ImageActions],
+ editor = await createTestEditor([Essentials, Paragraph, Image, ImageBlock, ImageInline, ImageActions], {
imageActions: {
copyToClipboard,
download: vi.fn()
@@ -202,17 +167,9 @@ describe("ImageActions", () => {
it("uses the translate config callback to label buttons when provided", () => {
return (async () => {
- editorElement.remove();
- await editor.destroy();
-
- editorElement = document.createElement("div");
- document.body.appendChild(editorElement);
-
const translate = (key: string) => `translated:${key}`;
- editor = await ClassicEditor.create(editorElement, {
- licenseKey: "GPL",
- plugins: [Essentials, Paragraph, Image, ImageBlock, ImageInline, ImageActions],
+ editor = await createTestEditor([Essentials, Paragraph, Image, ImageBlock, ImageInline, ImageActions], {
translate
});
diff --git a/packages/ckeditor5/src/plugins/include_note_box_size_dropdown.spec.ts b/packages/ckeditor5/src/plugins/include_note_box_size_dropdown.spec.ts
index f2f7bbf5a3..a45b97f4b3 100644
--- a/packages/ckeditor5/src/plugins/include_note_box_size_dropdown.spec.ts
+++ b/packages/ckeditor5/src/plugins/include_note_box_size_dropdown.spec.ts
@@ -1,11 +1,11 @@
import { ClassicEditor, Essentials, Paragraph, Widget, _setModelData as setModelData } from "ckeditor5";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+import { createTestEditor } from "../../test/editor-kit.js";
import IncludeNoteBoxSizeDropdown from "./include_note_box_size_dropdown.js";
import IncludeNote, { BOX_SIZE_COMMAND_NAME, BOX_SIZES } from "./includenote.js";
describe("IncludeNoteBoxSizeDropdown", () => {
- let editorElement: HTMLDivElement;
let editor: ClassicEditor;
beforeEach(async () => {
@@ -17,20 +17,12 @@ describe("IncludeNoteBoxSizeDropdown", () => {
// includenote.ts calls jQuery globally; provide a passthrough stub
(globalThis as unknown as { $: (x: unknown) => unknown }).$ = (x) => x;
- editorElement = document.createElement("div");
- document.body.appendChild(editorElement);
-
- editor = await ClassicEditor.create(editorElement, {
- licenseKey: "GPL",
- plugins: [Essentials, Paragraph, Widget, IncludeNote, IncludeNoteBoxSizeDropdown]
- });
+ editor = await createTestEditor([Essentials, Paragraph, Widget, IncludeNote, IncludeNoteBoxSizeDropdown]);
});
- afterEach(async () => {
+ afterEach(() => {
delete (globalThis as { glob?: unknown }).glob;
delete (globalThis as { $?: unknown }).$;
- editorElement.remove();
- await editor.destroy();
});
it("loads the plugin and registers the dropdown component", () => {
diff --git a/packages/ckeditor5/src/plugins/include_note_toolbar.spec.ts b/packages/ckeditor5/src/plugins/include_note_toolbar.spec.ts
index 6e915bd4fb..6182a56ecf 100644
--- a/packages/ckeditor5/src/plugins/include_note_toolbar.spec.ts
+++ b/packages/ckeditor5/src/plugins/include_note_toolbar.spec.ts
@@ -1,6 +1,7 @@
import { ClassicEditor, Essentials, Paragraph, Plugin, toWidget, Widget, WidgetToolbarRepository, _setModelData as setModelData } from "ckeditor5";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+import { createTestEditor } from "../../test/editor-kit.js";
import IncludeNote from "./includenote.js";
import IncludeNoteBoxSizeDropdown from "./include_note_box_size_dropdown.js";
import IncludeNoteToolbar from "./include_note_toolbar.js";
@@ -69,7 +70,6 @@ function getRelatedElementFn(ed: ClassicEditor): (selection: unknown) => unknown
// ---------------------------------------------------------------------------
describe("IncludeNoteToolbar", () => {
- let editorElement: HTMLDivElement;
let editor: ClassicEditor;
beforeEach(async () => {
@@ -81,20 +81,12 @@ describe("IncludeNoteToolbar", () => {
// includenote.ts calls jQuery globally; provide a passthrough stub.
(globalThis as unknown as { $: (x: unknown) => unknown }).$ = (x) => x;
- editorElement = document.createElement("div");
- document.body.appendChild(editorElement);
-
- editor = await ClassicEditor.create(editorElement, {
- licenseKey: "GPL",
- plugins: [Essentials, Paragraph, Widget, IncludeNote, IncludeNoteBoxSizeDropdown, IncludeNoteToolbar]
- });
+ editor = await createTestEditor([Essentials, Paragraph, Widget, IncludeNote, IncludeNoteBoxSizeDropdown, IncludeNoteToolbar]);
});
- afterEach(async () => {
+ afterEach(() => {
delete (globalThis as { glob?: unknown }).glob;
delete (globalThis as { $?: unknown }).$;
- editorElement.remove();
- await editor.destroy();
});
it("loads the plugin", () => {
@@ -159,7 +151,6 @@ describe("IncludeNoteToolbar", () => {
// ---------------------------------------------------------------------------
describe("isIncludeNoteWidget — section widget without a class attribute (|| '' branch)", () => {
- let editorElement: HTMLDivElement;
let editor: ClassicEditor;
beforeEach(async () => {
@@ -170,20 +161,12 @@ describe("isIncludeNoteWidget — section widget without a class attribute (|| '
(globalThis as unknown as { $: (x: unknown) => unknown }).$ = (x) => x;
- editorElement = document.createElement("div");
- document.body.appendChild(editorElement);
-
- editor = await ClassicEditor.create(editorElement, {
- licenseKey: "GPL",
- plugins: [Essentials, Paragraph, Widget, IncludeNote, IncludeNoteBoxSizeDropdown, IncludeNoteToolbar, SectionNoClassWidget]
- });
+ editor = await createTestEditor([Essentials, Paragraph, Widget, IncludeNote, IncludeNoteBoxSizeDropdown, IncludeNoteToolbar, SectionNoClassWidget]);
});
- afterEach(async () => {
+ afterEach(() => {
delete (globalThis as { glob?: unknown }).glob;
delete (globalThis as { $?: unknown }).$;
- editorElement.remove();
- await editor.destroy();
});
it("returns null for a section widget that has no class attribute (exercises the || '' fallback at line 43)", () => {
@@ -217,7 +200,6 @@ describe("isIncludeNoteWidget — section widget without a class attribute (|| '
// ---------------------------------------------------------------------------
describe("isIncludeNoteWidget — real non-include-note widgets (branch coverage)", () => {
- let editorElement: HTMLDivElement;
let editor: ClassicEditor;
beforeEach(async () => {
@@ -243,20 +225,12 @@ describe("isIncludeNoteWidget — real non-include-note widgets (branch coverage
// Both includenote.ts and linkembed.ts call jQuery globally.
(globalThis as unknown as { $: (x: unknown) => unknown }).$ = (x) => x;
- editorElement = document.createElement("div");
- document.body.appendChild(editorElement);
-
- editor = await ClassicEditor.create(editorElement, {
- licenseKey: "GPL",
- plugins: [Essentials, Paragraph, Widget, IncludeNote, IncludeNoteBoxSizeDropdown, IncludeNoteToolbar, LinkEmbed]
- });
+ editor = await createTestEditor([Essentials, Paragraph, Widget, IncludeNote, IncludeNoteBoxSizeDropdown, IncludeNoteToolbar, LinkEmbed]);
});
- afterEach(async () => {
+ afterEach(() => {
delete (globalThis as { glob?: unknown }).glob;
delete (globalThis as { $?: unknown }).$;
- editorElement.remove();
- await editor.destroy();
});
it("returns null when getSelectedElement returns a non-widget element (covers the !isWidget branch at line 35-36)", () => {
diff --git a/packages/ckeditor5/src/plugins/includenote.spec.ts b/packages/ckeditor5/src/plugins/includenote.spec.ts
index dadb760709..f8a524f627 100644
--- a/packages/ckeditor5/src/plugins/includenote.spec.ts
+++ b/packages/ckeditor5/src/plugins/includenote.spec.ts
@@ -9,10 +9,10 @@ import {
} from "ckeditor5";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+import { createTestEditor } from "../../test/editor-kit.js";
import IncludeNote, { BOX_SIZE_COMMAND_NAME, BOX_SIZES, COMMAND_NAME } from "./includenote.js";
describe("IncludeNote", () => {
- let editorElement: HTMLDivElement;
let editor: ClassicEditor;
let triggerCommand: ReturnType<typeof vi.fn>;
let loadIncludedNote: ReturnType<typeof vi.fn>;
@@ -27,20 +27,12 @@ describe("IncludeNote", () => {
// includenote.ts calls jQuery globally; provide a passthrough stub.
(globalThis as unknown as { $: (x: unknown) => unknown }).$ = (x) => x;
- editorElement = document.createElement("div");
- document.body.appendChild(editorElement);
-
- editor = await ClassicEditor.create(editorElement, {
- licenseKey: "GPL",
- plugins: [Essentials, Paragraph, Widget, IncludeNote]
- });
+ editor = await createTestEditor([Essentials, Paragraph, Widget, IncludeNote]);
});
- afterEach(async () => {
+ afterEach(() => {
delete (globalThis as { glob?: unknown }).glob;
delete (globalThis as { $?: unknown }).$;
- editorElement.remove();
- await editor.destroy();
});
// -----------------------------------------------------------------------
diff --git a/packages/ckeditor5/src/plugins/indent_block_shortcut.spec.ts b/packages/ckeditor5/src/plugins/indent_block_shortcut.spec.ts
index 72dbd17982..c2eca65b2c 100644
--- a/packages/ckeditor5/src/plugins/indent_block_shortcut.spec.ts
+++ b/packages/ckeditor5/src/plugins/indent_block_shortcut.spec.ts
@@ -1,25 +1,14 @@
import { _setModelData as setModelData, ClassicEditor, Essentials, Indent, IndentBlock, Paragraph, Table, TableEditing } from "ckeditor5";
-import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+import { beforeEach, describe, expect, it, vi } from "vitest";
+import { createTestEditor } from "../../test/editor-kit.js";
import IndentBlockShortcutPlugin from "./indent_block_shortcut.js";
describe("IndentBlockShortcutPlugin", () => {
- let editorElement: HTMLDivElement;
let editor: ClassicEditor;
beforeEach(async () => {
- editorElement = document.createElement("div");
- document.body.appendChild(editorElement);
-
- editor = await ClassicEditor.create(editorElement, {
- licenseKey: "GPL",
- plugins: [Essentials, Paragraph, Indent, IndentBlock, Table, TableEditing, IndentBlockShortcutPlugin]
- });
- });
-
- afterEach(async () => {
- editorElement.remove();
- await editor.destroy();
+ editor = await createTestEditor([Essentials, Paragraph, Indent, IndentBlock, Table, TableEditing, IndentBlockShortcutPlugin]);
});
it("loads the plugin", () => {
diff --git a/packages/ckeditor5/src/plugins/inline_code_no_spellcheck.spec.ts b/packages/ckeditor5/src/plugins/inline_code_no_spellcheck.spec.ts
index 931b3a9ec6..791abf1a88 100644
--- a/packages/ckeditor5/src/plugins/inline_code_no_spellcheck.spec.ts
+++ b/packages/ckeditor5/src/plugins/inline_code_no_spellcheck.spec.ts
@@ -1,25 +1,14 @@
import { _setModelData as setModelData, _getViewData as getViewData, ClassicEditor, Code, Essentials, Paragraph } from "ckeditor5";
-import { afterEach, beforeEach, describe, expect, it } from "vitest";
+import { beforeEach, describe, expect, it } from "vitest";
+import { createTestEditor } from "../../test/editor-kit.js";
import InlineCodeNoSpellcheck from "./inline_code_no_spellcheck.js";
describe("InlineCodeNoSpellcheck", () => {
- let editorElement: HTMLDivElement;
let editor: ClassicEditor;
beforeEach(async () => {
- editorElement = document.createElement("div");
- document.body.appendChild(editorElement);
-
- editor = await ClassicEditor.create(editorElement, {
- licenseKey: "GPL",
- plugins: [Essentials, Paragraph, Code, InlineCodeNoSpellcheck]
- });
- });
-
- afterEach(async () => {
- editorElement.remove();
- await editor.destroy();
+ editor = await createTestEditor([Essentials, Paragraph, Code, InlineCodeNoSpellcheck]);
});
it("loads the plugin", () => {
diff --git a/packages/ckeditor5/src/plugins/inline_code_toolbar.spec.ts b/packages/ckeditor5/src/plugins/inline_code_toolbar.spec.ts
index 661ae61064..8e5c089e4a 100644
--- a/packages/ckeditor5/src/plugins/inline_code_toolbar.spec.ts
+++ b/packages/ckeditor5/src/plugins/inline_code_toolbar.spec.ts
@@ -1,26 +1,15 @@
import { _setModelData as setModelData, ClassicEditor, Code, Essentials, Paragraph, WidgetToolbarRepository } from "ckeditor5";
-import { afterEach, beforeEach, describe, expect, it } from "vitest";
+import { beforeEach, describe, expect, it } from "vitest";
+import { createTestEditor } from "../../test/editor-kit.js";
import CopyToClipboardButton from "./copy_to_clipboard_button.js";
import InlineCodeToolbar from "./inline_code_toolbar.js";
describe("InlineCodeToolbar", () => {
- let editorElement: HTMLDivElement;
let editor: ClassicEditor;
beforeEach(async () => {
- editorElement = document.createElement("div");
- document.body.appendChild(editorElement);
-
- editor = await ClassicEditor.create(editorElement, {
- licenseKey: "GPL",
- plugins: [Essentials, Paragraph, Code, CopyToClipboardButton, InlineCodeToolbar]
- });
- });
-
- afterEach(async () => {
- editorElement.remove();
- await editor.destroy();
+ editor = await createTestEditor([Essentials, Paragraph, Code, CopyToClipboardButton, InlineCodeToolbar]);
});
it("loads the plugin successfully", () => {
diff --git a/packages/ckeditor5/src/plugins/insert_date_time.spec.ts b/packages/ckeditor5/src/plugins/insert_date_time.spec.ts
index de948bf90c..9f6e83bd82 100644
--- a/packages/ckeditor5/src/plugins/insert_date_time.spec.ts
+++ b/packages/ckeditor5/src/plugins/insert_date_time.spec.ts
@@ -1,10 +1,10 @@
import { ClassicEditor, Essentials, Paragraph } from "ckeditor5";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+import { createTestEditor } from "../../test/editor-kit.js";
import InsertDateTimePlugin, { COMMAND_NAME } from "./insert_date_time.js";
describe("InsertDateTimePlugin", () => {
- let editorElement: HTMLDivElement;
let editor: ClassicEditor;
let triggerCommand: ReturnType<typeof vi.fn>;
@@ -14,19 +14,11 @@ describe("InsertDateTimePlugin", () => {
getComponentByEl: () => ({ triggerCommand })
} as unknown as typeof glob;
- editorElement = document.createElement("div");
- document.body.appendChild(editorElement);
-
- editor = await ClassicEditor.create(editorElement, {
- licenseKey: "GPL",
- plugins: [Essentials, Paragraph, InsertDateTimePlugin]
- });
+ editor = await createTestEditor([Essentials, Paragraph, InsertDateTimePlugin]);
});
- afterEach(async () => {
+ afterEach(() => {
delete (globalThis as { glob?: unknown }).glob;
- editorElement.remove();
- await editor.destroy();
});
it("loads the plugin and registers the command and toolbar button", () => {
diff --git a/packages/ckeditor5/src/plugins/internallink.spec.ts b/packages/ckeditor5/src/plugins/internallink.spec.ts
index 6bad5b5b6a..f4affcb0bb 100644
--- a/packages/ckeditor5/src/plugins/internallink.spec.ts
+++ b/packages/ckeditor5/src/plugins/internallink.spec.ts
@@ -1,10 +1,10 @@
import { _setModelData as setModelData, ClassicEditor, CodeBlock, Essentials, Paragraph } from "ckeditor5";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+import { createTestEditor } from "../../test/editor-kit.js";
import InternalLinkPlugin, { COMMAND_NAME } from "./internallink.js";
describe("InternalLinkPlugin", () => {
- let editorElement: HTMLDivElement;
let editor: ClassicEditor;
let triggerCommand: ReturnType<typeof vi.fn>;
@@ -14,19 +14,11 @@ describe("InternalLinkPlugin", () => {
getComponentByEl: () => ({ triggerCommand })
} as unknown as typeof glob;
- editorElement = document.createElement("div");
- document.body.appendChild(editorElement);
-
- editor = await ClassicEditor.create(editorElement, {
- licenseKey: "GPL",
- plugins: [Essentials, Paragraph, CodeBlock, InternalLinkPlugin]
- });
+ editor = await createTestEditor([Essentials, Paragraph, CodeBlock, InternalLinkPlugin]);
});
- afterEach(async () => {
+ afterEach(() => {
delete (globalThis as { glob?: unknown }).glob;
- editorElement.remove();
- await editor.destroy();
});
it("loads the plugin and registers the command and toolbar button", () => {
diff --git a/packages/ckeditor5/src/plugins/italic_as_em.spec.ts b/packages/ckeditor5/src/plugins/italic_as_em.spec.ts
index d6324cea66..8046a54dfe 100644
--- a/packages/ckeditor5/src/plugins/italic_as_em.spec.ts
+++ b/packages/ckeditor5/src/plugins/italic_as_em.spec.ts
@@ -1,25 +1,14 @@
import { _setModelData as setModelData, Bold, ClassicEditor, Essentials, Italic, Paragraph } from "ckeditor5";
-import { afterEach, beforeEach, describe, expect, it } from "vitest";
+import { beforeEach, describe, expect, it } from "vitest";
+import { createTestEditor } from "../../test/editor-kit.js";
import ItalicAsEmPlugin from "./italic_as_em.js";
describe("ItalicAsEmPlugin", () => {
- let editorElement: HTMLDivElement;
let editor: ClassicEditor;
beforeEach(async () => {
- editorElement = document.createElement("div");
- document.body.appendChild(editorElement);
-
- editor = await ClassicEditor.create(editorElement, {
- licenseKey: "GPL",
- plugins: [Essentials, Paragraph, Bold, Italic, ItalicAsEmPlugin]
- });
- });
-
- afterEach(async () => {
- editorElement.remove();
- await editor.destroy();
+ editor = await createTestEditor([Essentials, Paragraph, Bold, Italic, ItalicAsEmPlugin]);
});
it("loads the plugin", () => {
diff --git a/packages/ckeditor5/src/plugins/link_embed_toolbar.spec.ts b/packages/ckeditor5/src/plugins/link_embed_toolbar.spec.ts
index 8c0413ecb8..c5ea8d1587 100644
--- a/packages/ckeditor5/src/plugins/link_embed_toolbar.spec.ts
+++ b/packages/ckeditor5/src/plugins/link_embed_toolbar.spec.ts
@@ -1,6 +1,7 @@
import { ClassicEditor, Essentials, Paragraph, WidgetToolbarRepository, _setModelData as setModelData } from "ckeditor5";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+import { createTestEditor } from "../../test/editor-kit.js";
import LinkEmbedToolbar from "./link_embed_toolbar.js";
import LinkEmbed, { CHANGE_LINK_DISPLAY_COMMAND, LINK_DISPLAY_MODES } from "./linkembed.js";
@@ -71,7 +72,6 @@ function getToolbarDef(editor: ClassicEditor): {
// ---------------------------------------------------------------------------
describe("LinkEmbedToolbar", () => {
- let editorElement: HTMLDivElement;
let editor: ClassicEditor;
beforeEach(async () => {
@@ -94,20 +94,12 @@ describe("LinkEmbedToolbar", () => {
(globalThis as unknown as { $: (x: unknown) => unknown }).$ = (x) => x;
- editorElement = document.createElement("div");
- document.body.appendChild(editorElement);
-
- editor = await ClassicEditor.create(editorElement, {
- licenseKey: "GPL",
- plugins: [Essentials, Paragraph, LinkEmbed, LinkEmbedToolbar]
- });
+ editor = await createTestEditor([Essentials, Paragraph, LinkEmbed, LinkEmbedToolbar]);
});
- afterEach(async () => {
+ afterEach(() => {
delete (globalThis as { glob?: unknown }).glob;
delete (globalThis as { $?: unknown }).$;
- editorElement.remove();
- await editor.destroy();
});
// -----------------------------------------------------------------------
diff --git a/packages/ckeditor5/src/plugins/linkembed.spec.ts b/packages/ckeditor5/src/plugins/linkembed.spec.ts
index a0de9c483d..fee25cc47c 100644
--- a/packages/ckeditor5/src/plugins/linkembed.spec.ts
+++ b/packages/ckeditor5/src/plugins/linkembed.spec.ts
@@ -10,6 +10,7 @@ import {
} from "ckeditor5";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+import { createTestEditor } from "../../test/editor-kit.js";
import LinkEmbed, { CHANGE_LINK_DISPLAY_COMMAND, LINK_EMBED_COMMAND } from "./linkembed.js";
const META = {
@@ -23,7 +24,6 @@ const META = {
};
describe("LinkEmbed", () => {
- let editorElement: HTMLDivElement;
let editor: ClassicEditor;
let triggerCommand: ReturnType<typeof vi.fn>;
let renderLinkEmbed: ReturnType<typeof vi.fn>;
@@ -49,19 +49,11 @@ describe("LinkEmbed", () => {
})
} as unknown as typeof glob;
- editorElement = document.createElement("div");
- document.body.appendChild(editorElement);
-
- editor = await ClassicEditor.create(editorElement, {
- licenseKey: "GPL",
- plugins: [Essentials, Paragraph, BlockQuote, Link, Undo, LinkEmbed]
- });
+ editor = await createTestEditor([Essentials, Paragraph, BlockQuote, Link, Undo, LinkEmbed]);
});
- afterEach(async () => {
+ afterEach(() => {
delete (globalThis as { glob?: unknown }).glob;
- editorElement.remove();
- await editor.destroy();
});
// -----------------------------------------------------------------------
diff --git a/packages/ckeditor5/src/plugins/markdownimport.spec.ts b/packages/ckeditor5/src/plugins/markdownimport.spec.ts
index 0a79c8189d..297fe32ed5 100644
--- a/packages/ckeditor5/src/plugins/markdownimport.spec.ts
+++ b/packages/ckeditor5/src/plugins/markdownimport.spec.ts
@@ -1,10 +1,10 @@
import { ClassicEditor, Essentials, Paragraph } from "ckeditor5";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+import { createTestEditor } from "../../test/editor-kit.js";
import MarkdownImportPlugin, { COMMAND_NAME } from "./markdownimport.js";
describe("MarkdownImportPlugin", () => {
- let editorElement: HTMLDivElement;
let editor: ClassicEditor;
let triggerCommand: ReturnType<typeof vi.fn>;
@@ -14,19 +14,11 @@ describe("MarkdownImportPlugin", () => {
getComponentByEl: () => ({ triggerCommand })
} as unknown as typeof glob;
- editorElement = document.createElement("div");
- document.body.appendChild(editorElement);
-
- editor = await ClassicEditor.create(editorElement, {
- licenseKey: "GPL",
- plugins: [Essentials, Paragraph, MarkdownImportPlugin]
- });
+ editor = await createTestEditor([Essentials, Paragraph, MarkdownImportPlugin]);
});
- afterEach(async () => {
+ afterEach(() => {
delete (globalThis as { glob?: unknown }).glob;
- editorElement.remove();
- await editor.destroy();
});
it("loads the plugin and registers the command and toolbar button", () => {
diff --git a/packages/ckeditor5/src/plugins/mention_customization.spec.ts b/packages/ckeditor5/src/plugins/mention_customization.spec.ts
index 9d6f020c17..741470c4ab 100644
--- a/packages/ckeditor5/src/plugins/mention_customization.spec.ts
+++ b/packages/ckeditor5/src/plugins/mention_customization.spec.ts
@@ -8,11 +8,11 @@ import {
} from "ckeditor5";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+import { createTestEditor } from "../../test/editor-kit.js";
import MentionCustomization from "./mention_customization.js";
import ReferenceLink from "./referencelink.js";
describe("MentionCustomization", () => {
- let editorElement: HTMLDivElement;
let editor: ClassicEditor;
let createNoteForReferenceLink: ReturnType<typeof vi.fn>;
@@ -30,20 +30,18 @@ describe("MentionCustomization", () => {
// referencelink.ts editing downcast converter calls jQuery globally.
(globalThis as unknown as { $: (x: unknown) => unknown }).$ = (x) => x;
- editorElement = document.createElement("div");
- document.body.appendChild(editorElement);
-
- editor = await ClassicEditor.create(editorElement, {
- licenseKey: "GPL",
- plugins: [Essentials, Paragraph, Mention, ReferenceLink, MentionCustomization]
- });
+ editor = await createTestEditor([
+ Essentials,
+ Paragraph,
+ Mention,
+ ReferenceLink,
+ MentionCustomization
+ ]);
});
- afterEach(async () => {
+ afterEach(() => {
delete (globalThis as { glob?: unknown }).glob;
delete (globalThis as { $?: unknown }).$;
- editorElement.remove();
- await editor.destroy();
});
it("loads the plugin, requires Mention and overrides the mention command", () => {
diff --git a/packages/ckeditor5/src/plugins/move_block_updown.spec.ts b/packages/ckeditor5/src/plugins/move_block_updown.spec.ts
index 9c7cc9676c..788ad651d1 100644
--- a/packages/ckeditor5/src/plugins/move_block_updown.spec.ts
+++ b/packages/ckeditor5/src/plugins/move_block_updown.spec.ts
@@ -8,8 +8,9 @@ import {
Widget,
} from "ckeditor5";
import type { ModelElement, ModelText } from "ckeditor5";
-import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+import { beforeEach, describe, expect, it, vi } from "vitest";
+import { createTestEditor } from "../../test/editor-kit.js";
import MoveBlockUpDownPlugin from "./move_block_updown.js";
/** Returns the text data of the Nth block in the editor root (0-indexed). */
@@ -64,22 +65,10 @@ class TestBoxPlugin extends Plugin {
}
describe("MoveBlockUpDownPlugin", () => {
- let editorElement: HTMLDivElement;
let editor: ClassicEditor;
beforeEach(async () => {
- editorElement = document.createElement("div");
- document.body.appendChild(editorElement);
-
- editor = await ClassicEditor.create(editorElement, {
- licenseKey: "GPL",
- plugins: [Essentials, Paragraph, MoveBlockUpDownPlugin, TestBoxPlugin]
- });
- });
-
- afterEach(async () => {
- editorElement.remove();
- await editor.destroy();
+ editor = await createTestEditor([Essentials, Paragraph, MoveBlockUpDownPlugin, TestBoxPlugin]);
});
it("registers moveBlockUp and moveBlockDown commands", () => {
@@ -440,13 +429,9 @@ describe("MoveBlockUpDownPlugin", () => {
});
describe("MoveBlockUpDownPlugin with collapsible summary element", () => {
- let editorElement: HTMLDivElement;
let editor: ClassicEditor;
beforeEach(async () => {
- editorElement = document.createElement("div");
- document.body.appendChild(editorElement);
-
// Use a minimal plugin that registers the details/summary schema
// so we can test the `el.name === 'summary'` hoisting branch.
class MinimalCollapsibleSchema extends Plugin {
@@ -467,15 +452,7 @@ describe("MoveBlockUpDownPlugin with collapsible summary element", () => {
}
}
- editor = await ClassicEditor.create(editorElement, {
- licenseKey: "GPL",
- plugins: [Essentials, Paragraph, MoveBlockUpDownPlugin, MinimalCollapsibleSchema]
- });
- });
-
- afterEach(async () => {
- editorElement.remove();
- await editor.destroy();
+ editor = await createTestEditor([Essentials, Paragraph, MoveBlockUpDownPlugin, MinimalCollapsibleSchema]);
});
it("hoists a caret in <summary> to the enclosing <details> when moving up", () => {
diff --git a/packages/ckeditor5/src/plugins/referencelink.spec.ts b/packages/ckeditor5/src/plugins/referencelink.spec.ts
index a85266989a..27a211feff 100644
--- a/packages/ckeditor5/src/plugins/referencelink.spec.ts
+++ b/packages/ckeditor5/src/plugins/referencelink.spec.ts
@@ -1,10 +1,10 @@
import { _getViewData as getViewData, _setModelData as setModelData, ClassicEditor, Essentials, LinkEditing, Paragraph } from "ckeditor5";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+import { createTestEditor } from "../../test/editor-kit.js";
import ReferenceLink from "./referencelink.js";
describe("ReferenceLink", () => {
- let editorElement: HTMLDivElement;
let editor: ClassicEditor;
let getReferenceLinkTitle: ReturnType<typeof vi.fn>;
let getReferenceLinkTitleSync: ReturnType<typeof vi.fn>;
@@ -23,20 +23,12 @@ describe("ReferenceLink", () => {
// The editingDowncast and upcast converters call jQuery via a global `$`.
(globalThis as unknown as { $: (x: unknown) => unknown }).$ = (x) => x;
- editorElement = document.createElement("div");
- document.body.appendChild(editorElement);
-
- editor = await ClassicEditor.create(editorElement, {
- licenseKey: "GPL",
- plugins: [Essentials, Paragraph, LinkEditing, ReferenceLink]
- });
+ editor = await createTestEditor([Essentials, Paragraph, LinkEditing, ReferenceLink]);
});
- afterEach(async () => {
+ afterEach(() => {
delete (globalThis as { glob?: unknown }).glob;
delete (globalThis as { $?: unknown }).$;
- editorElement.remove();
- await editor.destroy();
});
it("loads the plugin, registers the schema and the command", () => {
diff --git a/packages/ckeditor5/src/plugins/remove_format_links.spec.ts b/packages/ckeditor5/src/plugins/remove_format_links.spec.ts
index 84da3093c9..db61e7b3ba 100644
--- a/packages/ckeditor5/src/plugins/remove_format_links.spec.ts
+++ b/packages/ckeditor5/src/plugins/remove_format_links.spec.ts
@@ -1,25 +1,14 @@
import { ClassicEditor, Essentials, Link, Paragraph, RemoveFormat } from "ckeditor5";
-import { afterEach, beforeEach, describe, expect, it } from "vitest";
+import { beforeEach, describe, expect, it } from "vitest";
+import { createTestEditor } from "../../test/editor-kit.js";
import RemoveFormatLinksPlugin from "./remove_format_links.js";
describe("RemoveFormatLinksPlugin", () => {
- let editorElement: HTMLDivElement;
let editor: ClassicEditor;
beforeEach(async () => {
- editorElement = document.createElement("div");
- document.body.appendChild(editorElement);
-
- editor = await ClassicEditor.create(editorElement, {
- licenseKey: "GPL",
- plugins: [Essentials, Paragraph, Link, RemoveFormat, RemoveFormatLinksPlugin]
- });
- });
-
- afterEach(async () => {
- editorElement.remove();
- await editor.destroy();
+ editor = await createTestEditor([Essentials, Paragraph, Link, RemoveFormat, RemoveFormatLinksPlugin]);
});
it("registers itself as a plugin", () => {
diff --git a/packages/ckeditor5/src/plugins/scroll_on_undo_redo.spec.ts b/packages/ckeditor5/src/plugins/scroll_on_undo_redo.spec.ts
index ba5a5a65f7..1b90fe781d 100644
--- a/packages/ckeditor5/src/plugins/scroll_on_undo_redo.spec.ts
+++ b/packages/ckeditor5/src/plugins/scroll_on_undo_redo.spec.ts
@@ -1,27 +1,19 @@
import { _setModelData as setModelData, ClassicEditor, Essentials, Paragraph, Undo } from "ckeditor5";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+import { createTestEditor } from "../../test/editor-kit.js";
import ScrollOnUndoRedoPlugin from "./scroll_on_undo_redo.js";
describe("ScrollOnUndoRedoPlugin", () => {
- let editorElement: HTMLDivElement;
let editor: ClassicEditor;
beforeEach(async () => {
- editorElement = document.createElement("div");
- document.body.appendChild(editorElement);
-
// Create the editor with real timers so CKEditor's internal rAF/timeout usage works.
- editor = await ClassicEditor.create(editorElement, {
- licenseKey: "GPL",
- plugins: [Essentials, Paragraph, Undo, ScrollOnUndoRedoPlugin]
- });
+ editor = await createTestEditor([Essentials, Paragraph, Undo, ScrollOnUndoRedoPlugin]);
});
- afterEach(async () => {
+ afterEach(() => {
vi.useRealTimers();
- editorElement.remove();
- await editor.destroy();
});
it("loads the plugin", () => {
diff --git a/packages/ckeditor5/src/plugins/strikethrough_as_del.spec.ts b/packages/ckeditor5/src/plugins/strikethrough_as_del.spec.ts
index 688385e5ae..746ee73750 100644
--- a/packages/ckeditor5/src/plugins/strikethrough_as_del.spec.ts
+++ b/packages/ckeditor5/src/plugins/strikethrough_as_del.spec.ts
@@ -1,25 +1,14 @@
import { _setModelData as setModelData, ClassicEditor, Essentials, Paragraph, Strikethrough } from "ckeditor5";
-import { afterEach, beforeEach, describe, expect, it } from "vitest";
+import { beforeEach, describe, expect, it } from "vitest";
+import { createTestEditor } from "../../test/editor-kit.js";
import StrikethroughAsDel from "./strikethrough_as_del.js";
describe("StrikethroughAsDel", () => {
- let editorElement: HTMLDivElement;
let editor: ClassicEditor;
beforeEach(async () => {
- editorElement = document.createElement("div");
- document.body.appendChild(editorElement);
-
- editor = await ClassicEditor.create(editorElement, {
- licenseKey: "GPL",
- plugins: [Essentials, Paragraph, Strikethrough, StrikethroughAsDel]
- });
- });
-
- afterEach(async () => {
- editorElement.remove();
- await editor.destroy();
+ editor = await createTestEditor([Essentials, Paragraph, Strikethrough, StrikethroughAsDel]);
});
it("registers the plugin", () => {
diff --git a/packages/ckeditor5/src/plugins/syntax_highlighting/index.spec.ts b/packages/ckeditor5/src/plugins/syntax_highlighting/index.spec.ts
index 361adfd5ee..c5ea206073 100644
--- a/packages/ckeditor5/src/plugins/syntax_highlighting/index.spec.ts
+++ b/packages/ckeditor5/src/plugins/syntax_highlighting/index.spec.ts
@@ -1,6 +1,7 @@
import { _setModelData as setModelData, _getViewData as getViewData, ClassicEditor, CodeBlock, Essentials, GeneralHtmlSupport, HorizontalLine, Paragraph, type EditorConfig } from "ckeditor5";
-import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+import { beforeEach, describe, expect, it, vi } from "vitest";
+import { createTestEditor } from "../../../test/editor-kit.js";
import SyntaxHighlighting from "./index.js";
// A tiny fake highlight.js that wraps the whole text in a single hljs-keyword
@@ -21,39 +22,28 @@ function makeFakeHljs(html: (text: string) => string): FakeHljs {
const DEFAULT_MIME = "auto-detect";
async function createEditor(
- editorElement: HTMLDivElement,
syntaxHighlighting: EditorConfig["syntaxHighlighting"],
extraPlugins: EditorConfig["plugins"] = [],
extraConfig: Partial<EditorConfig> = {}
): Promise<ClassicEditor> {
- return ClassicEditor.create(editorElement, {
- licenseKey: "GPL",
- plugins: [Essentials, Paragraph, CodeBlock, SyntaxHighlighting, ...(extraPlugins ?? [])],
- syntaxHighlighting,
- ...extraConfig
- });
+ return createTestEditor(
+ [Essentials, Paragraph, CodeBlock, SyntaxHighlighting, ...(extraPlugins ?? [])],
+ {
+ syntaxHighlighting,
+ ...extraConfig
+ }
+ );
}
describe("SyntaxHighlighting", () => {
- let editorElement: HTMLDivElement;
let editor: ClassicEditor | undefined;
let fakeHljs: FakeHljs;
beforeEach(() => {
- editorElement = document.createElement("div");
- document.body.appendChild(editorElement);
// Wrap the text in one span so a marker is generated for every codeblock.
fakeHljs = makeFakeHljs((text) => `<span class="hljs-keyword">${escapeHtml(text)}</span>`);
});
- afterEach(async () => {
- editorElement.remove();
- if (editor) {
- await editor.destroy();
- editor = undefined;
- }
- });
-
function makeConfig(overrides: Partial<NonNullable<EditorConfig["syntaxHighlighting"]>> = {}): EditorConfig["syntaxHighlighting"] {
return {
enabled: true,
@@ -65,7 +55,7 @@ describe("SyntaxHighlighting", () => {
}
it("does nothing when the syntaxHighlighting config is missing", async () => {
- editor = await createEditor(editorElement, undefined);
+ editor = await createEditor(undefined);
const plugin = editor.plugins.get(SyntaxHighlighting);
expect(plugin).toBeInstanceOf(SyntaxHighlighting);
@@ -76,7 +66,7 @@ describe("SyntaxHighlighting", () => {
it("does nothing when syntaxHighlighting is disabled", async () => {
const loadHighlightJs = vi.fn(async () => fakeHljs);
- editor = await createEditor(editorElement, makeConfig({ enabled: false, loadHighlightJs }));
+ editor = await createEditor(makeConfig({ enabled: false, loadHighlightJs }));
setModelData(editor.model, '<codeBlock language="javascript">const a[]</codeBlock>');
expect(loadHighlightJs).not.toHaveBeenCalled();
@@ -84,7 +74,7 @@ describe("SyntaxHighlighting", () => {
});
it("highlights a code block, downcasting markers to spans", async () => {
- editor = await createEditor(editorElement, makeConfig());
+ editor = await createEditor(makeConfig());
setModelData(editor.model, '<codeBlock language="javascript">const a[]</codeBlock>');
await flushPostFixers(editor);
@@ -98,7 +88,7 @@ describe("SyntaxHighlighting", () => {
});
it("uses highlightAuto when the language matches the default mime type", async () => {
- editor = await createEditor(editorElement, makeConfig());
+ editor = await createEditor(makeConfig());
setModelData(editor.model, `<codeBlock language="${DEFAULT_MIME}">hello[]</codeBlock>`);
await flushPostFixers(editor);
@@ -108,7 +98,7 @@ describe("SyntaxHighlighting", () => {
});
it("does not highlight plaintext code blocks", async () => {
- editor = await createEditor(editorElement, makeConfig());
+ editor = await createEditor(makeConfig());
setModelData(editor.model, '<codeBlock language="text-plain">plain text[]</codeBlock>');
await flushPostFixers(editor);
@@ -119,7 +109,7 @@ describe("SyntaxHighlighting", () => {
});
it("re-highlights when the code block content changes, clearing old markers", async () => {
- editor = await createEditor(editorElement, makeConfig());
+ editor = await createEditor(makeConfig());
setModelData(editor.model, '<codeBlock language="javascript">const a[]</codeBlock>');
await flushPostFixers(editor);
@@ -140,7 +130,7 @@ describe("SyntaxHighlighting", () => {
});
it("handles softBreaks (newlines) in code blocks", async () => {
- editor = await createEditor(editorElement, makeConfig());
+ editor = await createEditor(makeConfig());
editor.model.change((writer) => {
const root = editor?.model.document.getRoot();
@@ -163,7 +153,7 @@ describe("SyntaxHighlighting", () => {
// Produce nested spans plus an escaped entity so the entity branch runs.
fakeHljs = makeFakeHljs(() =>
'<span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&lt;stdio.h&gt;</span></span>');
- editor = await createEditor(editorElement, makeConfig());
+ editor = await createEditor(makeConfig());
editor.model.change((writer) => {
const root = editor?.model.document.getRoot();
@@ -184,7 +174,7 @@ describe("SyntaxHighlighting", () => {
it("uses only the first class when highlight.js emits a scoped (space-separated) class", async () => {
fakeHljs = makeFakeHljs((text) => `<span class="hljs-title function_">${escapeHtml(text)}</span>`);
- editor = await createEditor(editorElement, makeConfig());
+ editor = await createEditor(makeConfig());
setModelData(editor.model, '<codeBlock language="python">def f[]</codeBlock>');
await flushPostFixers(editor);
@@ -195,7 +185,7 @@ describe("SyntaxHighlighting", () => {
});
it("does not highlight when the highlighted block is too large", async () => {
- editor = await createEditor(editorElement, makeConfig());
+ editor = await createEditor(makeConfig());
editor.model.change((writer) => {
const root = editor?.model.document.getRoot();
@@ -217,7 +207,7 @@ describe("SyntaxHighlighting", () => {
it("ignores a falsy highlight result", async () => {
fakeHljs.highlight.mockReturnValue(undefined);
- editor = await createEditor(editorElement, makeConfig());
+ editor = await createEditor(makeConfig());
setModelData(editor.model, '<codeBlock language="javascript">const a[]</codeBlock>');
await flushPostFixers(editor);
@@ -231,7 +221,7 @@ describe("SyntaxHighlighting", () => {
// recurse branch is skipped) and another <htmlDiv> (recursed into) that holds
// the code block. Inserting the whole subtree as one unit makes the postfixer
// take the recursive lookForCodeBlocks path.
- editor = await createEditor(editorElement, makeConfig(), [GeneralHtmlSupport, HorizontalLine], {
+ editor = await createEditor(makeConfig(), [GeneralHtmlSupport, HorizontalLine], {
htmlSupport: { allow: [{ name: /.*/, attributes: true, classes: true, styles: true }] }
});
@@ -246,7 +236,7 @@ describe("SyntaxHighlighting", () => {
// A closing </span> with no matching opening tag pops an empty stack, so
// posStart is undefined and no marker is added for it.
fakeHljs = makeFakeHljs((text) => `<span class="hljs-keyword">${escapeHtml(text)}</span></span>`);
- editor = await createEditor(editorElement, makeConfig());
+ editor = await createEditor(makeConfig());
setModelData(editor.model, '<codeBlock language="javascript">a[]</codeBlock>');
await flushPostFixers(editor);
@@ -260,7 +250,7 @@ describe("SyntaxHighlighting", () => {
// with no corresponding child text (child stays null -> startOffset fallback,
// and the child-fetch falls into the empty childText branch).
fakeHljs = makeFakeHljs(() => '<span class="hljs-comment"></span>');
- editor = await createEditor(editorElement, makeConfig());
+ editor = await createEditor(makeConfig());
editor.model.change((writer) => {
const root = editor?.model.document.getRoot();
@@ -278,7 +268,7 @@ describe("SyntaxHighlighting", () => {
});
it("clears markers when a code block is removed", async () => {
- editor = await createEditor(editorElement, makeConfig());
+ editor = await createEditor(makeConfig());
setModelData(editor.model,
'<codeBlock language="javascript">const a</codeBlock><paragraph>after[]</paragraph>');
diff --git a/packages/ckeditor5/src/plugins/todo_list_multistate/todo_list_multistate.spec.ts b/packages/ckeditor5/src/plugins/todo_list_multistate/todo_list_multistate.spec.ts
index d1ee875d5b..86e8986d0d 100644
--- a/packages/ckeditor5/src/plugins/todo_list_multistate/todo_list_multistate.spec.ts
+++ b/packages/ckeditor5/src/plugins/todo_list_multistate/todo_list_multistate.spec.ts
@@ -1,28 +1,17 @@
import { ClassicEditor, Essentials, Paragraph } from "ckeditor5";
-import { afterEach, beforeEach, describe, expect, it } from "vitest";
+import { beforeEach, describe, expect, it } from "vitest";
+import { createTestEditor } from "../../../test/editor-kit.js";
import TodoListMultistate from "./todo_list_multistate.js";
import TodoListMultistateEditing from "./todo_list_multistate_editing.js";
import TodoListMultistateToolbar from "./todo_list_multistate_toolbar.js";
import TodoListMultistateUI from "./todo_list_multistate_ui.js";
describe("TodoListMultistate", () => {
- let editorElement: HTMLDivElement;
let editor: ClassicEditor;
beforeEach(async () => {
- editorElement = document.createElement("div");
- document.body.appendChild(editorElement);
-
- editor = await ClassicEditor.create(editorElement, {
- licenseKey: "GPL",
- plugins: [Essentials, Paragraph, TodoListMultistate]
- });
- });
-
- afterEach(async () => {
- editorElement.remove();
- await editor.destroy();
+ editor = await createTestEditor([Essentials, Paragraph, TodoListMultistate]);
});
it("loads the plugin", () => {
diff --git a/packages/ckeditor5/src/plugins/todo_list_multistate/todo_list_multistate_editing.spec.ts b/packages/ckeditor5/src/plugins/todo_list_multistate/todo_list_multistate_editing.spec.ts
index 6056155517..2e2ab2034d 100644
--- a/packages/ckeditor5/src/plugins/todo_list_multistate/todo_list_multistate_editing.spec.ts
+++ b/packages/ckeditor5/src/plugins/todo_list_multistate/todo_list_multistate_editing.spec.ts
@@ -1,8 +1,9 @@
import { type TaskStateDef } from "@triliumnext/commons";
import { Tooltip } from "bootstrap";
import { _setModelData as setModelData, ClassicEditor, Essentials, keyCodes, List, Paragraph, TodoList, type ModelElement } from "ckeditor5";
-import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+import { beforeEach, describe, expect, it, vi } from "vitest";
+import { createTestEditor } from "../../../test/editor-kit.js";
import TodoListMultistateEditing, { getActiveTaskStates, getConfiguredTaskStates, TASK_STATE_ATTRIBUTE } from "./todo_list_multistate_editing.js";
const TODO_LIST_CHECKED_ATTRIBUTE = "todoListChecked";
@@ -16,15 +17,8 @@ const CUSTOM_STATES: TaskStateDef[] = [
const TODO_FIXTURE = '<paragraph listIndent="0" listItemId="t-a" listType="todo">Task[]</paragraph>';
-async function createEditor(config: Record<string, unknown> = {}): Promise<{ editorElement: HTMLDivElement; editor: ClassicEditor }> {
- const editorElement = document.createElement("div");
- document.body.appendChild(editorElement);
- const editor = await ClassicEditor.create(editorElement, {
- licenseKey: "GPL",
- plugins: [Essentials, Paragraph, List, TodoList, TodoListMultistateEditing],
- ...config
- });
- return { editorElement, editor };
+async function createEditor(config: Record<string, unknown> = {}): Promise<ClassicEditor> {
+ return await createTestEditor([Essentials, Paragraph, List, TodoList, TodoListMultistateEditing], config);
}
function getBlock(editor: ClassicEditor, index: number): ModelElement {
@@ -46,17 +40,11 @@ function pressCtrlShiftEnter(editor: ClassicEditor): void {
}
describe("TodoListMultistateEditing", () => {
- let editorElement: HTMLDivElement;
let editor: ClassicEditor;
- afterEach(async () => {
- editorElement.remove();
- await editor.destroy();
- });
-
describe("with custom configured states", () => {
beforeEach(async () => {
- ({ editorElement, editor } = await createEditor({ taskStates: CUSTOM_STATES }));
+ editor = await createEditor({ taskStates: CUSTOM_STATES });
setModelData(editor.model, TODO_FIXTURE);
});
@@ -322,7 +310,7 @@ describe("TodoListMultistateEditing", () => {
describe("post-fixer state-to-checkbox forcing on direct attribute writes", () => {
beforeEach(async () => {
- ({ editorElement, editor } = await createEditor({ taskStates: CUSTOM_STATES }));
+ editor = await createEditor({ taskStates: CUSTOM_STATES });
setModelData(editor.model, TODO_FIXTURE);
});
@@ -337,7 +325,7 @@ describe("TodoListMultistateEditing", () => {
describe("with default (no) config and no translate provider", () => {
beforeEach(async () => {
- ({ editorElement, editor } = await createEditor());
+ editor = await createEditor();
setModelData(editor.model, TODO_FIXTURE);
});
@@ -367,10 +355,10 @@ describe("TodoListMultistateEditing", () => {
beforeEach(async () => {
translate = vi.fn((key: string) => `T:${key}`);
- ({ editorElement, editor } = await createEditor({
+ editor = await createEditor({
taskStates: CUSTOM_STATES,
translate
- }));
+ });
setModelData(editor.model, TODO_FIXTURE);
});
diff --git a/packages/ckeditor5/src/plugins/todo_list_multistate/todo_list_multistate_toolbar.spec.ts b/packages/ckeditor5/src/plugins/todo_list_multistate/todo_list_multistate_toolbar.spec.ts
index 56cf24ac09..95bf512c5a 100644
--- a/packages/ckeditor5/src/plugins/todo_list_multistate/todo_list_multistate_toolbar.spec.ts
+++ b/packages/ckeditor5/src/plugins/todo_list_multistate/todo_list_multistate_toolbar.spec.ts
@@ -8,8 +8,9 @@ import {
type ModelElement,
type ViewElement
} from "ckeditor5";
-import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+import { describe, expect, it, vi } from "vitest";
+import { createTestEditor, getEditorElement } from "../../../test/editor-kit.js";
import TodoListMultistateToolbar from "./todo_list_multistate_toolbar.js";
import TodoListMultistateUI from "./todo_list_multistate_ui.js";
@@ -18,29 +19,21 @@ const TODO_FIXTURE =
'<paragraph listIndent="0" listItemId="todo-b" listType="todo">Second</paragraph>';
describe("TodoListMultistateToolbar", () => {
- let editorElement: HTMLDivElement;
let editor: ClassicEditor;
async function createEditor(config: Record<string, unknown> = {}) {
- editorElement = document.createElement("div");
- document.body.appendChild(editorElement);
- editor = await ClassicEditor.create(editorElement, {
- licenseKey: "GPL",
- plugins: [Essentials, Paragraph, List, TodoListMultistateUI, TodoListMultistateToolbar],
- ...config
- });
+ editor = await createTestEditor(
+ [Essentials, Paragraph, List, TodoListMultistateUI, TodoListMultistateToolbar],
+ config
+ );
setModelData(editor.model, TODO_FIXTURE);
// Make the editor visible & away from the viewport edge so positioning/DOM lookups work.
+ const editorElement = getEditorElement(editor);
editorElement.style.marginLeft = "120px";
editorElement.style.marginTop = "60px";
return editor;
}
- afterEach(async () => {
- editorElement.remove();
- await editor.destroy();
- });
-
function getCheckbox(index = 0): HTMLInputElement {
const domRoot = editor.editing.view.getDomRoot();
const inputs = domRoot ? Array.from(domRoot.querySelectorAll<HTMLInputElement>(".todo-list__label input[type=\"checkbox\"]")) : [];
diff --git a/packages/ckeditor5/src/plugins/todo_list_multistate/todo_list_multistate_ui.spec.ts b/packages/ckeditor5/src/plugins/todo_list_multistate/todo_list_multistate_ui.spec.ts
index 529cb9b65b..1e532f346d 100644
--- a/packages/ckeditor5/src/plugins/todo_list_multistate/todo_list_multistate_ui.spec.ts
+++ b/packages/ckeditor5/src/plugins/todo_list_multistate/todo_list_multistate_ui.spec.ts
@@ -1,7 +1,8 @@
import { DEFAULT_TASK_STATES, DONE_STATE_NAME, NONE_STATE_NAME, type TaskStateDef } from "@triliumnext/commons";
import { _setModelData as setModelData, ClassicEditor, Essentials, Paragraph, TodoList } from "ckeditor5";
-import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+import { beforeEach, describe, expect, it, vi } from "vitest";
+import { createTestEditor } from "../../../test/editor-kit.js";
import TodoListMultistateEditing, { getActiveTaskStates } from "./todo_list_multistate_editing.js";
import TodoListMultistateUI from "./todo_list_multistate_ui.js";
@@ -10,22 +11,10 @@ const TODO_FIXTURE =
'<paragraph listIndent="0" listItemId="i-a" listType="todo">Task[]</paragraph>';
describe("TodoListMultistateUI", () => {
- let editorElement: HTMLDivElement;
let editor: ClassicEditor;
beforeEach(async () => {
- editorElement = document.createElement("div");
- document.body.appendChild(editorElement);
-
- editor = await ClassicEditor.create(editorElement, {
- licenseKey: "GPL",
- plugins: [Essentials, Paragraph, TodoList, TodoListMultistateEditing, TodoListMultistateUI]
- });
- });
-
- afterEach(async () => {
- editorElement.remove();
- await editor.destroy();
+ editor = await createTestEditor([Essentials, Paragraph, TodoList, TodoListMultistateEditing, TodoListMultistateUI]);
});
it("loads the plugin", () => {
@@ -191,22 +180,14 @@ describe("TodoListMultistateUI", () => {
stateWithNoTitle
];
- const el = document.createElement("div");
- document.body.appendChild(el);
- const customEditor = await ClassicEditor.create(el, {
- licenseKey: "GPL",
- plugins: [Essentials, Paragraph, TodoList, TodoListMultistateEditing, TodoListMultistateUI],
- taskStates: customStates
- } as unknown as Parameters<typeof ClassicEditor.create>[1]);
-
- try {
- const button = customEditor.ui.componentFactory.create(`taskState:${stateWithNoTitle.name}`) as {
- label: string;
- };
- expect(button.label).toBe(stateWithNoTitle.name);
- } finally {
- el.remove();
- await customEditor.destroy();
- }
+ const customEditor = await createTestEditor(
+ [Essentials, Paragraph, TodoList, TodoListMultistateEditing, TodoListMultistateUI],
+ { taskStates: customStates } as unknown as Parameters<typeof ClassicEditor.create>[1]
+ );
+
+ const button = customEditor.ui.componentFactory.create(`taskState:${stateWithNoTitle.name}`) as unknown as {
+ label: string;
+ };
+ expect(button.label).toBe(stateWithNoTitle.name);
});
});
diff --git a/packages/ckeditor5/src/plugins/uploadimage.spec.ts b/packages/ckeditor5/src/plugins/uploadimage.spec.ts
index 151bbcce28..485c0d69f6 100644
--- a/packages/ckeditor5/src/plugins/uploadimage.spec.ts
+++ b/packages/ckeditor5/src/plugins/uploadimage.spec.ts
@@ -1,6 +1,7 @@
import { ClassicEditor, Essentials, FileRepository, Paragraph, type FileLoader, type UploadAdapter } from "ckeditor5";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+import { createTestEditor } from "../../test/editor-kit.js";
import UploadimagePlugin from "./uploadimage.js";
/**
@@ -73,7 +74,6 @@ function createFakeLoader(file: File | null): FileLoader {
}
describe("UploadimagePlugin", () => {
- let editorElement: HTMLDivElement;
let editor: ClassicEditor;
let getHeaders: ReturnType<typeof vi.fn>;
let originalXHR: typeof window.XMLHttpRequest;
@@ -89,20 +89,12 @@ describe("UploadimagePlugin", () => {
window.XMLHttpRequest = FakeXHR as unknown as typeof window.XMLHttpRequest;
FakeXHR.last = undefined;
- editorElement = document.createElement("div");
- document.body.appendChild(editorElement);
-
- editor = await ClassicEditor.create(editorElement, {
- licenseKey: "GPL",
- plugins: [Essentials, Paragraph, FileRepository, UploadimagePlugin]
- });
+ editor = await createTestEditor([Essentials, Paragraph, FileRepository, UploadimagePlugin]);
});
- afterEach(async () => {
+ afterEach(() => {
delete (globalThis as { glob?: unknown }).glob;
window.XMLHttpRequest = originalXHR;
- editorElement.remove();
- await editor.destroy();
});
function createAdapter(file: File | null): UploadAdapter {
diff --git a/packages/ckeditor5/src/plugins/widget_utils.spec.ts b/packages/ckeditor5/src/plugins/widget_utils.spec.ts
index d37275f25b..519cf1db89 100644
--- a/packages/ckeditor5/src/plugins/widget_utils.spec.ts
+++ b/packages/ckeditor5/src/plugins/widget_utils.spec.ts
@@ -7,8 +7,9 @@ import {
Widget,
_setModelData as setModelData,
} from "ckeditor5";
-import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+import { beforeEach, describe, expect, it, vi } from "vitest";
+import { createTestEditor } from "../../test/editor-kit.js";
import { preventCKEditorHandling } from "./widget_utils.js";
/**
@@ -54,22 +55,10 @@ class TestBoxPlugin extends Plugin {
}
describe("preventCKEditorHandling", () => {
- let editorElement: HTMLDivElement;
let editor: ClassicEditor;
beforeEach(async () => {
- editorElement = document.createElement("div");
- document.body.appendChild(editorElement);
-
- editor = await ClassicEditor.create(editorElement, {
- licenseKey: "GPL",
- plugins: [Essentials, Paragraph, TestBoxPlugin],
- });
- });
-
- afterEach(async () => {
- editorElement.remove();
- await editor.destroy();
+ editor = await createTestEditor([Essentials, Paragraph, TestBoxPlugin]);
});
it("attaches event listeners without throwing", () => {
diff --git a/packages/ckeditor5/src/translation_overrides.spec.ts b/packages/ckeditor5/src/translation_overrides.spec.ts
index cb5fc8a6b9..cc547f0154 100644
--- a/packages/ckeditor5/src/translation_overrides.spec.ts
+++ b/packages/ckeditor5/src/translation_overrides.spec.ts
@@ -1,29 +1,20 @@
import { Bookmark, ClassicEditor, Essentials, Paragraph } from "ckeditor5";
-import { afterEach, beforeEach, describe, expect, it } from "vitest";
+import { beforeEach, describe, expect, it } from "vitest";
+import { createTestEditor } from "../test/editor-kit.js";
// Side effect: assigns window.CKEDITOR_TRANSLATIONS, the global CKEditor reads to relabel its
// built-in strings. Imported before any editor is created so the override is in place.
import "./translation_overrides.js";
describe("translation overrides", () => {
- let editorElement: HTMLDivElement;
let editor: ClassicEditor;
beforeEach(async () => {
- editorElement = document.createElement("div");
- document.body.appendChild(editorElement);
- editor = await ClassicEditor.create(editorElement, {
- licenseKey: "GPL",
- plugins: [Essentials, Paragraph, Bookmark],
+ editor = await createTestEditor([Essentials, Paragraph, Bookmark], {
toolbar: { items: ["bookmark"] }
});
});
- afterEach(async () => {
- editorElement.remove();
- await editor.destroy();
- });
-
it("are picked up by the editor UI (Bookmark relabelled to Anchor)", () => {
const labels = toolbarLabels(editor);
expect(labels).toContain("Anchor");
diff --git a/packages/ckeditor5/src/utils.spec.ts b/packages/ckeditor5/src/utils.spec.ts
index 528c882bf7..5bd5b5b6a2 100644
--- a/packages/ckeditor5/src/utils.spec.ts
+++ b/packages/ckeditor5/src/utils.spec.ts
@@ -1,6 +1,7 @@
import { _setModelData as setModelData, ClassicEditor, Essentials, Heading, Paragraph, type DifferItemAttribute } from "ckeditor5";
-import { afterEach, beforeEach, describe, expect, it } from "vitest";
+import { beforeEach, describe, expect, it } from "vitest";
+import { createTestEditor } from "../test/editor-kit.js";
import { attributeChangeAffectsHeading, escapeHtml } from "./utils.js";
describe("escapeHtml", () => {
@@ -36,22 +37,10 @@ describe("escapeHtml", () => {
});
describe("attributeChangeAffectsHeading", () => {
- let editorElement: HTMLDivElement;
let editor: ClassicEditor;
beforeEach(async () => {
- editorElement = document.createElement("div");
- document.body.appendChild(editorElement);
-
- editor = await ClassicEditor.create(editorElement, {
- licenseKey: "GPL",
- plugins: [Essentials, Paragraph, Heading]
- });
- });
-
- afterEach(async () => {
- editorElement.remove();
- await editor.destroy();
+ editor = await createTestEditor([Essentials, Paragraph, Heading]);
});
it("returns false when change.type is not 'attribute'", () => {
diff --git a/packages/ckeditor5/test/editor-kit.ts b/packages/ckeditor5/test/editor-kit.ts
new file mode 100644
index 0000000000..524ff9a356
--- /dev/null
+++ b/packages/ckeditor5/test/editor-kit.ts
@@ -0,0 +1,75 @@
+import { ClassicEditor } from "ckeditor5";
+
+/**
+ * Shared editor lifecycle helpers for the spec suite.
+ *
+ * Every spec used to repeat the same `document.createElement("div")` +
+ * `document.body.appendChild(...)` + `ClassicEditor.create({ licenseKey: "GPL", ... })`
+ * scaffold in a `beforeEach`, plus a matching `editorElement.remove()` + `editor.destroy()`
+ * in an `afterEach`. `createTestEditor()` collapses the setup to a single call and records
+ * the editor so the global `afterEach` in `setup.ts` can tear it down — so specs no longer
+ * need an `afterEach` for editor cleanup at all.
+ *
+ * This file lives outside `src/`, so it is excluded from both the production build
+ * (tsconfig.lib.json compiles `src/**`) and the 100% coverage gate (coverage instruments
+ * `src/**` only) without any config carve-out.
+ */
+
+type EditorCreateConfig = NonNullable<Parameters<typeof ClassicEditor.create>[1]>;
+
+interface TrackedEditor {
+ editor: ClassicEditor;
+ editorElement: HTMLDivElement;
+}
+
+const trackedEditors: TrackedEditor[] = [];
+
+/**
+ * Create a real `ClassicEditor` over a fresh host element, with `licenseKey: "GPL"` and the
+ * given plugins. Any extra editor config (e.g. `balloonToolbar`, `toolbar`) can be passed via
+ * `extraConfig`. The editor is tracked for automatic teardown; use {@link getEditorElement} to
+ * retrieve the host element for the spec that needs it.
+ */
+export async function createTestEditor(
+ plugins: EditorCreateConfig["plugins"],
+ extraConfig: Omit<EditorCreateConfig, "plugins"> = {}
+): Promise<ClassicEditor> {
+ const editorElement = document.createElement("div");
+ document.body.appendChild(editorElement);
+
+ const editor = await ClassicEditor.create(editorElement, {
+ licenseKey: "GPL",
+ plugins,
+ ...extraConfig
+ });
+
+ trackedEditors.push({ editor, editorElement });
+ return editor;
+}
+
+/**
+ * Return the host `<div>` an editor was created over via {@link createTestEditor}. Throws if the
+ * editor was not created through the kit, so callers narrow the element without a non-null `!`.
+ */
+export function getEditorElement(editor: ClassicEditor): HTMLDivElement {
+ const tracked = trackedEditors.find((entry) => entry.editor === editor);
+ if (!tracked) {
+ throw new Error("Editor was not created via createTestEditor(); no host element is tracked.");
+ }
+ return tracked.editorElement;
+}
+
+/**
+ * Destroy every editor created via {@link createTestEditor} and remove its host element.
+ * Called from the global `afterEach` in `setup.ts`; safe to call when nothing is tracked.
+ */
+export async function destroyTrackedEditors(): Promise<void> {
+ const editors = trackedEditors.splice(0);
+ for (const { editor, editorElement } of editors) {
+ try {
+ await editor.destroy();
+ } finally {
+ editorElement.remove();
+ }
+ }
+}
diff --git a/packages/ckeditor5/test/setup.ts b/packages/ckeditor5/test/setup.ts
new file mode 100644
index 0000000000..6b8d08e9d9
--- /dev/null
+++ b/packages/ckeditor5/test/setup.ts
@@ -0,0 +1,12 @@
+import { afterEach } from "vitest";
+
+import { destroyTrackedEditors } from "./editor-kit.js";
+
+/**
+ * Global test setup, registered via `setupFiles` in vitest.config.ts and run before every
+ * spec. Owns the editor teardown that each spec used to repeat: any editor created through
+ * `createTestEditor()` is destroyed and its host element removed after every test.
+ */
+afterEach(async () => {
+ await destroyTrackedEditors();
+});
diff --git a/packages/ckeditor5/vitest.config.ts b/packages/ckeditor5/vitest.config.ts
index 235985fc44..a576ead55f 100644
--- a/packages/ckeditor5/vitest.config.ts
+++ b/packages/ckeditor5/vitest.config.ts
@@ -11,6 +11,7 @@ export default defineConfig({
instances: [{ browser: "chrome" }]
},
include: ["src/**/*.spec.ts"],
+ setupFiles: ["./test/setup.ts"],
globals: true,
watch: false,
reporters: ["default", ["junit", { outputFile: "./test-output/vitest/junit.xml", addFileAttribute: true }]],