Open History to see the last commit
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 }
20}
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 }
98}
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 }
115}
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 }
145}
146
147function findButton(label: string): FakeElement | undefined {
148 return (document as unknown as FakeDocument)
149 .querySelectorAll("button")
150 .find((button) => button.textContent.includes(label));
151}
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("opens nested submenus and resolves the clicked leaf id", async () => {
200 const selectionPromise = showContextMenuFallback([
201 {
202 id: "rename:submenu",
203 label: "Rename project",
204 children: [
205 { id: "rename:project-a", label: "/tmp/project-a" },
206 { id: "rename:project-b", label: "/tmp/project-b" },
207 ],
208 },
209 ]);
210
211 const parentButton = findButton("Rename project");
212 expect(parentButton).toBeTruthy();
213 parentButton?.dispatchEvent(new MouseEvent("mouseenter", { bubbles: true }));
214
215 const childButton = findButton("/tmp/project-b");
216 expect(childButton).toBeTruthy();
217 childButton?.dispatchEvent(new MouseEvent("click", { bubbles: true }));
218
219 await expect(selectionPromise).resolves.toBe("rename:project-b");
220 });
221});