|
| 1 | +// @vitest-environment happy-dom |
| 2 | + |
| 3 | +import React, { act } from "react"; |
| 4 | +import { createRoot, type Root } from "react-dom/client"; |
| 5 | +import { afterEach, expect, it, vi } from "vitest"; |
| 6 | +import { makeSelection } from "../../hooks/domSelectionTestHarness"; |
| 7 | +import type { DomEditSelection } from "./domEditing"; |
| 8 | +import "./domEditOverlayTestMocks"; |
| 9 | +import { DomEditOverlay } from "./DomEditOverlay"; |
| 10 | + |
| 11 | +(globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true; |
| 12 | +HTMLElement.prototype.setPointerCapture ??= () => {}; |
| 13 | +HTMLElement.prototype.releasePointerCapture ??= () => {}; |
| 14 | + |
| 15 | +const pointTarget = vi.hoisted(() => ({ current: null as HTMLElement | null })); |
| 16 | +const rect = vi.hoisted(() => ({ current: null as Record<string, number> | null })); |
| 17 | +vi.mock("../../utils/studioPreviewHelpers", async (importOriginal) => ({ |
| 18 | + ...(await importOriginal<typeof import("../../utils/studioPreviewHelpers")>()), |
| 19 | + getPreviewTargetFromPointer: () => pointTarget.current, |
| 20 | +})); |
| 21 | +vi.mock("./useDomEditOverlayRects", () => ({ |
| 22 | + useDomEditOverlayRects: () => ({ |
| 23 | + overlayRect: rect.current, |
| 24 | + overlayRectRef: rect, |
| 25 | + setOverlayRect: () => undefined, |
| 26 | + hoverRect: null, |
| 27 | + groupOverlayItems: [], |
| 28 | + groupOverlayItemsRef: { current: [] }, |
| 29 | + setGroupOverlayItems: () => undefined, |
| 30 | + childRects: [], |
| 31 | + }), |
| 32 | +})); |
| 33 | + |
| 34 | +let root: Root; |
| 35 | +const onCanvasMouseDown = vi.fn(); |
| 36 | +const onSelectionChange = vi.fn(); |
| 37 | + |
| 38 | +function mount() { |
| 39 | + const host = document.createElement("div"); |
| 40 | + document.body.append(host); |
| 41 | + root = createRoot(host); |
| 42 | + const element = document.createElement("h1"); |
| 43 | + document.body.append(element); |
| 44 | + return { host, element, hover: makeSelection("Title", element) }; |
| 45 | +} |
| 46 | + |
| 47 | +function render( |
| 48 | + hoverSelection: DomEditSelection | null, |
| 49 | + selection: DomEditSelection | null = null, |
| 50 | +) { |
| 51 | + act(() => |
| 52 | + root.render( |
| 53 | + <DomEditOverlay |
| 54 | + iframeRef={{ current: document.createElement("iframe") }} |
| 55 | + activeCompositionPath={null} |
| 56 | + selection={selection} |
| 57 | + hoverSelection={hoverSelection} |
| 58 | + onCanvasMouseDown={onCanvasMouseDown} |
| 59 | + onCanvasPointerMove={() => Promise.resolve(null)} |
| 60 | + onCanvasPointerLeave={() => undefined} |
| 61 | + onSelectionChange={onSelectionChange} |
| 62 | + onBlockedMove={() => undefined} |
| 63 | + onPathOffsetCommit={() => undefined} |
| 64 | + onGroupPathOffsetCommit={() => undefined} |
| 65 | + onBoxSizeCommit={() => undefined} |
| 66 | + onRotationCommit={() => undefined} |
| 67 | + onMarqueeSelect={() => undefined} |
| 68 | + />, |
| 69 | + ), |
| 70 | + ); |
| 71 | +} |
| 72 | + |
| 73 | +// Like Chrome: a default-prevented pointerdown sends no compatibility mousedown. |
| 74 | +function press(target: Element, shiftKey = false) { |
| 75 | + const init = { bubbles: true, cancelable: true, button: 0, pointerId: 1, shiftKey }; |
| 76 | + const down = new PointerEvent("pointerdown", init); |
| 77 | + act(() => void target.dispatchEvent(down)); |
| 78 | + if (!down.defaultPrevented) |
| 79 | + act(() => void target.dispatchEvent(new MouseEvent("mousedown", init))); |
| 80 | + act(() => void target.dispatchEvent(new PointerEvent("pointerup", init))); |
| 81 | + act(() => void target.dispatchEvent(new MouseEvent("click", init))); |
| 82 | +} |
| 83 | + |
| 84 | +afterEach(() => { |
| 85 | + act(() => root.unmount()); |
| 86 | + onCanvasMouseDown.mockClear(); |
| 87 | + onSelectionChange.mockClear(); |
| 88 | + pointTarget.current = null; |
| 89 | + rect.current = null; |
| 90 | + document.body.innerHTML = ""; |
| 91 | +}); |
| 92 | + |
| 93 | +it.each([ |
| 94 | + ["a press on empty canvas", false], |
| 95 | + ["a shift+click add", true], |
| 96 | +])("after %s, the next click on an element selects it", (_, shiftFirst) => { |
| 97 | + const { host, element, hover } = mount(); |
| 98 | + const overlay = () => host.firstElementChild!; |
| 99 | + |
| 100 | + pointTarget.current = shiftFirst ? element : null; |
| 101 | + render(shiftFirst ? hover : null); |
| 102 | + press(overlay(), shiftFirst); |
| 103 | + expect(onCanvasMouseDown).not.toHaveBeenCalled(); |
| 104 | + |
| 105 | + pointTarget.current = element; |
| 106 | + render(hover); |
| 107 | + press(overlay()); |
| 108 | + expect(onCanvasMouseDown).toHaveBeenCalledTimes(1); |
| 109 | +}); |
| 110 | + |
| 111 | +it("a shift+click on a selected box that cannot move toggles it once", () => { |
| 112 | + const { host, element, hover } = mount(); |
| 113 | + hover.capabilities.canApplyManualOffset = false; |
| 114 | + rect.current = { left: 20, top: 30, width: 100, height: 40, editScaleX: 1, editScaleY: 1 }; |
| 115 | + pointTarget.current = element; |
| 116 | + render(hover, hover); |
| 117 | + |
| 118 | + press(host.querySelector('[data-dom-edit-selection-box="true"]')!, true); |
| 119 | + |
| 120 | + expect(onSelectionChange).toHaveBeenCalledTimes(1); |
| 121 | + expect(onCanvasMouseDown).not.toHaveBeenCalled(); |
| 122 | +}); |
0 commit comments