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

FilePreviewSession.test.tsx

146 lines5.0 KB

1import { act, useState } from "react";
2import { createRoot } from "react-dom/client";
3import { expect, it, vi } from "vite-plus/test";
4import { EnvironmentId, SOURCE_CONTROL_MEDIA_ASSET } from "@t3tools/contracts";
5​
6const harness = vi.hoisted(() => ({
7 refreshAssetUrl: vi.fn<(environmentId: string, resource: { url: string }) => Promise<string>>(),
8 alert: vi.fn(),
9 previews: [] as unknown[],
10 mounts: 0,
11}));
12​
13vi.mock("../../state/assets", () => ({ refreshAssetUrl: harness.refreshAssetUrl }));
14vi.mock("../../lib/localAttachmentPreview", () => ({ loadLocalAttachmentPreview: vi.fn() }));
15vi.mock("../../lib/attachmentDownload", () => ({
16 downloadAndShareAttachment: vi.fn(),
17 nativeViewerErrorMessage: () => "",
18 shareLocalAttachment: vi.fn(),
19}));
20vi.mock("../../lib/previewPresentation", () => ({ usePreviewPresentation: () => {} }));
21vi.mock("./Dialog", () => ({ alert: harness.alert }));
22​
23import type { FilePreviewItem, FilePreviewViewerProps } from "./FilePreviewModal.types";
24import { FilePreviewSession } from "./FilePreviewSession";
25​
26function Viewer(props: FilePreviewViewerProps) {
27 useState(() => harness.mounts++);
28 harness.previews.push(props.source);
29 return null;
31​
32const environmentId = EnvironmentId.make("environment");
33const image = (url: string): FilePreviewItem => ({
34 kind: "image",
35 environmentId,
36 resource: { _tag: SOURCE_CONTROL_MEDIA_ASSET, cwd: "/repo", url },
37});
38​
39async function openGallery(
40 selected: FilePreviewItem,
41 gallery: readonly FilePreviewItem[],
42 progressiveGallery: boolean,
43) {
44 (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
45 harness.previews.length = 0;
46 harness.mounts = 0;
47 harness.alert.mockClear();
48 const root = createRoot(document.createElement("div"));
49 const onRequestClose = vi.fn();
50 await act(async () =>
51 root.render(
52 <FilePreviewSession
53 source={{ ...selected, gallery }}
54 onRequestClose={onRequestClose}
55 progressiveGallery={progressiveGallery}
56 Viewer={Viewer}
57 />,
58 ),
59 );
60 return { onRequestClose, [Symbol.asyncDispose]: () => act(async () => root.unmount()) };
62​
63it.each([true, false])(
64 "keeps the gallery stable while a sibling is still resolving (progressive: %s)",
65 async (progressiveGallery) => {
66 const slow = Promise.withResolvers<string>();
67 harness.refreshAssetUrl.mockImplementation(async (_environmentId, resource) =>
68 resource.url.endsWith("slow.png") ? slow.promise : "https://signed.test/a.png",
69 );
70 const selected = image("https://github.com/a.png");
71 await using _preview = await openGallery(
72 selected,
73 [image("https://github.com/slow.png"), selected],
74 progressiveGallery,
75 );
76​
77 if (progressiveGallery) {
78 expect(harness.previews.at(-1)).toMatchObject({
79 kind: "image",
80 selectedIndex: 1,
81 images: [
82 expect.not.objectContaining({ uri: expect.anything() }),
83 { uri: "https://signed.test/a.png" },
84 ],
85 });
86 } else {
87 expect(harness.previews).toEqual([]);
88 }
89 await act(async () => slow.resolve("https://signed.test/slow.png"));
90 expect(harness.previews.at(-1)).toMatchObject({
91 selectedIndex: 1,
92 images: [{ uri: "https://signed.test/slow.png" }, { uri: "https://signed.test/a.png" }],
93 });
94 expect(harness.mounts).toBe(1);
95 },
96);
97​
98it.each([true, false])(
99 "handles a failed sibling without leaving a pending page (progressive: %s)",
100 async (progressiveGallery) => {
101 harness.refreshAssetUrl.mockImplementation(async (_environmentId, resource) => {
102 if (resource.url.endsWith("missing.png")) throw new Error("not found");
103 return `https://signed.test/${resource.url.split("/").pop()}`;
104 });
105 const selected = image("https://github.com/a.png");
106 await using preview = await openGallery(
107 selected,
108 [selected, image("https://github.com/missing.png"), image("https://github.com/c.png")],
109 progressiveGallery,
110 );
111​
112 if (!progressiveGallery) {
113 expect(preview.onRequestClose).toHaveBeenCalledOnce();
114 expect(harness.alert).toHaveBeenCalledOnce();
115 expect(harness.previews).toEqual([]);
116 return;
117 }
118​
119 expect(preview.onRequestClose).not.toHaveBeenCalled();
120 expect(harness.alert).not.toHaveBeenCalled();
121 expect(harness.previews.at(-1)).toMatchObject({
122 selectedIndex: 0,
123 images: [
124 { uri: "https://signed.test/a.png" },
125 { failed: true },
126 { uri: "https://signed.test/c.png" },
127 ],
128 });
129 },
130);
131​
132it("closes the preview when the selected image cannot be resolved", async () => {
133 harness.refreshAssetUrl.mockImplementation(async (_environmentId, resource) => {
134 if (resource.url.endsWith("missing.png")) throw new Error("not found");
135 return `https://signed.test/${resource.url.split("/").pop()}`;
136 });
137 const selected = image("https://github.com/missing.png");
138 await using preview = await openGallery(
139 selected,
140 [image("https://github.com/a.png"), selected],
141 true,
142 );
143​
144 expect(preview.onRequestClose).toHaveBeenCalledOnce();
145 expect(harness.alert).toHaveBeenCalledOnce();
146});