Open History to see the last commit
1import { describe, expect, it, vi } from "vite-plus/test";
2import { userEvent } from "vite-plus/test/browser";
3import { fakeTimers, 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// userEvent cannot hold the mouse, so `act` runs inside the real press, after the guard saw it.
35function duringPress(act: () => void) {
36 const listener = () => act();
37 window.addEventListener("pointerdown", listener, { capture: true, once: true });
38 return {
39 [Symbol.dispose]: () => window.removeEventListener("pointerdown", listener, { capture: true }),
40 };
41}
42
43describe("sidebar shift guard", () => {
44 it("drops a press on a row that slid up on its own, then accepts it after the delay", async () => {
45 await using screen = await render(null);
46 using list = mountList(screen.container, ["A", "B", "C"]);
47 using _clock = fakeTimers();
48
49 list.remove("A");
50 list.guard.update();
51 await userEvent.click(list.button("B"));
52 expect(list.pressed).toEqual([]);
53
54 vi.advanceTimersByTime(500);
55 await userEvent.click(list.button("B"));
56 expect(list.pressed).toEqual(["B"]);
57 });
58
59 it("keeps rows that did not move, or barely moved, clickable", async () => {
60 await using screen = await render(null);
61 using list = mountList(screen.container, ["A", "B", "C"]);
62 using _clock = fakeTimers();
63
64 list.remove("C");
65 list.button("A").closest("ul")!.style.paddingTop = "2px";
66 list.guard.update();
67 await userEvent.click(list.button("A"));
68 await userEvent.click(list.button("B"));
69 expect(list.pressed).toEqual(["A", "B"]);
70 });
71
72 it("keeps rows clickable when an empty list first fills", async () => {
73 await using screen = await render(null);
74 using list = mountList(screen.container, []);
75 using _clock = fakeTimers();
76
77 list.add("A");
78 list.add("B");
79 list.guard.update();
80 await userEvent.click(list.button("B"));
81 expect(list.pressed).toEqual(["B"]);
82 });
83
84 it("lets the user act on the row their own press moved", async () => {
85 await using screen = await render(null);
86 using list = mountList(screen.container, ["A", "B", "C"]);
87 using _clock = fakeTimers();
88 list.button("A").addEventListener("click", () => {
89 list.remove("A");
90 list.guard.update();
91 });
92
93 await userEvent.click(list.button("A"));
94 await userEvent.click(list.button("B"));
95 expect(list.pressed).toEqual(["A", "B"]);
96 });
97
98 it("clears an earlier mark when the user's own input moves the row again", async () => {
99 await using screen = await render(null);
100 using list = mountList(screen.container, ["A", "B", "C", "D"]);
101 using _clock = fakeTimers();
102
103 list.remove("A");
104 list.guard.update();
105 await userEvent.keyboard("{Escape}");
106 list.remove("B");
107 list.guard.update();
108 await userEvent.click(list.button("C"));
109 expect(list.pressed).toEqual(["C"]);
110 });
111
112 it("still guards a move that lands while the user types into a field", async () => {
113 await using screen = await render(null);
114 const field = document.createElement("input");
115 screen.container.append(field);
116 using list = mountList(screen.container, ["A", "B", "C"]);
117 using _clock = fakeTimers();
118
119 field.focus();
120 await userEvent.keyboard("x");
121 list.remove("A");
122 list.guard.update();
123 await userEvent.click(list.button("B"));
124 expect(list.pressed).toEqual([]);
125 });
126
127 it("keeps a held press dropped until its release, whatever happens before", async () => {
128 await using screen = await render(null);
129 using list = mountList(screen.container, ["A", "B", "C"]);
130 using _clock = fakeTimers();
131
132 list.remove("A");
133 list.guard.update();
134 using _hold = duringPress(() => {
135 window.dispatchEvent(new KeyboardEvent("keydown", { key: "Shift" }));
136 const other = { bubbles: true, pointerId: 9 };
137 screen.container.dispatchEvent(new PointerEvent("pointerdown", other));
138 screen.container.dispatchEvent(new PointerEvent("pointerup", other));
139 vi.advanceTimersByTime(1_000);
140 });
141 await userEvent.click(list.button("B"));
142 expect(list.pressed).toEqual([]);
143 });
144
145 it("lets keyboard activation through while a dropped press is held", async () => {
146 await using screen = await render(null);
147 using list = mountList(screen.container, ["A", "B", "C"]);
148 using _clock = fakeTimers();
149
150 list.remove("A");
151 list.guard.update();
152 list
153 .button("B")
154 .dispatchEvent(new PointerEvent("pointerdown", { bubbles: true, pointerId: 9 }));
155 list.button("C").focus();
156 await userEvent.keyboard("{Enter}");
157 expect(list.pressed).toEqual(["C"]);
158 await userEvent.keyboard("{Shift>}{F10}{/Shift}");
159 expect(
160 list
161 .button("C")
162 .dispatchEvent(new MouseEvent("contextmenu", { bubbles: true, cancelable: true })),
163 ).toBe(true);
164 });
165
166 it("ends a dropped press the browser cancels", async () => {
167 await using screen = await render(null);
168 using list = mountList(screen.container, ["A", "B", "C"]);
169 using _clock = fakeTimers();
170
171 list.remove("A");
172 list.guard.update();
173 const touch = { bubbles: true, pointerId: 9, pointerType: "touch" };
174 list.button("B").dispatchEvent(new PointerEvent("pointerdown", touch));
175 list.button("B").dispatchEvent(new PointerEvent("pointercancel", touch));
176 vi.advanceTimersByTime(500);
177 await userEvent.click(list.button("B"));
178 expect(list.pressed).toEqual(["B"]);
179 });
180});