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

SourceControlWritingSettings.browser.test.tsx

203 lines7.3 KB

1import { DEFAULT_UNIFIED_SETTINGS } from "@t3tools/contracts/settings";
2import { act, StrictMode, type ReactNode } from "react";
3import { createRoot, type Root } from "react-dom/client";
4import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
5import { page, userEvent } from "vite-plus/test/browser";
6​
7import "../../index.css";
8import type { ScopedSettingsPatch } from "./scopedSettings";
9​
10type WritingStyle = typeof DEFAULT_UNIFIED_SETTINGS.sourceControlWritingStyle;
11const state = vi.hoisted(() => ({
12 styles: [] as WritingStyle[],
13 updateSettings: vi.fn<(patch: ScopedSettingsPatch) => void>(),
14}));
15​
16vi.mock(import("@tanstack/react-router"), async (original) => ({
17 ...(await original()),
18 useNavigate: (() => vi.fn()) as never,
19}));
20vi.mock(import("./useScopedSettings"), async (original) => ({
21 ...(await original()),
22 useScopedSettings: (() => ({
23 ...DEFAULT_UNIFIED_SETTINGS,
24 sourceControlWritingStyle: state.styles[0],
25 })) as never,
26 useScopedSettingsMixed: (keys) =>
27 !keys.includes("reviewGuidesEnabled") &&
28 JSON.stringify(state.styles[0]) !== JSON.stringify(state.styles[1]),
29 useUpdateScopedSettings: () => state.updateSettings,
30}));
31vi.mock(import("./SettingsScopeContext"), async (original) => ({
32 ...(await original()),
33 useSettingsScope: (() => ({
34 scope: { kind: "all", environmentIds: [] },
35 environment: null,
36 connectedEnvironments: [],
37 targets: state.styles.map((style) => ({
38 settings: { ...DEFAULT_UNIFIED_SETTINGS, sourceControlWritingStyle: style },
39 })),
40 target: null,
41 })) as never,
42}));
43vi.mock(import("../../state/environments"), async (original) => ({
44 ...(await original()),
45 useEnvironmentProviders: () => new Map(),
46}));
47// The real row reads write permission and overrides from live environment state; this
48// suite is about how the section writes mixed writing styles.
49vi.mock(import("./settingsLayout"), async (original) => ({
50 ...(await original()),
51 SettingsRow: ({ title, control, resetAction, children }) => (
52 <div>
53 <h3>{title}</h3>
54 {control}
55 {resetAction}
56 {children}
57 </div>
58 ),
59 SettingsSection: (({ children }: { children: ReactNode }) => children) as never,
60}));
61​
62import { SourceControlWritingSettingsSection } from "./SourceControlWritingSettings";
63​
64let root: Root;
65let container: HTMLDivElement;
66​
67const resetButton = (label: string) =>
68 page.getByRole("button", { name: `Reset ${label} to default` });
69const writeForAll = () => page.getByRole("button", { name: "Write custom instructions for all" });
70const applyToAll = () => page.getByRole("button", { name: "Apply instructions to all" });
71const bulkEditor = () =>
72 page.getByRole("textbox", {
73 name: "Custom source control instructions for all selected environments",
74 });
75​
76async function render() {
77 await act(async () =>
78 root.render(
79 <StrictMode>
80 <SourceControlWritingSettingsSection />
81 </StrictMode>,
82 ),
83 );
85​
86beforeEach(async () => {
87 state.styles = [
88 {
89 ...DEFAULT_UNIFIED_SETTINGS.sourceControlWritingStyle,
90 mode: "custom",
91 customInstructions: "First environment instructions",
92 followChangeRequestTemplates: true,
93 },
94 {
95 ...DEFAULT_UNIFIED_SETTINGS.sourceControlWritingStyle,
96 mode: "custom",
97 customInstructions: "Second environment instructions",
98 followChangeRequestTemplates: false,
99 },
100 ];
101 state.updateSettings.mockReset().mockImplementation((patch) => {
102 state.styles = state.styles.map((style) => ({ ...style, ...patch.sourceControlWritingStyle }));
103 void render();
104 });
105 container = document.createElement("div");
106 document.body.append(container);
107 root = createRoot(container);
108 await render();
109});
110​
111afterEach(async () => {
112 await act(async () => root.unmount());
113 container.remove();
114});
115​
116describe("mixed source control instructions", () => {
117 it("resets mixed template preferences without replacing each environment's instructions", async () => {
118 const initialInstructions = state.styles.map(({ mode, customInstructions }) => ({
119 mode,
120 customInstructions,
121 }));
122​
123 await userEvent.click(resetButton("change request templates"));
124​
125 expect(state.updateSettings).toHaveBeenCalledTimes(1);
126 expect(state.styles.map((style) => style.followChangeRequestTemplates)).toEqual([
127 DEFAULT_UNIFIED_SETTINGS.sourceControlWritingStyle.followChangeRequestTemplates,
128 DEFAULT_UNIFIED_SETTINGS.sourceControlWritingStyle.followChangeRequestTemplates,
129 ]);
130 expect(
131 state.styles.map(({ mode, customInstructions }) => ({ mode, customInstructions })),
132 ).toEqual(initialInstructions);
133 });
134​
135 it("resets every environment even when the representative already has default instructions", async () => {
136 state.styles[0] = { ...DEFAULT_UNIFIED_SETTINGS.sourceControlWritingStyle };
137 await render();
138​
139 await userEvent.click(resetButton("source control writing style"));
140​
141 expect(state.updateSettings).toHaveBeenCalledTimes(1);
142 expect(
143 state.styles.map(({ mode, customInstructions }) => ({ mode, customInstructions })),
144 ).toEqual(
145 [0, 1].map(() => ({
146 mode: DEFAULT_UNIFIED_SETTINGS.sourceControlWritingStyle.mode,
147 customInstructions: DEFAULT_UNIFIED_SETTINGS.sourceControlWritingStyle.customInstructions,
148 })),
149 );
150 expect(state.styles[1]!.followChangeRequestTemplates).toBe(false);
151 });
152​
153 it("does not write an untouched bulk draft", async () => {
154 const initialStyles = state.styles;
155 await userEvent.click(writeForAll());
156 await expect.element(bulkEditor()).toHaveValue("");
157 await expect.element(applyToAll()).toBeDisabled();
158​
159 expect(state.updateSettings).not.toHaveBeenCalled();
160 expect(state.styles).toEqual(initialStyles);
161 });
162​
163 it("applies edited instructions to every selected environment", async () => {
164 await userEvent.click(writeForAll());
165 await userEvent.fill(bulkEditor(), " Keep titles concise. ");
166 await expect.element(applyToAll()).toBeEnabled();
167 await userEvent.click(applyToAll());
168​
169 expect(state.updateSettings).toHaveBeenCalledTimes(1);
170 expect(state.styles.map((style) => style.customInstructions)).toEqual([
171 "Keep titles concise.",
172 "Keep titles concise.",
173 ]);
174 expect(state.styles.map((style) => style.followChangeRequestTemplates)).toEqual([true, false]);
175 });
176​
177 it("allows an intentional clear after editing", async () => {
178 await userEvent.click(writeForAll());
179 await userEvent.fill(bulkEditor(), "Temporary instructions");
180 await userEvent.clear(bulkEditor());
181 await expect.element(applyToAll()).toBeEnabled();
182 await userEvent.click(applyToAll());
183​
184 expect(state.updateSettings).toHaveBeenCalledTimes(1);
185 expect(state.styles.map((style) => style.customInstructions)).toEqual(["", ""]);
186 });
187​
188 it("shows the plain editor once instructions agree, even while templates differ", async () => {
189 await userEvent.click(writeForAll());
190 await userEvent.fill(bulkEditor(), "Shared instructions");
191 await userEvent.click(applyToAll());
192​
193 expect(state.updateSettings).toHaveBeenCalledTimes(1);
194 expect(state.styles.map((style) => style.customInstructions)).toEqual([
195 "Shared instructions",
196 "Shared instructions",
197 ]);
198 await expect.element(writeForAll()).not.toBeInTheDocument();
199 await expect
200 .element(page.getByRole("textbox", { name: "Custom source control writing instructions" }))
201 .toHaveValue("Shared instructions");
202 });
203});