Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
text editor/collapsible: add tests
Adorian Dorand8b81fd14319
diff --git a/packages/ckeditor5-collapsible/package.json b/packages/ckeditor5-collapsible/package.jsonindex d9c368e0ea..ebd0d09876 100644--- a/packages/ckeditor5-collapsible/package.json+++ b/packages/ckeditor5-collapsible/package.json@@ -18,7 +18,11 @@"npm": ">=5.7.1"},"devDependencies": {- "typescript": "6.0.3"+ "ckeditor5": "48.1.1",+ "happy-dom": "20.9.0",+ "typescript": "6.0.3",+ "vite-plugin-svgo": "2.0.0",+ "vitest": "4.1.7"},"peerDependencies": {"bootstrap": "^5.3.0",@@ -26,5 +30,8 @@},"author": "Trilium contributors","license": "GPL-2.0-or-later",- "scripts": {}+ "scripts": {+ "test": "vitest",+ "test:debug": "vitest --inspect-brk --no-file-parallelism"+ }}diff --git a/packages/ckeditor5-collapsible/tests/command.ts b/packages/ckeditor5-collapsible/tests/command.tsnew file mode 100644index 0000000000..7627c23601--- /dev/null+++ b/packages/ckeditor5-collapsible/tests/command.ts@@ -0,0 +1,87 @@+import { ClassicEditor, Essentials, Paragraph, _setModelData as setModelData, _getModelData as getModelData } from "ckeditor5";+import { afterEach, beforeEach, describe, expect, it } from "vitest";+import CollapsibleEditing from "../src/collapsible-editing.js";++describe("CollapsibleCommand", () => {+ let domElement: HTMLDivElement;+ let editor: ClassicEditor;+ let model: ClassicEditor["model"];++ beforeEach(async () => {+ domElement = document.createElement("div");+ document.body.appendChild(domElement);+ editor = await ClassicEditor.create(domElement, {+ licenseKey: "GPL",+ plugins: [Essentials, Paragraph, CollapsibleEditing]+ });+ model = editor.model;+ });++ afterEach(() => {+ domElement.remove();+ return editor.destroy();+ });++ describe("execute", () => {+ it("inserts an empty collapsible at a collapsed caret and parks the caret in the new summary", () => {+ setModelData(model, "<paragraph>[]</paragraph>");+ editor.execute("collapsible");+ // Caret in the new summary; the empty body paragraph follows it.+ expect(getModelData(model)).toContain(+ "<details><summary>[]</summary><paragraph></paragraph></details>"+ );+ });++ it("wraps an intra-block text selection into a paragraph in the new body", () => {+ setModelData(model, "<paragraph>Hello [selected] world</paragraph>");+ editor.execute("collapsible");+ expect(getModelData(model, { withoutSelection: true })).toContain(+ "<details><summary></summary><paragraph>selected</paragraph></details>"+ );+ });++ it("preserves a multi-block selection as the new body", () => {+ setModelData(model,+ "[<paragraph>Block one</paragraph><paragraph>Block two</paragraph>]"+ );+ editor.execute("collapsible");+ const data = getModelData(model, { withoutSelection: true });+ expect(data).toContain("<paragraph>Block one</paragraph>");+ expect(data).toContain("<paragraph>Block two</paragraph>");+ // Both selected paragraphs end up inside the new <details>.+ expect(data).toMatch(/<details><summary><\/summary><paragraph>Block one<\/paragraph><paragraph>Block two<\/paragraph><\/details>/);+ });++ it("unwraps a nested <details> in the selection so we don't gain an extra level of nesting", () => {+ setModelData(model,+ "[<details>" ++ "<summary>Inner</summary>" ++ "<paragraph>Inner body</paragraph>" ++ "</details>]"+ );+ editor.execute("collapsible");+ const data = getModelData(model, { withoutSelection: true });+ // Exactly ONE <details> in the output — the inner one was unwrapped.+ expect((data.match(/<details>/g) ?? []).length).toBe(1);+ // The inner body is preserved as the new collapsible's body.+ expect(data).toContain("<paragraph>Inner body</paragraph>");+ // The inner summary's text isn't pulled into the new summary (the+ // new summary is the editable title for the user to fill in).+ expect(data).not.toContain("<summary>Inner");+ });+ });++ describe("refresh", () => {+ it("is enabled at a root-level caret", () => {+ setModelData(model, "<paragraph>[]</paragraph>");+ expect(editor.commands.get("collapsible")!.isEnabled).toBe(true);+ });++ it("keeps value=false even when the caret is already inside a collapsible (it's not a toggle)", () => {+ setModelData(model,+ "<details><summary>X</summary><paragraph>[]</paragraph></details>"+ );+ expect(editor.commands.get("collapsible")!.value).toBe(false);+ });+ });+});diff --git a/packages/ckeditor5-collapsible/tests/conversion.ts b/packages/ckeditor5-collapsible/tests/conversion.tsnew file mode 100644index 0000000000..1bb073e386--- /dev/null+++ b/packages/ckeditor5-collapsible/tests/conversion.ts@@ -0,0 +1,74 @@+import { ClassicEditor, Essentials, Paragraph, _getModelData as getModelData } from "ckeditor5";+import { afterEach, beforeEach, describe, expect, it } from "vitest";+import CollapsibleEditing from "../src/collapsible-editing.js";++describe("CollapsibleEditing conversion", () => {+ let domElement: HTMLDivElement;+ let editor: ClassicEditor;++ beforeEach(async () => {+ domElement = document.createElement("div");+ document.body.appendChild(domElement);+ editor = await ClassicEditor.create(domElement, {+ licenseKey: "GPL",+ plugins: [Essentials, Paragraph, CollapsibleEditing]+ });+ });++ afterEach(() => {+ domElement.remove();+ return editor.destroy();+ });++ describe("upcast", () => {+ it("maps <details>/<summary>/<p> to the model details/summary/paragraph", () => {+ editor.setData("<details><summary>Title</summary><p>Body</p></details>");+ expect(getModelData(editor.model, { withoutSelection: true })).toBe(+ "<details><summary>Title</summary><paragraph>Body</paragraph></details>"+ );+ });++ it("preserves multiple body blocks", () => {+ editor.setData(+ "<details><summary>Title</summary><p>First</p><p>Second</p></details>"+ );+ expect(getModelData(editor.model, { withoutSelection: true })).toBe(+ "<details><summary>Title</summary>" ++ "<paragraph>First</paragraph><paragraph>Second</paragraph>" ++ "</details>"+ );+ });++ it("preserves nested collapsibles", () => {+ editor.setData(+ "<details><summary>Outer</summary>" ++ "<details><summary>Inner</summary><p>Nested</p></details>" ++ "</details>"+ );+ expect(getModelData(editor.model, { withoutSelection: true })).toBe(+ "<details><summary>Outer</summary>" ++ "<details><summary>Inner</summary><paragraph>Nested</paragraph></details>" ++ "</details>"+ );+ });+ });++ describe("data downcast", () => {+ it("emits the trilium-collapsible class on the rendered <details>", () => {+ editor.setData("<details><summary>Title</summary><p>Body</p></details>");+ expect(editor.getData()).toBe(+ "<details class=\"trilium-collapsible\">" ++ "<summary>Title</summary><p>Body</p>" ++ "</details>"+ );+ });++ it("roundtrips lossless when the source already has the class", () => {+ const html = "<details class=\"trilium-collapsible\">" ++ "<summary>Title</summary><p>Body</p>" ++ "</details>";+ editor.setData(html);+ expect(editor.getData()).toBe(html);+ });+ });+});diff --git a/packages/ckeditor5-collapsible/tests/post-fixer-gap.ts b/packages/ckeditor5-collapsible/tests/post-fixer-gap.tsnew file mode 100644index 0000000000..1cf7bcb9bc--- /dev/null+++ b/packages/ckeditor5-collapsible/tests/post-fixer-gap.ts@@ -0,0 +1,93 @@+import { ClassicEditor, Essentials, Paragraph, _setModelData as setModelData, _getModelData as getModelData } from "ckeditor5";+import { afterEach, beforeEach, describe, expect, it } from "vitest";+import CollapsibleEditing from "../src/collapsible-editing.js";++describe("gapPostFixer + onEnterInBody", () => {+ let domElement: HTMLDivElement;+ let editor: ClassicEditor;+ let model: ClassicEditor["model"];++ beforeEach(async () => {+ domElement = document.createElement("div");+ document.body.appendChild(domElement);+ editor = await ClassicEditor.create(domElement, {+ licenseKey: "GPL",+ plugins: [Essentials, Paragraph, CollapsibleEditing]+ });+ model = editor.model;+ });++ afterEach(() => {+ domElement.remove();+ return editor.destroy();+ });++ describe("gapPostFixer", () => {+ it("re-pins a caret that lands directly between <summary> and the body to the end of <summary>", () => {+ setModelData(model,+ "<details><summary>Title</summary><paragraph>body</paragraph></details>"+ );+ model.change(writer => {+ const details = model.document.getRoot()!.getChild(0)!;+ // Offset 1 inside details = the "gap" between summary (idx 0) and body (idx 1).+ writer.setSelection(writer.createPositionAt(details, 1));+ });+ // Caret should NOT be sitting directly in the details element — it should+ // have been moved to the end of the previous child (the summary).+ expect(getModelData(model)).toBe(+ "<details><summary>Title[]</summary><paragraph>body</paragraph></details>"+ );+ });++ it("dives into a nested <details>'s last block when the gap before the caret is a nested <details>", () => {+ setModelData(model,+ "<details>" ++ "<summary>Outer</summary>" ++ "<details><summary>Inner</summary><paragraph>nested</paragraph></details>" ++ "</details>"+ );+ // Open both details manually so hiddenBodyPostFixer doesn't rescue+ // the caret away from the nested body — we're isolating gapPostFixer.+ // (Auto-open only walks top-level siblings of the insert entry, so+ // nested details from a single setModelData stay closed in tests.)+ for (const d of editor.editing.view.getDomRoot()!.querySelectorAll("details")) {+ (d as HTMLDetailsElement).open = true;+ }+ model.change(writer => {+ const outer = model.document.getRoot()!.getChild(0)!;+ // Position at outer's end: between inner-details and end-of-outer.+ writer.setSelection(writer.createPositionAt(outer, 2));+ });+ // Caret should land at the end of the nested details' last block.+ expect(getModelData(model)).toBe(+ "<details><summary>Outer</summary>" ++ "<details><summary>Inner</summary><paragraph>nested[]</paragraph></details>" ++ "</details>"+ );+ });+ });++ describe("onEnterInBody", () => {+ it("escapes the collapsible when Enter is pressed in an empty trailing body paragraph", () => {+ setModelData(model,+ "<details>" ++ "<summary>X</summary>" ++ "<paragraph>existing</paragraph>" ++ "<paragraph>[]</paragraph>" ++ "</details>"+ );+ // Fire the view-level enter event the same way CKEditor would on Enter keydown.+ editor.editing.view.document.fire("enter", {+ preventDefault: () => {},+ stop: () => {},+ isSoft: false+ });+ // The empty trailing paragraph is gone; the caret has moved to a new+ // paragraph outside the details.+ expect(getModelData(model)).toBe(+ "<details><summary>X</summary><paragraph>existing</paragraph></details>" ++ "<paragraph>[]</paragraph>"+ );+ });+ });+});diff --git a/packages/ckeditor5-collapsible/tests/post-fixer-structural.ts b/packages/ckeditor5-collapsible/tests/post-fixer-structural.tsnew file mode 100644index 0000000000..d328e10be2--- /dev/null+++ b/packages/ckeditor5-collapsible/tests/post-fixer-structural.ts@@ -0,0 +1,58 @@+import { ClassicEditor, Essentials, Paragraph, _setModelData as setModelData, _getModelData as getModelData } from "ckeditor5";+import { afterEach, beforeEach, describe, expect, it } from "vitest";+import CollapsibleEditing from "../src/collapsible-editing.js";++describe("structuralPostFixer + bodyExistsPostFixer", () => {+ let domElement: HTMLDivElement;+ let editor: ClassicEditor;+ let model: ClassicEditor["model"];++ beforeEach(async () => {+ domElement = document.createElement("div");+ document.body.appendChild(domElement);+ editor = await ClassicEditor.create(domElement, {+ licenseKey: "GPL",+ plugins: [Essentials, Paragraph, CollapsibleEditing]+ });+ model = editor.model;+ });++ afterEach(() => {+ domElement.remove();+ return editor.destroy();+ });++ it("removes an orphan <summary> that ended up outside of <details>", () => {+ // Manually insert a summary at the root — the summary-invariant post-fixer+ // doesn't apply here (no <details> involved), so the structural one should+ // clean it up.+ model.change(writer => {+ const root = model.document.getRoot()!;+ writer.insert(writer.createElement("summary"), root, 0);+ });+ const data = getModelData(model, { withoutSelection: true });+ expect(data).not.toContain("<summary>");+ });++ it("removes a fully empty <details>", () => {+ // setModelData sets the model directly, bypassing schema; the structural+ // post-fixer then removes the empty container.+ setModelData(model, "<details></details>");+ const data = getModelData(model, { withoutSelection: true });+ expect(data).not.toContain("<details>");+ });++ it("re-inserts a body paragraph when a <details> is left with only its summary", () => {+ setModelData(model,+ "<details><summary>X</summary><paragraph>body</paragraph></details>"+ );+ model.change(writer => {+ const details = model.document.getRoot()!.getChild(0)!;+ // Body block (index 1) — remove it and expect the post-fixer to replace it.+ writer.remove(details.getChild(1)!);+ });+ expect(getModelData(model, { withoutSelection: true })).toBe(+ "<details><summary>X</summary><paragraph></paragraph></details>"+ );+ });+});diff --git a/packages/ckeditor5-collapsible/tests/post-fixer-summary.ts b/packages/ckeditor5-collapsible/tests/post-fixer-summary.tsnew file mode 100644index 0000000000..70dd43e4b2--- /dev/null+++ b/packages/ckeditor5-collapsible/tests/post-fixer-summary.ts@@ -0,0 +1,49 @@+import { ClassicEditor, Essentials, Paragraph, _setModelData as setModelData, _getModelData as getModelData } from "ckeditor5";+import { afterEach, beforeEach, describe, expect, it } from "vitest";+import CollapsibleEditing from "../src/collapsible-editing.js";++describe("summaryInvariantPostFixer", () => {+ let domElement: HTMLDivElement;+ let editor: ClassicEditor;+ let model: ClassicEditor["model"];++ beforeEach(async () => {+ domElement = document.createElement("div");+ document.body.appendChild(domElement);+ editor = await ClassicEditor.create(domElement, {+ licenseKey: "GPL",+ plugins: [Essentials, Paragraph, CollapsibleEditing]+ });+ model = editor.model;+ });++ afterEach(() => {+ domElement.remove();+ return editor.destroy();+ });++ it("inserts a blank summary when a <details> is loaded without one", () => {+ setModelData(model, "<details><paragraph>Body only</paragraph></details>");+ expect(getModelData(model, { withoutSelection: true })).toBe(+ "<details><summary></summary><paragraph>Body only</paragraph></details>"+ );+ });++ it("demotes extra <summary>s to paragraphs (preserving their text)", () => {+ setModelData(model,+ "<details><summary>First</summary><summary>Second</summary></details>"+ );+ expect(getModelData(model, { withoutSelection: true })).toBe(+ "<details><summary>First</summary><paragraph>Second</paragraph></details>"+ );+ });++ it("moves a misplaced summary back to position 0", () => {+ setModelData(model,+ "<details><paragraph>Body</paragraph><summary>Title</summary></details>"+ );+ expect(getModelData(model, { withoutSelection: true })).toBe(+ "<details><summary>Title</summary><paragraph>Body</paragraph></details>"+ );+ });+});diff --git a/packages/ckeditor5-collapsible/tests/schema.ts b/packages/ckeditor5-collapsible/tests/schema.tsnew file mode 100644index 0000000000..ce1526d4fd--- /dev/null+++ b/packages/ckeditor5-collapsible/tests/schema.ts@@ -0,0 +1,56 @@+import { ClassicEditor, Essentials, Paragraph } from "ckeditor5";+import { afterEach, beforeEach, describe, expect, it } from "vitest";+import CollapsibleEditing from "../src/collapsible-editing.js";++describe("CollapsibleEditing schema", () => {+ let domElement: HTMLDivElement;+ let editor: ClassicEditor;+ let schema: ClassicEditor["model"]["schema"];++ beforeEach(async () => {+ domElement = document.createElement("div");+ document.body.appendChild(domElement);+ editor = await ClassicEditor.create(domElement, {+ licenseKey: "GPL",+ plugins: [Essentials, Paragraph, CollapsibleEditing]+ });+ schema = editor.model.schema;+ });++ afterEach(() => {+ domElement.remove();+ return editor.destroy();+ });++ it("registers the details and summary elements", () => {+ expect(schema.isRegistered("details")).toBe(true);+ expect(schema.isRegistered("summary")).toBe(true);+ });++ it("allows details at the document root", () => {+ expect(schema.checkChild(["$root"], "details")).toBe(true);+ });++ it("allows summary only inside details", () => {+ expect(schema.checkChild(["$root", "details"], "summary")).toBe(true);+ expect(schema.checkChild(["$root"], "summary")).toBe(false);+ expect(schema.checkChild(["$root", "paragraph"], "summary")).toBe(false);+ });++ it("allows block content inside details (so the body can hold paragraphs and nested collapsibles)", () => {+ expect(schema.checkChild(["$root", "details"], "paragraph")).toBe(true);+ // Nested collapsibles are explicitly supported.+ expect(schema.checkChild(["$root", "details"], "details")).toBe(true);+ });++ it("treats summary as a block element so MoveBlockUpDown resolves caret-in-summary to the enclosing details", () => {+ // The collapsible plugin sets isBlock: true on <summary> deliberately —+ // see the comment in registerSchema. If this regresses, the "caret in+ // title acts as a handle for the whole block" UX breaks.+ expect(schema.isBlock("summary")).toBe(true);+ });++ it("allows plain text inside summary", () => {+ expect(schema.checkChild(["$root", "details", "summary"], "$text")).toBe(true);+ });+});diff --git a/packages/ckeditor5-collapsible/tsconfig.test.json b/packages/ckeditor5-collapsible/tsconfig.test.jsonnew file mode 100644index 0000000000..aae1597bdb--- /dev/null+++ b/packages/ckeditor5-collapsible/tsconfig.test.json@@ -0,0 +1,14 @@+{+ "extends": "./tsconfig.json",+ "compilerOptions": {+ "types": [+ "vitest/globals"+ ],+ "sourceMap": true+ },+ "include": [+ "./src",+ "./tests",+ "./typings"+ ]+}diff --git a/packages/ckeditor5-collapsible/vitest.config.ts b/packages/ckeditor5-collapsible/vitest.config.tsnew file mode 100644index 0000000000..1e3f43ac10--- /dev/null+++ b/packages/ckeditor5-collapsible/vitest.config.ts@@ -0,0 +1,17 @@+import { defineConfig } from "vitest/config";+import svg from "vite-plugin-svgo";++export default defineConfig({+ plugins: [+ svg()+ ],+ test: {+ environment: "happy-dom",+ include: [+ "tests/**/*.[jt]s"+ ],+ globals: true,+ watch: false,+ passWithNoTests: true+ }+});diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex e8966057f0..d6bc965212 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -1195,13 +1195,22 @@ importers:bootstrap:specifier: ^5.3.0version: 5.3.8(@popperjs/[email protected])+ devDependencies:ckeditor5:specifier: 48.1.1version: 48.1.1- devDependencies:+ happy-dom:+ specifier: 20.9.0+ version: 20.9.0typescript:specifier: 6.0.3version: 6.0.3+ vite-plugin-svgo:+ specifier: 2.0.0+ version: 2.0.0([email protected])([email protected](@types/[email protected])([email protected])([email protected])([email protected])([email protected])([email protected]))+ vitest:+ specifier: 4.1.7+ version: 4.1.7(@opentelemetry/[email protected])(@types/[email protected])(@vitest/[email protected])(@vitest/[email protected])(@vitest/[email protected])(@vitest/[email protected])([email protected])([email protected](@types/[email protected])([email protected])([email protected])([email protected])([email protected])([email protected]))packages/ckeditor5-footnotes:devDependencies: