|
| 1 | +// @vitest-environment jsdom |
| 2 | +/** |
| 3 | + * SelectionActionMenu.test.tsx — 共享选区菜单(批 8 REQ-317)补测。 |
| 4 | + * |
| 5 | + * @ai-context: 覆盖审查 P3-4——坐标钳制一次性:位置在打开瞬间按「含状态行区」 |
| 6 | + * 的最终高度计算(复制成功状态行出现不再重钳上跳);贴底场景下 |
| 7 | + * style.top 从打开到状态行出现全程不变。invoke 无依赖(复制走 |
| 8 | + * navigator.clipboard mock;菜单组件自身零后端调用)。 |
| 9 | + */ |
| 10 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 11 | +import { cleanup, fireEvent, render, screen, waitFor } from "@testing-library/react"; |
| 12 | +import SelectionActionMenu from "./SelectionActionMenu"; |
| 13 | + |
| 14 | +const { writeText } = vi.hoisted(() => ({ writeText: vi.fn() })); |
| 15 | + |
| 16 | +const originalInner = { w: window.innerWidth, h: window.innerHeight }; |
| 17 | +const originalClipboard = navigator.clipboard; |
| 18 | + |
| 19 | +const baseProps = { |
| 20 | + x: 500, |
| 21 | + y: 500, |
| 22 | + mode: "editing" as const, |
| 23 | + text: "选中文字快照", |
| 24 | + onClose: vi.fn(), |
| 25 | + onAction: vi.fn(), |
| 26 | +}; |
| 27 | + |
| 28 | +beforeEach(() => { |
| 29 | + writeText.mockReset(); |
| 30 | + writeText.mockResolvedValue(undefined); |
| 31 | + // jsdom 无 navigator.clipboard——按可用注入(configurable 属性,afterEach 还原) |
| 32 | + Object.defineProperty(navigator, "clipboard", { configurable: true, value: { writeText } }); |
| 33 | + // 小视口:底部钳制必命中(贴底 y=500 > vh-h-4) |
| 34 | + Object.defineProperty(window, "innerWidth", { configurable: true, value: 640 }); |
| 35 | + Object.defineProperty(window, "innerHeight", { configurable: true, value: 480 }); |
| 36 | +}); |
| 37 | + |
| 38 | +afterEach(() => { |
| 39 | + cleanup(); |
| 40 | + Object.defineProperty(window, "innerWidth", { configurable: true, value: originalInner.w }); |
| 41 | + Object.defineProperty(window, "innerHeight", { configurable: true, value: originalInner.h }); |
| 42 | + Object.defineProperty(navigator, "clipboard", { configurable: true, value: originalClipboard }); |
| 43 | +}); |
| 44 | + |
| 45 | +describe("SelectionActionMenu 坐标钳制一次性(审查 P3-4)", () => { |
| 46 | + it("贴底场景:打开位置即终位——复制状态行出现后 top 不变(预留状态行区钳制)", async () => { |
| 47 | + // 编辑态 5 项:HEAD(34) + 5×ROW(30) + 状态行预留(26) + padding(8) = 218 |
| 48 | + // 视口 480 → clamp 上限 = 480 - 218 - 4 = 258;y=500 贴底 → top=258 |
| 49 | + const { baseElement } = render(<SelectionActionMenu {...baseProps} />); |
| 50 | + const menu = screen.getByTestId("note-sel-menu"); |
| 51 | + expect(menu.style.top).toBe("258px"); |
| 52 | + // 点击复制 → 状态行出现(菜单保持打开)——位置不得因高度重算而位移 |
| 53 | + fireEvent.click(screen.getByTestId("note-sel-copy")); |
| 54 | + expect(await screen.findByTestId("note-sel-copy-status")).toBeTruthy(); |
| 55 | + await waitFor(() => expect(writeText).toHaveBeenCalledWith("选中文字快照")); |
| 56 | + expect(menu.style.top).toBe("258px"); |
| 57 | + expect(baseElement.querySelector('[data-testid="note-sel-copy-status"]')?.textContent).toContain("已复制"); |
| 58 | + }); |
| 59 | + |
| 60 | + it("常规位置不受预留影响(上方不越界时按请求坐标定位)", () => { |
| 61 | + Object.defineProperty(window, "innerHeight", { configurable: true, value: 1000 }); |
| 62 | + render(<SelectionActionMenu {...baseProps} x={120} y={200} />); |
| 63 | + const menu = screen.getByTestId("note-sel-menu"); |
| 64 | + expect(menu.style.top).toBe("200px"); |
| 65 | + expect(menu.style.left).toBe("120px"); |
| 66 | + }); |
| 67 | +}); |
0 commit comments