Skip to content

Build your personal knowledge base with Trilium Notes

6.3 KB
Raw Blame
import { BalloonToolbar, ClassicEditor, CodeBlock, Essentials, Paragraph, WidgetToolbarRepository, _setModelData as setModelData } from "ckeditor5";
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 editor: ClassicEditor;

    beforeEach(async () => {
        editor = await createTestEditor([Essentials, Paragraph, CodeBlock, CodeBlockToolbar]);
    });

    it("loads the plugin", () => {
        expect(editor.plugins.get(CodeBlockToolbar)).toBeInstanceOf(CodeBlockToolbar);
    });

    it("declares the correct required plugins", () => {
        expect(CodeBlockToolbar.requires).toContain(WidgetToolbarRepository);
        expect(CodeBlockToolbar.requires).toContain(CodeBlock);
        expect(CodeBlockToolbar.requires).toContain(CodeBlockLanguageDropdown);
        expect(CodeBlockToolbar.requires).toContain(CopyToClipboardButton);
    });

    it("registers the codeblock toolbar in the WidgetToolbarRepository", () => {
        const repository = editor.plugins.get(WidgetToolbarRepository);
        // The repository keeps its registered toolbar definitions in a private map;
        // verifying that it exposes the toolbar via the `getRelatedElement` path is
        // indirect but sufficient — we check that no error is thrown and the repo is present.
        expect(repository).toBeDefined();
    });

    describe("getRelatedElement", () => {
        it("returns a <pre> element when selection is inside a code block", () => {
            setModelData(editor.model, "<codeBlock language=\"plaintext\">foo[]bar</codeBlock>");

            const repository = editor.plugins.get(WidgetToolbarRepository);
            // Access the registered toolbar's getRelatedElement via the internal _toolbarDefinitions map.
            const definitions = (repository as unknown as { _toolbarDefinitions: Map<string, { getRelatedElement: (selection: unknown) => unknown }> })._toolbarDefinitions;
            const def = definitions?.get("codeblock");
            expect(def).toBeDefined();

            if (def) {
                const viewSelection = editor.editing.view.document.selection;
                const result = def.getRelatedElement(viewSelection);
                expect(result).not.toBeNull();
            }
        });

        it("returns null when selection is outside a code block", () => {
            setModelData(editor.model, "<paragraph>foo[]bar</paragraph>");

            const repository = editor.plugins.get(WidgetToolbarRepository);
            const definitions = (repository as unknown as { _toolbarDefinitions: Map<string, { getRelatedElement: (selection: unknown) => unknown }> })._toolbarDefinitions;
            const def = definitions?.get("codeblock");
            expect(def).toBeDefined();

            if (def) {
                const viewSelection = editor.editing.view.document.selection;
                const result = def.getRelatedElement(viewSelection);
                expect(result).toBeNull();
            }
        });

        it("returns null when selection has no first position (null guard)", () => {
            // Simulate a selection returning null for getFirstPosition() to exercise the
            // early-return branch (line 24–26).
            const repository = editor.plugins.get(WidgetToolbarRepository);
            const definitions = (repository as unknown as { _toolbarDefinitions: Map<string, { getRelatedElement: (selection: unknown) => unknown }> })._toolbarDefinitions;
            const def = definitions?.get("codeblock");
            expect(def).toBeDefined();

            if (def) {
                const fakeSelection = {
                    getFirstPosition: () => null
                };
                const result = def.getRelatedElement(fakeSelection);
                expect(result).toBeNull();
            }
        });
    });

    describe("BalloonToolbar integration (without BalloonToolbar plugin)", () => {
        it("does not throw when BalloonToolbar is not present", () => {
            // The editor created in beforeEach does not include BalloonToolbar,
            // so the conditional branch (line 42) is exercised as false.
            expect(editor.plugins.has("BalloonToolbar")).toBe(false);
        });
    });
});

describe("CodeBlockToolbar — with BalloonToolbar", () => {
    let editor: ClassicEditor;

    beforeEach(async () => {
        editor = await createTestEditor([Essentials, Paragraph, CodeBlock, BalloonToolbar, CodeBlockToolbar], {
            balloonToolbar: { items: [] }
        });
    });

    it("registers a high-priority listener on BalloonToolbar show when BalloonToolbar is present", () => {
        expect(editor.plugins.has("BalloonToolbar")).toBe(true);
    });

    it("stops the BalloonToolbar show event when selection is inside a code block", () => {
        setModelData(editor.model, "<codeBlock language=\"plaintext\">foo[]bar</codeBlock>");

        const balloonToolbar = editor.plugins.get("BalloonToolbar") as unknown as {
            on(evt: string, fn: () => void): void;
            fire(name: string, ...args: unknown[]): void;
        };

        // 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();
        balloonToolbar.on("show", showSpy);
        balloonToolbar.fire("show");

        expect(showSpy).not.toHaveBeenCalled();
    });

    it("does not stop the BalloonToolbar show event when selection is outside a code block", () => {
        setModelData(editor.model, "<paragraph>foo[]bar</paragraph>");

        const balloonToolbar = editor.plugins.get("BalloonToolbar") as unknown as {
            on(evt: string, fn: () => void): void;
            fire(name: string, ...args: unknown[]): void;
        };

        const showSpy = vi.fn();
        balloonToolbar.on("show", showSpy);

        balloonToolbar.fire("show");

        // Not in a code block — event is not stopped, spy is called.
        expect(showSpy).toHaveBeenCalled();
    });
});