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