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

BranchNamingSettings.test.tsx

122 lines3.6 KB

1import type { BranchNamingMode } from "@t3tools/contracts";
2import type { ReactNode } from "react";
3import { act } from "react";
4import { createRoot, type Root } from "react-dom/client";
5import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
6​
7type InputProps = {
8 accessibilityLabel: string;
9 defaultValue: string;
10 onChangeText: (text: string) => void;
11 onEndEditing: (event: { nativeEvent: { text: string } }) => void;
12};
13​
14const inputs = vi.hoisted(() => new Map<string, InputProps>());
15​
16vi.mock("react-native", () => ({
17 View: ({ children }: { children: ReactNode }) => <div>{children}</div>,
18}));
19vi.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}));
26vi.mock("./SettingsChoiceRow", () => ({ SettingsChoiceRow: () => null }));
27vi.mock("./SettingsSection", () => ({
28 SettingsSection: ({ children }: { children: ReactNode }) => <div>{children}</div>,
29}));
30​
31import { BranchNamingSettings } from "./BranchNamingSettings";
32​
33const PREFIX = "Branch prefix";
34const INSTRUCTIONS = "Branch naming instructions";
35​
36let root: Root;
37const onChange = vi.fn();
38​
39function 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={disabled}
51 onChange={onChange}
52 />,
53 ),
54 );
56​
57// Android Back hides the keyboard without ending editing; leaving then only unmounts.
58function leaveScreen() {
59 act(() => root.unmount());
60 root = createRoot(document.createElement("div"));
62​
63function input(label: string) {
64 const props = inputs.get(label);
65 if (!props) throw new Error(`${label} input not rendered`);
66 return props;
68​
69beforeEach(() => {
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​
76afterEach(() => {
77 act(() => root.unmount());
78});
79​
80describe("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});