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

useProjectScriptSettings.browser.test.tsx

162 lines4.4 KB

1import { act, render } from "../../../test/render";
2import {
3 DEFAULT_SERVER_SETTINGS,
4 EnvironmentId,
5 ProjectId,
6 type ProjectScript,
7 type ResolvedKeybindingsConfig,
8} from "@t3tools/contracts";
9import { AsyncResult } from "effect/reactivity";
10import { 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(import("../../state/session"), async (original) => ({
21 ...(await original()),
22 readEnvironmentScope: () => true,
23}));
24vi.mock(import("../../state/entities"), async (original) => ({
25 ...(await original()),
26 useProjects: () => [],
27}));
28vi.mock(import("../../state/server"), async (original) => {
29 const actual = await original();
30 return {
31 ...actual,
32 serverEnvironment: {
33 ...actual.serverEnvironment,
34 updateSettings: "updateSettings",
35 upsertKeybinding: "upsertKeybinding",
36 removeKeybinding: "removeKeybinding",
37 } as unknown as typeof actual.serverEnvironment,
38 };
39});
40vi.mock(import("../../state/use-atom-command"), async (original) => ({
41 ...(await original()),
42 useAtomCommand: ((command: keyof typeof commands) => commands[command]) as never,
43}));
44​
45import { useProjectScriptSettings } from "./useProjectScriptSettings";
46​
47const environmentId = EnvironmentId.make("remote");
48const lint: ProjectScript = {
49 id: "lint",
50 name: "Lint",
51 command: "vp lint",
52 icon: "lint",
53 runOnWorktreeCreate: false,
54};
55const lintBinding: ResolvedKeybindingsConfig = [
56 {
57 command: "script.lint.run",
58 shortcut: {
59 key: "k",
60 metaKey: false,
61 ctrlKey: false,
62 shiftKey: true,
63 altKey: false,
64 modKey: true,
65 },
66 },
67];
68const input: NewProjectScriptInput = {
69 name: lint.name,
70 command: lint.command,
71 icon: lint.icon,
72 runOnWorktreeCreate: false,
73 waitForSetup: false,
74 runOnSettle: false,
75 previewUrl: null,
76 autoOpenPreview: false,
77};
78​
79type ProjectScriptSettings = ReturnType<typeof useProjectScriptSettings>;
80​
81function Probe(props: {
82 targets: Parameters<typeof useProjectScriptSettings>[0];
83 onResult: (settings: ProjectScriptSettings) => void;
84}) {
85 props.onResult(useProjectScriptSettings(props.targets));
86 return null;
88​
89async function renderSettings(
90 keybindings: ResolvedKeybindingsConfig,
91 scope: "project" | "environment" = "project",
92) {
93 let settings: ProjectScriptSettings | undefined;
94 const screen = await render(
95 <Probe
96 targets={[
97 {
98 environmentId,
99 settings: {
100 ...DEFAULT_SERVER_SETTINGS,
101 defaultProjectScripts: scope === "environment" ? [lint] : [],
102 },
103 keybindings,
104 ...(scope === "project"
105 ? { project: { id: ProjectId.make("project"), scripts: [lint] } }
106 : {}),
107 },
108 ]}
109 onResult={(next) => (settings = next)}
110 />,
111 );
112 return {
113 ...screen,
114 get settings() {
115 return settings!;
116 },
117 };
119​
120async function submitInAct(
121 submit: ProjectScriptSettings["submit"],
122 changes: Partial<NewProjectScriptInput>,
123) {
124 let result: Awaited<ReturnType<typeof submit>> | undefined;
125 await act(async () => {
126 result = await submit(lint.id, { ...input, ...changes });
127 });
128 return result!;
130function setup() {
131 vi.clearAllMocks();
132 for (const command of Object.values(commands))
133 command.mockResolvedValue(AsyncResult.success(undefined));
135​
136describe("useProjectScriptSettings", () => {
137 it.each(["project", "environment"] as const)(
138 "preserves the shortcut for %s action name and command edits",
139 async (scope) => {
140 setup();
141 await using screen = await renderSettings(lintBinding, scope);
142 const { submit } = screen.settings;
143 const changes = { name: "Lint and fix", command: "vp lint --fix" };
144​
145 const result = await submitInAct(submit, changes);
146​
147 expect(result._tag).toBe("Success");
148 const scripts = [expect.objectContaining({ id: lint.id, ...changes })];
149 expect(commands.updateSettings).toHaveBeenCalledExactlyOnceWith({
150 environmentId,
151 input: {
152 patch:
153 scope === "project"
154 ? { projectSettingsOverrides: { project: { defaultProjectScripts: scripts } } }
155 : { defaultProjectScripts: scripts },
156 },
157 });
158 expect(commands.removeKeybinding).not.toHaveBeenCalled();
159 expect(commands.upsertKeybinding).not.toHaveBeenCalled();
160 },
161 );
162});