Open History to see the last commit
1// @vitest-environment jsdom
2import { DEFAULT_SERVER_SETTINGS, type ServerSettingsPatch } from "@t3tools/contracts";
3import type { ReactNode } from "react";
4import { act } from "react";
5import { createRoot, type Root } from "react-dom/client";
6import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
7
8const { Children, mocks } = vi.hoisted(() => ({
9 Children: ({ children }: { children?: ReactNode }) => children,
10 mocks: {
11 branchNaming: { onChange: (_patch: ServerSettingsPatch) => {} },
12 updateSettings: vi.fn(),
13 },
14}));
15
16vi.mock("react-native", () => ({ View: Children }));
17vi.mock("react-native-safe-area-context", () => ({ useSafeAreaInsets: () => ({ bottom: 0 }) }));
18vi.mock("@react-navigation/native", () => ({ useNavigation: () => ({}) }));
19vi.mock("../../state/session", () => ({
20 readEnvironmentScope: () => true,
21 useEnvironmentsWithScope: () => new Set(["local"]),
22}));
23vi.mock("../../state/server", () => ({ serverEnvironment: { updateSettings: {} } }));
24vi.mock("../../state/use-atom-command", () => ({
25 useAtomCommand: () => mocks.updateSettings,
26}));
27vi.mock("../threads/thread-settings-options", () => ({ RUNTIME_MODE_CHOICES: [] }));
28vi.mock("../../components/ui/ScreenScrollView", () => ({ ScreenScrollView: Children }));
29vi.mock("../../components/ui/AppText", () => ({ AppText: Children }));
30vi.mock("./components/SettingsScreen", () => ({ SettingsScreen: Children }));
31vi.mock("./components/SettingsEnvironmentFilterHeader", () => ({
32 AndroidSettingsEnvironmentFilter: () => null,
33 SettingsEnvironmentFilterHeader: () => null,
34}));
35vi.mock("./components/DatabaseStorageNotice", () => ({ DatabaseStorageNotice: () => null }));
36vi.mock("./components/SettingsSection", () => ({ SettingsSection: Children }));
37vi.mock("./components/SettingsChoiceRow", () => ({ SettingsChoiceRow: () => null }));
38vi.mock("./components/SettingsSwitchRow", () => ({ SettingsSwitchRow: () => null }));
39vi.mock("./components/SettingsRow", () => ({ SettingsRow: () => null }));
40vi.mock("./components/SettingsProjectOverridesSection", () => ({
41 SettingsProjectOverridesSection: () => null,
42}));
43vi.mock("./components/BranchNamingSettings", () => ({
44 BranchNamingSettings: (props: { onChange: (patch: ServerSettingsPatch) => void }) => {
45 mocks.branchNaming.onChange = props.onChange;
46 return null;
47 },
48}));
49vi.mock("./settings-environment-filter", () => ({
50 useSettingsEnvironmentFilter: () => ({
51 selectedTargets: [
52 {
53 environmentId: "local",
54 serverConfig: {
55 settings: DEFAULT_SERVER_SETTINGS,
56 environment: { capabilities: { projectSettingsOverrides: true } },
57 },
58 },
59 ],
60 projectGroups: [],
61 selectedProjectKey: null,
62 }),
63}));
64
65import { SettingsEnvironmentSourceControlRouteScreen } from "./SettingsServerControlsRouteScreen";
66
67let root: Root;
68
69beforeEach(() => {
70 (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
71 root = createRoot(document.createElement("div"));
72 mocks.updateSettings.mockReset();
73});
74
75afterEach(() => {
76 act(() => root.unmount());
77});
78
79describe("SettingsServerControlsRouteScreen", () => {
80 it("sends a draft saved during a pending write once that write settles", async () => {
81 let settle = () => {};
82 mocks.updateSettings.mockImplementationOnce(
83 () => new Promise<void>((resolve) => (settle = resolve)),
84 );
85 mocks.updateSettings.mockResolvedValue(undefined);
86 act(() => root.render(<SettingsEnvironmentSourceControlRouteScreen />));
87
88 act(() => mocks.branchNaming.onChange({ branchNamingMode: "custom" }));
89 // The branch naming view closes with a draft while the mode write is pending.
90 act(() => mocks.branchNaming.onChange({ branchNameInstructions: "Use issue IDs." }));
91 expect(mocks.updateSettings).toHaveBeenCalledOnce();
92
93 await act(async () => settle());
94
95 expect(mocks.updateSettings).toHaveBeenCalledTimes(2);
96 expect(mocks.updateSettings).toHaveBeenLastCalledWith({
97 environmentId: "local",
98 input: {
99 patch: { branchNamingMode: "custom", branchNameInstructions: "Use issue IDs." },
100 },
101 });
102 });
103});