Skip to content

Build your personal knowledge base with Trilium Notes

client/image viewer: add tests

Adorian Doran765d1d8545d7
diff --git a/apps/client/src/widgets/react/ImageViewer.spec.tsx b/apps/client/src/widgets/react/ImageViewer.spec.tsx
new file mode 100644
index 0000000000..4b50b2a3cb
--- /dev/null
+++ b/apps/client/src/widgets/react/ImageViewer.spec.tsx
@@ -0,0 +1,53 @@
+import { type ComponentChildren, render } from "preact";
+import { beforeEach, describe, expect, it, vi } from "vitest";
+
+// Capture the props handed to the zoom library without mounting it (it needs real layout).
+const { transformWrapperSpy } = vi.hoisted(() => ({ transformWrapperSpy: vi.fn() }));
+
+vi.mock("react-zoom-pan-pinch", () => ({
+ TransformWrapper: (props: { children?: ComponentChildren }) => {
+ transformWrapperSpy(props);
+ return props.children;
+ },
+ TransformComponent: (props: { children?: ComponentChildren }) => props.children
+}));
+
+import ImageViewer from "./ImageViewer";
+
+function renderViewer(props: Parameters<typeof ImageViewer>[0]) {
+ const container = document.createElement("div");
+ render(<ImageViewer {...props} />, container);
+ return container;
+}
+
+describe("ImageViewer", () => {
+ beforeEach(() => transformWrapperSpy.mockClear());
+
+ it("renders the image with the given src, alt and class", () => {
+ const container = renderViewer({ src: "api/images/abc/Pic", imgClassName: "note-detail-image-view", alt: "Pic" });
+ const img = container.querySelector("img");
+ expect(img).not.toBeNull();
+ expect(img?.getAttribute("src")).toBe("api/images/abc/Pic");
+ expect(img?.getAttribute("alt")).toBe("Pic");
+ expect(img?.className).toBe("note-detail-image-view");
+ });
+
+ it("wires up the interactive zoom behavior", () => {
+ renderViewer({ src: "x" });
+ const props = transformWrapperSpy.mock.calls[0][0];
+ expect(props.centerOnInit).toBe(true);
+ expect(props.centerZoomedOut).toBe(true);
+ expect(props.autoAlignment).toEqual({ disabled: true });
+ expect(props.doubleClick).toEqual({ mode: "reset" });
+ // Numeric envelope is user-tunable — assert sane relationships, not exact values.
+ expect(props.maxScale).toBeGreaterThan(props.minScale);
+ expect(props.wheel.step).toBeGreaterThan(0);
+ });
+
+ it("lets minScale and maxScale be overridden", () => {
+ renderViewer({ src: "x", minScale: 1, maxScale: 8 });
+ const props = transformWrapperSpy.mock.calls[0][0];
+ expect(props.minScale).toBe(1);
+ expect(props.maxScale).toBe(8);
+ });
+});
diff --git a/apps/client/src/widgets/type_widgets/Image.tsx b/apps/client/src/widgets/type_widgets/Image.tsx
index dee60fc0b5..8ae6fab030 100644
--- a/apps/client/src/widgets/type_widgets/Image.tsx
+++ b/apps/client/src/widgets/type_widgets/Image.tsx
@@ -9,10 +9,9 @@ import { createImageSrcUrl } from "../../services/utils";
import { useTriliumEvent, useTriliumEvents } from "../react/hooks";
import ImageViewer from "../react/ImageViewer";
import { refToJQuerySelector } from "../react/react_utils";
+import { applyImageZoom } from "./image_zoom";
import { TypeWidgetProps } from "./type_widget";
-const ZOOM_STEP = 0.5;
-
export default function Image({ note, ntxId }: TypeWidgetProps) {
const containerRef = useRef<HTMLDivElement>(null);
const copyRef = useRef<HTMLDivElement>(null);
@@ -32,13 +31,9 @@ export default function Image({ note, ntxId }: TypeWidgetProps) {
copyRef.current.replaceChildren();
});
- useTriliumEvents([ "imageZoomIn", "imageZoomOut", "imageZoomReset" ], ({ ntxId: eventNtxId }, eventName) => {
- const zoom = zoomRef.current;
- if (eventNtxId !== ntxId || !zoom) return;
- if (eventName === "imageZoomIn") zoom.zoomIn(ZOOM_STEP);
- else if (eventName === "imageZoomOut") zoom.zoomOut(ZOOM_STEP);
- else zoom.resetTransform();
- });
+ useTriliumEvents([ "imageZoomIn", "imageZoomOut", "imageZoomReset" ], ({ ntxId: eventNtxId }, eventName) =>
+ applyImageZoom(zoomRef.current, eventName, eventNtxId, ntxId)
+ );
// A new revision swaps the image content; remount so it re-fits to the viewport.
useTriliumEvent("entitiesReloaded", ({ loadResults }) => {
diff --git a/apps/client/src/widgets/type_widgets/image_zoom.spec.ts b/apps/client/src/widgets/type_widgets/image_zoom.spec.ts
new file mode 100644
index 0000000000..777c3d2256
--- /dev/null
+++ b/apps/client/src/widgets/type_widgets/image_zoom.spec.ts
@@ -0,0 +1,52 @@
+import type { ReactZoomPanPinchRef } from "react-zoom-pan-pinch";
+import { describe, expect, it, vi } from "vitest";
+
+import { applyImageZoom } from "./image_zoom";
+
+function stubZoom() {
+ return { zoomIn: vi.fn(), zoomOut: vi.fn(), resetTransform: vi.fn() };
+}
+
+const asRef = (zoom: ReturnType<typeof stubZoom>) => zoom as unknown as ReactZoomPanPinchRef;
+
+describe("applyImageZoom", () => {
+ it("routes each event to its matching zoom action", () => {
+ const zoomIn = stubZoom();
+ applyImageZoom(asRef(zoomIn), "imageZoomIn", "ntx", "ntx");
+ expect(zoomIn.zoomIn).toHaveBeenCalledOnce();
+ expect(zoomIn.zoomOut).not.toHaveBeenCalled();
+ expect(zoomIn.resetTransform).not.toHaveBeenCalled();
+
+ const zoomOut = stubZoom();
+ applyImageZoom(asRef(zoomOut), "imageZoomOut", "ntx", "ntx");
+ expect(zoomOut.zoomOut).toHaveBeenCalledOnce();
+ expect(zoomOut.zoomIn).not.toHaveBeenCalled();
+ expect(zoomOut.resetTransform).not.toHaveBeenCalled();
+
+ const reset = stubZoom();
+ applyImageZoom(asRef(reset), "imageZoomReset", "ntx", "ntx");
+ expect(reset.resetTransform).toHaveBeenCalledOnce();
+ expect(reset.zoomIn).not.toHaveBeenCalled();
+ expect(reset.zoomOut).not.toHaveBeenCalled();
+ });
+
+ it("zooms in and out by the same configured step", () => {
+ const zoom = stubZoom();
+ applyImageZoom(asRef(zoom), "imageZoomIn", "ntx", "ntx");
+ applyImageZoom(asRef(zoom), "imageZoomOut", "ntx", "ntx");
+ const inStep = zoom.zoomIn.mock.calls[0][0];
+ const outStep = zoom.zoomOut.mock.calls[0][0];
+ expect(inStep).toBeTypeOf("number");
+ expect(inStep).toBe(outStep);
+ });
+
+ it("ignores events from a different note context", () => {
+ const zoom = stubZoom();
+ applyImageZoom(asRef(zoom), "imageZoomIn", "ntx-a", "ntx-b");
+ expect(zoom.zoomIn).not.toHaveBeenCalled();
+ });
+
+ it("is a no-op when the zoom instance is not ready", () => {
+ expect(() => applyImageZoom(null, "imageZoomReset", "ntx", "ntx")).not.toThrow();
+ });
+});
diff --git a/apps/client/src/widgets/type_widgets/image_zoom.ts b/apps/client/src/widgets/type_widgets/image_zoom.ts
new file mode 100644
index 0000000000..c8621cc902
--- /dev/null
+++ b/apps/client/src/widgets/type_widgets/image_zoom.ts
@@ -0,0 +1,20 @@
+import type { ReactZoomPanPinchRef } from "react-zoom-pan-pinch";
+
+/** Scale factor applied per zoom-in/out button click (react-zoom-pan-pinch's zoomIn/zoomOut step). */
+export const IMAGE_ZOOM_STEP = 0.5;
+
+export type ImageZoomEvent = "imageZoomIn" | "imageZoomOut" | "imageZoomReset";
+
+/** Routes an image-zoom event to the matching zoom action, scoped to the originating note context. */
+export function applyImageZoom(
+ zoom: ReactZoomPanPinchRef | null,
+ eventName: ImageZoomEvent,
+ eventNtxId: string | null | undefined,
+ ntxId: string | null | undefined
+) {
+ if (!zoom || eventNtxId !== ntxId) return;
+
+ if (eventName === "imageZoomIn") zoom.zoomIn(IMAGE_ZOOM_STEP);
+ else if (eventName === "imageZoomOut") zoom.zoomOut(IMAGE_ZOOM_STEP);
+ else zoom.resetTransform();
+}