Open History to see the last commit
1import type { ReactNode } from "react";
2import { act } from "react";
3import { createRoot } from "react-dom/client";
4import { expect, it, vi } from "vite-plus/test";
5
6const harness = vi.hoisted(() => ({
7 failImage: () => {},
8 Passthrough: ({ children }: { children?: unknown }) => children,
9}));
10
11vi.mock("react-native", () => ({
12 Pressable: harness.Passthrough,
13 StyleSheet: { absoluteFill: {} },
14 View: harness.Passthrough,
15}));
16vi.mock("expo-image", () => ({
17 Image: (props: { onError: () => void }) => {
18 harness.failImage = props.onError;
19 return <img alt="" />;
20 },
21}));
22vi.mock("expo/fetch", () => ({ fetch: vi.fn() }));
23vi.mock("react-native-svg", () => ({ parse: vi.fn(), SvgAst: () => null }));
24vi.mock("@t3tools/client-runtime/state/assets", () => ({ MISSING_FILE_ACCESS_MESSAGE: "" }));
25vi.mock("../../components/ui/AppActivityIndicator", () => ({ AppActivityIndicator: () => null }));
26vi.mock("../../components/FilePreviewGallery", () => ({
27 useFilePreviewGallery: () => (source: unknown) => source,
28}));
29vi.mock("../../components/ui/AppText", () => ({
30 AppText: ({ children }: { children?: ReactNode }) => <span>{children}</span>,
31}));
32vi.mock("../../components/MediaActionsMenu", () => ({ MediaActionsMenu: harness.Passthrough }));
33vi.mock("../../components/ui/NativePresentation", () => ({
34 PresentationSource: harness.Passthrough,
35}));
36vi.mock("../../state/mediaActions", () => ({ useMediaActions: () => ({ actions: [] }) }));
37vi.mock("../../state/assets", () => ({ useAssetUrlState: vi.fn() }));
38
39import { RegistryContext } from "@effect/atom-react";
40import { AtomRegistry } from "effect/reactivity";
41import { fetch } from "expo/fetch";
42import { parse } from "react-native-svg";
43
44import { ThreadMarkdownImageView } from "./ThreadMarkdownImage";
45
46it("retries a failed image once its URI comes back through null", async () => {
47 (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
48 const container = document.createElement("div");
49 const root = createRoot(container);
50 const view = (uri: string | null) => (
51 <ThreadMarkdownImageView
52 uri={uri}
53 sourceKey="attachment:a"
54 unavailable={false}
55 alt={null}
56 onPressPreview={() => {}}
57 />
58 );
59 try {
60 await act(async () => root.render(view("https://example.test/a.png")));
61 await act(async () => harness.failImage());
62 expect(container.textContent).toContain("Image unavailable");
63
64 await act(async () => root.render(view(null)));
65 await act(async () => root.render(view("https://example.test/a.png")));
66 expect(container.textContent).not.toContain("Image unavailable");
67 expect(container.querySelector("img")).not.toBeNull();
68 } finally {
69 await act(async () => root.unmount());
70 }
71});
72
73it("fetches a failed SVG again on the next mount but keeps a parsed one", async () => {
74 (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
75 // Microtask scheduling lets act() flush an unmounted atom's removal before the next render.
76 const registry = AtomRegistry.make({
77 scheduleTask: (task) => {
78 queueMicrotask(task);
79 return () => {};
80 },
81 });
82 const fetchSvg = vi.mocked(fetch);
83 vi.mocked(parse).mockReturnValue({ props: {} } as ReturnType<typeof parse>);
84 const container = document.createElement("div");
85 const root = createRoot(container);
86 const uri = "https://example.test/a.svg";
87 const view = (mounted: boolean) => (
88 <RegistryContext.Provider value={registry}>
89 {mounted ? (
90 <ThreadMarkdownImageView
91 uri={uri}
92 sourceKey="attachment:svg"
93 unavailable={false}
94 alt={null}
95 format="svg"
96 onPressPreview={() => {}}
97 />
98 ) : null}
99 </RegistryContext.Provider>
100 );
101 try {
102 fetchSvg.mockRejectedValueOnce(new Error("offline"));
103 await act(async () => root.render(view(true)));
104 await vi.waitFor(() => expect(container.textContent).toContain("Image unavailable"));
105
106 await act(async () => root.render(view(false)));
107 fetchSvg.mockResolvedValue({ ok: true, status: 200, text: async () => "<svg />" } as never);
108 await act(async () => root.render(view(true)));
109 await vi.waitFor(() => expect(container.textContent).not.toContain("Image unavailable"));
110 expect(fetchSvg).toHaveBeenCalledTimes(2);
111
112 await act(async () => root.render(view(false)));
113 await act(async () => root.render(view(true)));
114 expect(container.textContent).not.toContain("Image unavailable");
115 expect(fetchSvg).toHaveBeenCalledTimes(2);
116 } finally {
117 await act(async () => root.unmount());
118 registry.dispose();
119 }
120});