Skip to content

Build your personal knowledge base with Trilium Notes

client/collections/board: improve test coverage

Adorian Doranb0211812778f
diff --git a/apps/client/src/widgets/collections/board/card.spec.tsx b/apps/client/src/widgets/collections/board/card.spec.tsx
index c41ce2acbf..7f64a52f9a 100644
--- a/apps/client/src/widgets/collections/board/card.spec.tsx
+++ b/apps/client/src/widgets/collections/board/card.spec.tsx
@@ -328,6 +328,45 @@ describe("Board card", () => {
await act(async () => { press(picker, " "); });
expect(openInPopup).not.toHaveBeenCalled();
expect(element.querySelector("textarea")).toBeTruthy();
+
+ // Tabbing back puts a `relatedTarget` inside `fieldRef`, which is not the editor losing focus.
+ await act(async () => {
+ picker.dispatchEvent(
+ new FocusEvent("focusout", { bubbles: true, relatedTarget: editor }));
+ });
+ expect(element.querySelector("textarea")).toBeTruthy();
+ expect(put).not.toHaveBeenCalled();
+
+ // A mouse press on the picker takes no focus off the field, which would close the editor
+ // before the click that opens the picker arrived.
+ const pressed = new MouseEvent("mousedown", { bubbles: true, cancelable: true });
+ await act(async () => { picker.dispatchEvent(pressed); });
+ expect(pressed.defaultPrevented).toBe(true);
+ });
+
+ it("closes the editor on Escape while the icon picker holds focus", async () => {
+ const { first } = await renderBoard();
+ const put = vi.spyOn(server, "put").mockResolvedValue(undefined);
+ // Held on to: the editor takes the place of the title the lookup goes by.
+ const element = card(first);
+
+ await act(async () => { press(element, "F2"); });
+ const editor = element.querySelector<HTMLTextAreaElement>("textarea");
+ if (!editor) throw new Error("expected the title editor");
+ await act(async () => {
+ editor.value = "Typed but not saved";
+ editor.focus();
+ press(editor, "Tab", { shiftKey: true });
+ });
+ const picker = element.querySelector<HTMLButtonElement>(".title-editor-icon button");
+ if (!picker) throw new Error("expected the icon picker");
+ expect(document.activeElement).toBe(picker);
+
+ await act(async () => { press(picker, "Escape"); });
+
+ expect(element.querySelector("textarea")).toBeFalsy();
+ // Escape discards, so nothing is written on the way out.
+ expect(put).not.toHaveBeenCalled();
});
it("ends the edit once the picker hands focus outside the field", async () => {
diff --git a/apps/client/src/widgets/collections/board/index.spec.tsx b/apps/client/src/widgets/collections/board/index.spec.tsx
index 0870fcf680..9d1543a7d2 100644
--- a/apps/client/src/widgets/collections/board/index.spec.tsx
+++ b/apps/client/src/widgets/collections/board/index.spec.tsx
@@ -1722,6 +1722,31 @@ describe("Board column rename", () => {
show.mockRestore();
});
+ /** `Ctrl+A` reaches the same cards through `keyboard.ts`; this is the menu's own route to them. */
+ it("picks out every card in the column from the menu", async () => {
+ const { container } = await setup();
+ const column = container.querySelectorAll<HTMLElement>(".board-column")[1];
+ const picked = () => [ ...container.querySelectorAll(".board-note.selected") ]
+ .map(element => element.getAttribute("data-note-id"));
+ expect(picked()).toEqual([]);
+
+ const show = vi.spyOn(contextMenu, "show").mockImplementation(async () => {});
+ column.querySelector("h3")?.dispatchEvent(new MouseEvent("contextmenu", { bubbles: true }));
+
+ const entry = (show.mock.calls.at(-1)?.[0].items ?? [])
+ .find(item => item && "uiIcon" in item && item.uiIcon === "bx bx-selection");
+ if (!entry || !("handler" in entry)) throw new Error("expected a select-all entry");
+
+ await act(async () => {
+ entry.handler?.(entry, {} as never);
+ await flush();
+ });
+ expect(picked()).toEqual(
+ [ ...column.querySelectorAll(".board-note") ].map(e => e.getAttribute("data-note-id")));
+
+ show.mockRestore();
+ });
+
it("keeps the new-item slot out of the column's scrolling body", async () => {
const { container } = await setup();
const column = container.querySelectorAll<HTMLElement>(".board-column")[1];
diff --git a/apps/client/src/widgets/collections/board/keyboard.spec.tsx b/apps/client/src/widgets/collections/board/keyboard.spec.tsx
index a5029facd8..f565a38be2 100644
--- a/apps/client/src/widgets/collections/board/keyboard.spec.tsx
+++ b/apps/client/src/widgets/collections/board/keyboard.spec.tsx
@@ -576,6 +576,16 @@ describe("Board keyboard", () => {
expect(pickedNames(board)).toEqual([ "First", "Second" ]);
});
+ /** A range runs through cards, so a header is not a place one can start from. */
+ it("leaves Shift and an arrow on a header unanswered", async () => {
+ const board = await renderBoard(undefined, undefined, [ "To Do" ]);
+ focusHeader(board, 0);
+
+ press(board, "ArrowDown", { shiftKey: true });
+
+ expect(pickedNames(board)).toEqual([]);
+ });
+
/**
* A range is measured against one column, so an anchor left in another names no place in
* the list the press is counted against.