|
| 1 | +import { assert, describe, test } from "@rezi-ui/testkit"; |
| 2 | +import type { RuntimeBackend } from "../../backend.js"; |
| 3 | +import type { ZrevEvent } from "../../events.js"; |
| 4 | +import { ui } from "../../index.js"; |
| 5 | +import { DEFAULT_TERMINAL_CAPS } from "../../terminalCaps.js"; |
| 6 | +import { defaultTheme } from "../../theme/defaultTheme.js"; |
| 7 | +import type { FileNode } from "../../widgets/types.js"; |
| 8 | +import { WidgetRenderer } from "../widgetRenderer.js"; |
| 9 | + |
| 10 | +function noRenderHooks(): { enterRender: () => void; exitRender: () => void } { |
| 11 | + return { enterRender: () => {}, exitRender: () => {} }; |
| 12 | +} |
| 13 | + |
| 14 | +function mouseEvent( |
| 15 | + x: number, |
| 16 | + y: number, |
| 17 | + mouseKind: 1 | 2 | 3 | 4 | 5, |
| 18 | + opts: Readonly<{ buttons?: number; timeMs?: number }> = {}, |
| 19 | +): ZrevEvent { |
| 20 | + return { |
| 21 | + kind: "mouse", |
| 22 | + timeMs: opts.timeMs ?? 0, |
| 23 | + x, |
| 24 | + y, |
| 25 | + mouseKind, |
| 26 | + mods: 0, |
| 27 | + buttons: opts.buttons ?? 0, |
| 28 | + wheelX: 0, |
| 29 | + wheelY: 0, |
| 30 | + }; |
| 31 | +} |
| 32 | + |
| 33 | +function createNoopBackend(): RuntimeBackend { |
| 34 | + return { |
| 35 | + start: async () => {}, |
| 36 | + stop: async () => {}, |
| 37 | + dispose: () => {}, |
| 38 | + requestFrame: async () => {}, |
| 39 | + pollEvents: async () => |
| 40 | + new Promise((_) => { |
| 41 | + // Not used by WidgetRenderer unit-style tests. |
| 42 | + }), |
| 43 | + postUserEvent: () => {}, |
| 44 | + getCaps: async () => DEFAULT_TERMINAL_CAPS, |
| 45 | + }; |
| 46 | +} |
| 47 | + |
| 48 | +describe("FileTreeExplorer context menu", () => { |
| 49 | + test("right click calls onContextMenu for the node under cursor", () => { |
| 50 | + const backend = createNoopBackend(); |
| 51 | + const renderer = new WidgetRenderer<void>({ |
| 52 | + backend, |
| 53 | + requestRender: () => {}, |
| 54 | + }); |
| 55 | + |
| 56 | + const calls: string[] = []; |
| 57 | + |
| 58 | + const data: readonly FileNode[] = Object.freeze([ |
| 59 | + Object.freeze({ name: "a", path: "/a", type: "file" }), |
| 60 | + Object.freeze({ name: "b", path: "/b", type: "file" }), |
| 61 | + ]); |
| 62 | + |
| 63 | + const vnode = ui.fileTreeExplorer({ |
| 64 | + id: "fte", |
| 65 | + data, |
| 66 | + expanded: [], |
| 67 | + onToggle: () => {}, |
| 68 | + onSelect: () => {}, |
| 69 | + onActivate: () => {}, |
| 70 | + onContextMenu: (node) => calls.push(node.path), |
| 71 | + }); |
| 72 | + |
| 73 | + const res = renderer.submitFrame( |
| 74 | + () => vnode, |
| 75 | + undefined, |
| 76 | + { cols: 20, rows: 5 }, |
| 77 | + defaultTheme, |
| 78 | + noRenderHooks(), |
| 79 | + ); |
| 80 | + assert.ok(res.ok); |
| 81 | + |
| 82 | + // Right-click (buttons bit 4) on the second row (index 1). |
| 83 | + renderer.routeEngineEvent(mouseEvent(0, 1, 3, { buttons: 4 })); |
| 84 | + assert.deepEqual(calls, ["/b"]); |
| 85 | + |
| 86 | + // Middle click should not fire context menu. |
| 87 | + renderer.routeEngineEvent(mouseEvent(0, 0, 3, { buttons: 2 })); |
| 88 | + assert.deepEqual(calls, ["/b"]); |
| 89 | + |
| 90 | + // Left click should not fire context menu. |
| 91 | + renderer.routeEngineEvent(mouseEvent(0, 0, 3, { buttons: 1 })); |
| 92 | + assert.deepEqual(calls, ["/b"]); |
| 93 | + }); |
| 94 | +}); |
0 commit comments