Open History to see the last commit
1import { act, render, type Screen } from "../../test/render";
2import { EnvironmentId } from "@t3tools/contracts";
3import { AsyncResult } from "effect/reactivity";
4import { StrictMode } from "react";
5import { describe, expect, it, vi } from "vite-plus/test";
6import { page, userEvent } from "vite-plus/test/browser";
7import { isMacPlatform } from "~/lib/utils";
8
9vi.mock(import("~/state/session"), async (original) => ({
10 ...(await original()),
11 readEnvironmentScope: () => true,
12 useEnvironmentScope: () => true,
13}));
14
15import {
16 EMPTY_PROJECT_SCRIPT_INPUT,
17 ProjectScriptEditorDialog,
18 type ProjectScriptActionResult,
19 type ProjectScriptEditorRequest,
20} from "./projectScriptEditor";
21
22const onSubmit = vi.fn<Parameters<typeof ProjectScriptEditorDialog>[0]["onSubmit"]>();
23const onClose = vi.fn();
24const onDelete = vi.fn();
25
26function request(name: string, error?: string): ProjectScriptEditorRequest {
27 return {
28 scriptId: name,
29 initial: { ...EMPTY_PROJECT_SCRIPT_INPUT, name, command: `run-${name}` },
30 ...(error === undefined ? {} : { error }),
31 };
32}
33
34const open = (screen: Screen, nextRequest: ProjectScriptEditorRequest) =>
35 screen.rerender(
36 <StrictMode>
37 <ProjectScriptEditorDialog
38 environmentId={EnvironmentId.make("test")}
39 request={nextRequest}
40 scripts={[]}
41 onSubmit={onSubmit}
42 onClose={onClose}
43 onDelete={onDelete}
44 />
45 </StrictMode>,
46 );
47
48const submit = () => document.querySelector("form")!.requestSubmit();
49const saveButton = () => document.querySelector<HTMLButtonElement>('button[type="submit"]')!;
50
51function deferredSave() {
52 let resolve!: (result: ProjectScriptActionResult) => void;
53 let reject!: (error: Error) => void;
54 const promise = new Promise<ProjectScriptActionResult>((resolveResult, rejectResult) => {
55 resolve = resolveResult;
56 reject = rejectResult;
57 });
58 return { promise, resolve, reject };
59}
60
61function setup() {
62 onSubmit.mockReset();
63 onClose.mockReset();
64 onDelete.mockReset();
65}
66
67describe("project action editor save lifecycle", () => {
68 it("does not close a replacement request or release its in-flight save", async () => {
69 setup();
70 await using screen = await render(null);
71 const first = deferredSave();
72 const second = deferredSave();
73 onSubmit.mockReturnValueOnce(first.promise).mockReturnValueOnce(second.promise);
74 await open(screen, request("build"));
75 await act(async () => submit());
76
77 await open(screen, request("test"));
78 expect(saveButton().disabled).toBe(false);
79 expect(document.querySelector<HTMLInputElement>("#script-name")!.value).toBe("test");
80 await act(async () => submit());
81
82 await act(async () => first.resolve(AsyncResult.success(undefined)));
83 expect(onClose).not.toHaveBeenCalled();
84 expect(saveButton().disabled).toBe(true);
85
86 await act(async () => second.resolve(AsyncResult.success(undefined)));
87 expect(onClose).toHaveBeenCalledTimes(1);
88 expect(onSubmit.mock.calls.map(([scriptId]) => scriptId)).toEqual(["build", "test"]);
89 });
90});
91
92describe("project action editor dismissal", () => {
93 it("stays open when the user clicks outside the editor", async () => {
94 setup();
95 await using screen = await render(null);
96 await open(screen, { scriptId: null, initial: EMPTY_PROJECT_SCRIPT_INPUT });
97 await expect.element(page.getByRole("dialog")).toBeVisible();
98
99 await userEvent.click(document.body, { position: { x: 1, y: 1 } });
100
101 expect(onClose).not.toHaveBeenCalled();
102 });
103});
104
105describe("project action editor keybinding capture", () => {
106 it("captures modified shortcuts, clears on bare Backspace/Delete, ignores IME composition, and lets bare Escape close", async () => {
107 setup();
108 await using screen = await render(null);
109 await open(screen, { scriptId: null, initial: EMPTY_PROJECT_SCRIPT_INPUT });
110 await expect.element(page.getByRole("dialog")).toBeVisible();
111
112 const field = page.getByLabelText("Keybinding");
113 await expect.element(field).toHaveAttribute("data-keybinding-capture", "");
114 await field.click();
115 await expect.element(field).toHaveFocus();
116
117 const mod = isMacPlatform(navigator.platform) ? "Meta" : "Control";
118 await userEvent.keyboard(`{${mod}>}{Backspace}{/${mod}}`);
119 await expect.element(field).toHaveValue("mod+backspace");
120 await userEvent.keyboard("{Backspace}");
121 await expect.element(field).toHaveValue("");
122
123 await userEvent.keyboard("{Alt>}{Delete}{/Alt}");
124 await expect.element(field).toHaveValue("alt+delete");
125 await userEvent.keyboard("{Delete}");
126 await expect.element(field).toHaveValue("");
127
128 await userEvent.keyboard("{Shift>}{Escape}{/Shift}");
129 await expect.element(field).toHaveValue("shift+esc");
130 expect(onClose).not.toHaveBeenCalled();
131
132 for (const composition of [
133 { key: "a", isComposing: true },
134 { key: "a", isComposing: false, keyCode: 229 },
135 ]) {
136 const keydown = new KeyboardEvent("keydown", {
137 ...composition,
138 bubbles: true,
139 cancelable: true,
140 });
141 await act(() => {
142 field.element().dispatchEvent(keydown);
143 });
144 expect(keydown.defaultPrevented).toBe(false);
145 await expect.element(field).toHaveValue("shift+esc");
146 }
147
148 await userEvent.keyboard("{Escape}");
149 await expect.element(field).toHaveValue("shift+esc");
150 expect(onClose).toHaveBeenCalledTimes(1);
151 });
152});