|
| 1 | +// @vitest-environment happy-dom |
| 2 | +import React, { act, type ComponentProps } from "react"; |
| 3 | +import { createRoot, type Root } from "react-dom/client"; |
| 4 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 5 | +import { DomEditGestureReadout } from "./DomEditGestureReadout"; |
| 6 | +import { createGestureReadout, updateGestureReadout } from "./domEditGestureReadoutState"; |
| 7 | + |
| 8 | +const frames = vi.hoisted(() => ({ update: () => {} })); |
| 9 | +vi.mock("./overlayFrameLoop", () => ({ |
| 10 | + subscribeOverlayFrame: (update: () => void) => { |
| 11 | + frames.update = update; |
| 12 | + return () => { |
| 13 | + frames.update = () => {}; |
| 14 | + }; |
| 15 | + }, |
| 16 | +})); |
| 17 | +// React needs its runtime test flag, which the global typings do not declare. |
| 18 | +(globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true; |
| 19 | +let root: Root | undefined; |
| 20 | +afterEach(() => { |
| 21 | + act(() => root?.unmount()); |
| 22 | + document.body.replaceChildren(); |
| 23 | + vi.clearAllMocks(); |
| 24 | +}); |
| 25 | + |
| 26 | +function mount(kind: "drag" | "resize" | "rotate") { |
| 27 | + const host = document.createElement("div"); |
| 28 | + const overlay = document.createElement("div"); |
| 29 | + const iframe = document.createElement("iframe"); |
| 30 | + document.body.append(host, overlay, iframe); |
| 31 | + const doc = iframe.contentDocument; |
| 32 | + if (!doc) throw new Error("Missing preview document"); |
| 33 | + const composition = doc.createElement("div"); |
| 34 | + composition.setAttribute("data-composition-id", "scene"); |
| 35 | + composition.setAttribute("data-width", "1000"); |
| 36 | + composition.setAttribute("data-height", "800"); |
| 37 | + const element = doc.createElement("div"); |
| 38 | + composition.append(element); |
| 39 | + doc.body.append(composition); |
| 40 | + let x = 70.25; |
| 41 | + let y = 40.75; |
| 42 | + const width = 100; |
| 43 | + const height = 60; |
| 44 | + overlay.getBoundingClientRect = () => new DOMRect(20, 30, 500, 400); |
| 45 | + iframe.getBoundingClientRect = () => new DOMRect(20, 30, 500, 400); |
| 46 | + composition.getBoundingClientRect = () => new DOMRect(0, 0, 1000, 800); |
| 47 | + element.getBoundingClientRect = () => new DOMRect(x, y, width, height); |
| 48 | + Object.defineProperties(element, { |
| 49 | + offsetWidth: { value: width }, |
| 50 | + offsetHeight: { value: height }, |
| 51 | + }); |
| 52 | + const gestureRef: ComponentProps<typeof DomEditGestureReadout>["gestureRef"] = { |
| 53 | + current: { |
| 54 | + travelled: true, |
| 55 | + readout: createGestureReadout(overlay, iframe, { |
| 56 | + left: x / 2, |
| 57 | + top: y / 2, |
| 58 | + width: width / 2, |
| 59 | + height: height / 2, |
| 60 | + editScaleX: 0.5, |
| 61 | + editScaleY: 0.5, |
| 62 | + }), |
| 63 | + }, |
| 64 | + }; |
| 65 | + const update = (text?: string) => |
| 66 | + updateGestureReadout( |
| 67 | + gestureRef.current?.readout, |
| 68 | + { |
| 69 | + left: x / 2, |
| 70 | + top: y / 2, |
| 71 | + width: width / 2, |
| 72 | + height: height / 2, |
| 73 | + editScaleX: 0.5, |
| 74 | + editScaleY: 0.5, |
| 75 | + }, |
| 76 | + text, |
| 77 | + ); |
| 78 | + update(kind === "resize" ? "100 × 60" : kind === "rotate" ? "0°" : undefined); |
| 79 | + root = createRoot(host); |
| 80 | + act(() => |
| 81 | + root?.render( |
| 82 | + <DomEditGestureReadout |
| 83 | + gestureRef={gestureRef} |
| 84 | + overlayRef={{ current: overlay }} |
| 85 | + iframeRef={{ current: iframe }} |
| 86 | + />, |
| 87 | + ), |
| 88 | + ); |
| 89 | + const label = host.querySelector<HTMLDivElement>("[data-dom-edit-gesture-readout]"); |
| 90 | + if (!label) throw new Error("Missing readout"); |
| 91 | + Object.defineProperties(label, { offsetWidth: { value: 120 }, offsetHeight: { value: 24 } }); |
| 92 | + const draw = () => frames.update(); |
| 93 | + const end = () => { |
| 94 | + gestureRef.current = null; |
| 95 | + draw(); |
| 96 | + }; |
| 97 | + return { |
| 98 | + measurements: [overlay, iframe, composition, element].map((node) => |
| 99 | + vi.spyOn(node, "getBoundingClientRect"), |
| 100 | + ), |
| 101 | + element, |
| 102 | + gestureRef, |
| 103 | + update, |
| 104 | + doc, |
| 105 | + label, |
| 106 | + draw, |
| 107 | + end, |
| 108 | + move: (nextX: number, nextY: number) => { |
| 109 | + x = nextX; |
| 110 | + y = nextY; |
| 111 | + update(); |
| 112 | + }, |
| 113 | + }; |
| 114 | +} |
| 115 | + |
| 116 | +describe("gesture readout", () => { |
| 117 | + it("adds no layout reads while drawing and stays hidden on a plain press", () => { |
| 118 | + const view = mount("drag"); |
| 119 | + const measure = vi.spyOn(Element.prototype, "getBoundingClientRect"); |
| 120 | + if (!view.gestureRef.current) throw new Error("Missing gesture"); |
| 121 | + view.gestureRef.current.travelled = false; |
| 122 | + view.draw(); |
| 123 | + expect(view.label.style.display).toBe("none"); |
| 124 | + view.gestureRef.current.travelled = true; |
| 125 | + view.move(120, 70); |
| 126 | + view.draw(); |
| 127 | + view.draw(); |
| 128 | + expect(view.label.textContent).toBe("Canvas X 120 · Y 70"); |
| 129 | + expect(measure).not.toHaveBeenCalled(); |
| 130 | + for (const measurement of view.measurements) expect(measurement).not.toHaveBeenCalled(); |
| 131 | + measure.mockRestore(); |
| 132 | + for (const measurement of view.measurements) measurement.mockRestore(); |
| 133 | + }); |
| 134 | + it("shows rounded composition coordinates during a move, accounting for preview zoom", () => { |
| 135 | + const view = mount("drag"); |
| 136 | + view.draw(); |
| 137 | + expect(view.label.textContent).toBe("Canvas X 70 · Y 41"); |
| 138 | + view.move(120.25, 70.75); |
| 139 | + view.draw(); |
| 140 | + expect(view.label.textContent).toBe("Canvas X 120 · Y 71"); |
| 141 | + expect(view.doc.querySelector("[data-dom-edit-gesture-readout]")).toBeNull(); |
| 142 | + view.end(); |
| 143 | + expect(view.label.style.display).toBe("none"); |
| 144 | + }); |
| 145 | + it("shows the actual size draft during resize and hides on release", () => { |
| 146 | + const view = mount("resize"); |
| 147 | + view.draw(); |
| 148 | + expect(view.label.textContent).toBe("100 × 60"); |
| 149 | + view.update("144 × 87"); |
| 150 | + view.draw(); |
| 151 | + expect(view.label.textContent).toBe("144 × 87"); |
| 152 | + view.end(); |
| 153 | + expect(view.label.style.display).toBe("none"); |
| 154 | + }); |
| 155 | + it("reads the live angle during rotation and hides on cancellation", () => { |
| 156 | + const view = mount("rotate"); |
| 157 | + view.update("32°"); |
| 158 | + view.draw(); |
| 159 | + expect(view.label.textContent).toBe("32°"); |
| 160 | + view.update("-46°"); |
| 161 | + view.draw(); |
| 162 | + expect(view.label.textContent).toBe("-46°"); |
| 163 | + view.end(); |
| 164 | + expect(view.label.style.display).toBe("none"); |
| 165 | + }); |
| 166 | + it("clamps the label inside the visible overlay at either edge", () => { |
| 167 | + const view = mount("drag"); |
| 168 | + view.move(900, -100); |
| 169 | + view.draw(); |
| 170 | + expect(view.label.style.left).toBe("496px"); |
| 171 | + expect(view.label.style.top).toBe("4px"); |
| 172 | + view.move(-900, 900); |
| 173 | + view.draw(); |
| 174 | + expect(view.label.style.left).toBe("4px"); |
| 175 | + expect(view.label.style.top).toBe("396px"); |
| 176 | + }); |
| 177 | +}); |
0 commit comments