Skip to content

Build your personal knowledge base with Trilium Notes

client/collections/board: increase test coverage

Adorian Doranccf63c0c6f67
diff --git a/apps/client/src/widgets/collections/board/drag_measure.spec.ts b/apps/client/src/widgets/collections/board/drag_measure.spec.ts
index 45e9a08ecd..c24f14bc99 100644
--- a/apps/client/src/widgets/collections/board/drag_measure.spec.ts
+++ b/apps/client/src/widgets/collections/board/drag_measure.spec.ts
@@ -285,3 +285,85 @@ describe("placeInModel", () => {
expect(placeInModel({ heights: [], spacing: 9 }, 40, undefined)).toBe(0);
});
});
+
+describe("measuring a windowed column", () => {
+ /**
+ * A column that draws a slice of its cards states what it holds and where the slice begins, and
+ * stands a spacer at either end for the rest. The measurement counts those in, so an index it
+ * leads to names a place in the column rather than a place among the cards on screen.
+ */
+ function buildWindowed({ total = 100, from = 40, above = 2400, below = 3300 } = {}) {
+ const board = buildBoard({ cardCounts: [ 2 ], areaScrollTop: 0 });
+ const area = board.querySelector<HTMLElement>(".board-column-content");
+ if (!area) throw new Error("expected a card area");
+
+ area.dataset.windowCount = String(total);
+ area.dataset.windowFrom = String(from);
+
+ // The drawn cards stand below the spacer, which is where a windowed column draws them.
+ for (const [ index, card ] of [ ...area.querySelectorAll<HTMLElement>(".board-note") ].entries()) {
+ place(card, { left: 0, top: 40 + above + 10 + index * 60, width: 100, height: 50 });
+ }
+ for (const [ index, height ] of [ above, below ].entries()) {
+ const spacer = document.createElement("div");
+ spacer.className = "board-window-spacer";
+ Object.defineProperty(spacer, "offsetHeight", { value: height, configurable: true });
+ // The one above stands before the cards, the one below after them.
+ if (index === 0) {
+ area.insertBefore(spacer, area.firstChild);
+ } else {
+ area.appendChild(spacer);
+ }
+ }
+
+ return board;
+ }
+
+ it("counts the cards the column is not drawing, so an index names a place in the column", () => {
+ const board = buildWindowed();
+
+ const [ column ] = measureBoard(board).columns;
+
+ // Two drawn cards, and the rest of the hundred stood for by the spacers.
+ expect(column.cards).toHaveLength(100);
+ expect(column.cards[40].height).toBe(50);
+ expect(column.cards[41].height).toBe(50);
+ });
+
+ it("stands the undrawn cards in order, above the drawn ones and below them", () => {
+ const board = buildWindowed();
+
+ const [ column ] = measureBoard(board).columns;
+ const tops = column.cards.map((card) => card.top);
+
+ expect(tops).toHaveLength(100);
+ // Never doubles back: a drop read against these has to walk them in order.
+ for (const [ index, top ] of tops.entries()) {
+ if (index > 0) {
+ expect(top).toBeGreaterThanOrEqual(tops[index - 1]);
+ }
+ }
+ expect(tops[0]).toBe(0);
+ expect(tops[39]).toBeLessThan(tops[40]);
+ });
+
+ it("measures a column drawing all of itself exactly as it always did", () => {
+ const board = buildBoard({ cardCounts: [ 2 ], areaScrollTop: 0 });
+
+ const [ column ] = measureBoard(board).columns;
+
+ expect(column.cards).toHaveLength(2);
+ });
+
+ it("counts a column that states a window but is drawing none of it", () => {
+ const board = buildWindowed({ total: 60, from: 60, above: 3600, below: 0 });
+ const area = board.querySelector<HTMLElement>(".board-column-content");
+ for (const card of area?.querySelectorAll(".board-note") ?? []) {
+ card.remove();
+ }
+
+ const [ column ] = measureBoard(board).columns;
+
+ expect(column.cards).toHaveLength(60);
+ });
+});
diff --git a/apps/client/src/widgets/collections/board/keyboard.spec.tsx b/apps/client/src/widgets/collections/board/keyboard.spec.tsx
index 17276d0c16..1291e22336 100644
--- a/apps/client/src/widgets/collections/board/keyboard.spec.tsx
+++ b/apps/client/src/widgets/collections/board/keyboard.spec.tsx
@@ -1274,3 +1274,171 @@ describe("Board keyboard", () => {
}
});
+
+/**
+ * A column of thousands draws only a slice of its cards, so walking it steps onto cards that are
+ * not in the page. The keyboard names a card by the place it holds in the column and asks the
+ * column to draw it, rather than counting the ones on screen.
+ */
+describe("Board keyboard over a windowed column", () => {
+ let container: HTMLElement | undefined;
+ const CARDS = 400;
+
+ beforeEach(() => {
+ vi.restoreAllMocks();
+ vi.spyOn(server, "put").mockResolvedValue(undefined);
+ vi.spyOn(server, "post").mockResolvedValue(undefined);
+ });
+
+ afterEach(() => {
+ if (container) {
+ render(null, container);
+ container.remove();
+ container = undefined;
+ }
+ });
+
+ it("draws fewer cards than it holds, and states what it holds", async () => {
+ const board = await renderBig();
+ const area = board.querySelector<HTMLElement>(".board-column-content");
+
+ expect(area?.dataset.windowCount).toBe(String(CARDS));
+ expect(area?.querySelectorAll(".board-note").length).toBeLessThan(CARDS);
+ });
+
+ it("walks onto a card the column is not drawing, and focuses it", async () => {
+ const board = await renderBig();
+ const column = board.querySelector<HTMLElement>(".board-column");
+ const drawn = [ ...(column?.querySelectorAll<HTMLElement>(".board-note") ?? []) ];
+ const last = drawn[drawn.length - 1];
+ if (!column || !last) throw new Error("expected the column to draw some cards");
+
+ const beyond = Number(last.dataset.index) + 1;
+ expect(column.querySelector(`.board-note[data-index="${beyond}"]`)).toBeNull();
+
+ last.focus();
+ await act(async () => {
+ last.dispatchEvent(new KeyboardEvent("keydown", { key: "ArrowDown", bubbles: true }));
+ await flush();
+ });
+
+ const arrived = column.querySelector<HTMLElement>(`.board-note[data-index="${beyond}"]`);
+ expect(arrived).not.toBeNull();
+ expect(document.activeElement).toBe(arrived);
+ });
+
+ it("takes End to the last card of the column, not the last one drawn", async () => {
+ const board = await renderBig();
+ const first = board.querySelector<HTMLElement>(".board-note");
+ if (!first) throw new Error("expected a card");
+
+ first.focus();
+ await act(async () => {
+ first.dispatchEvent(new KeyboardEvent("keydown", { key: "End", bubbles: true }));
+ await flush();
+ });
+
+ const focused = (document.activeElement as HTMLElement | null)?.closest<HTMLElement>(".board-note");
+ expect(focused?.dataset.index).toBe(String(CARDS - 1));
+ });
+
+ it("walks back up out of the window it moved to", async () => {
+ const board = await renderBig();
+ const first = board.querySelector<HTMLElement>(".board-note");
+ if (!first) throw new Error("expected a card");
+
+ first.focus();
+ await act(async () => {
+ first.dispatchEvent(new KeyboardEvent("keydown", { key: "End", bubbles: true }));
+ await flush();
+ });
+
+ const atEnd = (document.activeElement as HTMLElement | null)?.closest<HTMLElement>(".board-note");
+ if (!atEnd) throw new Error("expected End to land on a card");
+
+ await act(async () => {
+ atEnd.dispatchEvent(new KeyboardEvent("keydown", { key: "Home", bubbles: true }));
+ await flush();
+ });
+
+ const home = (document.activeElement as HTMLElement | null)?.closest<HTMLElement>(".board-note");
+ expect(home?.dataset.index).toBe("0");
+ });
+
+ /**
+ * The card lands at the end of a column that is not drawing its end, so there is nothing in the
+ * page to focus. The board asks that column for it and focuses it once it has been drawn.
+ */
+ it("focuses a card sent into a column that is not drawing where it lands", async () => {
+ const board = await renderBig();
+ const columns = board.querySelectorAll<HTMLElement>(".board-column");
+ const sent = columns[1]?.querySelector<HTMLElement>(".board-note");
+ if (!sent) throw new Error("expected a card in the small column");
+
+ const noteId = sent.dataset.noteId;
+ sent.focus();
+ await act(async () => {
+ sent.dispatchEvent(new KeyboardEvent("keydown", {
+ key: "ArrowLeft", ctrlKey: true, bubbles: true, cancelable: true
+ }));
+ await flush();
+ });
+ // The reveal waits for the column to draw the card, which takes a frame.
+ await act(async () => { await frame(); await flush(); });
+ await act(async () => { await frame(); await flush(); });
+
+ const arrived = board.querySelector<HTMLElement>(`.board-note[data-note-id="${noteId}"]`);
+ expect(arrived).not.toBeNull();
+ expect(arrived?.closest(".board-column")).toBe(columns[0]);
+ expect(document.activeElement).toBe(arrived);
+ });
+
+ function frame() {
+ return new Promise((resolve) => requestAnimationFrame(() => resolve(undefined)));
+ }
+
+ async function renderBig() {
+ const note = buildNote({
+ title: "Big board",
+ "#collection": "",
+ "#viewType": "board",
+ children: [
+ ...Array.from({ length: CARDS }, (_, i) => ({
+ title: `Card ${i}`, "#status": "To Do"
+ })),
+ { title: "Only", "#status": "Doing" }
+ ]
+ });
+
+ const mountPoint = document.createElement("div");
+ container = mountPoint;
+ document.body.appendChild(mountPoint);
+
+ await act(async () => {
+ render(
+ <ParentComponent.Provider value={new Component()}>
+ <BoardView
+ note={note}
+ notePath={`root/${note.noteId}`}
+ noteIds={[ ...note.getChildNoteIds() ]}
+ highlightedTokens={null}
+ viewConfig={{
+ columns: [ { value: "To Do" }, { value: "Doing" } ]
+ }}
+ saveConfig={() => {}}
+ media="screen"
+ onReady={() => {}}
+ />
+ </ParentComponent.Provider>,
+ mountPoint
+ );
+ });
+ await act(async () => { await flush(); });
+
+ return mountPoint;
+ }
+
+ function flush() {
+ return new Promise((resolve) => setTimeout(resolve));
+ }
+});
diff --git a/apps/client/src/widgets/collections/board/windowing.ts b/apps/client/src/widgets/collections/board/windowing.ts
index d474426575..ccfd4c692d 100644
--- a/apps/client/src/widgets/collections/board/windowing.ts
+++ b/apps/client/src/widgets/collections/board/windowing.ts
@@ -316,10 +316,15 @@ export function useColumnWindow(
// Placed a little above the foot of the area, so the card is not left under its own edge.
area.scrollTop = Math.max(0, offset - area.clientHeight / 2);
+ // Moved here rather than left to the scroll event the write raises: that event arrives a
+ // frame later, and the card has to be drawn for the ask to have been worth making.
+ const moved = { top: area.scrollTop, viewport: area.clientHeight };
if (immediate) {
- // The scroll event that would move the window arrives a frame later, so the window is
- // moved here instead and the card is in the page before this returns.
- flushSync(() => setScroll({ top: area.scrollTop, viewport: area.clientHeight }));
+ // Drawn before this returns, for a keyboard walk that focuses the card in the same
+ // keystroke.
+ flushSync(() => setScroll(moved));
+ } else {
+ setScroll(moved);
}
}, [ areaRef, enabled, heights, spacing ]);