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 { Children, loaded } = vi.hoisted(() => ({
7 Children: ({ children }: { children?: ReactNode }) => <>{children}</>,
8 loaded: [] as string[],
9}));
10
11vi.mock("react-native", () => ({ View: Children }));
12vi.mock("react-native-safe-area-context", () => ({ SafeAreaView: Children }));
13vi.mock("@react-navigation/native", () => ({ useNavigation: () => ({ goBack: vi.fn() }) }));
14vi.mock("../../components/ui/AppText", () => ({ AppText: Children }));
15vi.mock("../../components/ui/Dialog", () => ({ alert: vi.fn() }));
16vi.mock("../../components/ui/EmptyState", () => ({ EmptyState: () => null }));
17vi.mock("../../state/entities", () => ({ useEnvironmentPreviewTunnelOff: () => false }));
18vi.mock("../../state/environments", () => ({
19 useEnvironments: () => ({ presentationById: new Map() }),
20}));
21vi.mock("../settings/appearance/AppearancePreferencesProvider", () => ({
22 useAppearancePreferences: () => ({ themeVariables: {} }),
23}));
24vi.mock("./BrowserBar", () => ({
25 BrowserChrome: (props: {
26 children: (page: { topInset: number; onScrollDirection: () => void }) => ReactNode;
27 }) => <>{props.children({ topInset: 0, onScrollDirection: () => {} })}</>,
28}));
29vi.mock("./TunnelWebView", () => ({
30 TunnelWebView: ({ url }: { url: string }) => {
31 loaded.push(url);
32 return null;
33 },
34}));
35
36import { LocalPreviewRouteScreen } from "./LocalPreviewRouteScreen";
37
38it("gives the webview only web addresses from the route, on open and on a later link", () => {
39 (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
40 const root = createRoot(document.createElement("div"));
41 const show = (url: string) => {
42 loaded.length = 0;
43 act(() =>
44 root.render(<LocalPreviewRouteScreen route={{ params: { environmentId: "env-1", url } }} />),
45 );
46 return loaded.at(-1);
47 };
48
49 try {
50 expect(show("http://0.0.0.0:5173/app")).toBe("http://localhost:5173/app");
51 expect(show("javascript:document.title='x'")).toBe("");
52 expect(show("https://example.com/docs")).toBe("https://example.com/docs");
53 expect(show("file:///data/local/tmp/a.html")).toBe("");
54 expect(show("intent://example.com#Intent;scheme=http;end")).toBe("");
55 } finally {
56 act(() => root.unmount());
57 }
58});