Open History to see the last commit
1import { describe, expect, it, vi } from "vite-plus/test";
2import { userEvent } from "vite-plus/test/browser";
3import { render } from "../../test/render";
4import { createSidebarShiftGuard } from "./Sidebar.shiftGuard";
5
6function mountList(container: HTMLElement, names: readonly string[]) {
7 const list = document.createElement("ul");
8 list.style.cssText = "position: relative; margin: 0; padding: 0; width: 200px; list-style: none";
9 const pressed: string[] = [];
10 const rows = new Map<string, HTMLLIElement>();
11 const add = (name: string) => {
12 const row = document.createElement("li");
13 row.style.height = "40px";
14 const button = document.createElement("button");
15 button.textContent = name;
16 button.addEventListener("click", () => pressed.push(name));
17 row.append(button);
18 list.append(row);
19 rows.set(name, row);
20 };
21 for (const name of names) add(name);
22 container.append(list);
23 const guard = createSidebarShiftGuard(list);
24 return {
25 pressed,
26 guard,
27 button: (name: string) => rows.get(name)!.querySelector("button")!,
28 add,
29 remove: (name: string) => rows.get(name)!.remove(),
30 [Symbol.dispose]: () => guard.dispose(),
31 };
32}
33
34// Fake timers would not do: the browser runner advances them with real time while a userEvent
35// command runs, so a slow click could outlast the guard's window before its press lands.
36function manualClock() {
37 let now = 0;
38 const spy = vi.spyOn(performance, "now").mockImplementation(() => now);
39 return {
40 advance: (ms: number) => void (now += ms),
41 [Symbol.dispose]: () => spy.mockRestore(),
42 };
43}
44
45// userEvent cannot hold the mouse, so `act` runs inside the real press, after the guard saw it.
46function duringPress(act: () => void) {
47 const listener = () => act();
48 window.addEventListener("pointerdown", listener, { capture: true, once: true });
49 return {
50 [Symbol.dispose]: () => window.removeEventListener("pointerdown", listener, { capture: true }),
51 };
52}
53
54describe("sidebar shift guard", () => {
55 it("drops a press on a row that slid up on its own, then accepts it after the delay", async () => {
56 await using screen = await render(null);
57 using list = mountList(screen.container, ["A", "B", "C"]);
58 using clock = manualClock();
59
60 list.remove("A");
61 list.guard.update();
62 await userEvent.click(list.button("B"));
63 expect(list.pressed).toEqual([]);
64
65 clock.advance(500);
66 await userEvent.click(list.button("B"));
67 expect(list.pressed).toEqual(["B"]);
68 });
69
70 it("keeps rows that did not move, or barely moved, clickable", async () => {
71 await using screen = await render(null);
72 using list = mountList(screen.container, ["A", "B", "C"]);
73 using _clock = manualClock();
74
75 list.remove("C");
76 list.button("A").closest("ul")!.style.paddingTop = "2px";
77 list.guard.update();
78 await userEvent.click(list.button("A"));
79 await userEvent.click(list.button("B"));
80 expect(list.pressed).toEqual(["A", "B"]);
81 });
82
83 it("keeps rows clickable when an empty list first fills", async () => {
84 await using screen = await render(null);
85 using list = mountList(screen.container, []);
86 using _clock = manualClock();
87
88 list.add("A");
89 list.add("B");
90 list.guard.update();
91 await userEvent.click(list.button("B"));
92 expect(list.pressed).toEqual(["B"]);
93 });
94
95 it("lets the user act on the row their own press moved", async () => {
96 await using screen = await render(null);
97 using list = mountList(screen.container, ["A", "B", "C"]);
98 using _clock = manualClock();
99 list.button("A").addEventListener("click", () => {
100 list.remove("A");
101 list.guard.update();
102 });
103
104 await userEvent.click(list.button("A"));
105 await userEvent.click(list.button("B"));
106 expect(list.pressed).toEqual(["A", "B"]);
107 });
108
109 it("clears an earlier mark when the user's own input moves the row again", async () => {
110 await using screen = await render(null);
111 using list = mountList(screen.container, ["A", "B", "C", "D"]);
112 using _clock = manualClock();
113
114 list.remove("A");
115 list.guard.update();
116 await userEvent.keyboard("{Escape}");
117 list.remove("B");
118 list.guard.update();
119 await userEvent.click(list.button("C"));
120 expect(list.pressed).toEqual(["C"]);
121 });
122
123 it("still guards a move that lands while the user types into a field", async () => {
124 await using screen = await render(null);
125 const field = document.createElement("input");
126 screen.container.append(field);
127 using list = mountList(screen.container, ["A", "B", "C"]);
128 using _clock = manualClock();
129
130 field.focus();
131 await userEvent.keyboard("x");
132 list.remove("A");
133 list.guard.update();
134 await userEvent.click(list.button("B"));
135 expect(list.pressed).toEqual([]);
136 });
137
138 it("keeps a held press dropped until its release, whatever happens before", async () => {
139 await using screen = await render(null);
140 using list = mountList(screen.container, ["A", "B", "C"]);
141 using clock = manualClock();
142
143 list.remove("A");
144 list.guard.update();
145 using _hold = duringPress(() => {
146 window.dispatchEvent(new KeyboardEvent("keydown", { key: "Shift" }));
147 const other = { bubbles: true, pointerId: 9 };
148 screen.container.dispatchEvent(new PointerEvent("pointerdown", other));
149 screen.container.dispatchEvent(new PointerEvent("pointerup", other));
150 clock.advance(1_000);
151 });
152 await userEvent.click(list.button("B"));
153 expect(list.pressed).toEqual([]);
154 });
155
156 it("lets keyboard activation through while a dropped press is held", async () => {
157 await using screen = await render(null);
158 using list = mountList(screen.container, ["A", "B", "C"]);
159 using _clock = manualClock();
160
161 list.remove("A");
162 list.guard.update();
163 list
164 .button("B")
165 .dispatchEvent(new PointerEvent("pointerdown", { bubbles: true, pointerId: 9 }));
166 list.button("C").focus();
167 await userEvent.keyboard("{Enter}");
168 expect(list.pressed).toEqual(["C"]);
169 await userEvent.keyboard("{Shift>}{F10}{/Shift}");
170 expect(
171 list
172 .button("C")
173 .dispatchEvent(new MouseEvent("contextmenu", { bubbles: true, cancelable: true })),
174 ).toBe(true);
175 });
176
177 it("ends a dropped press the browser cancels", async () => {
178 await using screen = await render(null);
179 using list = mountList(screen.container, ["A", "B", "C"]);
180 using clock = manualClock();
181
182 list.remove("A");
183 list.guard.update();
184 const touch = { bubbles: true, pointerId: 9, pointerType: "touch" };
185 list.button("B").dispatchEvent(new PointerEvent("pointerdown", touch));
186 list.button("B").dispatchEvent(new PointerEvent("pointercancel", touch));
187 clock.advance(500);
188 await userEvent.click(list.button("B"));
189 expect(list.pressed).toEqual(["B"]);
190 });
191});