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

projectScriptEditor.browser.test.tsx

140 lines4.7 KB

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