Skip to content

Build your personal knowledge base with Trilium Notes

test(ckeditor5): assert real behavior in plugin specs (review follow-up)

Elian Dorand71fb40b1fd5
diff --git a/apps/client/src/widgets/type_widgets/text/toolbar.spec.ts b/apps/client/src/widgets/type_widgets/text/toolbar.spec.ts
index 0f17ce3480..5888d179b9 100644
--- a/apps/client/src/widgets/type_widgets/text/toolbar.spec.ts
+++ b/apps/client/src/widgets/type_widgets/text/toolbar.spec.ts
@@ -1,8 +1,20 @@
-import { describe, expect, it } from "vitest";
-import { buildClassicToolbar, buildFloatingToolbar } from "./toolbar.js";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+import { buildClassicToolbar, buildFloatingToolbar, buildMobileToolbar, buildToolbarConfig } from "./toolbar.js";
type ToolbarConfig = string | "|" | { items: ToolbarConfig[] };
+// `buildToolbarConfig` reads the multiline preference via the options service; stub it so the
+// classic/multiline branch is deterministic. `isMobile()`/`isDesktop()` read `window.glob.device`,
+// which the per-test setup controls directly (no module mock needed for those).
+const optionsState = vi.hoisted(() => ({ values: {} as Record<string, string | undefined> }));
+vi.mock("../../../services/options.js", () => ({
+ default: { get: (name: string) => optionsState.values[name] }
+}));
+
+function setDevice(device: "mobile" | "desktop") {
+ (window as unknown as { glob?: { device?: string } }).glob = { device };
+}
+
describe("CKEditor config", () => {
it("has same toolbar items for fixed and floating", () => {
function traverseItems(config: ToolbarConfig): string[] {
@@ -29,3 +41,56 @@ describe("CKEditor config", () => {
.toStrictEqual([...floatingToolbarAllItems ].toSorted());
});
});
+
+describe("buildClassicToolbar", () => {
+ it("reflects the multiline flag via shouldNotGroupWhenFull", () => {
+ expect(buildClassicToolbar(false).toolbar.shouldNotGroupWhenFull).toBe(false);
+ expect(buildClassicToolbar(true).toolbar.shouldNotGroupWhenFull).toBe(true);
+ });
+});
+
+describe("buildMobileToolbar", () => {
+ it("flattens nested toolbar groups into a single flat item list", () => {
+ const mobile = buildMobileToolbar();
+
+ // Items nested inside group objects (text-formatting, alignment, ...) are hoisted up.
+ expect(mobile.toolbar.items).toContain("underline");
+ expect(mobile.toolbar.items).toContain("alignment:left");
+ // No nested group objects survive the flattening.
+ expect(mobile.toolbar.items.some((item) => typeof item === "object")).toBe(false);
+ });
+});
+
+describe("buildToolbarConfig dispatch", () => {
+ beforeEach(() => {
+ optionsState.values = {};
+ setDevice("desktop");
+ });
+
+ afterEach(() => {
+ delete (window as unknown as { glob?: unknown }).glob;
+ vi.clearAllMocks();
+ });
+
+ it("returns the flattened mobile toolbar on a mobile device", () => {
+ setDevice("mobile");
+ const config = buildToolbarConfig(true);
+ expect(config.toolbar.items.every((item) => typeof item === "string")).toBe(true);
+ });
+
+ it("returns the multiline classic toolbar when on desktop and the option is enabled", () => {
+ optionsState.values["textNoteEditorMultilineToolbar"] = "true";
+ const config = buildToolbarConfig(true) as ReturnType<typeof buildClassicToolbar>;
+ expect(config.toolbar.shouldNotGroupWhenFull).toBe(true);
+ });
+
+ it("returns the single-line classic toolbar when the multiline option is not enabled", () => {
+ const config = buildToolbarConfig(true) as ReturnType<typeof buildClassicToolbar>;
+ expect(config.toolbar.shouldNotGroupWhenFull).toBe(false);
+ });
+
+ it("returns the floating toolbar (with a block toolbar) when not in classic mode", () => {
+ const config = buildToolbarConfig(false);
+ expect("blockToolbar" in config).toBe(true);
+ });
+});
diff --git a/packages/ckeditor5/src/plugins.spec.ts b/packages/ckeditor5/src/plugins.spec.ts
index d02bf085cd..93aef29468 100644
--- a/packages/ckeditor5/src/plugins.spec.ts
+++ b/packages/ckeditor5/src/plugins.spec.ts
@@ -1,5 +1,15 @@
+import { BlockToolbar } from "ckeditor5";
import { describe, expect, it } from "vitest";
-import { CORE_PLUGINS, COMMON_PLUGINS, POPUP_EDITOR_PLUGINS, loadPremiumPlugins } from "./plugins.js";
+
+import { Admonition } from "@triliumnext/ckeditor5-admonition";
+import { COMMON_PLUGINS, CORE_PLUGINS, loadPremiumPlugins, POPUP_EDITOR_PLUGINS } from "./plugins.js";
+import CutToNotePlugin from "./plugins/cuttonote.js";
+import Uploadfileplugin from "./plugins/file_upload/uploadfileplugin.js";
+import IncludeNote from "./plugins/includenote.js";
+import InternalLinkPlugin from "./plugins/internallink.js";
+import LinkEmbed from "./plugins/linkembed.js";
+import MentionCustomization from "./plugins/mention_customization.js";
+import ReferenceLink from "./plugins/referencelink.js";
describe("plugin lists", () => {
it("CORE_PLUGINS is a non-empty array", () => {
@@ -46,6 +56,33 @@ describe("plugin lists", () => {
expect(typeof plugin).toBe("function");
}
});
+
+ // The shape checks above are tautological for catching a *dropped* registration: because
+ // COMMON_PLUGINS is built by spreading CORE_PLUGINS (and POPUP by spreading COMMON), removing
+ // a plugin shrinks both lists and the superset loops still pass. These presence assertions pin
+ // specific load-bearing plugins so deleting a registration line in plugins.ts turns the suite red.
+
+ it("CORE_PLUGINS includes the Trilium-specific core plugins", () => {
+ expect(CORE_PLUGINS).toContain(MentionCustomization);
+ expect(CORE_PLUGINS).toContain(ReferenceLink);
+ });
+
+ it("COMMON_PLUGINS includes the in-tree Trilium feature plugins", () => {
+ expect(COMMON_PLUGINS).toContain(CutToNotePlugin);
+ expect(COMMON_PLUGINS).toContain(InternalLinkPlugin);
+ expect(COMMON_PLUGINS).toContain(IncludeNote);
+ expect(COMMON_PLUGINS).toContain(LinkEmbed);
+ expect(COMMON_PLUGINS).toContain(Uploadfileplugin);
+ });
+
+ it("COMMON_PLUGINS includes the external widget plugins", () => {
+ expect(COMMON_PLUGINS).toContain(Admonition);
+ });
+
+ it("POPUP_EDITOR_PLUGINS adds BlockToolbar on top of COMMON_PLUGINS", () => {
+ expect(POPUP_EDITOR_PLUGINS).toContain(BlockToolbar);
+ expect(COMMON_PLUGINS).not.toContain(BlockToolbar);
+ });
});
describe("loadPremiumPlugins", () => {
diff --git a/packages/ckeditor5/src/plugins/code_block_toolbar.spec.ts b/packages/ckeditor5/src/plugins/code_block_toolbar.spec.ts
index e945faccac..4525d37fd4 100644
--- a/packages/ckeditor5/src/plugins/code_block_toolbar.spec.ts
+++ b/packages/ckeditor5/src/plugins/code_block_toolbar.spec.ts
@@ -108,44 +108,17 @@ describe("CodeBlockToolbar — with BalloonToolbar", () => {
setModelData(editor.model, "<codeBlock language=\"plaintext\">foo[]bar</codeBlock>");
const balloonToolbar = editor.plugins.get("BalloonToolbar") as unknown as {
- fire(event: string, ...args: unknown[]): unknown;
- };
-
- // Spy on the fire call; the listener should call evt.stop() which prevents
- // further propagation. We simulate the show event and check it is stopped.
- const stopSpy = vi.fn();
- const fakeEvt = { stop: stopSpy };
-
- // Directly fire on the BalloonToolbar observable to trigger our listener.
- const bToolbar = editor.plugins.get("BalloonToolbar") as unknown as {
- fire(name: string, evt: unknown, ...args: unknown[]): void;
- };
-
- // The listener uses `editor.listenTo(bToolbar, "show", ...)` with high priority.
- // We can fire the "show" event on the balloonToolbar to exercise the listener.
- editor.fire("test-show-proxy"); // Warm-up — not the real path.
-
- // Use the CKEditor event system to fire "show" with a stoppable event info.
- // CKEditor passes EventInfo as the first arg; we need to invoke our callback.
- // The safest way: access the listener registered via listenTo by firing through
- // the observable itself.
- const observable = editor.plugins.get("BalloonToolbar") as unknown as {
- _events?: Record<string, unknown>;
+ on(evt: string, fn: () => void): void;
fire(name: string, ...args: unknown[]): void;
};
- // Fire "show" — the high-priority listener will call evt.stop() on the EventInfo.
- // We cannot easily intercept the EventInfo, so instead we verify via a spy on
- // the BalloonToolbar#show to confirm it fires but the balloon toolbar itself
- // is suppressed (no balloon rendered):
+ // The plugin registers a high-priority "show" listener that calls evt.stop() when the
+ // selection is inside a code block. CKEditor fires listeners high -> normal, so a
+ // default-priority listener added here must never run once the event is stopped.
const showSpy = vi.fn();
- (editor.plugins.get("BalloonToolbar") as unknown as { on(evt: string, fn: () => void): void })
- .on?.("show", showSpy);
-
- observable.fire("show");
+ balloonToolbar.on("show", showSpy);
+ balloonToolbar.fire("show");
- // Our high-priority listener stopped the event, so lower-priority listeners
- // (like showSpy registered above at default priority) should not fire.
expect(showSpy).not.toHaveBeenCalled();
});
diff --git a/packages/ckeditor5/src/plugins/inline_code_toolbar.spec.ts b/packages/ckeditor5/src/plugins/inline_code_toolbar.spec.ts
index 8e5c089e4a..cc71864d6d 100644
--- a/packages/ckeditor5/src/plugins/inline_code_toolbar.spec.ts
+++ b/packages/ckeditor5/src/plugins/inline_code_toolbar.spec.ts
@@ -5,6 +5,11 @@ import { createTestEditor } from "../../test/editor-kit.js";
import CopyToClipboardButton from "./copy_to_clipboard_button.js";
import InlineCodeToolbar from "./inline_code_toolbar.js";
+/** The toolbar definition shape we reach into (WidgetToolbarRepository keeps these private). */
+interface ToolbarDefinition {
+ getRelatedElement: (selection: unknown) => { is(type: string, name: string): boolean } | null;
+}
+
describe("InlineCodeToolbar", () => {
let editor: ClassicEditor;
@@ -23,172 +28,47 @@ describe("InlineCodeToolbar", () => {
});
it("registers the inlineCode toolbar in the WidgetToolbarRepository", () => {
- // The toolbar is registered during afterInit — verify the registration exists
- // by checking that WidgetToolbarRepository loaded and that the toolbar items are set
- const widgetToolbarRepository = editor.plugins.get(WidgetToolbarRepository);
- expect(widgetToolbarRepository).toBeDefined();
+ expect(getInlineCodeDefinition()).toBeDefined();
});
+ // These exercise the plugin's actual getRelatedElement callback (registered in
+ // WidgetToolbarRepository), rather than a re-implementation of the same walk — so a regression
+ // in inline_code_toolbar.ts itself fails the suite.
describe("getRelatedElement", () => {
- // Access the registered toolbar config via WidgetToolbarRepository internals
- function getRelatedElement(selection: unknown): unknown {
- // We call getRelatedElement with the view document selection to exercise the logic.
- // Re-create a mock selection that exposes getFirstPosition().
- const viewSelection = editor.editing.view.document.selection;
- return (viewSelection as unknown as { getFirstPosition: () => unknown }).getFirstPosition;
- }
-
- it("returns the code element when cursor is inside inline code", () => {
- // Place cursor inside inline code
+ it("returns the <code> attribute element when the selection is inside inline code", () => {
setModelData(editor.model, "<paragraph><$text code=\"true\">hello[]world</$text></paragraph>");
- // The view selection should now be inside a <code> element
- const viewSelection = editor.editing.view.document.selection;
- const firstPosition = viewSelection.getFirstPosition();
- expect(firstPosition).not.toBeNull();
-
- if (!firstPosition) {
- return;
- }
-
- // Walk parent chain manually as the plugin does
- let parent = firstPosition.parent;
- let foundCode = false;
- while (parent) {
- if (parent.is("attributeElement", "code")) {
- foundCode = true;
- break;
- }
- parent = parent.parent;
- }
-
- expect(foundCode).toBe(true);
+ const def = getInlineCodeDefinition();
+ expect(def).toBeDefined();
+
+ const related = def?.getRelatedElement(editor.editing.view.document.selection);
+ expect(related).not.toBeNull();
+ expect(related?.is("attributeElement", "code")).toBe(true);
});
- it("returns null (no code element found) when cursor is in plain text", () => {
- setModelData(editor.model, "<paragraph>plain[]text</paragraph>");
+ it("walks up multiple ancestor levels (bold+code) to find the <code> element", () => {
+ setModelData(editor.model, "<paragraph><$text bold=\"true\" code=\"true\">bold[]code</$text></paragraph>");
- const viewSelection = editor.editing.view.document.selection;
- const firstPosition = viewSelection.getFirstPosition();
- expect(firstPosition).not.toBeNull();
-
- if (!firstPosition) {
- return;
- }
-
- // Walk parent chain as the plugin does — should not find code
- let parent = firstPosition.parent;
- let foundCode = false;
- while (parent) {
- if (parent.is("attributeElement", "code")) {
- foundCode = true;
- break;
- }
- parent = parent.parent;
- }
-
- expect(foundCode).toBe(false);
+ const related = getInlineCodeDefinition()?.getRelatedElement(editor.editing.view.document.selection);
+ expect(related?.is("attributeElement", "code")).toBe(true);
});
- });
- describe("getRelatedElement via plugin integration", () => {
- it("resolves to non-null when inline code is selected", () => {
- setModelData(editor.model, "<paragraph><$text code=\"true\">inline[]code</$text></paragraph>");
-
- // Verify the view contains a <code> attributeElement
- const viewSelection = editor.editing.view.document.selection;
- const firstPosition = viewSelection.getFirstPosition();
- expect(firstPosition).not.toBeNull();
-
- if (!firstPosition) {
- return;
- }
-
- // The parent should be a <code> attributeElement somewhere in the tree
- let node = firstPosition.parent;
- let found = false;
- while (node) {
- if (node.is("attributeElement", "code")) {
- found = true;
- break;
- }
- node = node.parent;
- }
- expect(found).toBe(true);
- });
+ it("returns null when the selection is in plain text", () => {
+ setModelData(editor.model, "<paragraph>plain[]text</paragraph>");
- it("resolves to null when selection is in plain paragraph text", () => {
- setModelData(editor.model, "<paragraph>no[]code</paragraph>");
-
- const viewSelection = editor.editing.view.document.selection;
- const firstPosition = viewSelection.getFirstPosition();
- expect(firstPosition).not.toBeNull();
-
- if (!firstPosition) {
- return;
- }
-
- let node = firstPosition.parent;
- let found = false;
- while (node) {
- if (node.is("attributeElement", "code")) {
- found = true;
- break;
- }
- node = node.parent;
- }
- expect(found).toBe(false);
+ const related = getInlineCodeDefinition()?.getRelatedElement(editor.editing.view.document.selection);
+ expect(related).toBeNull();
});
- it("walks multiple levels up the parent chain to find a code element", () => {
- // Place selection inside inline code mixed with bold to create nested attribute elements
- setModelData(editor.model,
- "<paragraph><$text bold=\"true\" code=\"true\">bold[]code</$text></paragraph>");
-
- const viewSelection = editor.editing.view.document.selection;
- const firstPosition = viewSelection.getFirstPosition();
- expect(firstPosition).not.toBeNull();
-
- if (!firstPosition) {
- return;
- }
-
- // Must find code element at some ancestor level
- let node = firstPosition.parent;
- let found = false;
- while (node) {
- if (node.is("attributeElement", "code")) {
- found = true;
- break;
- }
- node = node.parent;
- }
- expect(found).toBe(true);
+ it("returns null when the selection has no first position (null guard)", () => {
+ const related = getInlineCodeDefinition()?.getRelatedElement({ getFirstPosition: () => null });
+ expect(related).toBeNull();
});
});
- describe("getRelatedElement null position branch", () => {
- it("handles an empty selection gracefully by not finding a code element", () => {
- // With a fresh editor and no explicit setModelData, getFirstPosition() returns
- // a position in an empty paragraph — not inside <code> — so no code element found.
- const viewSelection = editor.editing.view.document.selection;
- const firstPosition = viewSelection.getFirstPosition();
-
- if (!firstPosition) {
- // The null-position path is exercised: nothing to assert further
- return;
- }
-
- let node = firstPosition.parent;
- let found = false;
- while (node) {
- if (node.is("attributeElement", "code")) {
- found = true;
- break;
- }
- node = node.parent;
- }
- expect(found).toBe(false);
- });
- });
+ function getInlineCodeDefinition(): ToolbarDefinition | undefined {
+ const repository = editor.plugins.get(WidgetToolbarRepository);
+ const definitions = (repository as unknown as { _toolbarDefinitions: Map<string, ToolbarDefinition> })._toolbarDefinitions;
+ return definitions?.get("inlineCode");
+ }
});
diff --git a/packages/ckeditor5/src/plugins/strikethrough_as_del.spec.ts b/packages/ckeditor5/src/plugins/strikethrough_as_del.spec.ts
index 746ee73750..813bf85a7a 100644
--- a/packages/ckeditor5/src/plugins/strikethrough_as_del.spec.ts
+++ b/packages/ckeditor5/src/plugins/strikethrough_as_del.spec.ts
@@ -28,20 +28,20 @@ describe("StrikethroughAsDel", () => {
expect(data).toContain("<del>");
});
- it("upcasts <del> from HTML so the model has strikethrough attribute", () => {
+ it("upcasts <del> from HTML so the model text node carries the strikethrough attribute", () => {
editor.setData("<p><del>test</del></p>");
const root = editor.model.document.getRoot();
if (!root) {
throw new Error("Model root not found");
}
const paragraph = root.getChild(0);
- if (!paragraph) {
+ if (!paragraph?.is("element")) {
throw new Error("Paragraph not found");
}
- // After upcast (via Strikethrough plugin), the text should have strikethrough attribute
- // and re-downcast via our plugin to <del>
- const output = editor.getData();
- expect(output).toContain("<del>test</del>");
+ // The Strikethrough plugin upcasts <del> to the `strikethrough` model attribute on the text.
+ const textNode = paragraph.getChild(0);
+ expect(textNode?.is("$text")).toBe(true);
+ expect(textNode?.getAttribute("strikethrough")).toBe(true);
});
it("produces <del> wrapping when strikethrough spans partial text", () => {