|
1 | | -// @vitest-environment jsdom |
2 | | - |
3 | | -import { act, createRef, useImperativeHandle } from "react"; |
| 1 | +import { act } from "react"; |
4 | 2 | import { createRoot, type Root } from "react-dom/client"; |
5 | | -import { create, type ReactTestRenderer } from "react-test-renderer"; |
6 | 3 | import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test"; |
7 | 4 |
|
8 | 5 | import { useMessageLinkMenu } from "./useMessageLinkMenu"; |
9 | 6 |
|
10 | | -let renderer: ReactTestRenderer | undefined; |
| 7 | +describe("message link menu positioning", () => { |
| 8 | + let root: Root; |
| 9 | + let container: HTMLDivElement; |
11 | 10 |
|
12 | | -afterEach(async () => { |
13 | | - await act(() => renderer?.unmount()); |
14 | | - renderer = undefined; |
15 | | - vi.unstubAllGlobals(); |
16 | | -}); |
| 11 | + beforeEach(() => { |
| 12 | + vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true); |
| 13 | + container = document.body.appendChild(document.createElement("div")); |
| 14 | + root = createRoot(container); |
| 15 | + }); |
| 16 | + |
| 17 | + afterEach(async () => { |
| 18 | + await act(async () => root.unmount()); |
| 19 | + container.remove(); |
| 20 | + vi.unstubAllGlobals(); |
| 21 | + }); |
17 | 22 |
|
18 | | -describe("message link menu positioning", () => { |
19 | 23 | it.each([ |
20 | 24 | { position: { x: 0, y: 0 }, expected: { x: 120, y: 260 } }, |
21 | 25 | { position: { x: 145, y: 245 }, expected: { x: 145, y: 245 } }, |
22 | 26 | { position: { x: 0, y: 245 }, expected: { x: 0, y: 245 } }, |
23 | 27 | { position: { x: 145, y: 0 }, expected: { x: 145, y: 0 } }, |
24 | | - ])("anchors a menu opened at $position to $expected", async ({ position, expected }) => { |
25 | | - vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true); |
26 | | - vi.stubGlobal( |
27 | | - "DOMRect", |
28 | | - class { |
29 | | - constructor( |
30 | | - readonly x: number, |
31 | | - readonly y: number, |
32 | | - readonly width: number, |
33 | | - readonly height: number, |
34 | | - ) {} |
35 | | - }, |
36 | | - ); |
37 | | - const linkMenuRef = createRef<ReturnType<typeof useMessageLinkMenu>>(); |
| 28 | + ])("opens a menu requested at $position at $expected", async ({ position, expected }) => { |
38 | 29 | function TestLinkMenu() { |
39 | | - const linkMenu = useMessageLinkMenu(); |
40 | | - useImperativeHandle(linkMenuRef, () => linkMenu, [linkMenu]); |
41 | | - return null; |
| 30 | + const { show, menu } = useMessageLinkMenu(); |
| 31 | + return ( |
| 32 | + <> |
| 33 | + <a |
| 34 | + href="https://example.com" |
| 35 | + style={{ position: "fixed", left: 120, top: 240, width: 80, height: 20 }} |
| 36 | + onContextMenu={(event) => { |
| 37 | + event.preventDefault(); |
| 38 | + void show( |
| 39 | + [{ id: "copy-link", label: "Copy Link" }], |
| 40 | + { x: event.clientX, y: event.clientY }, |
| 41 | + event.currentTarget, |
| 42 | + ); |
| 43 | + }} |
| 44 | + > |
| 45 | + Example |
| 46 | + </a> |
| 47 | + {menu} |
| 48 | + </> |
| 49 | + ); |
42 | 50 | } |
43 | | - const trigger = { |
44 | | - getBoundingClientRect: () => ({ left: 120, bottom: 260 }), |
45 | | - } as HTMLElement; |
| 51 | + await act(async () => root.render(<TestLinkMenu />)); |
46 | 52 |
|
47 | | - await act(() => { |
48 | | - renderer = create(<TestLinkMenu />); |
49 | | - }); |
50 | | - await act(() => { |
51 | | - void linkMenuRef.current!.show([{ id: "copy-link", label: "Copy Link" }], position, trigger); |
| 53 | + // A keyboard-opened context menu reports (0, 0). |
| 54 | + await act(async () => { |
| 55 | + container.querySelector("a")!.dispatchEvent( |
| 56 | + new MouseEvent("contextmenu", { |
| 57 | + bubbles: true, |
| 58 | + cancelable: true, |
| 59 | + clientX: position.x, |
| 60 | + clientY: position.y, |
| 61 | + }), |
| 62 | + ); |
52 | 63 | }); |
53 | 64 |
|
54 | | - // Evaluate the virtual anchor consumed by the popup's positioning engine. |
55 | | - const popup = linkMenuRef.current!.menu?.props.children; |
56 | | - expect(popup.props.anchor.getBoundingClientRect()).toMatchObject({ |
57 | | - ...expected, |
58 | | - width: 0, |
59 | | - height: 0, |
| 65 | + // Base UI nudges the popup up to 5px off its anchor; the cases differ by far more. |
| 66 | + await vi.waitFor(() => { |
| 67 | + const menu = document.querySelector<HTMLElement>('[role="menu"]'); |
| 68 | + expect(menu).not.toBeNull(); |
| 69 | + const bounds = menu!.getBoundingClientRect(); |
| 70 | + expect(Math.abs(bounds.left - expected.x)).toBeLessThanOrEqual(5); |
| 71 | + expect(Math.abs(bounds.top - expected.y)).toBeLessThanOrEqual(5); |
60 | 72 | }); |
61 | 73 | }); |
62 | 74 | }); |
|
0 commit comments