Skip to content

Commit 4bcf98b

Browse files
test(web): consolidate action shortcut regressions
1 parent 7f8ab64 commit 4bcf98b

2 files changed

Lines changed: 92 additions & 131 deletions

File tree

‎apps/web/src/components/settings/useProjectScriptSettings.browser.test.tsx‎

Lines changed: 92 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -66,52 +66,65 @@ const lintBinding: ResolvedKeybindingsConfig = [
6666
},
6767
},
6868
];
69-
const input = (keybinding: string | null): NewProjectScriptInput => ({
69+
const input: NewProjectScriptInput = {
7070
name: lint.name,
7171
command: lint.command,
7272
icon: lint.icon,
7373
runOnWorktreeCreate: false,
7474
waitForSetup: false,
7575
runOnSettle: false,
76-
keybinding,
7776
previewUrl: null,
7877
autoOpenPreview: false,
79-
});
78+
};
8079

8180
type ProjectScriptSettings = ReturnType<typeof useProjectScriptSettings>;
8281

8382
function Probe(props: {
84-
keybindings: ResolvedKeybindingsConfig;
83+
targets: Parameters<typeof useProjectScriptSettings>[0];
8584
onResult: (settings: ProjectScriptSettings) => void;
8685
}) {
87-
props.onResult(
88-
useProjectScriptSettings([
89-
{
90-
environmentId,
91-
settings: DEFAULT_SERVER_SETTINGS,
92-
keybindings: props.keybindings,
93-
project: { id: ProjectId.make("project"), scripts: [lint] },
94-
},
95-
]),
96-
);
86+
props.onResult(useProjectScriptSettings(props.targets));
9787
return null;
9888
}
9989

10090
let root: Root;
10191
let container: HTMLDivElement;
10292

103-
async function renderSettings(keybindings: ResolvedKeybindingsConfig) {
93+
async function renderSettings(
94+
keybindings: ResolvedKeybindingsConfig,
95+
scope: "project" | "environment" = "project",
96+
) {
10497
let settings: ProjectScriptSettings | undefined;
10598
await act(async () =>
106-
root.render(<Probe keybindings={keybindings} onResult={(next) => (settings = next)} />),
99+
root.render(
100+
<Probe
101+
targets={[
102+
{
103+
environmentId,
104+
settings: {
105+
...DEFAULT_SERVER_SETTINGS,
106+
defaultProjectScripts: scope === "environment" ? [lint] : [],
107+
},
108+
keybindings,
109+
...(scope === "project"
110+
? { project: { id: ProjectId.make("project"), scripts: [lint] } }
111+
: {}),
112+
},
113+
]}
114+
onResult={(next) => (settings = next)}
115+
/>,
116+
),
107117
);
108118
return settings!;
109119
}
110120

111-
async function submitInAct(submit: ProjectScriptSettings["submit"], keybinding: string | null) {
121+
async function submitInAct(
122+
submit: ProjectScriptSettings["submit"],
123+
changes: Partial<NewProjectScriptInput>,
124+
) {
112125
let result: Awaited<ReturnType<typeof submit>> | undefined;
113126
await act(async () => {
114-
result = await submit(lint.id, input(keybinding));
127+
result = await submit(lint.id, { ...input, ...changes });
115128
});
116129
return result!;
117130
}
@@ -134,10 +147,34 @@ afterEach(async () => {
134147
});
135148

136149
describe("useProjectScriptSettings", () => {
150+
it.each(["project", "environment"] as const)(
151+
"preserves the shortcut for %s action name and command edits",
152+
async (scope) => {
153+
const { submit } = await renderSettings(lintBinding, scope);
154+
const changes = { name: "Lint and fix", command: "vp lint --fix" };
155+
156+
const result = await submitInAct(submit, changes);
157+
158+
expect(result._tag).toBe("Success");
159+
const scripts = [expect.objectContaining({ id: lint.id, ...changes })];
160+
expect(commands.updateSettings).toHaveBeenCalledExactlyOnceWith({
161+
environmentId,
162+
input: {
163+
patch:
164+
scope === "project"
165+
? { projectSettingsOverrides: { project: { defaultProjectScripts: scripts } } }
166+
: { defaultProjectScripts: scripts },
167+
},
168+
});
169+
expect(commands.removeKeybinding).not.toHaveBeenCalled();
170+
expect(commands.upsertKeybinding).not.toHaveBeenCalled();
171+
},
172+
);
173+
137174
it("saves a recorded action shortcut", async () => {
138175
const { submit } = await renderSettings([]);
139176

140-
const result = await submitInAct(submit, "mod+shift+k");
177+
const result = await submitInAct(submit, { keybinding: "mod+shift+k" });
141178

142179
expect(result._tag).toBe("Success");
143180
expect(commands.upsertKeybinding).toHaveBeenCalledWith({
@@ -149,7 +186,7 @@ describe("useProjectScriptSettings", () => {
149186
it("removes a cleared action shortcut", async () => {
150187
const { submit } = await renderSettings(lintBinding);
151188

152-
const result = await submitInAct(submit, null);
189+
const result = await submitInAct(submit, { keybinding: null });
153190

154191
expect(result._tag).toBe("Success");
155192
expect(commands.removeKeybinding).toHaveBeenCalledWith({
@@ -158,4 +195,39 @@ describe("useProjectScriptSettings", () => {
158195
});
159196
expect(commands.upsertKeybinding).not.toHaveBeenCalled();
160197
});
198+
199+
it("replaces an explicitly changed action shortcut", async () => {
200+
const { submit } = await renderSettings(lintBinding);
201+
202+
const result = await submitInAct(submit, { keybinding: "mod+shift+l" });
203+
204+
expect(result._tag).toBe("Success");
205+
expect(commands.upsertKeybinding).toHaveBeenCalledExactlyOnceWith({
206+
environmentId,
207+
input: {
208+
command: "script.lint.run",
209+
key: "mod+shift+l",
210+
replace: { command: "script.lint.run", key: "mod+shift+k" },
211+
},
212+
});
213+
expect(commands.removeKeybinding).not.toHaveBeenCalled();
214+
});
215+
216+
it("removes the shortcut when its action is deleted", async () => {
217+
const { persist } = await renderSettings(lintBinding);
218+
219+
await act(async () => {
220+
expect((await persist(() => []))._tag).toBe("Success");
221+
});
222+
223+
expect(commands.updateSettings).toHaveBeenCalledExactlyOnceWith({
224+
environmentId,
225+
input: { patch: { projectSettingsOverrides: { project: { defaultProjectScripts: [] } } } },
226+
});
227+
expect(commands.removeKeybinding).toHaveBeenCalledExactlyOnceWith({
228+
environmentId,
229+
input: { key: "mod+shift+k", command: "script.lint.run" },
230+
});
231+
expect(commands.upsertKeybinding).not.toHaveBeenCalled();
232+
});
161233
});

‎apps/web/src/components/settings/useProjectScriptSettings.test.ts‎

Lines changed: 0 additions & 111 deletions
This file was deleted.

0 commit comments

Comments
 (0)