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

HtmlRenderWebView.test.tsx

107 lines3.8 KB

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 openedLinks: [] as string[],
8 openWindow: null as ((event: { nativeEvent: { targetUrl: string } }) => void) | null,
9 message: null as ((event: { nativeEvent: { data: string } }) => void) | null,
10 injectedJavaScript: "",
11 preview: null as unknown,
12}));
13​
14type Children = { children?: ReactNode };
15​
16vi.mock("react-native", () => ({
17 Platform: { OS: "ios" },
18 View: ({ children }: Children) => <div>{children}</div>,
19 Pressable: ({ children }: Children) => <div>{children}</div>,
20}));
21vi.mock("react-native-webview", () => ({
22 WebView: (props: {
23 onOpenWindow: typeof harness.openWindow;
24 onMessage: typeof harness.message;
25 injectedJavaScript: string;
26 }) => {
27 harness.openWindow = props.onOpenWindow;
28 harness.message = props.onMessage;
29 harness.injectedJavaScript = props.injectedJavaScript;
30 return null;
31 },
32}));
33vi.mock("../../components/ui/FilePreviewModal", () => ({
34 FilePreviewModal: (props: { source: unknown }) => {
35 harness.preview = props.source;
36 return null;
37 },
38}));
39vi.mock("../../components/ui/AppActivityIndicator", () => ({ AppActivityIndicator: () => null }));
40vi.mock("../../components/ui/AppSymbol", () => ({ SymbolView: () => null }));
41vi.mock("../../components/ui/AppText", () => ({
42 AppText: ({ children }: Children) => <span>{children}</span>,
43}));
44vi.mock("../../lib/htmlRenderTheme", () => ({
45 mobileHtmlRenderTheme: () => ({ variables: {} }),
46}));
47vi.mock("../settings/appearance/AppearancePreferencesProvider", () => ({
48 useAppearancePreferences: () => ({}),
49}));
50vi.mock("../../state/assets", () => ({
51 useAssetUrlState: () => ({ _tag: "Initial" }),
52 useRefreshAssetUrl: () => async () => null,
53}));
54vi.mock("../../native/useOpenPullRequest", () => ({
55 useOpenPullRequest: () => async (url: string) => {
56 harness.openedLinks.push(url);
57 return true;
58 },
59}));
60vi.mock("@react-navigation/native", () => ({ useNavigation: () => ({}) }));
61​
62import { HtmlRenderWebView } from "./HtmlRenderWebView";
63​
64it("opens a render's issue link the way the thread's own links open", async () => {
65 (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
66 const root = createRoot(document.createElement("div"));
67 await act(async () =>
68 root.render(<HtmlRenderWebView uri="https://assets.test/render.html" title="Links" nested />),
69 );
70​
71 const issueUrl = "https://github.com/pingdotgg/t3code/issues/41939";
72 await act(async () => harness.openWindow?.({ nativeEvent: { targetUrl: issueUrl } }));
73​
74 expect(harness.openedLinks).toEqual([issueUrl]);
75 await act(async () => root.unmount());
76});
77​
78it("opens a tapped render image in the image viewer with the page's other images", async () => {
79 (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
80 const root = createRoot(document.createElement("div"));
81 await act(async () =>
82 root.render(
83 <HtmlRenderWebView uri="https://assets.test/render.html" title="Gallery" nested={false} />,
84 ),
85 );
86​
87 expect(harness.injectedJavaScript).toContain("t3/notifications/open-image");
88 const images = [
89 { src: "data:image/png;base64,AAAA", name: "settings.png" },
90 { src: "https://assets.test/profile.png", name: "profile.png" },
91 ];
92 await act(async () =>
93 harness.message?.({
94 nativeEvent: {
95 data: JSON.stringify({
96 jsonrpc: "2.0",
97 method: "t3/notifications/open-image",
98 params: { src: images[1]!.src, name: images[1]!.name, images, index: 1 },
99 }),
100 },
101 }),
102 );
103​
104 const gallery = images.map((image) => ({ kind: "image", uri: image.src, name: image.name }));
105 expect(harness.preview).toEqual({ ...gallery[1], gallery });
106 await act(async () => root.unmount());
107});