Open History to see the last commit
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});