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

contextMenuFallback.keyboard.browser.test.ts

131 lines4.4 KB

1import { expect, it } from "vite-plus/test";
2import { userEvent } from "vite-plus/test/browser";
3import {
4 dismissContextMenu,
5 isContextMenuOpen,
6 showContextMenuFallback,
7} from "./contextMenuFallback";
8import { createLocalApi } from "./localApi";
9​
10const items = [
11 { id: "heading", label: "Thread actions", header: true },
12 { id: "disabled", label: "Unavailable", disabled: true },
13 { id: "rename", label: "Rename" },
14 { id: "pin", label: "Pinned", checked: true, separatorBefore: true },
15 {
16 id: "copy",
17 label: "Copy",
18 children: [
19 { id: "copy-unavailable", label: "Unavailable copy", disabled: true },
20 { id: "path", label: "Path" },
21 { id: "branch", label: "Branch" },
22 ],
23 },
24];
25​
26function setup() {
27 const origin = document.createElement("button");
28 origin.textContent = "Selected thread";
29 document.body.append(origin);
30 origin.focus();
31 return {
32 origin,
33 [Symbol.dispose]() {
34 dismissContextMenu();
35 origin.remove();
36 },
37 };
39​
40function press(key: string, shiftKey = false) {
41 const event = new KeyboardEvent("keydown", { key, shiftKey, bubbles: true, cancelable: true });
42 document.activeElement!.dispatchEvent(event);
43 return event;
45​
46const label = () => document.activeElement?.textContent;
47​
48it("starts a keyboard-opened menu on its first action and wraps through enabled actions", () => {
49 using _fixture = setup();
50 void showContextMenuFallback(items);
51 expect(label()).toBe("Rename");
52 press("ArrowUp");
53 expect(label()).toBe("Copy");
54 press("ArrowDown");
55 expect(label()).toBe("Rename");
56 press("End");
57 expect(label()).toBe("Copy");
58 press("Home");
59 expect(label()).toBe("Rename");
60});
61​
62it("opens and cancels one submenu level at a time, then returns to the thread", async () => {
63 using fixture = setup();
64 const result = showContextMenuFallback(items);
65 press("End");
66 const parent = document.activeElement;
67 press("ArrowRight");
68 expect(label()).toBe("Path");
69 press("ArrowLeft");
70 expect(document.activeElement).toBe(parent);
71 press("Enter");
72 expect(label()).toBe("Path");
73 press("Escape");
74 expect(document.activeElement).toBe(parent);
75 expect(isContextMenuOpen()).toBe(true);
76 press("Escape");
77 expect(document.activeElement).toBe(fixture.origin);
78 await expect(result).resolves.toBeNull();
79});
80​
81it("activates the focused action with Space or Enter", async () => {
82 using _fixture = setup();
83 const first = showContextMenuFallback(items);
84 press(" ");
85 await expect(first).resolves.toBe("rename");
86 const second = showContextMenuFallback(items);
87 press("Enter");
88 await expect(second).resolves.toBe("rename");
89});
90​
91it("closes on Tab and returns focus to the opener so Tab continues from there", async () => {
92 using fixture = setup();
93 const result = showContextMenuFallback(items);
94 expect(press("Tab").defaultPrevented).toBe(false);
95 await expect(result).resolves.toBeNull();
96 expect(document.activeElement).toBe(fixture.origin);
97});
98​
99it("closes without taking focus back when focus moves elsewhere", async () => {
100 using _fixture = setup();
101 const result = showContextMenuFallback(items);
102 const destination = document.createElement("button");
103 document.body.append(destination);
104 using _destination = { [Symbol.dispose]: () => destination.remove() };
105 destination.focus();
106 await expect(result).resolves.toBeNull();
107 expect(document.activeElement).toBe(destination);
108});
109​
110it("takes thumb Back and Forward presses so the app router stays put", () => {
111 using _fixture = setup();
112 void showContextMenuFallback(items);
113 for (const button of [3, 4]) {
114 const event = new MouseEvent("mouseup", { button, bubbles: true, cancelable: true });
115 document.activeElement!.dispatchEvent(event);
116 // Chromium runs history back/forward on a thumb-button mouseup nobody cancelled.
117 expect(event.defaultPrevented).toBe(true);
118 }
119 expect(isContextMenuOpen()).toBe(true);
120});
121​
122// NVDA browse mode sends Shift+F10 without keyboard focus on the opener, so the opener does
123// not match :focus-visible; the caller's keyboard source must still start on the first action.
124it("starts a keyboard-sourced menu on its first action when the opener was focused by pointer", async () => {
125 using fixture = setup();
126 fixture.origin.blur();
127 await userEvent.click(fixture.origin);
128 expect(fixture.origin.matches(":focus-visible")).toBe(false);
129 void createLocalApi().contextMenu.show(items, { x: 0, y: 0 }, "keyboard");
130 expect(label()).toBe("Rename");
131});