Open History to see the last commit
1import {
2 DEFAULT_SERVER_SETTINGS,
3 EnvironmentId,
4 ProjectId,
5 type ProjectScript,
6 type ResolvedKeybindingsConfig,
7} from "@t3tools/contracts";
8import { AsyncResult } from "effect/reactivity";
9import { act } from "react";
10import { createRoot, type Root } from "react-dom/client";
11import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
12
13import type { NewProjectScriptInput } from "../projectScriptEditor";
14
15const commands = vi.hoisted(() => ({
16 updateSettings: vi.fn(),
17 upsertKeybinding: vi.fn(),
18 removeKeybinding: vi.fn(),
19}));
20
21vi.mock(import("../../state/session"), async (original) => ({
22 ...(await original()),
23 readEnvironmentScope: () => true,
24}));
25vi.mock(import("../../state/entities"), async (original) => ({
26 ...(await original()),
27 useProjects: () => [],
28}));
29vi.mock(import("../../state/server"), async (original) => {
30 const actual = await original();
31 return {
32 ...actual,
33 serverEnvironment: {
34 ...actual.serverEnvironment,
35 updateSettings: "updateSettings",
36 upsertKeybinding: "upsertKeybinding",
37 removeKeybinding: "removeKeybinding",
38 } as unknown as typeof actual.serverEnvironment,
39 };
40});
41vi.mock(import("../../state/use-atom-command"), async (original) => ({
42 ...(await original()),
43 useAtomCommand: ((command: keyof typeof commands) => commands[command]) as never,
44}));
45
46import { useProjectScriptSettings } from "./useProjectScriptSettings";
47
48const environmentId = EnvironmentId.make("remote");
49const lint: ProjectScript = {
50 id: "lint",
51 name: "Lint",
52 command: "vp lint",
53 icon: "lint",
54 runOnWorktreeCreate: false,
55};
56const lintBinding: ResolvedKeybindingsConfig = [
57 {
58 command: "script.lint.run",
59 shortcut: {
60 key: "k",
61 metaKey: false,
62 ctrlKey: false,
63 shiftKey: true,
64 altKey: false,
65 modKey: true,
66 },
67 },
68];
69const input = (keybinding: string | null): NewProjectScriptInput => ({
70 name: lint.name,
71 command: lint.command,
72 icon: lint.icon,
73 runOnWorktreeCreate: false,
74 waitForSetup: false,
75 runOnSettle: false,
76 keybinding,
77 previewUrl: null,
78 autoOpenPreview: false,
79});
80
81type ProjectScriptSettings = ReturnType<typeof useProjectScriptSettings>;
82
83function Probe(props: {
84 keybindings: ResolvedKeybindingsConfig;
85 onResult: (settings: ProjectScriptSettings) => void;
86}) {
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 );
97 return null;
98}
99
100let root: Root;
101let container: HTMLDivElement;
102
103async function renderSettings(keybindings: ResolvedKeybindingsConfig) {
104 let settings: ProjectScriptSettings | undefined;
105 await act(async () =>
106 root.render(<Probe keybindings={keybindings} onResult={(next) => (settings = next)} />),
107 );
108 return settings!;
109}
110
111async function submitInAct(submit: ProjectScriptSettings["submit"], keybinding: string | null) {
112 let result: Awaited<ReturnType<typeof submit>> | undefined;
113 await act(async () => {
114 result = await submit(lint.id, input(keybinding));
115 });
116 return result!;
117}
118
119beforeEach(() => {
120 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
121 vi.clearAllMocks();
122 for (const command of Object.values(commands)) {
123 command.mockResolvedValue(AsyncResult.success(undefined));
124 }
125 container = document.createElement("div");
126 document.body.append(container);
127 root = createRoot(container);
128});
129
130afterEach(async () => {
131 await act(async () => root.unmount());
132 container.remove();
133 vi.unstubAllGlobals();
134});
135
136describe("useProjectScriptSettings", () => {
137 it("saves a recorded action shortcut", async () => {
138 const { submit } = await renderSettings([]);
139
140 const result = await submitInAct(submit, "mod+shift+k");
141
142 expect(result._tag).toBe("Success");
143 expect(commands.upsertKeybinding).toHaveBeenCalledWith({
144 environmentId,
145 input: expect.objectContaining({ key: "mod+shift+k", command: "script.lint.run" }),
146 });
147 });
148
149 it("removes a cleared action shortcut", async () => {
150 const { submit } = await renderSettings(lintBinding);
151
152 const result = await submitInAct(submit, null);
153
154 expect(result._tag).toBe("Success");
155 expect(commands.removeKeybinding).toHaveBeenCalledWith({
156 environmentId,
157 input: expect.objectContaining({ key: "mod+shift+k", command: "script.lint.run" }),
158 });
159 expect(commands.upsertKeybinding).not.toHaveBeenCalled();
160 });
161});