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

SourceControlWritingSettings.browser.test.tsx

115 lines4.0 KB

1import { render } from "../../../test/render";
2import { DEFAULT_UNIFIED_SETTINGS } from "@t3tools/contracts/settings";
3import { StrictMode, type ReactNode } from "react";
4import { 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​
64const resetButton = (label: string) =>
65 page.getByRole("button", { name: `Reset ${label} to default` });
66​
67const content = () => (
68 <StrictMode>
69 <SourceControlWritingSettingsSection />
70 </StrictMode>
71);
72​
73async function setup() {
74 state.styles = [
75 {
76 ...DEFAULT_UNIFIED_SETTINGS.sourceControlWritingStyle,
77 mode: "custom",
78 customInstructions: "First environment instructions",
79 followChangeRequestTemplates: true,
80 },
81 {
82 ...DEFAULT_UNIFIED_SETTINGS.sourceControlWritingStyle,
83 mode: "custom",
84 customInstructions: "Second environment instructions",
85 followChangeRequestTemplates: false,
86 },
87 ];
88 const screen = await render(content());
89 state.updateSettings.mockReset().mockImplementation((patch) => {
90 state.styles = state.styles.map((style) => ({ ...style, ...patch.sourceControlWritingStyle }));
91 void screen.rerender(content());
92 });
93 return screen;
95​
96describe("mixed source control instructions", () => {
97 it("resets mixed template preferences without replacing each environment's instructions", async () => {
98 await using _screen = await setup();
99 const initialInstructions = state.styles.map(({ mode, customInstructions }) => ({
100 mode,
101 customInstructions,
102 }));
103​
104 await userEvent.click(resetButton("change request templates"));
105​
106 expect(state.updateSettings).toHaveBeenCalledTimes(1);
107 expect(state.styles.map((style) => style.followChangeRequestTemplates)).toEqual([
108 DEFAULT_UNIFIED_SETTINGS.sourceControlWritingStyle.followChangeRequestTemplates,
109 DEFAULT_UNIFIED_SETTINGS.sourceControlWritingStyle.followChangeRequestTemplates,
110 ]);
111 expect(
112 state.styles.map(({ mode, customInstructions }) => ({ mode, customInstructions })),
113 ).toEqual(initialInstructions);
114 });
115});