Skip to content

Build your personal knowledge base with Trilium Notes

chore(mobile): cover the note panel's attributes entry and tab strip

Elian Doran1c7cc152e018
diff --git a/apps/client/src/widgets/mobile_widgets/MobileNotePanel.spec.tsx b/apps/client/src/widgets/mobile_widgets/MobileNotePanel.spec.tsx
index 13b60ed460..5e4b7273e1 100644
--- a/apps/client/src/widgets/mobile_widgets/MobileNotePanel.spec.tsx
+++ b/apps/client/src/widgets/mobile_widgets/MobileNotePanel.spec.tsx
@@ -5,10 +5,11 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import type FNote from "../../entities/fnote";
const shownNote = vi.hoisted(() => ({ current: null as FNote | null }));
+const shownContext = vi.hoisted(() => ({ current: null as { isCommandTarget: (ntxId?: string | null) => boolean } | null }));
vi.mock("../react/hooks", async (importOriginal) => ({
...(await importOriginal<typeof import("../react/hooks")>()),
useActiveNoteContext: () => ({ note: shownNote.current }),
- useNoteContext: () => ({ note: shownNote.current, noteContext: null }),
+ useNoteContext: () => ({ note: shownNote.current, noteContext: shownContext.current }),
useStaticTooltip: vi.fn()
}));
@@ -82,6 +83,7 @@ describe("MobileNotePanel", () => {
});
afterEach(() => {
+ shownContext.current = null;
act(() => render(null, container));
container.remove();
for (const stub of document.body.querySelectorAll(".modal-stub")) stub.remove();
@@ -89,15 +91,7 @@ describe("MobileNotePanel", () => {
it("mounts the tabs on the first open and keeps them mounted once closed", () => {
shownNote.current = buildNote({ title: "Read" });
- const parent = { componentId: "cid", registerHandler: () => {}, removeHandler: () => {} } as unknown as Component;
- const widgetsByParent = { getLegacyWidgets: () => [], getPreactWidgets: () => [] };
- act(() => {
- render((
- <ParentComponent.Provider value={parent}>
- <MobileNotePanel widgetsByParent={widgetsByParent as never} />
- </ParentComponent.Provider>
- ), container);
- });
+ renderPanel();
expect(document.body.querySelector(".toc-stub")).toBeNull();
const button = container.querySelector("button");
@@ -108,4 +102,42 @@ describe("MobileNotePanel", () => {
act(() => modal.onHidden?.());
expect(document.body.querySelector(".toc-stub")).not.toBeNull();
});
+
+ it("opens on the attributes tab when the attributes are asked for in its own note context only", () => {
+ shownNote.current = buildNote({ title: "Read" });
+ shownContext.current = { isCommandTarget: (ntxId) => ntxId === "ntx1" };
+ const handlers = renderPanel();
+ const showNoteAttributes = handlers.get("showNoteAttributes");
+ expect(showNoteAttributes).toBeDefined();
+
+ act(() => showNoteAttributes?.({ ntxId: "ntx2" }));
+ expect(document.body.querySelector(".modal-stub")).toBeNull();
+
+ act(() => showNoteAttributes?.({ ntxId: "ntx1" }));
+ const attributesTab = document.body.querySelector(".right-pane-flat-tab-body[data-tab-id='attributes']");
+ expect(attributesTab?.classList.contains("hidden-ext")).toBe(false);
+ expect(attributesTab?.querySelector(".attribute-list-stub")).not.toBeNull();
+ expect(document.body.querySelector(".toc-stub")).toBeNull();
+ });
+
+ /** Renders the panel, returning the event handlers it registers by event name. */
+ function renderPanel() {
+ const handlers = new Map<string, (data: unknown) => void>();
+ const parent = {
+ componentId: "cid",
+ registerHandler: (name: string, handler: (data: unknown) => void) => handlers.set(name, handler),
+ removeHandler: (name: string, handler: (data: unknown) => void) => {
+ if (handlers.get(name) === handler) handlers.delete(name);
+ }
+ } as unknown as Component;
+ const widgetsByParent = { getLegacyWidgets: () => [], getPreactWidgets: () => [] };
+ act(() => {
+ render((
+ <ParentComponent.Provider value={parent}>
+ <MobileNotePanel widgetsByParent={widgetsByParent as never} />
+ </ParentComponent.Provider>
+ ), container);
+ });
+ return handlers;
+ }
});
diff --git a/apps/client/src/widgets/react/TabStrip.spec.tsx b/apps/client/src/widgets/react/TabStrip.spec.tsx
new file mode 100644
index 0000000000..c0395c9800
--- /dev/null
+++ b/apps/client/src/widgets/react/TabStrip.spec.tsx
@@ -0,0 +1,45 @@
+import { describe, expect, it, vi } from "vitest";
+
+const useStaticTooltip = vi.hoisted(() => vi.fn());
+vi.mock("./hooks", () => ({ useStaticTooltip }));
+
+import { renderInto } from "../../test/render";
+import TabStrip from "./TabStrip";
+
+const tabs = [
+ { id: "outline", title: "Outline", icon: "bx bx-list-ul" },
+ { id: "chat", title: "AI chat", icon: "bx bx-bot" }
+] as const;
+
+describe("TabStrip", () => {
+ it("names each icon by its tooltip, and selects a tab on a click", () => {
+ const onSelect = vi.fn();
+ const container = renderInto(<TabStrip tabs={[ ...tabs ]} activeTabId="chat" onSelect={onSelect} />);
+ const buttons = [ ...container.querySelectorAll("button.tab-strip-tab") ];
+
+ expect(buttons.map((b) => b.getAttribute("aria-label"))).toEqual([ "Outline", "AI chat" ]);
+ expect(buttons.map((b) => b.getAttribute("aria-selected"))).toEqual([ "false", "true" ]);
+ expect(container.querySelectorAll(".tab-strip-tab-icon")).toHaveLength(2);
+ expect(container.querySelector(".tab-strip-tab-label")).toBeNull();
+ expect(useStaticTooltip).toHaveBeenCalledWith(expect.anything(), expect.objectContaining({ title: "Outline" }));
+
+ (buttons[0] as HTMLButtonElement).click();
+ expect(onSelect).toHaveBeenCalledWith("outline");
+ });
+
+ it("names each tab in text when labelled, with its icon above only when asked to", () => {
+ useStaticTooltip.mockClear();
+ const labelled = renderInto(<TabStrip tabs={[ ...tabs ]} onSelect={vi.fn()} labelled />);
+ expect(labelled.querySelector(".tab-strip")?.classList.contains("tab-strip-labelled")).toBe(true);
+ expect([ ...labelled.querySelectorAll(".tab-strip-tab-label") ].map((l) => l.textContent)).toEqual([ "Outline", "AI chat" ]);
+ expect(labelled.querySelector(".tab-strip-tab-icon")).toBeNull();
+ expect(labelled.querySelector("button")?.hasAttribute("aria-label")).toBe(false);
+ expect(useStaticTooltip).toHaveBeenCalledWith(expect.anything(), undefined);
+ expect(useStaticTooltip).not.toHaveBeenCalledWith(expect.anything(), expect.objectContaining({ title: "Outline" }));
+
+ const withIcons = renderInto(<TabStrip tabs={[ ...tabs ]} onSelect={vi.fn()} labelled withIcons />);
+ expect(withIcons.querySelector(".tab-strip")?.classList.contains("tab-strip-with-icons")).toBe(true);
+ expect(withIcons.querySelectorAll(".tab-strip-tab-icon")).toHaveLength(2);
+ expect(withIcons.querySelectorAll(".tab-strip-tab-label")).toHaveLength(2);
+ });
+});
diff --git a/apps/client/src/widgets/sidebar/SidebarChat.spec.tsx b/apps/client/src/widgets/sidebar/SidebarChat.spec.tsx
index c20bd3f30a..9761b8ea4e 100644
--- a/apps/client/src/widgets/sidebar/SidebarChat.spec.tsx
+++ b/apps/client/src/widgets/sidebar/SidebarChat.spec.tsx
@@ -6,6 +6,8 @@ import { renderInto } from "../../test/render";
/** The chat notes on the server, most recently modified first. */
let serverChats: string[] = [];
let createdCount = 0;
+/** The title of the open chat note. */
+let chatTitle: string | undefined;
vi.mock("../../services/date_notes.js", () => ({
default: {
@@ -57,7 +59,7 @@ vi.mock("../type_widgets/llm_chat/ChatInputBar.js", () => ({
vi.mock("../react/hooks.js", () => ({
useActiveNoteContext: () => ({ noteId: null, note: null }),
useNote: () => null,
- useNoteProperty: () => undefined,
+ useNoteProperty: () => chatTitle,
useNoteLabelBoolean: () => [ false ],
useSpacedUpdate: () => ({ scheduleUpdate: vi.fn(), updateNowIfNecessary: async () => undefined })
}));
@@ -65,7 +67,7 @@ vi.mock("../react/ActionButton.js", () => ({
default: ({ icon, onClick }: { icon: string; onClick: (e: MouseEvent) => void }) => <button data-icon={icon} onClick={onClick} />
}));
vi.mock("../react/Dropdown.js", () => ({
- default: ({ children }: { children: ComponentChildren }) => <div>{children}</div>
+ default: ({ text, children }: { text?: ComponentChildren; children: ComponentChildren }) => <div>{text}{children}</div>
}));
vi.mock("../react/FormList.js", () => ({
FormListItem: ({ icon, onClick, children }: { icon?: string; onClick?: () => void; children: ComponentChildren }) =>
@@ -87,6 +89,7 @@ describe("SidebarChat", () => {
beforeEach(() => {
serverChats = [ "chatOpen", "chatOlder" ];
createdCount = 0;
+ chatTitle = undefined;
fakeChat.chatNoteId = undefined;
fakeChat.submittedTo = undefined;
});
@@ -110,7 +113,8 @@ describe("SidebarChat", () => {
expect(serverChats).toEqual([ "chatNew1", "chatOlder" ]);
});
- it("puts its controls in the host's header slot when there is one", async () => {
+ it("puts its controls in the host's header slot when there is one, titled by the conversation", async () => {
+ chatTitle = "Trip planning";
const slot = document.createElement("div");
document.body.appendChild(slot);
const container = renderInto(
@@ -123,6 +127,7 @@ describe("SidebarChat", () => {
expect(slot.querySelector("[data-icon='bx bx-plus']")).not.toBeNull();
expect(slot.querySelector("li[data-icon='bx bx-trash']")).not.toBeNull();
expect(container.querySelector("[data-icon='bx bx-plus']")).toBeNull();
+ expect(slot.querySelector(".sidebar-chat-title")?.textContent).toBe("Trip planning");
slot.remove();
});
});