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, create, type ReactTestRenderer } from "react-test-renderer";
10import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
11
12import type { NewProjectScriptInput } from "../projectScriptEditor";
13
14const commands = vi.hoisted(() => ({
15 updateSettings: vi.fn(),
16 upsertKeybinding: vi.fn(),
17 removeKeybinding: vi.fn(),
18}));
19
20vi.mock("../../env", () => ({ isElectron: false }));
21vi.mock("../../state/session", () => ({ readEnvironmentScope: () => true }));
22vi.mock("../../state/entities", () => ({ useProjects: () => [] }));
23vi.mock("../../state/server", () => ({
24 serverEnvironment: {
25 updateSettings: "updateSettings",
26 upsertKeybinding: "upsertKeybinding",
27 removeKeybinding: "removeKeybinding",
28 },
29}));
30vi.mock("../../state/use-atom-command", () => ({
31 useAtomCommand: (command: keyof typeof commands) => commands[command],
32}));
33vi.mock("../ui/toast", () => ({ toastManager: { add: vi.fn() } }));
34
35import { useProjectScriptSettings } from "./useProjectScriptSettings";
36
37const environmentId = EnvironmentId.make("remote");
38const lint: ProjectScript = {
39 id: "lint",
40 name: "Lint",
41 command: "vp lint",
42 icon: "lint",
43 runOnWorktreeCreate: false,
44};
45const lintBinding: ResolvedKeybindingsConfig = [
46 {
47 command: "script.lint.run",
48 shortcut: {
49 key: "k",
50 metaKey: false,
51 ctrlKey: false,
52 shiftKey: true,
53 altKey: false,
54 modKey: true,
55 },
56 },
57];
58const input = (keybinding: string | null): NewProjectScriptInput => ({
59 name: lint.name,
60 command: lint.command,
61 icon: lint.icon,
62 runOnWorktreeCreate: false,
63 waitForSetup: false,
64 runOnSettle: false,
65 keybinding,
66 previewUrl: null,
67 autoOpenPreview: false,
68});
69
70type ProjectScriptSettings = ReturnType<typeof useProjectScriptSettings>;
71
72function Probe(props: {
73 keybindings: ResolvedKeybindingsConfig;
74 onResult: (settings: ProjectScriptSettings) => void;
75}) {
76 props.onResult(
77 useProjectScriptSettings([
78 {
79 environmentId,
80 settings: DEFAULT_SERVER_SETTINGS,
81 keybindings: props.keybindings,
82 project: { id: ProjectId.make("project"), scripts: [lint] },
83 },
84 ]),
85 );
86 return null;
87}
88
89let renderer: ReactTestRenderer | undefined;
90
91function renderSettings(keybindings: ResolvedKeybindingsConfig) {
92 let settings: ProjectScriptSettings | undefined;
93 act(() => {
94 renderer = create(<Probe keybindings={keybindings} onResult={(next) => (settings = next)} />);
95 });
96 return settings!;
97}
98
99async function submitInAct(submit: ProjectScriptSettings["submit"], keybinding: string | null) {
100 let result: Awaited<ReturnType<typeof submit>> | undefined;
101 await act(async () => {
102 result = await submit(lint.id, input(keybinding));
103 });
104 return result!;
105}
106
107beforeEach(() => {
108 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
109 vi.clearAllMocks();
110 for (const command of Object.values(commands)) {
111 command.mockResolvedValue(AsyncResult.success(undefined));
112 }
113});
114
115afterEach(() => {
116 act(() => renderer?.unmount());
117 renderer = undefined;
118 vi.unstubAllGlobals();
119});
120
121describe("useProjectScriptSettings in a browser client", () => {
122 it("saves a recorded action shortcut", async () => {
123 const { submit } = renderSettings([]);
124
125 const result = await submitInAct(submit, "mod+shift+k");
126
127 expect(result._tag).toBe("Success");
128 expect(commands.upsertKeybinding).toHaveBeenCalledWith({
129 environmentId,
130 input: expect.objectContaining({ key: "mod+shift+k", command: "script.lint.run" }),
131 });
132 });
133
134 it("removes a cleared action shortcut", async () => {
135 const { submit } = renderSettings(lintBinding);
136
137 const result = await submitInAct(submit, null);
138
139 expect(result._tag).toBe("Success");
140 expect(commands.removeKeybinding).toHaveBeenCalledWith({
141 environmentId,
142 input: expect.objectContaining({ key: "mod+shift+k", command: "script.lint.run" }),
143 });
144 expect(commands.upsertKeybinding).not.toHaveBeenCalled();
145 });
146});