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

HtmlRenderWebView.test.tsx

61 lines2.3 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}));
10​
11type Children = { children?: ReactNode };
12​
13vi.mock("react-native", () => ({
14 Platform: { OS: "ios" },
15 View: ({ children }: Children) => <div>{children}</div>,
16 Pressable: ({ children }: Children) => <div>{children}</div>,
17}));
18vi.mock("react-native-webview", () => ({
19 WebView: (props: { onOpenWindow: typeof harness.openWindow }) => {
20 harness.openWindow = props.onOpenWindow;
21 return null;
22 },
23}));
24vi.mock("../../components/ui/AppActivityIndicator", () => ({ AppActivityIndicator: () => null }));
25vi.mock("../../components/ui/AppSymbol", () => ({ SymbolView: () => null }));
26vi.mock("../../components/ui/AppText", () => ({
27 AppText: ({ children }: Children) => <span>{children}</span>,
28}));
29vi.mock("../../lib/htmlRenderTheme", () => ({
30 mobileHtmlRenderTheme: () => ({ variables: {} }),
31}));
32vi.mock("../settings/appearance/AppearancePreferencesProvider", () => ({
33 useAppearancePreferences: () => ({}),
34}));
35vi.mock("../../state/assets", () => ({
36 useAssetUrlState: () => ({ _tag: "Initial" }),
37 useRefreshAssetUrl: () => async () => null,
38}));
39vi.mock("../../native/useOpenPullRequest", () => ({
40 useOpenPullRequest: () => async (url: string) => {
41 harness.openedLinks.push(url);
42 return true;
43 },
44}));
45vi.mock("@react-navigation/native", () => ({ useNavigation: () => ({}) }));
46​
47import { HtmlRenderWebView } from "./HtmlRenderWebView";
48​
49it("opens a render's issue link the way the thread's own links open", async () => {
50 (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
51 const root = createRoot(document.createElement("div"));
52 await act(async () =>
53 root.render(<HtmlRenderWebView uri="https://assets.test/render.html" title="Links" nested />),
54 );
55​
56 const issueUrl = "https://github.com/pingdotgg/t3code/issues/41939";
57 await act(async () => harness.openWindow?.({ nativeEvent: { targetUrl: issueUrl } }));
58​
59 expect(harness.openedLinks).toEqual([issueUrl]);
60 await act(async () => root.unmount());
61});