Skip to content

Build your personal knowledge base with Trilium Notes

test(ckeditor5): cover the Mermaid insert button again

Elian Dorana5c381e43cdf
diff --git a/packages/ckeditor5/src/plugins/ai_assistant/ai_assistant_ui.spec.ts b/packages/ckeditor5/src/plugins/ai_assistant/ai_assistant_ui.spec.ts
index 51a29ce2f3..8a573f2e67 100644
--- a/packages/ckeditor5/src/plugins/ai_assistant/ai_assistant_ui.spec.ts
+++ b/packages/ckeditor5/src/plugins/ai_assistant/ai_assistant_ui.spec.ts
@@ -1039,6 +1039,19 @@ describe("AiAssistantUI toolbar entry", () => {
await vi.waitFor(() => expect(openForm(editor).phase).toBe("review"));
expect(previewHtml(editor)).toBe(DIAGRAM_HTML);
+
+ const mermaid = createMermaidStub();
+ const unlisted = await createTestEditor(
+ [Essentials, Paragraph, CodeBlockEditing, TriliumAiAssistant], {
+ aiAssistant: { sanitizeHtml, quickActions: DIAGRAM_ACTION, stream: stub.stream },
+ contentEmbed: { renderInlinePreview: mermaid.render }
+ });
+ setModelData(unlisted.model, "<paragraph>[foo]</paragraph>");
+ quickActionButtons(createComponent(unlisted))[0].fire("execute");
+
+ await vi.waitFor(() => expect(openForm(unlisted).phase).toBe("review"));
+ expect(previewHtml(unlisted)).toBe(DIAGRAM_HTML);
+ expect(mermaid.render).not.toHaveBeenCalled();
});
it("leaves the code block alone in the diff, whose text is two responses at once", async () => {
diff --git a/packages/ckeditor5/src/plugins/mermaid/mermaid_ui.spec.ts b/packages/ckeditor5/src/plugins/mermaid/mermaid_ui.spec.ts
new file mode 100644
index 0000000000..1f2f91ac30
--- /dev/null
+++ b/packages/ckeditor5/src/plugins/mermaid/mermaid_ui.spec.ts
@@ -0,0 +1,96 @@
+import { NEW_MERMAID_CONTENT } from "@triliumnext/commons";
+import {
+ ButtonView,
+ ClassicEditor,
+ DropdownView,
+ Essentials,
+ type ListItemView,
+ Paragraph,
+ _setModelData as setModelData,
+ SplitButtonView,
+ Widget
+} from "ckeditor5";
+import { beforeEach, describe, expect, it, vi } from "vitest";
+
+import { createTestEditor } from "../../../test/editor-kit.js";
+import { installGlobMock } from "../../../test/globals-test-kit.js";
+import ContentEmbed from "../content_embed/content_embed.js";
+import Mermaid from "./mermaid.js";
+
+const SAMPLES = [
+ { name: "Flowchart", content: "graph LR; A --> B" },
+ { name: "Pie", content: "pie title Pets" }
+];
+
+describe("MermaidUI", () => {
+ let editor: ClassicEditor;
+
+ beforeEach(async () => {
+ installGlobMock({
+ getComponentByEl: () => ({
+ triggerCommand: vi.fn(), loadEmbeddedNote: vi.fn(), loadInlineEmbed: vi.fn()
+ })
+ });
+
+ editor = await createTestEditor([ Essentials, Paragraph, Widget, ContentEmbed, Mermaid ], {
+ contentEmbed: { inlineLanguages: [ "mermaid" ] },
+ mermaid: { samples: SAMPLES }
+ });
+ setModelData(editor.model, "<paragraph>[]</paragraph>");
+ });
+
+ function createDropdown() {
+ const dropdown = editor.ui.componentFactory.create("mermaid");
+ if (!(dropdown instanceof DropdownView)) throw new Error("Expected a dropdown.");
+ dropdown.render();
+ return dropdown;
+ }
+
+ function getSampleButtons(dropdown: DropdownView) {
+ dropdown.isOpen = true;
+ return Array.from(dropdown.listView?.items ?? [])
+ .map((item) => (item as ListItemView).children.first)
+ .filter((button): button is ButtonView => button instanceof ButtonView);
+ }
+
+ function getInsertedDiagram() {
+ const embed = editor.model.document.selection.getSelectedElement();
+ expect(embed?.getAttribute("language")).toBe("mermaid");
+ return embed?.getAttribute("source");
+ }
+
+ it("inserts a new diagram from the main action, and a template from the list", () => {
+ const dropdown = createDropdown();
+ const button = dropdown.buttonView;
+ expect(button).toBeInstanceOf(SplitButtonView);
+ expect(button.label).toBe("Mermaid diagram");
+
+ button.fire("execute");
+ expect(getInsertedDiagram()).toBe(NEW_MERMAID_CONTENT);
+
+ const samples = getSampleButtons(dropdown);
+ expect(samples.map(({ label }) => label)).toEqual([ "Flowchart", "Pie" ]);
+ samples[1]?.fire("execute");
+ expect(getInsertedDiagram()).toBe("pie title Pets");
+ });
+
+ it("is disabled while a diagram cannot be inserted", () => {
+ const dropdown = createDropdown();
+ expect(dropdown.isEnabled).toBe(true);
+
+ editor.enableReadOnlyMode("spec");
+ expect(dropdown.isEnabled).toBe(false);
+ expect(dropdown.buttonView.isEnabled).toBe(false);
+ });
+
+ it("lists no templates without any, and stays enabled without the embed command", async () => {
+ const bare = await createTestEditor([ Essentials, Paragraph, Mermaid ]);
+ const dropdown = bare.ui.componentFactory.create("mermaid");
+ if (!(dropdown instanceof DropdownView)) throw new Error("Expected a dropdown.");
+ dropdown.render();
+ dropdown.isOpen = true;
+
+ expect(dropdown.listView?.items.length).toBe(0);
+ expect(dropdown.isEnabled).toBe(true);
+ });
+});