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
test(text): cover the collapsible list items plugin
Elian Doranac21b1a53fa7
diff --git a/.github/copilot-instructions.md b/.github/copilot-instructions.mdindex 7ff1965fe9..98dd01986f 100644--- a/.github/copilot-instructions.md+++ b/.github/copilot-instructions.md@@ -147,7 +147,7 @@ pnpm server:start-prod # Production mode serverpnpm desktop:start # Desktop app developmentpnpm server:test spec/etapi/search.spec.ts # Run specific testpnpm test:parallel # Client tests (can run parallel)-pnpm test:sequential # Server tests (sequential due to shared DB)+pnpm test:sequential # Server tests (shared DB) + browser-mode editor testspnpm test:all # All tests (parallel + sequential)pnpm coverage # Generate coverage reportspnpm typecheck # Type check all projectsdiff --git a/CLAUDE.md b/CLAUDE.mdindex 895fd89e19..6e15c1cae6 100644--- a/CLAUDE.md+++ b/CLAUDE.md@@ -27,7 +27,7 @@ pnpm desktop:build # Electron# Testpnpm test:all # All tests (parallel + sequential)pnpm test:parallel # Client + most package tests-pnpm test:sequential # Server, ckeditor5-mermaid, ckeditor5-math (shared DB)+pnpm test:sequential # Server (shared DB) + browser-mode tests (ckeditor5, ckeditor5-mermaid, ckeditor5-math)pnpm --filter server test # Single package testspnpm coverage # Coverage reportsdiff --git a/package.json b/package.jsonindex 37f6253460..05cc464f0e 100644--- a/package.json+++ b/package.json@@ -40,8 +40,8 @@"docs:preview": "pnpm http-server site -p 9000","edit-docs:edit-demo": "pnpm run --filter edit-docs edit-demo","test:all": "pnpm test:parallel && pnpm test:sequential",- "test:parallel": "pnpm --filter=!server --filter=!ckeditor5-mermaid --filter=!ckeditor5-math --parallel test",- "test:sequential": "pnpm --filter=server --filter=ckeditor5-mermaid --filter=ckeditor5-math --sequential test",+ "test:parallel": "pnpm --filter=!server --filter=!ckeditor5-mermaid --filter=!ckeditor5-math --filter=!ckeditor5 --parallel test",+ "test:sequential": "pnpm --filter=server --filter=ckeditor5-mermaid --filter=ckeditor5-math --filter=ckeditor5 --sequential test","typecheck": "tsx scripts/filter-tsc-output.mts","dev:format-check": "eslint -c eslint.format.config.mjs .","dev:format-fix": "eslint -c eslint.format.config.mjs . --fix",diff --git a/packages/ckeditor5/package.json b/packages/ckeditor5/package.jsonindex 6ae6430eb1..0decf26650 100644--- a/packages/ckeditor5/package.json+++ b/packages/ckeditor5/package.json@@ -5,6 +5,14 @@"private": true,"type": "module","main": "./src/index.ts",+ "scripts": {+ "test": "vitest"+ },+ "devDependencies": {+ "@vitest/browser": "4.1.8",+ "vitest": "4.1.8",+ "webdriverio": "9.27.2"+ },"dependencies": {"@triliumnext/commons": "workspace:*","@triliumnext/ckeditor5-admonition": "workspace:*",diff --git a/packages/ckeditor5/tests/collapsible_list_items.spec.ts b/packages/ckeditor5/tests/collapsible_list_items.spec.tsnew file mode 100644index 0000000000..c7b47dd35c--- /dev/null+++ b/packages/ckeditor5/tests/collapsible_list_items.spec.ts@@ -0,0 +1,203 @@+import { _setModelData as setModelData, ClassicEditor, List, Paragraph, Typing, Undo, type Batch, type ModelElement } from "ckeditor5";+import { afterEach, beforeEach, describe, expect, it } from "vitest";++import CollapsibleListItems, { isListCollapseToggleBatch, LIST_COLLAPSED_ATTRIBUTE } from "../src/plugins/collapsible_list_items.js";++// Lists are flat in the model: sibling blocks related by listIndent/listItemId.+const LIST_FIXTURE =+ '<paragraph listIndent="0" listItemId="i-a" listType="bulleted">Parent[]</paragraph>' ++ '<paragraph listIndent="1" listItemId="i-b" listType="bulleted">Child</paragraph>' ++ '<paragraph listIndent="1" listItemId="i-c" listType="bulleted">Other child</paragraph>' ++ '<paragraph listIndent="0" listItemId="i-d" listType="bulleted">Sibling</paragraph>' ++ "<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, Paragraph, Typing, Undo],+ licenseKey: "GPL"+ });++ setModelData(editor.model, LIST_FIXTURE);+ });++ afterEach(async () => {+ editorElement.remove();+ await editor.destroy();+ });++ it("collapses and expands via the command without changing the saved data", () => {+ const dataBefore = editor.getData();+ const command = editor.commands.get("toggleListCollapse");+ expect(command?.isEnabled).toBe(true);++ editor.execute("toggleListCollapse");++ expect(getBlock(editor, 0).getAttribute(LIST_COLLAPSED_ATTRIBUTE)).toBe(true);+ expect(command?.value).toBe(true);++ const domRoot = editor.editing.view.getDomRoot();+ const collapsedItem = domRoot?.querySelector("li.tn-list-collapsed");+ expect(collapsedItem).not.toBeNull();+ const nestedList = collapsedItem?.querySelector("ul");+ expect(nestedList).not.toBeNull();+ if (nestedList) {+ expect(getComputedStyle(nestedList).display).toBe("none");+ }++ // The collapsed state must never leak into the serialized content.+ expect(editor.getData()).toBe(dataBefore);++ editor.execute("toggleListCollapse");+ expect(getBlock(editor, 0).hasAttribute(LIST_COLLAPSED_ATTRIBUTE)).toBe(false);+ expect(command?.value).toBe(false);+ expect(domRoot?.querySelector("li.tn-list-collapsed")).toBeNull();+ });++ it("is only enabled on list items that have nested items", () => {+ const command = editor.commands.get("toggleListCollapse");++ setSelectionIn(editor, 1); // leaf child+ expect(command?.isEnabled).toBe(false);++ setSelectionIn(editor, 4); // outside of the list+ expect(command?.isEnabled).toBe(false);+ });++ it("applies to every block of a multi-block list item", () => {+ setModelData(editor.model,+ '<paragraph listIndent="0" listItemId="i-a" listType="bulleted">First[]</paragraph>' ++ '<paragraph listIndent="0" listItemId="i-a" listType="bulleted">Second</paragraph>' ++ '<paragraph listIndent="1" listItemId="i-b" listType="bulleted">Child</paragraph>');++ editor.execute("toggleListCollapse");+ expect(getBlock(editor, 0).getAttribute(LIST_COLLAPSED_ATTRIBUTE)).toBe(true);+ expect(getBlock(editor, 1).getAttribute(LIST_COLLAPSED_ATTRIBUTE)).toBe(true);++ editor.execute("toggleListCollapse");+ expect(getBlock(editor, 0).hasAttribute(LIST_COLLAPSED_ATTRIBUTE)).toBe(false);+ expect(getBlock(editor, 1).hasAttribute(LIST_COLLAPSED_ATTRIBUTE)).toBe(false);+ });++ it("creates non-undoable batches that are recognizable for save suppression", () => {+ const batches: Batch[] = [];+ editor.model.document.on("change:data", (_evt, batch) => batches.push(batch as Batch));++ editor.execute("toggleListCollapse");+ expect(batches).toHaveLength(1);+ expect(batches.every(isListCollapseToggleBatch)).toBe(true);++ editor.execute("insertText", { text: "!" });+ const typingBatch = batches.at(-1);+ expect(typingBatch && isListCollapseToggleBatch(typingBatch)).toBe(false);++ // Undo reverts the typing but not the (non-undoable) collapse.+ editor.execute("undo");+ expect(getBlockText(editor, 0)).toBe("Parent");+ expect(getBlock(editor, 0).getAttribute(LIST_COLLAPSED_ATTRIBUTE)).toBe(true);+ });++ it("expands automatically when the selection moves into a hidden item", () => {+ editor.execute("toggleListCollapse");+ expect(getBlock(editor, 0).getAttribute(LIST_COLLAPSED_ATTRIBUTE)).toBe(true);++ setSelectionIn(editor, 1);++ expect(getBlock(editor, 0).hasAttribute(LIST_COLLAPSED_ATTRIBUTE)).toBe(false);+ });++ it("expands automatically when an item is indented under a collapsed parent", () => {+ editor.execute("toggleListCollapse");+ setSelectionIn(editor, 3); // "Sibling", still visible at indent 0++ expect(getBlock(editor, 0).getAttribute(LIST_COLLAPSED_ATTRIBUTE)).toBe(true);++ editor.execute("indentList");++ expect(getBlock(editor, 3).getAttribute("listIndent")).toBe(1);+ expect(getBlock(editor, 0).hasAttribute(LIST_COLLAPSED_ATTRIBUTE)).toBe(false);+ });++ it("drops the collapsed attribute from new items that have nothing to collapse", () => {+ // Simulates e.g. an Enter split copying the attribute onto the new block.+ editor.model.change((writer) => {+ const root = editor.model.document.getRoot();+ if (!root) {+ throw new Error("The editor has no root.");+ }+ const block = writer.createElement("paragraph", {+ listIndent: 0,+ listItemId: "i-z",+ listType: "bulleted",+ [LIST_COLLAPSED_ATTRIBUTE]: true+ });+ writer.insert(block, root, "end");+ });++ expect(getBlock(editor, 5).hasAttribute(LIST_COLLAPSED_ATTRIBUTE)).toBe(false);+ });++ it("toggles when clicking the arrow in the gutter and ignores other clicks", () => {+ const domRoot = editor.editing.view.getDomRoot();+ const items = domRoot ? Array.from(domRoot.querySelectorAll("li")) : [];+ const parentItem = items.at(0);+ const leafItem = items.at(1);+ expect(parentItem).toBeDefined();+ expect(leafItem).toBeDefined();+ if (!parentItem || !leafItem) {+ return;+ }++ mouseDownAt(parentItem, -10); // on the gutter arrow+ expect(getBlock(editor, 0).getAttribute(LIST_COLLAPSED_ATTRIBUTE)).toBe(true);++ mouseDownAt(parentItem, -10); // toggles back+ expect(getBlock(editor, 0).hasAttribute(LIST_COLLAPSED_ATTRIBUTE)).toBe(false);++ mouseDownAt(parentItem, 5); // inside the item box: not a gutter click+ expect(getBlock(editor, 0).hasAttribute(LIST_COLLAPSED_ATTRIBUTE)).toBe(false);++ mouseDownAt(leafItem, -10); // leaf items have nothing to collapse+ expect(getBlock(editor, 1).hasAttribute(LIST_COLLAPSED_ATTRIBUTE)).toBe(false);+ });+});++function getBlock(editor: ClassicEditor, index: number): ModelElement {+ const child = editor.model.document.getRoot()?.getChild(index);+ if (!child || !child.is("element")) {+ throw new Error(`No element block at index ${index}.`);+ }+ return child;+}++function getBlockText(editor: ClassicEditor, index: number): string {+ let text = "";+ for (const node of getBlock(editor, index).getChildren()) {+ if (node.is("$text")) {+ text += node.data;+ }+ }+ return text;+}++function setSelectionIn(editor: ClassicEditor, blockIndex: number): void {+ editor.model.change((writer) => {+ writer.setSelection(writer.createPositionAt(getBlock(editor, blockIndex), 0));+ });+}++function mouseDownAt(item: HTMLElement, offsetX: number): void {+ const rect = item.getBoundingClientRect();+ item.dispatchEvent(new MouseEvent("mousedown", {+ bubbles: true,+ cancelable: true,+ clientX: rect.left + offsetX,+ clientY: rect.top + 5+ }));+}diff --git a/packages/ckeditor5/vitest.config.ts b/packages/ckeditor5/vitest.config.tsnew file mode 100644index 0000000000..aa77a7dfe4--- /dev/null+++ b/packages/ckeditor5/vitest.config.ts@@ -0,0 +1,17 @@+import { webdriverio } from "@vitest/browser-webdriverio";+import { defineConfig } from "vitest/config";++export default defineConfig({+ test: {+ browser: {+ enabled: true,+ provider: webdriverio(),+ headless: true,+ ui: false,+ instances: [{ browser: "chrome" }]+ },+ include: ["tests/**/*.spec.ts"],+ globals: true,+ watch: false+ }+});diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex 3ad9287200..bd1bd820e4 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -1103,6 +1103,16 @@ importers:ckeditor5-premium-features:specifier: 48.2.0version: 48.2.0([email protected])+ devDependencies:+ '@vitest/browser':+ specifier: 4.1.8+ version: 4.1.8([email protected](@types/[email protected])([email protected])([email protected])([email protected])([email protected])([email protected]))([email protected])+ vitest:+ specifier: 4.1.8+ version: 4.1.8(@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]))+ webdriverio:+ specifier: 9.27.2+ version: 9.27.2packages/ckeditor5-admonition:devDependencies:@@ -25910,7 +25920,7 @@ snapshots:proc-log: 6.1.0semver: 7.8.0tar: 7.5.15- tinyglobby: 0.2.16+ tinyglobby: 0.2.17undici: 6.25.0which: 6.0.1@@ -28795,7 +28805,7 @@ snapshots:std-env: 4.1.0tinybench: 2.9.0tinyexec: 1.1.2- tinyglobby: 0.2.16+ tinyglobby: 0.2.17tinyrainbow: 3.1.0vite: 8.0.16(@types/[email protected])([email protected])([email protected])([email protected])([email protected])([email protected])why-is-node-running: 2.3.0@@ -28898,7 +28908,7 @@ snapshots:deepmerge-ts: 7.1.5https-proxy-agent: 7.0.6undici: 6.25.0- ws: 8.20.1+ ws: 8.21.0transitivePeerDependencies:- bare-abort-controller- bare-buffer