Skip to content

Build your personal knowledge base with Trilium Notes

test(collections/dashboard): cover layout helpers

Elian Doran8cb5ba432678
diff --git a/apps/client/src/widgets/collections/dashboard/index.tsx b/apps/client/src/widgets/collections/dashboard/index.tsx
index 0222c29c95..c4e27ab099 100644
--- a/apps/client/src/widgets/collections/dashboard/index.tsx
+++ b/apps/client/src/widgets/collections/dashboard/index.tsx
@@ -18,23 +18,13 @@ import NoteLink from "../../react/NoteLink";
import { ViewModeProps } from "../interface";
import { getNotePath, NoteContent } from "../legacy/ListOrGridView";
import openWidgetContextMenu from "./context_menu";
-
-interface DashboardWidgetLayout {
- x: number;
- y: number;
- w: number;
- h: number;
-}
+import { computeDropCell, DEFAULT_WIDGET_SIZE, GRID_COLUMNS, sameLayout, WidgetLayouts } from "./layout";
export interface DashboardViewConfig {
- widgets?: Record<string, DashboardWidgetLayout>;
+ widgets?: WidgetLayouts;
}
-type WidgetLayouts = Record<string, DashboardWidgetLayout>;
-
-const DEFAULT_WIDGET_SIZE = { w: 4, h: 3 };
const INITIALIZED_CLASS = "dashboard-widget-initialized";
-const GRID_COLUMNS = 12;
const CELL_HEIGHT = 80;
const GRID_MARGIN = 8;
/** Below this container width (in pixels) the dashboard collapses to a single column. */
@@ -371,31 +361,3 @@ function DashboardEmptyState({ collapsed }: { collapsed: boolean }) {
</div>
);
}
-
-/** Translate a drop event's viewport coordinates into a grid cell, or null when auto-positioning
- * is preferable (the grid is in collapsed single-column mode or has no measurable geometry yet). */
-function computeDropCell(grid: GridStack, container: HTMLElement, e: DragEvent): Pick<DashboardWidgetLayout, "x" | "y"> | null {
- if (grid.getColumn() !== GRID_COLUMNS) {
- return null;
- }
- const rect = container.getBoundingClientRect();
- const cellWidth = rect.width / GRID_COLUMNS;
- const cellHeight = grid.getCellHeight();
- if (!cellWidth || !cellHeight) {
- return null;
- }
- const x = Math.min(GRID_COLUMNS - DEFAULT_WIDGET_SIZE.w, Math.max(0, Math.floor((e.clientX - rect.left) / cellWidth)));
- const y = Math.max(0, Math.floor((e.clientY - rect.top) / cellHeight));
- return { x, y };
-}
-
-function sameLayout(a: WidgetLayouts, b: WidgetLayouts) {
- const aKeys = Object.keys(a);
- if (aKeys.length !== Object.keys(b).length) {
- return false;
- }
- return aKeys.every((key) => {
- const other = b[key];
- return other && a[key].x === other.x && a[key].y === other.y && a[key].w === other.w && a[key].h === other.h;
- });
-}
diff --git a/apps/client/src/widgets/collections/dashboard/layout.spec.ts b/apps/client/src/widgets/collections/dashboard/layout.spec.ts
new file mode 100644
index 0000000000..0b0cd16510
--- /dev/null
+++ b/apps/client/src/widgets/collections/dashboard/layout.spec.ts
@@ -0,0 +1,74 @@
+import type { GridStack } from "gridstack";
+import { describe, expect, it } from "vitest";
+
+import { computeDropCell, DEFAULT_WIDGET_SIZE, GRID_COLUMNS, sameLayout, WidgetLayouts } from "./layout";
+
+/** Minimal GridStack stand-in exposing only what computeDropCell reads. */
+function fakeGrid({ columns = GRID_COLUMNS, cellHeight = 80 }: { columns?: number; cellHeight?: number } = {}) {
+ return { getColumn: () => columns, getCellHeight: () => cellHeight } as unknown as GridStack;
+}
+
+/** Container whose rect is a 1200×600 box at the origin → cells are 100px wide. */
+function fakeContainer(rect: Partial<DOMRect> = {}) {
+ const full = { left: 0, top: 0, width: 1200, height: 600, ...rect } as DOMRect;
+ return { getBoundingClientRect: () => full } as HTMLElement;
+}
+
+describe("computeDropCell", () => {
+ it("maps a drop position to the grid cell under the cursor", () => {
+ // 1200px / 12 columns = 100px per column; cellHeight 80px.
+ const cell = computeDropCell(fakeGrid(), fakeContainer(), { clientX: 250, clientY: 170 });
+ expect(cell).toEqual({ x: 2, y: 2 });
+ });
+
+ it("accounts for the container's offset on screen", () => {
+ const cell = computeDropCell(fakeGrid(), fakeContainer({ left: 100, top: 50 }), { clientX: 350, clientY: 210 });
+ // (350-100)/100 = 2.5 → 2; (210-50)/80 = 2 → 2
+ expect(cell).toEqual({ x: 2, y: 2 });
+ });
+
+ it("clamps x so a default-width widget never overflows the grid", () => {
+ const cell = computeDropCell(fakeGrid(), fakeContainer(), { clientX: 1190, clientY: 0 });
+ expect(cell?.x).toBe(GRID_COLUMNS - DEFAULT_WIDGET_SIZE.w);
+ });
+
+ it("clamps negative coordinates (dropped above/left of the grid) to zero", () => {
+ const cell = computeDropCell(fakeGrid(), fakeContainer({ left: 100, top: 100 }), { clientX: 20, clientY: 20 });
+ expect(cell).toEqual({ x: 0, y: 0 });
+ });
+
+ it("returns null in collapsed single-column mode so the caller auto-positions", () => {
+ expect(computeDropCell(fakeGrid({ columns: 1 }), fakeContainer(), { clientX: 100, clientY: 100 })).toBeNull();
+ });
+
+ it("returns null when the grid has no measurable geometry yet", () => {
+ expect(computeDropCell(fakeGrid({ cellHeight: 0 }), fakeContainer(), { clientX: 100, clientY: 100 })).toBeNull();
+ expect(computeDropCell(fakeGrid(), fakeContainer({ width: 0 }), { clientX: 100, clientY: 100 })).toBeNull();
+ });
+});
+
+describe("sameLayout", () => {
+ const base: WidgetLayouts = { a: { x: 0, y: 0, w: 4, h: 3 }, b: { x: 4, y: 0, w: 2, h: 2 } };
+
+ it("treats identical layouts (independent of key order) as equal", () => {
+ expect(sameLayout(base, { b: { x: 4, y: 0, w: 2, h: 2 }, a: { x: 0, y: 0, w: 4, h: 3 } })).toBe(true);
+ });
+
+ it("treats two empty layouts as equal", () => {
+ expect(sameLayout({}, {})).toBe(true);
+ });
+
+ it("differs when a widget is added or removed", () => {
+ expect(sameLayout(base, { a: base.a })).toBe(false);
+ expect(sameLayout(base, { ...base, c: { x: 0, y: 3, w: 1, h: 1 } })).toBe(false);
+ });
+
+ it("differs when any coordinate or size changes", () => {
+ expect(sameLayout(base, { ...base, a: { ...base.a, x: 1 } })).toBe(false);
+ expect(sameLayout(base, { ...base, a: { ...base.a, h: 5 } })).toBe(false);
+ });
+
+ it("differs when the same number of keys don't match (no shared key)", () => {
+ expect(sameLayout({ a: base.a }, { z: base.a })).toBe(false);
+ });
+});
diff --git a/apps/client/src/widgets/collections/dashboard/layout.ts b/apps/client/src/widgets/collections/dashboard/layout.ts
new file mode 100644
index 0000000000..a8bf346217
--- /dev/null
+++ b/apps/client/src/widgets/collections/dashboard/layout.ts
@@ -0,0 +1,42 @@
+import type { GridStack } from "gridstack";
+
+export interface DashboardWidgetLayout {
+ x: number;
+ y: number;
+ w: number;
+ h: number;
+}
+
+export type WidgetLayouts = Record<string, DashboardWidgetLayout>;
+
+export const GRID_COLUMNS = 12;
+export const DEFAULT_WIDGET_SIZE = { w: 4, h: 3 };
+
+/** Translate a drop event's viewport coordinates into a grid cell, or null when auto-positioning
+ * is preferable (the grid is in collapsed single-column mode or has no measurable geometry yet). */
+export function computeDropCell(grid: GridStack, container: HTMLElement, e: { clientX: number; clientY: number }): Pick<DashboardWidgetLayout, "x" | "y"> | null {
+ if (grid.getColumn() !== GRID_COLUMNS) {
+ return null;
+ }
+ const rect = container.getBoundingClientRect();
+ const cellWidth = rect.width / GRID_COLUMNS;
+ const cellHeight = grid.getCellHeight();
+ if (!cellWidth || !cellHeight) {
+ return null;
+ }
+ const x = Math.min(GRID_COLUMNS - DEFAULT_WIDGET_SIZE.w, Math.max(0, Math.floor((e.clientX - rect.left) / cellWidth)));
+ const y = Math.max(0, Math.floor((e.clientY - rect.top) / cellHeight));
+ return { x, y };
+}
+
+/** Whether two layouts describe the same widgets in the same positions. */
+export function sameLayout(a: WidgetLayouts, b: WidgetLayouts) {
+ const aKeys = Object.keys(a);
+ if (aKeys.length !== Object.keys(b).length) {
+ return false;
+ }
+ return aKeys.every((key) => {
+ const other = b[key];
+ return other && a[key].x === other.x && a[key].y === other.y && a[key].w === other.w && a[key].h === other.h;
+ });
+}