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

ThemeEditorPanel.browser.test.tsx

207 lines7.9 KB

1import { describe, expect, it, vi } from "vite-plus/test";
2import { page, userEvent } from "vite-plus/test/browser";
3​
4import { act, render } from "../../../test/render";
5import { Dialog, DialogPopup, DialogTitle } from "../ui/dialog";
6import { ThemeEditorPanel } from "./ThemeEditorPanel";
7​
8function dragHeader(panel: HTMLElement, toX: number, toY: number) {
9 const header = panel.querySelector<HTMLElement>("h2")!.closest<HTMLElement>(".cursor-grab")!;
10 const rect = panel.getBoundingClientRect();
11 const start = { clientX: rect.left + 40, clientY: rect.top + 10 };
12 const init = { bubbles: true, button: 0, pointerId: 1 };
13 header.dispatchEvent(new PointerEvent("pointerdown", { ...init, ...start }));
14 header.dispatchEvent(
15 new PointerEvent("pointermove", { ...init, clientX: toX + 40, clientY: toY + 10 }),
16 );
17 header.dispatchEvent(new PointerEvent("pointerup", init));
19​
20function dragResizeHandle(panel: HTMLElement, byX: number) {
21 const handle = panel.querySelector<HTMLElement>(".cursor-se-resize")!;
22 const rect = handle.getBoundingClientRect();
23 const init = { bubbles: true, button: 0, pointerId: 2 };
24 const start = { clientX: rect.left + 4, clientY: rect.top + 4 };
25 handle.dispatchEvent(new PointerEvent("pointerdown", { ...init, ...start }));
26 handle.dispatchEvent(
27 new PointerEvent("pointermove", {
28 ...init,
29 clientX: start.clientX + byX,
30 clientY: start.clientY,
31 }),
32 );
33 handle.dispatchEvent(new PointerEvent("pointerup", init));
35​
36function pointerAt(target: HTMLElement, type: string, xFraction: number) {
37 const rect = target.getBoundingClientRect();
38 target.dispatchEvent(
39 new PointerEvent(type, {
40 bubbles: true,
41 button: 0,
42 pointerId: 3,
43 clientX: rect.left + rect.width * xFraction,
44 clientY: rect.top + rect.height / 2,
45 }),
46 );
48​
49async function renderPanel() {
50 await page.viewport(1280, 900);
51 const stack = new AsyncDisposableStack();
52 // Synthetic pointer ids are not active pointers, so the browser would reject capture.
53 stack.use(vi.spyOn(HTMLElement.prototype, "setPointerCapture").mockImplementation(() => {}));
54 // Tailwind is not loaded here; give the panel its fixed, card-sized box.
55 const style = document.head.appendChild(document.createElement("style"));
56 style.textContent = "[data-theme-editor-panel]{position:fixed;width:400px;height:300px}";
57 stack.defer(() => style.remove());
58 const screen = stack.use(
59 await render(
60 <ThemeEditorPanel
61 open
62 onOpenChange={() => {}}
63 onSaved={() => true}
64 editingTheme={null}
65 initialAppearance="dark"
66 restoreTheme={() => {}}
67 />,
68 ),
69 );
70 const panel = screen.getByRole("dialog", { name: "Create theme" });
71 await expect.element(panel).toBeVisible();
72 return {
73 panel: panel.element() as HTMLElement,
74 [Symbol.asyncDispose]: () => stack.disposeAsync(),
75 };
77​
78// The editor floats beside the Settings dialog, whose Esc handler sits on the document.
79async function renderBesideSettings() {
80 const onSettingsOpenChange = vi.fn();
81 const onEditorOpenChange = vi.fn();
82 const screen = await render(
83 <>
84 <Dialog onOpenChange={onSettingsOpenChange} open>
85 <DialogPopup>
86 <DialogTitle>Settings</DialogTitle>
87 </DialogPopup>
88 </Dialog>
89 <ThemeEditorPanel
90 open
91 onOpenChange={onEditorOpenChange}
92 onSaved={() => true}
93 editingTheme={null}
94 initialAppearance="light"
95 restoreTheme={() => {}}
96 />
97 </>,
98 );
99 return {
100 onSettingsOpenChange,
101 onEditorOpenChange,
102 [Symbol.asyncDispose]: () => screen[Symbol.asyncDispose](),
103 };
105​
106// The modal Settings dialog marks the editor inert, hiding it from role queries and pointer input.
107async function editorElement(selector: string) {
108 const query = () => document.querySelector<HTMLElement>(`[data-theme-editor-panel] ${selector}`);
109 await expect.poll(query).toBeTruthy();
110 return query()!;
112​
113describe("ThemeEditorPanel", () => {
114 it("keeps its left edge when resized after the window shrank", async () => {
115 await using view = await renderPanel();
116 dragHeader(view.panel, 700, 40);
117 await expect.poll(() => view.panel.style.left).toBe("700px");
118​
119 await page.viewport(600, 900);
120 await expect.poll(() => Number.parseFloat(view.panel.style.left)).toBeLessThan(600);
121 const clampedLeft = view.panel.style.left;
122​
123 dragResizeHandle(view.panel, -100);
124 await expect.poll(() => view.panel.style.width).not.toBe("");
125 expect(view.panel.style.left).toBe(clampedLeft);
126 });
127​
128 it("returns to where the user left it when the window shrinks and grows back", async () => {
129 await page.viewport(1280, 900);
130 // Synthetic pointer ids are not active pointers, so the browser would reject capture.
131 using _capture = vi
132 .spyOn(HTMLElement.prototype, "setPointerCapture")
133 .mockImplementation(() => {});
134 // Tailwind is not loaded here; give the panel its fixed, card-sized box.
135 const style = document.head.appendChild(document.createElement("style"));
136 style.textContent = "[data-theme-editor-panel]{position:fixed;width:400px;height:300px}";
137 using _style = { [Symbol.dispose]: () => style.remove() };
138 await using screen = await render(
139 <ThemeEditorPanel
140 open
141 onOpenChange={() => {}}
142 onSaved={() => true}
143 editingTheme={null}
144 initialAppearance="dark"
145 restoreTheme={() => {}}
146 />,
147 );
148 const panel = screen.getByRole("dialog", { name: "Create theme" });
149 await expect.element(panel).toBeVisible();
150​
151 dragHeader(panel.element() as HTMLElement, 700, 40);
152 await expect.poll(() => (panel.element() as HTMLElement).style.left).toBe("700px");
153​
154 await page.viewport(600, 900);
155 await expect
156 .poll(() => Number.parseFloat((panel.element() as HTMLElement).style.left))
157 .toBeLessThan(600);
158​
159 await page.viewport(1280, 900);
160 await expect.poll(() => (panel.element() as HTMLElement).style.left).toBe("700px");
161 });
162​
163 it("restores the colors from before the picker opened when Escape closes it", async () => {
164 await using _view = await renderPanel();
165 const hex = page.getByRole("textbox", { name: "Accent hex value" });
166 const original = (hex.element() as HTMLInputElement).value;
167 await userEvent.click(page.getByRole("button", { name: "Choose Accent color" }));
168 const hue = page.getByRole("slider", { name: "Accent hue" });
169 await expect.element(hue).toBeInTheDocument();
170 const hueSlider = hue.element() as HTMLElement;
171​
172 await act(async () => {
173 pointerAt(hueSlider, "pointerdown", 0.5);
174 pointerAt(hueSlider, "pointerup", 0.5);
175 });
176 await expect.element(hex).not.toHaveValue(original);
177​
178 await act(async () => {
179 pointerAt(hueSlider, "pointerdown", 0.25);
180 hueSlider.dispatchEvent(new KeyboardEvent("keydown", { bubbles: true, key: "Escape" }));
181 // The drag keeps going while the picker animates out.
182 pointerAt(hueSlider, "pointermove", 0.75);
183 pointerAt(hueSlider, "pointerup", 0.75);
184 });
185 expect((hex.element() as HTMLInputElement).value).toBe(original);
186 });
187​
188 it("closes on Escape without closing Settings", async () => {
189 await using view = await renderBesideSettings();
190 (await editorElement("input")).focus();
191 await userEvent.keyboard("{Escape}");
192​
193 expect(view.onEditorOpenChange).toHaveBeenCalledWith(false);
194 expect(view.onSettingsOpenChange).not.toHaveBeenCalled();
195 });
196​
197 it("leaves inspection on Escape without closing the editor or Settings", async () => {
198 await using view = await renderBesideSettings();
199 (await editorElement('[aria-label="Inspect app colors"]')).click();
200 await editorElement('[aria-label="Cancel inspecting app colors"]');
201 await userEvent.keyboard("{Escape}");
202​
203 await editorElement('[aria-label="Inspect app colors"]');
204 expect(view.onEditorOpenChange).not.toHaveBeenCalled();
205 expect(view.onSettingsOpenChange).not.toHaveBeenCalled();
206 });
207});