|
| 1 | +// @vitest-environment jsdom |
| 2 | +import { describe, expect, it } from "vitest"; |
| 3 | +import { applyElementPositionOffset } from "./elementPositionOffset"; |
| 4 | + |
| 5 | +// jsdom has no layout: offsets follow left/top unless the box is anchored elsewhere. |
| 6 | +function word(style: string, anchoredRight = false): HTMLElement { |
| 7 | + const el = document.createElement("span"); |
| 8 | + el.setAttribute("style", style); |
| 9 | + const px = (v: string) => Number.parseFloat(v) || 0; |
| 10 | + Object.defineProperties(el, { |
| 11 | + offsetLeft: { get: () => (anchoredRight ? 500 : 100 + px(el.style.left)) }, |
| 12 | + offsetTop: { get: () => 200 + px(el.style.top) }, |
| 13 | + offsetWidth: { get: () => 80 }, |
| 14 | + offsetHeight: { get: () => 40 }, |
| 15 | + }); |
| 16 | + el.setAttribute("data-hf-drag-initial-offset-x", "0"); |
| 17 | + el.setAttribute("data-hf-drag-initial-offset-y", "0"); |
| 18 | + document.body.append(el); |
| 19 | + return el; |
| 20 | +} |
| 21 | + |
| 22 | +describe("applyElementPositionOffset", () => { |
| 23 | + it("persists position for a static word the Layers panel lifted to relative", () => { |
| 24 | + const el = word("position: relative"); |
| 25 | + el.setAttribute("data-hf-reveal-prior-pos", "static"); |
| 26 | + expect(applyElementPositionOffset(el, { x: 40, y: 20 })).toEqual([ |
| 27 | + { type: "inline-style", property: "position", value: "relative" }, |
| 28 | + { type: "inline-style", property: "left", value: "40px" }, |
| 29 | + { type: "inline-style", property: "top", value: "20px" }, |
| 30 | + ]); |
| 31 | + expect(el.hasAttribute("data-hf-reveal-prior-pos")).toBe(false); |
| 32 | + }); |
| 33 | + |
| 34 | + it("names percent positioning as the reason it refuses", () => { |
| 35 | + const el = word("position: relative; left: 10%; top: 0px"); |
| 36 | + expect(applyElementPositionOffset(el, { x: 40, y: 20 })).toBe("percent"); |
| 37 | + expect(el.style.left).toBe("10%"); |
| 38 | + }); |
| 39 | + |
| 40 | + it("moves a static element by relative left/top", () => { |
| 41 | + const el = word(""); |
| 42 | + expect(applyElementPositionOffset(el, { x: 40, y: 20 })).toEqual([ |
| 43 | + { type: "inline-style", property: "position", value: "relative" }, |
| 44 | + { type: "inline-style", property: "left", value: "40px" }, |
| 45 | + { type: "inline-style", property: "top", value: "20px" }, |
| 46 | + ]); |
| 47 | + }); |
| 48 | + |
| 49 | + it("adds to left/top the element already has, so a second drag accumulates", () => { |
| 50 | + const el = word("position: relative; left: 40px; top: 20px"); |
| 51 | + expect(applyElementPositionOffset(el, { x: 40, y: 20 })).toEqual([ |
| 52 | + { type: "inline-style", property: "left", value: "80px" }, |
| 53 | + { type: "inline-style", property: "top", value: "40px" }, |
| 54 | + ]); |
| 55 | + }); |
| 56 | + |
| 57 | + it("refuses, leaving the element untouched, when left does not move the box", () => { |
| 58 | + const el = word("position: absolute; right: 30px", true); |
| 59 | + expect(applyElementPositionOffset(el, { x: 40, y: 20 })).toBe("anchored"); |
| 60 | + expect([el.style.left, el.style.top, el.style.right]).toEqual(["", "", "30px"]); |
| 61 | + }); |
| 62 | +}); |
0 commit comments