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

ExpandedImageDialog.browser.test.tsx

74 lines2.4 KB

1import { act } from "react";
2import { createRoot, type Root } from "react-dom/client";
3import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
4​
5import { AppAtomRegistryProvider } from "~/rpc/atomRegistry";
6import { ExpandedImageDialogHost, openExpandedImagePreview } from "./ExpandedImageDialog";
7​
8let root: Root;
9let container: HTMLDivElement;
10​
11const preview = {
12 images: [
13 { src: "data:image/gif;base64,R0lGODlhAQABAAAAACw=", name: "one.png" },
14 { src: "data:image/gif;base64,R0lGODlhAQABAAAAACw=", name: "two.png" },
15 ],
16 index: 0,
17};
18​
19beforeEach(() => {
20 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
21 container = document.createElement("div");
22 document.body.append(container);
23 root = createRoot(container);
24});
25​
26afterEach(async () => {
27 await act(() => root.unmount());
28 container.remove();
29 vi.unstubAllGlobals();
30});
31​
32const host = (threadKey: string) => (
33 <AppAtomRegistryProvider>
34 <ExpandedImageDialogHost threadKey={threadKey} />
35 </AppAtomRegistryProvider>
36);
37​
38const dialog = () => document.querySelector('[role="dialog"]');
39​
40describe("ExpandedImageDialogHost", () => {
41 it("opens for its thread, steps with arrows, and closes on Escape with focus back on the opener", async () => {
42 const opener = document.createElement("button");
43 document.body.append(opener);
44 await act(() => root.render(host("env:a")));
45 opener.focus();
46​
47 await act(() => openExpandedImagePreview("env:a", preview));
48 expect(dialog()?.textContent).toContain("one.png (1/2)");
49​
50 await act(() => {
51 dialog()?.dispatchEvent(new KeyboardEvent("keydown", { key: "ArrowRight", bubbles: true }));
52 });
53 expect(dialog()?.textContent).toContain("two.png (2/2)");
54​
55 await act(() => {
56 window.dispatchEvent(new KeyboardEvent("keydown", { key: "Escape", bubbles: true }));
57 });
58 await vi.waitFor(() => expect(dialog()).toBeNull());
59 expect(document.activeElement).toBe(opener);
60 opener.remove();
61 });
62​
63 it("ignores another thread's preview and drops its own when the thread changes", async () => {
64 await act(() => root.render(host("env:a")));
65 await act(() => openExpandedImagePreview("env:b", preview));
66 expect(dialog()).toBeNull();
67​
68 await act(() => openExpandedImagePreview("env:a", preview));
69 expect(dialog()).not.toBeNull();
70​
71 await act(() => root.render(host("env:b")));
72 await vi.waitFor(() => expect(dialog()).toBeNull());
73 });
74});