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
import { ClassicEditor, Essentials, Paragraph } from "ckeditor5";
import { describe, expect, it } from "vitest";
import { applyTabs, getTabTitle, revealFragment, revealTab } from "./tabs_read_only.js";
const PLACEHOLDER = "Tab title";
const OPTIONS = { placeholder: PLACEHOLDER, scrollLabels: { start: "Back", end: "Next" } };
function renderTabs(html: string) {
const container = document.createElement("div");
container.innerHTML = html;
applyTabs(container, OPTIONS);
return container;
}
function tab(title: string, body: string) {
return `<section class="trilium-tab"><p class="trilium-tab-title">${title}</p>` +
`<div class="trilium-tab-panel">${body}</div></section>`;
}
function titleOf(container: HTMLElement, text: string) {
const title = [...container.querySelectorAll<HTMLElement>(".trilium-tab-title")]
.find(element => element.textContent === text);
expect(title).toBeDefined();
return title as HTMLElement;
}
function activeTitles(container: HTMLElement) {
return [...container.querySelectorAll(".trilium-tab-title--active")]
.map(title => title.textContent);
}
function press(element: HTMLElement, key: string) {
const event = new KeyboardEvent("keydown", { key, bubbles: true, cancelable: true });
element.dispatchEvent(event);
return event;
}
describe("applyTabs", () => {
it("shows the first tab and links each title to its panel", () => {
const container = renderTabs(`<div class="trilium-tabs">${tab("A", "<p>a</p>")}${tab("B", "<p>b</p>")}</div>`);
expect(activeTitles(container)).toEqual(["A"]);
const titles = [...container.querySelectorAll<HTMLElement>(".trilium-tab-title")];
const panels = [...container.querySelectorAll<HTMLElement>(".trilium-tab-panel")];
expect(titles.map(title => title.getAttribute("aria-expanded"))).toEqual(["true", "false"]);
for (const [index, title] of titles.entries()) {
const panel = panels[index];
expect(title.getAttribute("role")).toBe("button");
expect(title.tabIndex).toBe(0);
expect(panel.id).not.toBe("");
expect(title.getAttribute("aria-controls")).toBe(panel.id);
expect(panel.getAttribute("role")).toBe("region");
expect(panel.getAttribute("aria-labelledby")).toBe(title.id);
}
expect(new Set([...titles, ...panels].map(element => element.id)).size).toBe(4);
});
it("moves the titles into a title bar at the start of the block, in order", () => {
const container = renderTabs(
`<div class="trilium-tabs">${tab("A", "<p>a</p>")}${tab("B", "<p>b</p>")}</div>`
);
const block = container.querySelector(".trilium-tabs");
const bar = block?.firstElementChild;
expect(bar?.className).toBe("scrollable-bar trilium-tabs-bar");
expect([...bar?.querySelectorAll(":scope > .scrollable-bar-edge > button") ?? []]
.map(button => button.getAttribute("aria-label"))).toEqual(["Back", "Next"]);
expect([...bar?.querySelector(".scrollable-bar-viewport")?.children ?? []]
.map(title => title.textContent)).toEqual(["A", "B"]);
const sections = [...block?.querySelectorAll(":scope > .trilium-tab") ?? []];
expect(sections).toHaveLength(2);
for (const [index, section] of sections.entries()) {
const panel = section.firstElementChild as HTMLElement | null;
const title = getTabTitle(section);
expect([...section.children]).toEqual([panel]);
expect(title?.textContent).toBe(["A", "B"][index]);
for (const element of [title, panel]) {
expect(element?.style.getPropertyValue("--trilium-tab-index")).toBe(String(index));
}
}
});
it("switches tabs on click, Enter and Space", () => {
const container = renderTabs(
`<div class="trilium-tabs">${tab("A", "")}${tab("B", "")}${tab("C", "")}</div>`
);
titleOf(container, "B").click();
expect(activeTitles(container)).toEqual(["B"]);
expect(titleOf(container, "A").getAttribute("aria-expanded")).toBe("false");
expect(titleOf(container, "B").getAttribute("aria-expanded")).toBe("true");
press(titleOf(container, "C"), "Enter");
expect(activeTitles(container)).toEqual(["C"]);
expect(press(titleOf(container, "A"), " ").defaultPrevented).toBe(true);
expect(activeTitles(container)).toEqual(["A"]);
});
it("moves between titles with the arrow, Home and End keys", () => {
const container = renderTabs(
`<div class="trilium-tabs">${tab("A", "")}${tab("B", "")}${tab("C", "")}</div>`
);
document.body.appendChild(container);
press(titleOf(container, "A"), "ArrowRight");
expect(activeTitles(container)).toEqual(["B"]);
expect(document.activeElement).toBe(titleOf(container, "B"));
press(titleOf(container, "B"), "End");
expect(activeTitles(container)).toEqual(["C"]);
press(titleOf(container, "C"), "ArrowRight");
expect(activeTitles(container)).toEqual(["A"]);
press(titleOf(container, "A"), "ArrowLeft");
expect(activeTitles(container)).toEqual(["C"]);
press(titleOf(container, "C"), "Home");
expect(activeTitles(container)).toEqual(["A"]);
expect(document.activeElement).toBe(titleOf(container, "A"));
titleOf(container, "B").click();
applyTabs(container, OPTIONS);
expect(activeTitles(container)).toEqual(["B"]);
press(titleOf(container, "B"), "ArrowRight");
expect(activeTitles(container)).toEqual(["C"]);
container.remove();
});
it("labels a title that has no text with the editor's placeholder", () => {
const container = renderTabs(
`<div class="trilium-tabs">${tab(" ", "")}${tab("", "")}${tab("Named", "")}</div>`
);
const label = PLACEHOLDER;
const titles = [...container.querySelectorAll<HTMLElement>(".trilium-tab-title")];
expect(titles).toHaveLength(3);
for (const title of titles.slice(0, 2)) {
expect(title.textContent).toBe("");
expect(title.dataset.placeholder).toBe(label);
expect(title.getAttribute("aria-label")).toBe(label);
}
expect(titles[2].dataset.placeholder).toBeUndefined();
expect(titles[2].hasAttribute("aria-label")).toBe(false);
});
it("reverses the arrow keys right to left and ignores other keys", () => {
const container = renderTabs(
`<div class="trilium-tabs" dir="rtl">${tab("A", "")}${tab("B", "")}${tab("C", "")}</div>`
);
document.body.appendChild(container);
press(titleOf(container, "A"), "ArrowLeft");
expect(activeTitles(container)).toEqual(["B"]);
press(titleOf(container, "B"), "ArrowRight");
expect(activeTitles(container)).toEqual(["A"]);
expect(press(titleOf(container, "A"), "a").defaultPrevented).toBe(false);
expect(activeTitles(container)).toEqual(["A"]);
container.remove();
});
it("skips a tab without a title or a panel, and a block left without tabs", () => {
const container = renderTabs(
`<div class="trilium-tabs">` +
`<section class="trilium-tab"><p class="trilium-tab-title">Orphan</p></section>` +
`<section class="trilium-tab"><div class="trilium-tab-panel"><p id="untitled">x</p></div></section>` +
`</div>` +
`<div class="trilium-tabs">${tab("A", "")}${tab("B", "")}</div>`
);
expect(titleOf(container, "Orphan").hasAttribute("role")).toBe(false);
expect(activeTitles(container)).toEqual(["A"]);
revealTab(container.querySelector("#untitled") as Element);
expect(activeTitles(container)).toEqual(["A"]);
});
it("keeps nested tabs blocks independent", () => {
const inner = `<div class="trilium-tabs">${tab("Inner 1", "")}${tab("Inner 2", "")}</div>`;
const container = renderTabs(
`<div class="trilium-tabs">${tab("Outer 1", inner)}${tab("Outer 2", "")}</div>`
);
expect(activeTitles(container)).toEqual(["Outer 1", "Inner 1"]);
titleOf(container, "Inner 2").click();
expect(activeTitles(container)).toEqual(["Outer 1", "Inner 2"]);
press(titleOf(container, "Inner 2"), "ArrowRight");
expect(activeTitles(container)).toEqual(["Outer 1", "Inner 1"]);
});
it("reveals every tab that encloses an element", () => {
const inner = `<div class="trilium-tabs">${tab("Inner 1", "")}${tab("Inner 2", "<p id=\"target\">x</p>")}</div>`;
const container = renderTabs(
`<div class="trilium-tabs">${tab("Outer 1", "")}${tab("Outer 2", inner)}</div><p id="outside">y</p>`
);
expect(activeTitles(container)).toEqual(["Outer 1", "Inner 1"]);
const target = container.querySelector("#target");
expect(target).not.toBeNull();
revealTab(target as Element);
expect(activeTitles(container)).toEqual(["Outer 2", "Inner 2"]);
expect(titleOf(container, "Outer 1").getAttribute("aria-expanded")).toBe("false");
titleOf(container, "Outer 1").click();
revealTab(container.querySelector("#outside") as Element);
expect(activeTitles(container)).toEqual(["Outer 1", "Inner 2"]);
});
it("reveals the element a URL fragment names", () => {
const container = renderTabs(
`<div class="trilium-tabs">${tab("A", "")}${tab("B", "<h2 id=\"step two\">x</h2>")}</div>`
);
document.body.append(container);
expect(revealFragment("#step%20two")?.textContent).toBe("x");
expect(activeTitles(container)).toEqual(["B"]);
expect(revealFragment("")).toBeNull();
expect(revealFragment("#missing")).toBeNull();
expect(revealFragment("#%E0%A4%A")).toBeNull();
container.remove();
});
it("leaves an editor without the tabs plugin alone", async () => {
const domElement = document.createElement("div");
document.body.appendChild(domElement);
const editor = await ClassicEditor.create(domElement, {
licenseKey: "GPL",
plugins: [Essentials, Paragraph]
});
editor.setData("<p>x</p>");
const paragraph = editor.editing.view.getDomRoot()?.querySelector("p");
expect(paragraph).toBeTruthy();
expect(() => revealTab(paragraph as Element)).not.toThrow();
await editor.destroy();
domElement.remove();
});
});