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

ConnectionsNewRouteScreen.test.tsx

158 lines6.1 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 { Children, harness } = vi.hoisted(() => ({
7 Children: ({ children }: { children?: ReactNode }) => <>{children}</>,
8 harness: {
9 // The app-wide error slot other features (composer sends, branch refreshes) write to.
10 pendingConnectionError: null as string | null,
11 connectPress: vi.fn(),
12 },
13}));
14​
15vi.mock("react-native", () => ({
16 View: Children,
17 Pressable: Children,
18 Linking: { openSettings: vi.fn() },
19 StyleSheet: { absoluteFill: {} },
20}));
21vi.mock("react-native-svg", () => ({
22 default: Children,
23 Defs: Children,
24 RadialGradient: Children,
25 Rect: () => null,
26 Stop: () => null,
27}));
28vi.mock("react-native-safe-area-context", () => ({ useSafeAreaInsets: () => ({ bottom: 0 }) }));
29vi.mock("@react-navigation/native", () => ({
30 StackActions: { replace: vi.fn() },
31 useIsFocused: () => true,
32 useNavigation: () => ({ canGoBack: () => true, goBack: vi.fn(), dispatch: vi.fn() }),
33 useRoute: () => ({ name: "ConnectionsNew" }),
34}));
35vi.mock("@effect/atom-react", () => ({ useAtomValue: () => null }));
36vi.mock("@clerk/expo", () => ({ useAuth: () => ({ isLoaded: false, isSignedIn: false }) }));
37vi.mock("expo-camera", () => ({
38 CameraView: () => null,
39 useCameraPermissions: () => [null, vi.fn()],
40}));
41vi.mock("expo-haptics", () => ({
42 notificationAsync: vi.fn(async () => {}),
43 NotificationFeedbackType: { Success: "success", Error: "error" },
44}));
45vi.mock("../../components/ui/ListRow", () => ({ ListRow: () => null }));
46vi.mock("../../components/ui/Dialog", () => ({ alert: vi.fn() }));
47vi.mock("../../components/ui/ScreenScrollView", () => ({ ScreenScrollView: Children }));
48vi.mock("../../components/ui/AppText", () => ({
49 AppText: ({ children }: { children?: ReactNode }) => <span>{children}</span>,
50 AppTextInput: ({
51 accessibilityLabel,
52 value,
53 onChangeText,
54 }: {
55 accessibilityLabel: string;
56 value: string;
57 onChangeText: (value: string) => void;
58 }) => (
59 <input
60 aria-label={accessibilityLabel}
61 value={value}
62 onChange={(event) => onChangeText(event.currentTarget.value)}
63 />
64 ),
65}));
66vi.mock("../../components/ui/FrostedCutout", () => ({ FrostedCutout: () => null }));
67vi.mock("../../components/ui/AppSymbol", () => ({ SymbolView: () => null }));
68vi.mock("../../lib/useUniwindTheme", () => ({ useUniwindTheme: () => ({}) }));
69vi.mock("../settings/appearance/AppearancePreferencesProvider", () => ({
70 useAppearancePreferences: () => ({ themeAppearance: "dark" }),
71}));
72vi.mock("../settings/components/SettingsScreen", () => ({ SettingsScreen: Children }));
73vi.mock("../settings/settings-navigation", () => ({
74 leaveSettingsPage: vi.fn(),
75 openSettings: vi.fn(),
76}));
77vi.mock("../layout/workspace/workspace-selection-store", () => ({
78 workspaceHasColumns: () => false,
79}));
80vi.mock("../cloud/publicConfig", () => ({ hasCloudPublicConfig: () => false }));
81vi.mock("./CloudEnvironmentRows", () => ({ CloudEnvironmentRows: () => null }));
82vi.mock("./environmentSections", () => ({
83 splitEnvironmentSections: () => ({ connectedCloudEnvironments: [] }),
84}));
85vi.mock("./devAutoConnect", () => ({
86 autoConnectReport: () => ({ connecting: false, error: null }),
87 devAutoConnectAttemptAtom: {},
88 pairingLinksAllowed: () => __DEV__,
89}));
90vi.mock("../../state/use-remote-environment-registry", () => ({
91 setPendingConnectionError: (message: string | null) => {
92 harness.pendingConnectionError = message;
93 },
94 useRemoteConnections: () => ({
95 connectionPairingUrl: "",
96 connectedEnvironments: [],
97 pairingConnectionError: harness.pendingConnectionError,
98 onChangeConnectionPairingUrl: vi.fn(),
99 onConnectPress: harness.connectPress,
100 onSetEnvironmentEnabled: vi.fn(),
101 onRemoveEnvironmentPress: vi.fn(),
102 }),
103}));
104​
105import { ConnectionsNewRouteScreen } from "./ConnectionsNewRouteScreen";
106​
107it("does not show another feature's error as a pairing failure", () => {
108 (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
109 vi.stubGlobal("__DEV__", false);
110 harness.pendingConnectionError = "Could not send the message.";
111 const container = document.createElement("div");
112 const root = createRoot(container);
113 act(() => root.render(<ConnectionsNewRouteScreen route={{ params: undefined }} />));
114​
115 expect(container.textContent).not.toContain("Could not send the message.");
116 expect(container.textContent).toContain("For machines on your local network or tailnet.");
117​
118 act(() => root.unmount());
119 // Leaving the form must not wipe an error that belongs to another screen.
120 expect(harness.pendingConnectionError).toBe("Could not send the message.");
121});
122​
123it("splits a pasted pairing link and preserves the code during address edits", () => {
124 (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
125 vi.stubGlobal("__DEV__", false);
126 harness.pendingConnectionError = null;
127 const container = document.createElement("div");
128 const root = createRoot(container);
129​
130 try {
131 act(() => root.render(<ConnectionsNewRouteScreen route={{ params: undefined }} />));
132 const address = container.querySelector<HTMLInputElement>('input[aria-label="Address"]')!;
133 const code = container.querySelector<HTMLInputElement>('input[aria-label="Code"]')!;
134 const enter = (input: HTMLInputElement, value: string) => {
135 act(() => {
136 Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, "value")!.set!.call(
137 input,
138 value,
139 );
140 input.dispatchEvent(new Event("input", { bubbles: true }));
141 });
142 };
143​
144 enter(code, "manual-code");
145 enter(address, " http://100.82.53.91:3773/pair#token=test-pairing-code\n");
146 expect(address.value).toBe("http://100.82.53.91:3773");
147 expect(code.value).toBe("test-pairing-code");
148​
149 enter(code, "replacement-code");
150 for (const value of ["", "100.82.53.92:3773", "http://100.82.53.92:3773/"]) {
151 enter(address, value);
152 expect(address.value).toBe(value);
153 expect(code.value).toBe("replacement-code");
154 }
155 } finally {
156 act(() => root.unmount());
157 }
158});