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

SourceControlWritingSettings.browser.test.tsx

117 lines4.2 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}));
47vi.mock(import("./settingsLayout"), async (original) => ({
48 ...(await original()),
49 SettingsRow: ({ title, control, resetAction, children }) => (
50 <div>
51 <h3>{title}</h3>
52 {control}
53 {resetAction}
54 {children}
55 </div>
56 ),
57 SettingsSection: (({ children }: { children: ReactNode }) => children) as never,
58}));
59​
60import { SourceControlWritingSettingsSection } from "./SourceControlWritingSettings";
61​
62const resetButton = (label: string) =>
63 page.getByRole("button", { name: `Reset ${label} to default` });
64​
65const content = () => (
66 <StrictMode>
67 <SourceControlWritingSettingsSection />
68 </StrictMode>
69);
70​
71async function setup() {
72 state.styles = [
73 {
74 ...DEFAULT_UNIFIED_SETTINGS.sourceControlWritingStyle,
75 mode: "custom",
76 customInstructions: "First environment instructions",
77 followChangeRequestTemplates: true,
78 },
79 {
80 ...DEFAULT_UNIFIED_SETTINGS.sourceControlWritingStyle,
81 mode: "custom",
82 customInstructions: "Second environment instructions",
83 followChangeRequestTemplates: false,
84 },
85 ];
86 const screen = await render(content());
87 state.updateSettings.mockReset().mockImplementation((patch) => {
88 state.styles = state.styles.map((style) => ({ ...style, ...patch.sourceControlWritingStyle }));
89 void screen.rerender(content());
90 });
91 return screen;
93​
94describe("mixed source control instructions", () => {
95 it("resets mixed template preferences without replacing each environment's instructions", async () => {
96 await using _screen = await setup();
97 const initialInstructions = state.styles.map(({ mode, customInstructions }) => ({
98 mode,
99 customInstructions,
100 }));
101​
102 await userEvent.click(page.getByRole("button", { name: "Advanced" }));
103 // Mid-open, Playwright scrolls the clipped panel to reach the button, then the growing panel
104 // shifts it between press and release and the click misses. Wait until the panel shows it.
105 await expect.element(resetButton("pull request templates")).toBeInViewport({ ratio: 1 });
106 await userEvent.click(resetButton("pull request templates"));
107​
108 expect(state.updateSettings).toHaveBeenCalledTimes(1);
109 expect(state.styles.map((style) => style.followChangeRequestTemplates)).toEqual([
110 DEFAULT_UNIFIED_SETTINGS.sourceControlWritingStyle.followChangeRequestTemplates,
111 DEFAULT_UNIFIED_SETTINGS.sourceControlWritingStyle.followChangeRequestTemplates,
112 ]);
113 expect(
114 state.styles.map(({ mode, customInstructions }) => ({ mode, customInstructions })),
115 ).toEqual(initialInstructions);
116 });
117});