Skip to content

Build your personal knowledge base with Trilium Notes

test(attributes): cover the detail popup's positioning

Elian Doran0824e839973f
diff --git a/apps/client/src/widgets/attribute_widgets/attribute_detail.spec.ts b/apps/client/src/widgets/attribute_widgets/attribute_detail.spec.ts
new file mode 100644
index 0000000000..85ccb692ba
--- /dev/null
+++ b/apps/client/src/widgets/attribute_widgets/attribute_detail.spec.ts
@@ -0,0 +1,176 @@
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+
+// The popup docks differently under the new layout, and the flag behind that is read once when the
+// module loads — so the two are exercised by loading it twice.
+const newLayout = vi.hoisted(() => ({ enabled: false }));
+vi.mock("../../services/experimental_features", async (importOriginal) => ({
+ ...(await importOriginal<typeof import("../../services/experimental_features")>()),
+ isExperimentalFeatureEnabled: () => newLayout.enabled
+}));
+
+import type { AttributeDetailOpts } from "./attribute_detail";
+
+const VIEWPORT = { width: 1200, height: 800 };
+
+describe("attribute detail popup positioning", () => {
+ beforeEach(() => {
+ sizeViewport(VIEWPORT.width, VIEWPORT.height);
+ });
+
+ afterEach(() => {
+ document.body.replaceChildren();
+ });
+
+ it("leaves a popup that has not been laid out where it is", async () => {
+ const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
+ const popup = sized(document.createElement("div"), 0, 0);
+
+ (await load()).positionPopup(popup, opts({ x: 100, y: 100 }), { top: 0, left: 0 });
+
+ expect(popup.style.left).toBe("");
+ expect(warn).toHaveBeenCalledWith(expect.stringContaining("position popup"));
+ warn.mockRestore();
+ });
+
+ it("puts an anchored popup on whichever side of its anchor has the room for it", async () => {
+ const { positionPopup } = await load();
+ const popup = sized(document.createElement("div"), 300, 400);
+
+ // Room to the left of the anchor: 300 wide plus the gap and the margin fit before it.
+ positionPopup(popup, opts({ anchor: anchoredAt({ left: 900, right: 1100, top: 120 }) }), NO_OFFSET);
+ expect(popup.style.left).toBe("592px"); // 900 - 300 - 8
+ expect(popup.style.top).toBe("120px");
+ expect(popup.style.right).toBe("");
+ expect(popup.style.maxHeight).toBe("780px"); // the viewport less a margin above and below
+
+ // No room to the left, so it takes the right — and slides up to stay wholly on screen.
+ positionPopup(popup, opts({ anchor: anchoredAt({ left: 40, right: 200, top: 700 }) }), NO_OFFSET);
+ expect(popup.style.left).toBe("208px"); // 200 + 8
+ expect(popup.style.top).toBe("390px"); // 800 - 400 - 10
+
+ // Narrower than the popup on either side: it keeps to the viewport's left margin.
+ sizeViewport(305, VIEWPORT.height);
+ positionPopup(popup, opts({ anchor: anchoredAt({ left: 10, right: 300, top: 40 }) }), NO_OFFSET);
+ expect(popup.style.left).toBe("10px");
+ });
+
+ it("places an unanchored popup below the click, against whichever edge it falls nearest", async () => {
+ const { positionPopup } = await load();
+ const popup = sized(document.createElement("div"), 300, 200);
+
+ // A press far enough left that the popup would run off the edge is held against it.
+ positionPopup(popup, opts({ x: 100, y: 100 }), { top: 30, left: 0 });
+ expect(popup.style.left).toBe("10px");
+ expect(popup.style.right).toBe("");
+ expect(popup.style.top).toBe("140px"); // 100 - 30 + 70
+ // Room enough below the click for the whole popup, so it is left uncapped.
+ expect(popup.style.maxHeight).toBe("10000px");
+
+ // Anywhere else the popup pins to the right edge, and a press low down caps its height.
+ positionPopup(popup, opts({ x: 600, y: 700 }), NO_OFFSET);
+ expect(popup.style.left).toBe("");
+ expect(popup.style.right).toBe("10px");
+ expect(popup.style.maxHeight).toBe("50px"); // 800 - 700 - 50
+ });
+
+ it("sits above the attributes pane under the new layout, and above the status bar without one", async () => {
+ newLayout.enabled = true;
+ const { positionPopup } = await load();
+ const popup = sized(document.createElement("div"), 300, 200);
+ sizeBody(VIEWPORT.height);
+
+ const pane = document.createElement("div");
+ pane.className = "bottom-panel attribute-list";
+ pane.getBoundingClientRect = (() => ({ top: 600 })) as Element["getBoundingClientRect"];
+ document.body.append(pane);
+
+ positionPopup(popup, opts({ x: 500, y: 100 }), NO_OFFSET);
+ expect(popup.style.top).toBe("unset");
+ expect(popup.style.bottom).toBe("200px"); // the body's height down to the pane
+ expect(popup.style.maxHeight).toBe("600px");
+ expect(popup.style.left).toBe("350px"); // centred on the press
+
+ // With the pane away, the status bar is what the popup docks to instead.
+ pane.classList.add("hidden-ext");
+ const statusBar = sized(document.createElement("div"), 1200, 40);
+ statusBar.className = "component status-bar";
+ document.body.append(statusBar);
+
+ // A press near the left edge is held a margin away from it rather than centred.
+ positionPopup(popup, opts({ x: 100, y: 100 }), NO_OFFSET);
+ expect(popup.style.bottom).toBe("40px");
+ expect(popup.style.maxHeight).toBe("760px");
+ expect(popup.style.left).toBe("10px");
+
+ newLayout.enabled = false;
+ });
+
+ /** Re-imports the module so the layout flag above is read afresh. */
+ async function load() {
+ vi.resetModules();
+ return await import("./attribute_detail");
+ }
+});
+
+describe("attribute detail popup naming", () => {
+ it("edits a definition under its bare name and stores it back under its prefix", async () => {
+ const { addDefinitionPrefix, stripDefinitionPrefix } = await import("./attribute_detail");
+
+ expect(stripDefinitionPrefix("label:priority", "label-definition")).toBe("priority");
+ expect(stripDefinitionPrefix("relation:author", "relation-definition")).toBe("author");
+ expect(stripDefinitionPrefix("priority", "label")).toBe("priority");
+
+ expect(addDefinitionPrefix("priority", "label-definition")).toBe("label:priority");
+ expect(addDefinitionPrefix("author", "relation-definition")).toBe("relation:author");
+ expect(addDefinitionPrefix("priority", "relation")).toBe("priority");
+ });
+
+ it("searches for every note carrying the attribute by name alone", async () => {
+ const { formatAttributeForSearch } = await import("./attribute_detail");
+
+ expect(formatAttributeForSearch({ type: "label", name: "author" })).toBe("#author");
+ expect(formatAttributeForSearch({ type: "relation", name: "template" })).toBe("~template");
+ });
+
+ it("explains only the names Trilium attaches a meaning to, and points at a help page where there is one", async () => {
+ const { lookupAttributeHelp } = await import("./attribute_detail");
+
+ expect(lookupAttributeHelp("label", "customRequestHandler")).toMatchObject({ helpPage: "J5Ex1ZrMbyJ6" });
+ expect(lookupAttributeHelp("label", "somethingInvented")).toBeUndefined();
+ expect(lookupAttributeHelp(undefined, "customRequestHandler")).toBeUndefined();
+ });
+});
+
+const NO_OFFSET = { top: 0, left: 0 };
+
+function opts(overrides: Partial<AttributeDetailOpts>): AttributeDetailOpts {
+ return {
+ attribute: { type: "label", name: "author", value: "" },
+ isOwned: true,
+ x: 0,
+ y: 0,
+ ...overrides
+ };
+}
+
+/** happy-dom lays nothing out, so the sizes the positioning reads are given to it directly. */
+function sized(el: HTMLElement, width: number, height: number) {
+ Object.defineProperty(el, "offsetWidth", { value: width, configurable: true });
+ Object.defineProperty(el, "offsetHeight", { value: height, configurable: true });
+ return el;
+}
+
+function anchoredAt({ left, right, top }: { left: number; right: number; top: number }) {
+ const anchor = document.createElement("div");
+ anchor.getBoundingClientRect = (() => ({ left, right, top })) as Element["getBoundingClientRect"];
+ return anchor;
+}
+
+function sizeViewport(width: number, height: number) {
+ Object.defineProperty(document.documentElement, "clientWidth", { value: width, configurable: true });
+ Object.defineProperty(document.documentElement, "clientHeight", { value: height, configurable: true });
+}
+
+function sizeBody(height: number) {
+ Object.defineProperty(document.body, "clientHeight", { value: height, configurable: true });
+}
diff --git a/apps/client/src/widgets/attribute_widgets/attribute_detail.tsx b/apps/client/src/widgets/attribute_widgets/attribute_detail.tsx
index f0e5eb9815..47a8c34b27 100644
--- a/apps/client/src/widgets/attribute_widgets/attribute_detail.tsx
+++ b/apps/client/src/widgets/attribute_widgets/attribute_detail.tsx
@@ -630,7 +630,7 @@ function AttributeNameSuggestion({ type, name }: { type: "label" | "relation"; n
}
/** Normalised so that the shorthand entries, which are the description alone, read like the rest. */
-function lookupAttributeHelp(attrType: AttrType, name: string): AttrHelpEntry | undefined {
+export function lookupAttributeHelp(attrType: AttrType, name: string): AttrHelpEntry | undefined {
const entry = attrType ? ATTR_HELP[attrType]?.[name] : undefined;
return typeof entry === "string" ? { description: entry } : entry;
}
@@ -803,7 +803,7 @@ function RelatedNotesBadge({ attribute, currentNoteId }: { attribute: Attribute;
}
/** The query for every note carrying the attribute, mirroring the name-only search behind the count. */
-function formatAttributeForSearch({ type, name }: Attribute) {
+export function formatAttributeForSearch({ type, name }: Attribute) {
// Names are filtered as they are typed, so there is nothing in one that would need quoting.
return `${type === "label" ? "#" : "~"}${name}`;
}
@@ -823,7 +823,7 @@ const ATTR_TITLES: Record<string, string> = {
const isNewLayout = isExperimentalFeatureEnabled("new-layout");
-function positionPopup(popup: HTMLElement, { x, y, anchor }: AttributeDetailOpts, parentOffset: { top: number; left: number }) {
+export function positionPopup(popup: HTMLElement, { x, y, anchor }: AttributeDetailOpts, parentOffset: { top: number; left: number }) {
const outerWidth = popup.offsetWidth;
const outerHeight = popup.offsetHeight;
const windowHeight = document.documentElement.clientHeight;
@@ -938,7 +938,7 @@ export function getAttrType(attribute: Attribute): AttrType {
}
/** Definitions are stored prefixed (`label:foo`), but the popup edits the bare name. */
-function stripDefinitionPrefix(name: string, attrType: AttrType) {
+export function stripDefinitionPrefix(name: string, attrType: AttrType) {
if (attrType === "label-definition") {
return name.substring("label:".length);
} else if (attrType === "relation-definition") {
@@ -947,7 +947,7 @@ function stripDefinitionPrefix(name: string, attrType: AttrType) {
return name;
}
-function addDefinitionPrefix(name: string, attrType: AttrType) {
+export function addDefinitionPrefix(name: string, attrType: AttrType) {
if (attrType === "label-definition") {
return `label:${name}`;
} else if (attrType === "relation-definition") {