The best way to control Claude Code, Codex and more from any computer or phone

main
tsx 122 lines 3.69 KB
Raw
1 import type { BranchNamingMode } from "@t3tools/contracts";
2 import type { ReactNode } from "react";
3 import { act } from "react";
4 import { createRoot, type Root } from "react-dom/client";
5 import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
6
7 type InputProps = {
8 accessibilityLabel: string;
9 defaultValue: string;
10 onChangeText: (text: string) => void;
11 onEndEditing: (event: { nativeEvent: { text: string } }) => void;
12 };
13
14 const inputs = vi.hoisted(() => new Map<string, InputProps>());
15
16 vi.mock("react-native", () => ({
17 View: ({ children }: { children: ReactNode }) => <div>{children}</div>,
18 }));
19 vi.mock("../../../components/ui/AppText", () => ({
20 AppText: ({ children }: { children: ReactNode }) => <span>{children}</span>,
21 AppTextInput: (props: InputProps) => {
22 inputs.set(props.accessibilityLabel, props);
23 return null;
24 },
25 }));
26 vi.mock("../../../components/ui/ListChoiceRow", () => ({ ListChoiceRow: () => null }));
27 vi.mock("./SettingsSection", () => ({
28 SettingsSection: ({ children }: { children: ReactNode }) => <div>{children}</div>,
29 }));
30
31 import { BranchNamingSettings } from "./BranchNamingSettings";
32
33 const PREFIX = "Branch prefix";
34 const INSTRUCTIONS = "Branch naming instructions";
35
36 let root: Root;
37 const onChange = vi.fn();
38
39 function render(
40 mode: BranchNamingMode,
41 saved: { prefix?: string; instructions?: string } = {},
42 disabled = false,
43 ) {
44 act(() =>
45 root.render(
46 <BranchNamingSettings
47 mode={mode}
48 prefix={saved.prefix ?? "t3"}
49 instructions={saved.instructions ?? "Use issue IDs."}
50 disabled={{ mode: disabled, prefix: disabled, instructions: disabled }}
51 onChange={onChange}
52 />,
53 ),
54 );
55 }
56
57 // Android Back hides the keyboard without ending editing; leaving then only unmounts.
58 function leaveScreen() {
59 act(() => root.unmount());
60 root = createRoot(document.createElement("div"));
61 }
62
63 function input(label: string) {
64 const props = inputs.get(label);
65 if (!props) throw new Error(`${label} input not rendered`);
66 return props;
67 }
68
69 beforeEach(() => {
70 (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
71 root = createRoot(document.createElement("div"));
72 inputs.clear();
73 onChange.mockClear();
74 });
75
76 afterEach(() => {
77 act(() => root.unmount());
78 });
79
80 describe("BranchNamingSettings", () => {
81 it("saves typed instructions when the screen closes before editing ends", () => {
82 render("custom");
83 act(() => input(INSTRUCTIONS).onChangeText(" Use julius/ prefixes. "));
84 leaveScreen();
85
86 expect(onChange).toHaveBeenCalledExactlyOnceWith({
87 branchNameInstructions: "Use julius/ prefixes.",
88 });
89 });
90
91 // regression: #18381 branch naming text saves when you leave the screen
92 it("saves both drafts in one patch when the screen closes", () => {
93 render("static");
94 act(() => input(PREFIX).onChangeText("feat/"));
95 render("custom");
96 act(() => input(INSTRUCTIONS).onChangeText("New rules."));
97 leaveScreen();
98
99 expect(onChange).toHaveBeenCalledExactlyOnceWith({
100 branchNamePrefix: "feat/",
101 branchNameInstructions: "New rules.",
102 });
103 });
104
105 it("still saves a draft when a pending write disables the field", () => {
106 render("custom");
107 act(() => input(INSTRUCTIONS).onChangeText("New rules."));
108 render("custom", {}, true);
109 leaveScreen();
110
111 expect(onChange).toHaveBeenCalledExactlyOnceWith({ branchNameInstructions: "New rules." });
112 });
113
114 it("drops a draft once a newer saved value replaces the field", () => {
115 render("custom");
116 act(() => input(INSTRUCTIONS).onChangeText("Old draft."));
117 render("custom", { instructions: "Saved elsewhere." });
118 leaveScreen();
119
120 expect(onChange).not.toHaveBeenCalled();
121 });
122 });