Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ExpandedImageDialog.browser.test.tsx

151 lines6.2 KB

1import type { SnapShotSource } from "@t3tools/contracts";
2import { describe, expect, it, vi } from "vite-plus/test";
3import { userEvent } from "vite-plus/test/browser";
4​
5import "~/index.css";
6import { render } from "../../../test/render";
7import { ExpandedImageDialog } from "./ExpandedImageDialog";
8​
9const PIXEL =
10 "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=";
11​
12const snapShotSource: SnapShotSource = {
13 kind: "snap-shot",
14 capturedAt: "2026-01-01T00:00:00.000Z",
15 appName: "Finder",
16 windowTitle: "Downloads",
17};
18​
19const D2_SVG =
20 '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1174 920"><rect width="1174" height="920"/></svg>';
21​
22describe("ExpandedImageDialog", () => {
23 it.each(["/work/pipeline.svg", "/work/diagram#v2.svg", "/work/diagram?v2.svg"])(
24 "fits a markdown SVG without intrinsic size to the viewer: %s",
25 async (path) => {
26 const src = URL.createObjectURL(new Blob([D2_SVG], { type: "image/svg+xml" }));
27 using _src = { [Symbol.dispose]: () => URL.revokeObjectURL(src) };
28 await using screen = await render(
29 <ExpandedImageDialog
30 preview={{
31 images: [
32 {
33 src,
34 name: "Pipeline",
35 actionsSource: {
36 kind: "image",
37 name: "Pipeline",
38 src,
39 reference: { kind: "file", path },
40 },
41 },
42 ],
43 index: 0,
44 }}
45 onClose={vi.fn()}
46 />,
47 // The dialog renders no links and reads no route.
48 { router: false },
49 );
50​
51 const viewer = screen.getByRole("region", { name: "Pipeline, zoomable image" });
52 const availableWidth = window.innerWidth * 0.92 - (window.innerWidth >= 640 ? 96 : 0);
53 await vi.waitFor(() =>
54 expect(viewer.element().getBoundingClientRect().width).toBeCloseTo(availableWidth, 0),
55 );
56 },
57 );
58​
59 it("Escape closes a popup opened inside the dialog before the dialog", async () => {
60 const onClose = vi.fn();
61 await using screen = await render(
62 <ExpandedImageDialog
63 preview={{ images: [{ src: PIXEL, name: "shot.png", source: snapShotSource }], index: 0 }}
64 onClose={onClose}
65 />,
66 // The dialog renders no links and reads no route.
67 { router: false },
68 );
69​
70 await userEvent.click(screen.getByRole("button", { name: "No accessibility data" }));
71 await expect.element(screen.getByText("Accessibility data")).toBeVisible();
72​
73 await userEvent.keyboard("{Escape}");
74 await expect.element(screen.getByText("Accessibility data")).not.toBeInTheDocument();
75 expect(onClose).not.toHaveBeenCalled();
76​
77 // Focus returns to the popup's trigger and opens its tooltip; leave it for a plain control.
78 (screen.getByRole("button", { name: "Close image preview" }).element() as HTMLElement).focus();
79 await userEvent.keyboard("{Escape}");
80 await vi.waitFor(() => expect(onClose).toHaveBeenCalledOnce());
81 });
82​
83 it("scrolling pans the image while Ctrl+wheel zooms around the pointer", async () => {
84 const canvas = document.createElement("canvas");
85 canvas.width = 1600;
86 canvas.height = 1200;
87 await using screen = await render(
88 <ExpandedImageDialog
89 preview={{ images: [{ src: canvas.toDataURL(), name: "large.png" }], index: 0 }}
90 onClose={() => {}}
91 />,
92 // The dialog renders no links and reads no route.
93 { router: false },
94 );
95 const viewportLocator = screen.getByRole("region", { name: "large.png, zoomable image" });
96 const viewport = viewportLocator.element() as HTMLElement;
97 const image = screen.getByRole("img", { name: "large.png" }).element() as HTMLElement;
98 await vi.waitFor(() => expect(image.style.width).not.toBe(""));
99 const fitWidth = image.getBoundingClientRect().width;
100 const wheels: { ctrlKey: boolean; canceled: boolean }[] = [];
101 const recordWheel = (event: WheelEvent) =>
102 wheels.push({ ctrlKey: event.ctrlKey, canceled: event.defaultPrevented });
103 document.addEventListener("wheel", recordWheel);
104 using _wheelListener = {
105 [Symbol.dispose]: () => document.removeEventListener("wheel", recordWheel),
106 };
107 // Wheels land on the viewport's center; this is the image fraction under it.
108 const pointerFraction = () => {
109 const viewportBounds = viewport.getBoundingClientRect();
110 const imageBounds = image.getBoundingClientRect();
111 return {
112 x: (viewportBounds.left + viewportBounds.width / 2 - imageBounds.left) / imageBounds.width,
113 y: (viewportBounds.top + viewportBounds.height / 2 - imageBounds.top) / imageBounds.height,
114 };
115 };
116 const ctrlWheel = async (deltaY: number) => {
117 await userEvent.keyboard("{Control>}");
118 await userEvent.wheel(viewportLocator, { delta: { y: deltaY } });
119 await userEvent.keyboard("{/Control}");
120 };
121​
122 await userEvent.wheel(viewportLocator, { delta: { y: 120 } });
123 expect(wheels.at(-1)).toEqual({ ctrlKey: false, canceled: false });
124 expect(image.getBoundingClientRect().width).toBe(fitWidth);
125​
126 await ctrlWheel(-100);
127 expect(wheels.at(-1)).toEqual({ ctrlKey: true, canceled: true });
128 const zoomedWidth = image.getBoundingClientRect().width;
129 expect(zoomedWidth).toBeCloseTo(fitWidth * Math.E, 0);
130​
131 const pan = async (delta: { x: number; y: number }) => {
132 const { scrollLeft, scrollTop } = viewport;
133 await userEvent.wheel(viewportLocator, { delta });
134 await vi.waitFor(() => {
135 expect(Math.sign(viewport.scrollLeft - scrollLeft)).toBe(Math.sign(delta.x));
136 expect(Math.sign(viewport.scrollTop - scrollTop)).toBe(Math.sign(delta.y));
137 });
138 };
139 await pan({ x: 60, y: 0 });
140 await pan({ x: 0, y: 60 });
141 await pan({ x: -40, y: -40 });
142 expect(wheels.filter((wheel) => !wheel.ctrlKey).every((wheel) => !wheel.canceled)).toBe(true);
143 expect(image.getBoundingClientRect().width).toBe(zoomedWidth);
144​
145 const anchor = pointerFraction();
146 await ctrlWheel(-50);
147 expect(image.getBoundingClientRect().width).toBeGreaterThan(zoomedWidth);
148 expect(pointerFraction().x).toBeCloseTo(anchor.x, 2);
149 expect(pointerFraction().y).toBeCloseTo(anchor.y, 2);
150 });
151});