Skip to content

Build your personal knowledge base with Trilium Notes

test(client): cover PR patch lines for link/dialog/note_tree/Dropdown

Elian Doran0c35870e6843
diff --git a/apps/client/src/services/dialog.spec.ts b/apps/client/src/services/dialog.spec.ts
index 922a59879d..80237f3085 100644
--- a/apps/client/src/services/dialog.spec.ts
+++ b/apps/client/src/services/dialog.spec.ts
@@ -211,6 +211,18 @@ describe("dialog service", () => {
expect($dialog[0].style.zIndex).toBe("");
});
+ it("treats popups without a numeric z-index as layer 0 and excludes the dialog itself from the scan", async () => {
+ document.body.classList.add("tree-popup-stacked");
+ // No inline z-index -> getComputedStyle returns a non-numeric value -> parseInt NaN -> 0.
+ openStackedPopup("");
+
+ // The dialog itself matches `.modal.show`; it must be filtered out of the max-z scan.
+ const $dialog = makeDialog().addClass("show").appendTo(document.body);
+ await openDialog($dialog, true);
+
+ expect($dialog[0].style.zIndex).toBe("10"); // max(0) + 10
+ });
+
it("never lifts a self-managed popup dialog, even while another popup is stacked", async () => {
document.body.classList.add("tree-popup-stacked");
openStackedPopup("1100");
diff --git a/apps/client/src/services/link.spec.ts b/apps/client/src/services/link.spec.ts
index 7bf1898eaa..2f0811c2e9 100644
--- a/apps/client/src/services/link.spec.ts
+++ b/apps/client/src/services/link.spec.ts
@@ -220,6 +220,15 @@ describe("createLink", () => {
expect($el.find("a").text()).toBe("Override");
});
+ it("appends a .note-link-suffix span right after the link when titleSuffix is set", async () => {
+ const note = buildNote({ title: "Base" });
+ const $el = await linkService.createLink(`root/${note.noteId}`, { titleSuffix: "— dropped" });
+ const $suffix = $el.children("span.note-link-suffix");
+ expect($suffix.length).toBe(1);
+ expect($suffix.text()).toBe("— dropped");
+ expect($suffix.prev().is("a")).toBe(true);
+ });
+
it("auto-converts image notes to an <img> element", async () => {
const note = buildNote({ title: "Pic", type: "image" });
const $el = await linkService.createLink(`root/${note.noteId}`, { autoConvertToImage: true, title: "Pic" });
diff --git a/apps/client/src/widgets/note_tree.spec.ts b/apps/client/src/widgets/note_tree.spec.ts
index c8bab340f0..02d0e4847d 100644
--- a/apps/client/src/widgets/note_tree.spec.ts
+++ b/apps/client/src/widgets/note_tree.spec.ts
@@ -1,7 +1,10 @@
-import "./note_tree.js";
-
import $ from "jquery";
-import { describe, expect, it } from "vitest";
+import { afterEach, describe, expect, it, vi } from "vitest";
+
+import hoistedNoteService from "../services/hoisted_note.js";
+import noteCreateService from "../services/note_create.js";
+import treeService from "../services/tree.js";
+import NoteTreeWidget from "./note_tree.js";
describe("fancytree scrollIntoView patch", () => {
it("resolves instead of crashing for a node without rendered markup (#10407)", async () => {
@@ -49,3 +52,61 @@ describe("fancytree scrollIntoView patch", () => {
}
});
});
+
+describe("NoteTreeWidget", () => {
+ afterEach(() => {
+ vi.restoreAllMocks();
+ });
+
+ /** Renders the widget shallowly — fancytree init (which needs a live froca tree) is stubbed out. */
+ async function renderWidget() {
+ const widget = new NoteTreeWidget();
+ vi.spyOn(widget, "initFancyTree").mockImplementation(() => {});
+ widget.doRender();
+ // Let doRender's init promise chain settle against the stub above.
+ await new Promise((resolve) => setTimeout(resolve));
+ return widget;
+ }
+
+ it("creates new notes in the tree's own note context (not the active tab's)", async () => {
+ const widget = await renderWidget();
+ const noteContext = { ntxId: "popup-ctx" };
+ (widget as { noteContext?: unknown }).noteContext = noteContext;
+
+ const fakeNode = { data: { isProtected: true } };
+ vi.spyOn($.ui.fancytree, "getNode").mockReturnValue(fakeNode as unknown as Fancytree.FancytreeNode);
+ vi.spyOn(treeService, "getNotePath").mockReturnValue("root/parent");
+ const createNote = vi.spyOn(noteCreateService, "createNote").mockResolvedValue(undefined as never);
+
+ const button = document.createElement("div");
+ button.className = "add-note-button";
+ widget.$widget.find(".tree").append(button);
+ button.dispatchEvent(new MouseEvent("mousedown", { bubbles: true, button: 0 }));
+
+ expect(createNote).toHaveBeenCalledWith("root/parent", {
+ isProtected: true,
+ noteContext
+ });
+ });
+
+ it("refresh() consults hoisting with the tree's own hoistedNoteId for hidden-subtree paths", async () => {
+ const widget = await renderWidget();
+ (widget as { noteContext?: unknown }).noteContext = {
+ notePath: "root/_hidden/_taskStates",
+ hoistedNoteId: "_taskStates"
+ };
+
+ // Neutralise the parts of refresh() that need a live fancytree.
+ vi.spyOn(widget, "isEnabled").mockReturnValue(false);
+ vi.spyOn(widget, "activityDetected").mockImplementation(() => {});
+ vi.spyOn(widget, "getActiveNode").mockReturnValue(null);
+ vi.spyOn(widget, "getNodeFromPath").mockResolvedValue(null);
+ vi.spyOn(widget, "filterHoistedBranch").mockResolvedValue(undefined);
+ const isHoisted = vi.spyOn(hoistedNoteService, "isHoistedInHiddenSubtree").mockResolvedValue(false);
+
+ await widget.refresh();
+
+ // The popup's own hoisted note is passed explicitly — not the active tab's.
+ expect(isHoisted).toHaveBeenCalledWith("_taskStates");
+ });
+});
diff --git a/apps/client/src/widgets/react/Dropdown.spec.tsx b/apps/client/src/widgets/react/Dropdown.spec.tsx
new file mode 100644
index 0000000000..a0090f6171
--- /dev/null
+++ b/apps/client/src/widgets/react/Dropdown.spec.tsx
@@ -0,0 +1,151 @@
+import $ from "jquery";
+import { render } from "preact";
+import { act } from "preact/test-utils";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+
+// A single fake Bootstrap dropdown instance so the component wiring (dropdownRef, `_menu`
+// re-pointing, dispose-on-unmount) can be asserted without real Bootstrap/Popper.
+const { instance, getOrCreateInstance } = vi.hoisted(() => {
+ const instance = {
+ show: vi.fn(),
+ hide: vi.fn(),
+ update: vi.fn(),
+ dispose: vi.fn(),
+ _menu: null as HTMLElement | null
+ };
+ return { instance, getOrCreateInstance: vi.fn(() => instance) };
+});
+
+vi.mock("bootstrap", () => ({
+ Dropdown: { getOrCreateInstance },
+ Tooltip: class {}
+}));
+
+// Stub only the Bootstrap-Tooltip hook; keep the rest of the hooks module real. The stubs must be
+// referentially stable across renders — onShown/onHidden depend on hideTooltip's identity.
+const tooltipStub = vi.hoisted(() => ({ showTooltip: vi.fn(), hideTooltip: vi.fn() }));
+vi.mock("./hooks", async (importOriginal) => ({
+ ...(await importOriginal<typeof import("./hooks")>()),
+ useTooltip: () => tooltipStub
+}));
+
+import Dropdown from "./Dropdown";
+
+// happy-dom has no ResizeObserver; the component only needs observe/disconnect to exist.
+class ResizeObserverStub {
+ observe() {}
+ unobserve() {}
+ disconnect() {}
+}
+globalThis.ResizeObserver = globalThis.ResizeObserver ?? (ResizeObserverStub as unknown as typeof ResizeObserver);
+
+let container: HTMLDivElement | undefined;
+function renderInto(vnode: preact.ComponentChild) {
+ const el = document.createElement("div");
+ container = el;
+ document.body.appendChild(el);
+ void act(() => render(vnode, el));
+ return el;
+}
+
+function getToggle() {
+ const button = container?.querySelector("button");
+ expect(button).toBeTruthy();
+ return button as HTMLButtonElement;
+}
+
+function fire(target: EventTarget, eventName: string) {
+ void act(() => {
+ target.dispatchEvent(new Event(eventName, { bubbles: true }));
+ });
+}
+
+describe("Dropdown", () => {
+ beforeEach(() => {
+ vi.clearAllMocks();
+ instance._menu = null;
+ });
+
+ afterEach(() => {
+ if (container) {
+ const el = container;
+ void act(() => render(null, el));
+ el.remove();
+ container = undefined;
+ }
+ });
+
+ it("renders a non-portaled menu inline, exposes the instance via dropdownRef and disposes it on unmount", () => {
+ const dropdownRef = { current: null };
+ const el = renderInto(<Dropdown dropdownRef={dropdownRef}>item</Dropdown>);
+
+ // Menu is nested next to the toggle, not in the body.
+ expect(el.querySelector(".dropdown .dropdown-menu")).toBeTruthy();
+ expect(dropdownRef.current).toBe(instance);
+
+ void act(() => render(null, el));
+ expect(instance.dispose).toHaveBeenCalledTimes(1);
+ });
+
+ it("mounts the portaled menu on arm (pointerdown/focus), wires _menu, and tears down on blur without open", () => {
+ renderInto(<Dropdown portalToBody className="my-scope" text="btn">item</Dropdown>);
+
+ // Not mounted while idle — no empty wrapper left in the body.
+ expect(document.body.querySelector(":scope > .my-scope")).toBeNull();
+
+ // Pointer press arms it: the wrapper + menu mount in the body and _menu is re-pointed.
+ fire(getToggle(), "pointerdown");
+ const menu = document.body.querySelector(":scope > .my-scope > .dropdown-menu");
+ expect(menu).toBeTruthy();
+ expect(instance._menu).toBe(menu);
+
+ // Releasing focus without opening tears the empty menu back down.
+ fire(getToggle(), "focusout");
+ expect(document.body.querySelector(":scope > .my-scope")).toBeNull();
+
+ // Focusing the toggle (keyboard path) arms it too.
+ fire(getToggle(), "focusin");
+ expect(document.body.querySelector(":scope > .my-scope > .dropdown-menu")).toBeTruthy();
+ });
+
+ it("keeps an open portaled menu mounted through blur and unmounts it on hide", () => {
+ const onShown = vi.fn();
+ const onHidden = vi.fn();
+ const el = renderInto(
+ <Dropdown portalToBody className="my-scope" onShown={onShown} onHidden={onHidden}>
+ <li>entry</li>
+ </Dropdown>
+ );
+
+ fire(getToggle(), "pointerdown");
+
+ // Bootstrap opens the dropdown: children render and blur no longer unmounts.
+ const dropdownEl = el.querySelector(".dropdown");
+ expect(dropdownEl).toBeTruthy();
+ void act(() => {
+ $(dropdownEl as HTMLElement).trigger("show.bs.dropdown");
+ });
+ expect(onShown).toHaveBeenCalledTimes(1);
+ expect(document.body.querySelector(":scope > .my-scope li")).toBeTruthy();
+
+ fire(getToggle(), "focusout");
+ expect(document.body.querySelector(":scope > .my-scope > .dropdown-menu")).toBeTruthy();
+
+ // Hiding resets both `shown` and `armed`, unmounting the portaled menu.
+ void act(() => {
+ $(dropdownEl as HTMLElement).trigger("hide.bs.dropdown");
+ });
+ expect(onHidden).toHaveBeenCalledTimes(1);
+ expect(document.body.querySelector(":scope > .my-scope")).toBeNull();
+ });
+
+ it("mounts and shows a forceShown portaled dropdown immediately", () => {
+ // No className: the portaled wrapper falls back to an empty class.
+ renderInto(<Dropdown portalToBody forceShown>item</Dropdown>);
+
+ expect(instance.show).toHaveBeenCalledTimes(1);
+ // The menu was mounted into the body (via the class-less wrapper) and wired as _menu.
+ expect(instance._menu?.classList.contains("dropdown-menu")).toBe(true);
+ expect(instance._menu?.closest("body")).toBeTruthy();
+ });
+});