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

contextMenuFallback.test.ts

238 lines6.1 KB

1import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
2​
3import { showContextMenuFallback } from "./contextMenuFallback";
4​
5type FakeListener = (event: FakeDomEvent) => void;
6​
7class FakeDomEvent {
8 defaultPrevented = false;
9​
10 constructor(
11 readonly type: string,
12 init: Record<string, unknown> = {},
13 ) {
14 Object.assign(this, init);
15 }
16​
17 preventDefault() {
18 this.defaultPrevented = true;
19 }
21​
22class FakeElement {
23 children: FakeElement[] = [];
24 parent: FakeElement | null = null;
25 style: Record<string, string> & { cssText?: string } = {};
26 dataset: Record<string, string> = {};
27 className = "";
28 disabled = false;
29 type = "";
30 private textValue = "";
31 private readonly listeners = new Map<string, FakeListener[]>();
32​
33 constructor(readonly tagName: string) {}
34​
35 appendChild(child: FakeElement) {
36 child.parent = this;
37 this.children.push(child);
38 return child;
39 }
40​
41 remove() {
42 if (!this.parent) {
43 return;
44 }
45 const index = this.parent.children.indexOf(this);
46 if (index >= 0) {
47 this.parent.children.splice(index, 1);
48 }
49 this.parent = null;
50 }
51​
52 addEventListener(type: string, listener: FakeListener) {
53 const existing = this.listeners.get(type) ?? [];
54 existing.push(listener);
55 this.listeners.set(type, existing);
56 }
57​
58 dispatchEvent(event: FakeDomEvent) {
59 for (const listener of this.listeners.get(event.type) ?? []) {
60 listener(event);
61 }
62 return true;
63 }
64​
65 set textContent(value: string) {
66 this.textValue = value;
67 }
68​
69 get textContent() {
70 return `${this.textValue}${this.children.map((child) => child.textContent).join("")}`;
71 }
72​
73 querySelectorAll(tagName: string): FakeElement[] {
74 const matches: FakeElement[] = [];
75 if (this.tagName === tagName) {
76 matches.push(this);
77 }
78 for (const child of this.children) {
79 matches.push(...child.querySelectorAll(tagName));
80 }
81 return matches;
82 }
83​
84 getBoundingClientRect() {
85 const left = Number.parseInt(this.style.left ?? "0", 10) || 0;
86 const top = Number.parseInt(this.style.top ?? "0", 10) || 0;
87 const width = this.tagName === "div" ? 180 : 140;
88 const height = this.tagName === "div" ? 120 : 28;
89 return {
90 left,
91 top,
92 width,
93 height,
94 right: left + width,
95 bottom: top + height,
96 };
97 }
99​
100class FakeBody extends FakeElement {
101 private html = "";
102​
103 constructor() {
104 super("body");
105 }
106​
107 set innerHTML(value: string) {
108 this.html = value;
109 this.children = [];
110 }
111​
112 get innerHTML() {
113 return this.html;
114 }
116​
117class FakeDocument {
118 body = new FakeBody();
119 private readonly listeners = new Map<string, FakeListener[]>();
120​
121 createElement(tagName: string) {
122 return new FakeElement(tagName);
123 }
124​
125 addEventListener(type: string, listener: FakeListener) {
126 const existing = this.listeners.get(type) ?? [];
127 existing.push(listener);
128 this.listeners.set(type, existing);
129 }
130​
131 removeEventListener(type: string, listener: FakeListener) {
132 const existing = this.listeners.get(type);
133 if (!existing) {
134 return;
135 }
136 const index = existing.indexOf(listener);
137 if (index >= 0) {
138 existing.splice(index, 1);
139 }
140 }
141​
142 querySelectorAll(tagName: string) {
143 return this.body.querySelectorAll(tagName);
144 }
146​
147function findButton(label: string): FakeElement | undefined {
148 return (document as unknown as FakeDocument)
149 .querySelectorAll("button")
150 .find((button) => button.textContent.includes(label));
152​
153beforeEach(() => {
154 vi.stubGlobal("document", new FakeDocument());
155 vi.stubGlobal("window", {
156 innerWidth: 1280,
157 innerHeight: 800,
158 });
159 vi.stubGlobal("requestAnimationFrame", (callback: (time: number) => void) => {
160 callback(0);
161 return 0;
162 });
163 vi.stubGlobal(
164 "MouseEvent",
165 class extends FakeDomEvent {
166 constructor(type: string, init: Record<string, unknown> = {}) {
167 super(type, init);
168 }
169 },
170 );
171 vi.stubGlobal(
172 "KeyboardEvent",
173 class extends FakeDomEvent {
174 constructor(type: string, init: Record<string, unknown> = {}) {
175 super(type, init);
176 }
177 },
178 );
179});
180​
181afterEach(() => {
182 vi.unstubAllGlobals();
183});
184​
185describe("showContextMenuFallback", () => {
186 it("resolves a clicked flat menu item", async () => {
187 const selectionPromise = showContextMenuFallback([
188 { id: "rename", label: "Rename" },
189 { id: "delete", label: "Delete", destructive: true },
190 ]);
191​
192 const renameButton = findButton("Rename");
193 expect(renameButton).toBeTruthy();
194 renameButton?.dispatchEvent(new MouseEvent("click", { bubbles: true }));
195​
196 await expect(selectionPromise).resolves.toBe("rename");
197 });
198​
199 it("ignores a click from the gesture that opened the menu", async () => {
200 let enablePointerSelection: ((time: number) => void) | undefined;
201 vi.stubGlobal("requestAnimationFrame", (callback: (time: number) => void) => {
202 enablePointerSelection = callback;
203 return 0;
204 });
205​
206 const selectionPromise = showContextMenuFallback([{ id: "rename", label: "Rename" }]);
207 const renameButton = findButton("Rename");
208​
209 renameButton?.dispatchEvent(new MouseEvent("click", { bubbles: true }));
210 enablePointerSelection?.(0);
211 renameButton?.dispatchEvent(new MouseEvent("click", { bubbles: true }));
212​
213 await expect(selectionPromise).resolves.toBe("rename");
214 });
215​
216 it("opens nested submenus and resolves the clicked leaf id", async () => {
217 const selectionPromise = showContextMenuFallback([
218 {
219 id: "rename:submenu",
220 label: "Rename project",
221 children: [
222 { id: "rename:project-a", label: "/tmp/project-a" },
223 { id: "rename:project-b", label: "/tmp/project-b" },
224 ],
225 },
226 ]);
227​
228 const parentButton = findButton("Rename project");
229 expect(parentButton).toBeTruthy();
230 parentButton?.dispatchEvent(new MouseEvent("mouseenter", { bubbles: true }));
231​
232 const childButton = findButton("/tmp/project-b");
233 expect(childButton).toBeTruthy();
234 childButton?.dispatchEvent(new MouseEvent("click", { bubbles: true }));
235​
236 await expect(selectionPromise).resolves.toBe("rename:project-b");
237 });
238});