Skip to content

Build your personal knowledge base with Trilium Notes

client/content manager: improve test coverage

Adorian Doran651eb7104437
diff --git a/apps/client/src/menus/link_context_menu.spec.ts b/apps/client/src/menus/link_context_menu.spec.ts
new file mode 100644
index 0000000000..c11c1789b8
--- /dev/null
+++ b/apps/client/src/menus/link_context_menu.spec.ts
@@ -0,0 +1,187 @@
+import { beforeEach, describe, expect, it, vi } from "vitest";
+
+import type { ContextMenuEvent } from "./context_menu";
+
+const mocks = vi.hoisted(() => ({
+ show: vi.fn(),
+ triggerCommand: vi.fn(),
+ openContextWithNote: vi.fn(),
+ isMobile: vi.fn(() => false),
+ isDesktop: vi.fn(() => true),
+ getClosestNtxId: vi.fn((): string | null => null),
+ /** The splits of the active tab, which decide the "new split" vs "other split" wording. */
+ subContexts: [ { ntxId: "ntx-first" }, { ntxId: "ntx-last" } ] as { ntxId: string }[],
+ activeNtxId: "ntx-active" as string | null,
+ /** False when no tab is open at all, which leaves both the hoisting and the split unresolvable. */
+ hasActiveContext: true
+}));
+
+vi.mock("./context_menu", () => ({ default: { show: mocks.show } }));
+
+vi.mock("../services/i18n", () => ({ t: (key: string) => key }));
+
+vi.mock("../services/utils", () => ({
+ default: { isDesktop: mocks.isDesktop },
+ isMobile: mocks.isMobile
+}));
+
+vi.mock("../widgets/widget_utils", () => ({ getClosestNtxId: mocks.getClosestNtxId }));
+
+vi.mock("../components/app_context", () => ({
+ default: {
+ triggerCommand: mocks.triggerCommand,
+ tabManager: {
+ get activeNtxId() {
+ return mocks.activeNtxId;
+ },
+ openContextWithNote: mocks.openContextWithNote,
+ getActiveContext: () => mocks.hasActiveContext
+ ? { hoistedNoteId: "hoistedNote", getSubContexts: () => mocks.subContexts }
+ : undefined,
+ getNoteContextById: () => ({
+ getMainContext: () => ({ getSubContexts: () => mocks.subContexts })
+ })
+ }
+ }
+}));
+
+import linkContextMenu from "./link_context_menu";
+
+const VIEW_SCOPE = { viewMode: "attachments" as const, attachmentId: "att-1" };
+
+function contextMenuEvent(target?: HTMLElement) {
+ return { pageX: 12, pageY: 34, target } as unknown as ContextMenuEvent;
+}
+
+function handle(command: string | undefined, viewScope = {}, hoistedNoteId: string | null = null) {
+ return linkContextMenu.handleLinkContextMenuItem(command, contextMenuEvent(), "root/n1", viewScope, hoistedNoteId);
+}
+
+beforeEach(() => {
+ vi.clearAllMocks();
+ mocks.isMobile.mockReturnValue(false);
+ mocks.isDesktop.mockReturnValue(true);
+ mocks.getClosestNtxId.mockReturnValue(null);
+ mocks.subContexts = [ { ntxId: "ntx-first" }, { ntxId: "ntx-last" } ];
+ mocks.activeNtxId = "ntx-active";
+ mocks.hasActiveContext = true;
+});
+
+describe("getItems", () => {
+ it("offers the four ways of opening a link, the split one worded for the layout", () => {
+ const items = linkContextMenu.getItems(contextMenuEvent());
+
+ expect(items.map((item) => "command" in item && item.command)).toEqual([
+ "openNoteInNewTab", "openNoteInNewSplit", "openNoteInNewWindow", "openNoteInPopup"
+ ]);
+ // On the desktop the split always opens beside the current one.
+ expect(items[1]).toMatchObject({ title: "link_context_menu.open_note_in_new_split" });
+
+ // On mobile a tab holds one split at a time, so with one already open the link goes to the
+ // *other* split rather than to a new one.
+ mocks.isMobile.mockReturnValue(true);
+ expect(linkContextMenu.getItems(contextMenuEvent())[1])
+ .toMatchObject({ title: "link_context_menu.open_note_in_other_split" });
+
+ mocks.subContexts = [ { ntxId: "ntx-only" } ];
+ expect(linkContextMenu.getItems(contextMenuEvent())[1])
+ .toMatchObject({ title: "link_context_menu.open_note_in_new_split" });
+ });
+});
+
+describe("handleLinkContextMenuItem", () => {
+ it("opens a new tab under the given hoisting, falling back to the active context's", () => {
+ expect(handle("openNoteInNewTab", VIEW_SCOPE, "explicitHoist")).toBe(true);
+ expect(mocks.openContextWithNote).toHaveBeenCalledWith("root/n1", {
+ hoistedNoteId: "explicitHoist",
+ viewScope: VIEW_SCOPE
+ });
+
+ handle("openNoteInNewTab");
+ expect(mocks.openContextWithNote).toHaveBeenLastCalledWith("root/n1", {
+ hoistedNoteId: "hoistedNote",
+ viewScope: {}
+ });
+ });
+
+ it("opens a split beside the last one, a window, and the popup — all carrying the view scope", () => {
+ expect(handle("openNoteInNewSplit", VIEW_SCOPE)).toBe(true);
+ expect(mocks.triggerCommand).toHaveBeenLastCalledWith("openNewNoteSplit", {
+ ntxId: "ntx-last",
+ notePath: "root/n1",
+ hoistedNoteId: "hoistedNote",
+ viewScope: VIEW_SCOPE
+ });
+
+ expect(handle("openNoteInNewWindow", VIEW_SCOPE)).toBe(true);
+ expect(mocks.triggerCommand).toHaveBeenLastCalledWith("openInWindow", {
+ notePath: "root/n1",
+ hoistedNoteId: "hoistedNote",
+ viewScope: VIEW_SCOPE
+ });
+
+ // The view scope is what lets the popup show an attachment rather than its owning note.
+ expect(handle("openNoteInPopup", VIEW_SCOPE)).toBe(true);
+ expect(mocks.triggerCommand).toHaveBeenLastCalledWith("openInPopup", {
+ noteIdOrPath: "root/n1",
+ viewScope: VIEW_SCOPE
+ });
+ });
+
+ it("does nothing for a command it does not own, or a split with no context to open into", () => {
+ expect(handle("someOtherCommand")).toBe(false);
+ expect(handle(undefined)).toBe(false);
+
+ // Nothing to resolve a context from: no pane under the pointer and no active tab either.
+ mocks.isDesktop.mockReturnValue(false);
+ mocks.activeNtxId = null;
+ expect(handle("openNoteInNewSplit")).toBe(false);
+ expect(mocks.triggerCommand).not.toHaveBeenCalled();
+ });
+
+ it("opens with no hoisting, and no split at all, when there is no tab open", () => {
+ mocks.hasActiveContext = false;
+
+ expect(handle("openNoteInNewTab")).toBe(true);
+ expect(mocks.openContextWithNote).toHaveBeenCalledWith("root/n1", {
+ hoistedNoteId: null,
+ viewScope: {}
+ });
+
+ // On the desktop the split is resolved from that same missing tab.
+ expect(handle("openNoteInNewSplit")).toBe(false);
+ expect(mocks.triggerCommand).not.toHaveBeenCalled();
+ });
+
+ it("resolves the split from the event's own pane on mobile, and from the active tab otherwise", () => {
+ mocks.isDesktop.mockReturnValue(false);
+ mocks.getClosestNtxId.mockReturnValue("ntx-from-dom");
+ const event = contextMenuEvent(document.createElement("div"));
+
+ linkContextMenu.handleLinkContextMenuItem("openNoteInNewSplit", event, "root/n1");
+ expect(mocks.triggerCommand).toHaveBeenLastCalledWith("openNewNoteSplit",
+ expect.objectContaining({ ntxId: "ntx-from-dom" }));
+
+ // Outside any note-context DOM (e.g. the mobile sidebar) the active context stands in.
+ mocks.getClosestNtxId.mockReturnValue(null);
+ linkContextMenu.handleLinkContextMenuItem("openNoteInNewSplit", event, "root/n1");
+ expect(mocks.triggerCommand).toHaveBeenLastCalledWith("openNewNoteSplit",
+ expect.objectContaining({ ntxId: "ntx-active" }));
+ });
+});
+
+describe("openContextMenu", () => {
+ it("shows the menu at the pointer and routes the chosen item with the link's own state", () => {
+ linkContextMenu.openContextMenu("root/n1", contextMenuEvent(), VIEW_SCOPE, "explicitHoist");
+
+ const shown = mocks.show.mock.calls[0][0];
+ expect(shown).toMatchObject({ x: 12, y: 34 });
+ expect(shown.items).toHaveLength(4);
+
+ shown.selectMenuItemHandler({ command: "openNoteInPopup" });
+ expect(mocks.triggerCommand).toHaveBeenCalledWith("openInPopup", {
+ noteIdOrPath: "root/n1",
+ viewScope: VIEW_SCOPE
+ });
+ });
+});
diff --git a/apps/client/src/widgets/react/master_detail.spec.tsx b/apps/client/src/widgets/react/master_detail.spec.tsx
new file mode 100644
index 0000000000..aac0fdf6a5
--- /dev/null
+++ b/apps/client/src/widgets/react/master_detail.spec.tsx
@@ -0,0 +1,295 @@
+import { type ComponentChildren, type RefObject, render } from "preact";
+import { useRef } from "preact/hooks";
+import { act } from "preact/test-utils";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+
+const mocks = vi.hoisted(() => ({
+ isMobile: vi.fn(() => true),
+ windowWidth: 500,
+ /** The header the modal stub was handed, so the flow's own header can be asserted on. */
+ modalHeader: undefined as ComponentChildren
+}));
+
+vi.mock("../../services/utils", async (importActual) => {
+ const actual = await importActual<typeof import("../../services/utils")>();
+ return { ...actual, default: { ...actual.default, isMobile: mocks.isMobile } };
+});
+
+vi.mock("./hooks", async (importOriginal) => ({
+ ...(await importOriginal<typeof import("./hooks")>()),
+ useWindowSize: () => ({ windowWidth: mocks.windowWidth, windowHeight: 800 })
+}));
+
+// Bootstrap drives the real modal; what matters here is the header the flow hands it.
+interface ModalStubProps {
+ header?: ComponentChildren;
+ children?: ComponentChildren;
+ modalRef?: RefObject<HTMLDivElement>;
+}
+
+vi.mock("./Modal", () => ({
+ default: ({ header, children, modalRef }: ModalStubProps) => {
+ mocks.modalHeader = header;
+ return <div ref={modalRef} className="modal-stub">{header}{children}</div>;
+ }
+}));
+
+vi.mock("./ActionButton", () => ({
+ default: ({ icon, text, onClick }: { icon: string, text: string, onClick?: () => void }) => (
+ <button type="button" className={`action-stub ${icon}`} title={text} onClick={onClick} />
+ )
+}));
+
+import {
+ DetailPane,
+ MasterDetailHeader,
+ MasterDetailModal,
+ MasterPane,
+ type MobileMasterDetail,
+ useMasterDetail,
+ useMasterDetailPage,
+ useMobileMasterDetail
+} from "./master_detail";
+
+let container: HTMLDivElement;
+/** The flow's API, as the host component last saw it. */
+let flow: MobileMasterDetail | undefined;
+
+function Host() {
+ const modalRef = useRef<HTMLDivElement>(null);
+ flow = useMobileMasterDetail(modalRef);
+ return <div ref={modalRef} className="host" />;
+}
+
+function host() {
+ const el = container.querySelector<HTMLElement>(".host");
+ expect(el).not.toBeNull();
+ return el;
+}
+
+/** The classes master_detail.css lays the flow out with, in the order the hook sets them. */
+function flowClasses() {
+ return [ ...(host()?.classList ?? []) ].filter((name) => name !== "host");
+}
+
+/** Mounts afresh: the flow reads the viewport as it is when it mounts, so each case gets its own. */
+function renderHost() {
+ act(() => render(null, container));
+ act(() => render(<Host />, container));
+}
+
+function endSlide() {
+ act(() => {
+ host()?.dispatchEvent(Object.assign(new Event("animationend", { bubbles: true }), {
+ animationName: "tn-master-detail-slide-in"
+ }));
+ });
+}
+
+beforeEach(() => {
+ vi.clearAllMocks();
+ mocks.isMobile.mockReturnValue(true);
+ mocks.windowWidth = 500;
+ mocks.modalHeader = undefined;
+ flow = undefined;
+ container = document.body.appendChild(document.createElement("div"));
+});
+
+afterEach(() => {
+ render(null, container);
+ container.remove();
+ document.body.classList.remove("motion-disabled");
+});
+
+describe("useMobileMasterDetail", () => {
+ it("collapses to one pane at a time only on a narrow mobile viewport", () => {
+ renderHost();
+ expect(flow?.isMasterDetail).toBe(true);
+ expect(flowClasses()).toContain("mobile-master-detail");
+
+ // A tablet is wide enough to hold both panes, and so is any desktop window.
+ mocks.windowWidth = 900;
+ renderHost();
+ expect(flow?.isMasterDetail).toBe(false);
+ expect(flowClasses()).not.toContain("mobile-master-detail");
+
+ mocks.windowWidth = 500;
+ mocks.isMobile.mockReturnValue(false);
+ renderHost();
+ expect(flow?.isMasterDetail).toBe(false);
+ });
+
+ it("slides between the panes, holding both on screen until the slide finishes", () => {
+ renderHost();
+ expect(flowClasses()).toContain("mobile-view-list");
+
+ act(() => flow?.switchMobileView("page"));
+ expect(flowClasses()).toEqual(
+ expect.arrayContaining([ "mobile-view-page", "mobile-transition-to-page" ]));
+
+ endSlide();
+ expect(flowClasses()).toContain("mobile-view-page");
+ expect(flowClasses()).not.toContain("mobile-transition-to-page");
+
+ act(() => flow?.switchMobileView("list"));
+ expect(flowClasses()).toContain("mobile-transition-to-list");
+ endSlide();
+ expect(flowClasses()).not.toContain("mobile-transition-to-list");
+
+ // An animation of something else inside the panes must not cut a slide short.
+ act(() => flow?.switchMobileView("page"));
+ act(() => {
+ host()?.dispatchEvent(Object.assign(new Event("animationend", { bubbles: true }), {
+ animationName: "fade-in"
+ }));
+ });
+ expect(flowClasses()).toContain("mobile-transition-to-page");
+ });
+
+ it("switches without a slide where there would be nothing to end it", () => {
+ // Nothing animates with motion off, so there would be no animationend to clear the class.
+ document.body.classList.add("motion-disabled");
+ renderHost();
+ act(() => flow?.switchMobileView("page"));
+ expect(flowClasses()).toContain("mobile-view-page");
+ expect(flowClasses()).not.toContain("mobile-transition-to-page");
+ document.body.classList.remove("motion-disabled");
+
+ // Side by side, both panes are on show: there is nothing to slide between.
+ mocks.windowWidth = 900;
+ renderHost();
+ act(() => flow?.switchMobileView("page"));
+ expect(flowClasses()).not.toContain("mobile-transition-to-page");
+ });
+
+ it("stays put when asked for the pane already on show, and resets without animating", () => {
+ renderHost();
+ act(() => flow?.switchMobileView("list"));
+ expect(flowClasses()).not.toContain("mobile-transition-to-list");
+
+ act(() => flow?.switchMobileView("page"));
+ act(() => flow?.resetMobileView("list"));
+ expect(flowClasses()).toContain("mobile-view-list");
+ expect(flowClasses()).not.toContain("mobile-transition-to-list");
+ });
+});
+
+describe("MasterDetailHeader", () => {
+ it("heads the list with its own name and actions, and a page with the way back", () => {
+ const onBack = vi.fn();
+ const header = (inPage: boolean) => (
+ <MasterDetailHeader
+ inPage={inPage}
+ onBack={onBack}
+ backTitle="back to the list"
+ pageTitle="Page"
+ listTitle="Dialog"
+ listIcon="bx bx-cog"
+ listActions={<button type="button" className="list-action" />}
+ />
+ );
+
+ act(() => render(header(false), container));
+ expect(container.querySelector(".tn-master-detail-title")?.textContent).toBe("Dialog");
+ expect(container.querySelector(".tn-master-detail-icon")?.className).toContain("bx-cog");
+ expect(container.querySelector(".list-action")).not.toBeNull();
+ expect(container.querySelector(".action-stub")).toBeNull();
+
+ act(() => render(header(true), container));
+ expect(container.querySelector(".tn-master-detail-title")?.textContent).toBe("Page");
+ // The dialog's own actions belong to the list, not to one page of it.
+ expect(container.querySelector(".list-action")).toBeNull();
+
+ container.querySelector<HTMLElement>(".action-stub")?.click();
+ expect(onBack).toHaveBeenCalled();
+ });
+
+ it("takes no room for a title a page carries itself, nor for a missing icon", () => {
+ act(() => render(
+ <MasterDetailHeader inPage onBack={() => {}} backTitle="back" listTitle="Dialog" />,
+ container
+ ));
+
+ expect(container.querySelector(".tn-master-detail-title")).toBeNull();
+ expect(container.querySelector(".tn-master-detail-icon")).toBeNull();
+ });
+});
+
+describe("MasterDetailModal", () => {
+ /** A component deep inside the modal, announcing whatever page it is showing. */
+ function Page({ title, close }: { title: string | null, close: () => void }) {
+ const { isMasterDetail } = useMasterDetail();
+ useMasterDetailPage(title, close);
+ return <div className="page-stub" data-in-flow={String(isMasterDetail)} />;
+ }
+
+ function renderModal(title: string | null, close: () => void, show = true) {
+ act(() => render(
+ <MasterDetailModal
+ backTitle="back to the list"
+ title="Dialog"
+ listIcon="bx bx-cog"
+ show={show}
+ className="host-dialog"
+ size="lg"
+ onHidden={() => {}}
+ >
+ <Page title={title} close={close} />
+ </MasterDetailModal>,
+ container
+ ));
+ }
+
+ it("heads and slides to the page a nested component announces, and back out of it", () => {
+ const close = vi.fn();
+ // However a previous show was left, the dialog opens on the list.
+ renderModal(null, close);
+ expect(container.querySelector(".tn-master-detail-title")?.textContent).toBe("Dialog");
+
+ renderModal("Attributes", close);
+
+ expect(container.querySelector<HTMLElement>(".page-stub")?.dataset.inFlow).toBe("true");
+ expect(container.querySelector(".tn-master-detail-title")?.textContent).toBe("Attributes");
+ expect(container.querySelector<HTMLElement>(".modal-stub")?.classList.contains("mobile-view-page"))
+ .toBe(true);
+
+ container.querySelector<HTMLElement>(".action-stub")?.click();
+ expect(close).toHaveBeenCalled();
+
+ // Announcing no page at all is what returns the flow to the list.
+ renderModal(null, close);
+ expect(container.querySelector(".tn-master-detail-title")?.textContent).toBe("Dialog");
+ expect(container.querySelector<HTMLElement>(".modal-stub")?.classList.contains("mobile-view-list"))
+ .toBe(true);
+ });
+
+ it("leaves the header to the dialog where both panes fit side by side", () => {
+ mocks.windowWidth = 900;
+ renderModal("Attributes", vi.fn());
+
+ expect(container.querySelector<HTMLElement>(".page-stub")?.dataset.inFlow).toBe("false");
+ expect(container.querySelector(".tn-master-detail-header")).toBeNull();
+ expect(mocks.modalHeader).toBeUndefined();
+ });
+
+ it("holds the pane it was left on while closed, resetting only as it opens", () => {
+ const close = vi.fn();
+ renderModal("Attributes", close, false);
+
+ // Closed, so nothing resets it: the announced page is still what the header names.
+ expect(container.querySelector(".tn-master-detail-title")?.textContent).toBe("Attributes");
+ });
+
+ it("wraps each half in the class master_detail.css lays it out with", () => {
+ act(() => render(
+ <>
+ <MasterPane className="dialog-list"><span className="in-list" /></MasterPane>
+ <DetailPane className="dialog-page"><span className="in-page" /></DetailPane>
+ </>,
+ container
+ ));
+
+ expect(container.querySelector(".tn-master-pane.dialog-list > .in-list")).not.toBeNull();
+ expect(container.querySelector(".tn-detail-pane.dialog-page > .in-page")).not.toBeNull();
+ });
+});
diff --git a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/browse.spec.tsx b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/browse.spec.tsx
index e10f2b627d..44ba928c9d 100644
--- a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/browse.spec.tsx
+++ b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/browse.spec.tsx
@@ -168,6 +168,22 @@ describe("Browse", () => {
expect(probe.querySelector<HTMLButtonElement>(".back-stub")?.disabled).toBe(true);
});
+ it("consolidates the children too small to see into one counted segment", async () => {
+ mocks.usage = {
+ ...usageOf("root", []),
+ children: [
+ { noteId: "big", subtreeSize: 100000, subtreeNoteCount: 1 },
+ { noteId: "sliver", subtreeSize: 1, subtreeNoteCount: 1 }
+ ]
+ };
+ renderBrowse();
+
+ await vi.waitFor(() => expect(donutProps().outerRings[0].segments).toHaveLength(2));
+ // The sliver is folded away rather than drawn as an invisible arc, and the bucket that
+ // replaces it stands for a crowd, so it navigates nowhere.
+ expect(donutProps().outerRings[0].segments[1]).toMatchObject({ id: "others", data: {} });
+ });
+
it("descends into a clicked child, then navigates back via button and breadcrumb", async () => {
mocks.usage = usageOf("root", [ "child1" ]);
const probe = renderBrowse();
diff --git a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/context_menu.spec.ts b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/context_menu.spec.ts
index d45d3e242d..1d3cc0d9f2 100644
--- a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/context_menu.spec.ts
+++ b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/context_menu.spec.ts
@@ -222,11 +222,26 @@ describe("openSpaceUsageContextMenu", () => {
expect(itemByIcon(DELETE)).toBeUndefined();
});
- it("shows no menu at all for a note deleted since the usage was measured", async () => {
+ it("shows no menu at all for a note deleted since the usage was measured, or for no note", async () => {
await openFor([ "root", "gone" ]);
+ expect(mocks.shown).toBeUndefined();
+ // A cell that names no note at all: nothing to look up, so nothing to offer.
+ await openFor([]);
expect(mocks.shown).toBeUndefined();
});
+
+ it("deletes nothing when the branch it would remove is gone by the time the item is invoked", async () => {
+ givenNote("n1");
+ // No branch id registered: the item is offered struck through, and refuses to act even if
+ // something invokes it anyway.
+ await openFor([ "root", "p", "n1" ]);
+
+ expect(itemByIcon(DELETE)?.enabled).toBe(false);
+ invoke(DELETE);
+ expect(mocks.deleteNotes).not.toHaveBeenCalled();
+ expect(mocks.contentChanged).not.toHaveBeenCalled();
+ });
});
describe("openDeletedNotesContextMenu", () => {
diff --git a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/index.spec.tsx b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/index.spec.tsx
index d87c6a0c34..99deacfef5 100644
--- a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/index.spec.tsx
+++ b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/index.spec.tsx
@@ -23,18 +23,22 @@ vi.mock("./use_space_usage_fetch", () => ({
}));
vi.mock("./browse", () => ({
- default: ({ refreshToken, onLoadingChange }: {
+ default: ({ path, refreshToken, onLoadingChange }: {
+ path: string[],
refreshToken: number,
onLoadingChange: (loading: boolean) => void
}) => {
mocks.browseToken = refreshToken;
mocks.reportBrowseLoading = onLoadingChange;
- return <div className="browse-stub" />;
+ return <div className="browse-stub" data-path={path.join("/")} />;
}
}));
vi.mock("./overview", () => ({
- default: () => <div className="overview-stub" />
+ default: ({ onShowDetails }: { onShowDetails: (notePath: string[]) => void }) => (
+ // Stands in for the menu's "Show details" on one of the map's cells.
+ <div className="overview-stub" onClick={() => onShowDetails([ "root", "p1", "n1" ])} />
+ )
}));
vi.mock("../../components/OptionsPageHeader", () => ({
@@ -151,6 +155,18 @@ describe("SpaceUsage section", () => {
expect(probe.querySelector(".space-usage-status")).not.toBeNull();
});
+ it("lands Browse on the note the map asked details for, rather than back at the root", async () => {
+ mocks.overview = OVERVIEW;
+ const probe = renderSection();
+
+ probe.querySelector(".overview-stub")?.dispatchEvent(new MouseEvent("click", { bubbles: true }));
+ await flushRender();
+
+ const browse = probe.querySelector<HTMLElement>(".browse-stub");
+ expect(browse).not.toBeNull();
+ expect(browse?.dataset.path).toBe("root/p1/n1");
+ });
+
it("asks both views for a fresh reading when refresh is pressed, and only then", async () => {
mocks.overview = OVERVIEW;
const probe = renderSection();
diff --git a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/overview.spec.tsx b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/overview.spec.tsx
index 1f3baea95c..35fead1d21 100644
--- a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/overview.spec.tsx
+++ b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/overview.spec.tsx
@@ -90,6 +90,29 @@ describe("Overview", () => {
expect(probe.querySelector(".treemap-cell-deleted")).not.toBeNull();
});
+ it("draws a cell for a note whose weight is partly attachments, beside one with none", () => {
+ container = document.body.appendChild(document.createElement("div"));
+ render(
+ <Overview
+ overview={{
+ ...OVERVIEW,
+ notes: [
+ ...OVERVIEW.notes,
+ { noteId: "n2", notePath: [ "n2" ], ownSize: 100, attachmentsSize: 120, revisionsSize: 0 }
+ ]
+ }}
+ onShowDetails={mocks.showDetails}
+ onContentChanged={mocks.contentChanged}
+ />,
+ container
+ );
+
+ // Both are drawn the same way; only the size line beneath them differs, and it is the model's
+ // spec that pins its wording.
+ expect(container.querySelector('[data-href="#root/n1"]')).not.toBeNull();
+ expect(container.querySelector('[data-href="#root/n2"]')).not.toBeNull();
+ });
+
it("dresses note cells in their froca icons and the buckets in what they stand for", async () => {
const probe = renderOverview();
const iconIn = (selector: string) =>
diff --git a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/use_space_usage_fetch.spec.tsx b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/use_space_usage_fetch.spec.tsx
index 58b093d2f7..982afd8bb7 100644
--- a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/use_space_usage_fetch.spec.tsx
+++ b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/use_space_usage_fetch.spec.tsx
@@ -98,6 +98,20 @@ describe("useSpaceUsageFetch", () => {
expect(probe.textContent).toBe("fast");
});
+ it("drops a stale failure too, so an abandoned request cannot report over a newer reading", async () => {
+ let rejectSlow: ((reason?: unknown) => void) | undefined;
+ serverGet.mockReturnValueOnce(new Promise((_resolve, reject) => { rejectSlow = reject; }));
+ const probe = renderProbe("space-usage/note/slow");
+
+ serverGet.mockResolvedValueOnce({ label: "fast" });
+ renderProbe("space-usage/note/fast");
+ await vi.waitFor(() => expect(probe.textContent).toBe("fast"));
+
+ rejectSlow?.(new Error("network down"));
+ await Promise.resolve();
+ expect(probe.textContent).toBe("fast");
+ });
+
it("never re-measures on its own when notes change", async () => {
serverGet.mockResolvedValue({ label: "data" });
const probe = renderProbe("space-usage/overview");