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

IntegrationsSettings.test.tsx

138 lines4.3 KB

1import {
2 DEFAULT_CLIENT_SETTINGS,
3 DEFAULT_UNIFIED_SETTINGS,
4 type DeviceServiceState,
5} from "@t3tools/contracts";
6import {
7 createMemoryHistory,
8 createRootRoute,
9 createRouter,
10 RouterProvider,
11} from "@tanstack/react-router";
12import { act, StrictMode, type ReactNode } from "react";
13import { create, type ReactTestRenderer } from "react-test-renderer";
14import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
15​
16const { listBrowserImportSources } = vi.hoisted(() => ({
17 listBrowserImportSources: vi.fn().mockResolvedValue([]),
18}));
19​
20vi.mock("../preview/previewBridge", () => ({
21 previewBridge: { listBrowserImportSources },
22}));
23vi.mock("../../env", () => ({ isElectron: true }));
24vi.mock("../../state/environments", () => ({
25 useEnvironments: () => ({ environments: [], isReady: true }),
26 usePrimaryEnvironment: () => null,
27}));
28vi.mock("../../hooks/useSettings", () => ({
29 PRIMARY_SETTINGS_UNAVAILABLE_MESSAGE: "Connect to an environment",
30 useClientSettings: (selector: (settings: typeof DEFAULT_CLIENT_SETTINGS) => unknown) =>
31 selector(DEFAULT_CLIENT_SETTINGS),
32 useClientSettingsHydrated: () => true,
33 usePrimarySettingsAvailable: () => true,
34 usePrimarySettings: () => DEFAULT_UNIFIED_SETTINGS,
35 useUpdatePrimarySettings: () => vi.fn(),
36}));
37vi.mock("./settingsLayout", async (importOriginal) => ({
38 ...(await importOriginal<typeof import("./settingsLayout")>()),
39 SettingsPageContainer: ({ children }: { children: ReactNode }) => children,
40}));
41​
42import { IntegrationsSettingsPanel } from "./IntegrationsSettings";
43import { platformSetupStatus } from "../device/DeviceSetup";
44​
45let renderer: ReactTestRenderer | undefined;
46​
47beforeEach(() => {
48 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
49 listBrowserImportSources.mockClear();
50});
51​
52afterEach(async () => {
53 await act(() => renderer?.unmount());
54 vi.unstubAllGlobals();
55});
56​
57async function openSettings() {
58 const router = createRouter({
59 routeTree: createRootRoute({ component: IntegrationsSettingsPanel }),
60 history: createMemoryHistory(),
61 });
62 await router.load();
63 await act(() => {
64 renderer = create(
65 <StrictMode>
66 <RouterProvider router={router} />
67 </StrictMode>,
68 );
69 });
70 expect(renderer!.root.findByType(IntegrationsSettingsPanel)).toBeDefined();
72​
73describe("Integrations browser discovery", () => {
74 it("does not scan browser files when entering or revisiting settings", async () => {
75 await openSettings();
76 expect(listBrowserImportSources).not.toHaveBeenCalled();
77​
78 await act(() => renderer?.unmount());
79 await openSettings();
80 expect(listBrowserImportSources).not.toHaveBeenCalled();
81 });
82​
83 it("places device settings directly after browser settings", async () => {
84 await openSettings();
85 const sections = renderer!.root
86 .findAll((node) => node.type === "section")
87 .map((node) => node.props.id)
88 .filter(Boolean);
89 expect(sections.indexOf("devices")).toBeGreaterThan(sections.indexOf("browser"));
90 });
91});
92​
93const deviceState = (overrides: Partial<DeviceServiceState> = {}): DeviceServiceState => ({
94 hosts: [
95 {
96 id: "local",
97 kind: "local",
98 label: "This machine",
99 hubInstalled: false,
100 agentDeviceInstalled: false,
101 platforms: [
102 { platform: "ios", available: true },
103 { platform: "android", available: true },
104 ],
105 },
106 ],
107 hostStatus: "ready",
108 hostStatuses: {},
109 devices: [],
110 sessions: [],
111 onboardingCompleted: false,
112 agentAccessEnabled: false,
113 hubBasePath: "/api/device-hub",
114 revision: 0,
115 ...overrides,
116});
117​
118describe("device setup guidance", () => {
119 it("directs users to install an iOS runtime and create an Android virtual device", () => {
120 expect(platformSetupStatus(deviceState(), "ios").message).toContain("Xcode Settings");
121 expect(platformSetupStatus(deviceState(), "android").message).toContain("Device Manager");
122 });
123​
124 it("preserves a specific missing-tool explanation from the server", () => {
125 const state = deviceState({
126 hosts: [
127 {
128 ...deviceState().hosts[0]!,
129 platforms: [
130 { platform: "ios", available: true },
131 { platform: "android", available: false, reason: "Android Emulator is missing." },
132 ],
133 },
134 ],
135 });
136 expect(platformSetupStatus(state, "android").message).toBe("Android Emulator is missing.");
137 });
138});