Files
hyperframes/packages/studio/src/hooks/plainRotation.test.ts
T
Miguel Ángel 74f5013210 fix(studio): a rotate without GSAP saves where you let go, nested too (#4802)
* fix(studio): a rotate GSAP does not own saves where you let go, as the element's own CSS rotate

* fix(studio): a rotate keeps a transform-centred element in place

* fix(studio): a plain rotate shows its angle in the panel and turns inline elements too

* fix(studio): a rotate turns a mirrored or stretched centred element with the cursor

* fix(studio): keep the rotate's panel readout within the comment budget after the move landed

* fix(studio): an inline element's turn saves the inline-block it was drawn with

The overlay passes the rotate target it read at press into the commit, so the
save draws and patches exactly what the draft did instead of re-reading an
element the draft already made inline-block. The GSAP-aware commit also routes
on that press-time decision.

* test(studio): bank the 27 rotate cases a GSAP-free rotate now passes

baseline.json is the edit-accuracy-gate artifact of the CI run at cb75cc37,
copied byte for byte: 530 to 557 passing, all rotate-none cases, none
regressed.

* fix(studio): a word an older Studio turned keeps its inline-block and whole angle on a new rotate

Clearing the legacy rotation marks puts the element back to inline and drops
their turn, so the save reads the target again instead of using the one the
press read before the clear. A test also pins that a turn the press drew as CSS
stays on the CSS writer.

* test(studio): bank the edit accuracy baseline from the rebased rotate run
2026-10-01 03:38:30 -07:00

123 lines
5.2 KiB
TypeScript

// @vitest-environment happy-dom
import { describe, expect, it, vi } from "vitest";
import type { DomEditSelection } from "../components/editor/domEditing";
import {
applyStudioRotation,
reapplyPositionEditsAfterSeek,
} from "../components/editor/manualEdits";
import { STUDIO_ROTATION_ATTR } from "../components/editor/manualEditsTypes";
import type { ElementOffsetStagerDeps } from "./elementOffsetStager";
import { savePlainRotation } from "./plainRotation";
import { readShownRotation } from "../components/editor/plainTranslate";
function rotate(
element: HTMLElement,
angle: number,
options: { save?: () => Promise<void>; readOnlyPreview?: boolean } = {},
) {
element.id = "box";
document.body.append(element);
const commitPositionPatchToHtml = vi.fn<ElementOffsetStagerDeps["commitPositionPatchToHtml"]>(
options.save ?? (() => Promise.resolve()),
);
const selection = { element, id: "box", selector: "#box" } as unknown as DomEditSelection;
const saved = savePlainRotation(
{ commitPositionPatchToHtml, readOnlyPreview: options.readOnlyPreview },
selection,
{ angle },
);
return { saved, commitPositionPatchToHtml, selection };
}
describe("savePlainRotation", () => {
it("saves the element's own rotate, less what its transform turns, with no Studio marks", async () => {
const element = document.createElement("div");
element.style.transform = "rotate(10deg)";
const { saved, commitPositionPatchToHtml, selection } = rotate(element, 55);
await saved;
expect(element.style.getPropertyValue("rotate")).toBe("45deg");
expect(commitPositionPatchToHtml).toHaveBeenCalledWith(
selection,
[{ type: "inline-style", property: "rotate", value: "45deg" }],
expect.objectContaining({ label: "Rotate layer" }),
);
expect(element.hasAttribute(STUDIO_ROTATION_ATTR)).toBe(false);
});
it("drops a legacy Studio rotation in the same write, so a seek does not put its angle back", async () => {
const element = document.createElement("div");
applyStudioRotation(element, { angle: 15 });
const { saved, commitPositionPatchToHtml } = rotate(element, 40);
await saved;
const patches = commitPositionPatchToHtml.mock.calls[0]![1];
expect(patches).toContainEqual({
type: "attribute",
property: STUDIO_ROTATION_ATTR,
value: null,
});
expect(patches.at(-1)).toEqual({ type: "inline-style", property: "rotate", value: "40deg" });
reapplyPositionEditsAfterSeek(document);
expect(element.style.getPropertyValue("rotate")).toBe("40deg");
});
it("puts the live rotate back when the save fails", async () => {
const element = document.createElement("div");
element.style.setProperty("rotate", "12deg");
const { saved } = rotate(element, 30, { save: () => Promise.reject(new Error("offline")) });
await expect(saved).rejects.toThrow("offline");
expect(element.style.getPropertyValue("rotate")).toBe("12deg");
});
it("writes nothing in a read-only preview", async () => {
const element = document.createElement("div");
const { saved, commitPositionPatchToHtml } = rotate(element, 30, { readOnlyPreview: true });
await saved;
expect(element.style.getPropertyValue("rotate")).toBe("");
expect(commitPositionPatchToHtml).not.toHaveBeenCalled();
});
it("saves a transform-centred element's turn in its transform and puts it back on a failed save", async () => {
const element = document.createElement("div");
element.style.transform = "translate(-120px, -80px)";
const { saved, commitPositionPatchToHtml } = rotate(element, 25, {
save: () => Promise.reject(new Error("offline")),
});
expect(commitPositionPatchToHtml.mock.calls[0]![1]).toEqual([
{
type: "inline-style",
property: "transform",
value: "translate(-120px, -80px) rotate(25deg)",
},
]);
await expect(saved).rejects.toThrow("offline");
expect(element.style.getPropertyValue("transform")).toBe("translate(-120px, -80px)");
});
it("makes an inline element inline-block, as a turn needs, and puts it back on a failed save", async () => {
const element = document.createElement("span");
element.style.setProperty("display", "inline");
const { saved, commitPositionPatchToHtml } = rotate(element, 20, {
save: () => Promise.reject(new Error("offline")),
});
expect(commitPositionPatchToHtml.mock.calls[0]![1]).toEqual([
{ type: "inline-style", property: "display", value: "inline-block" },
{ type: "inline-style", property: "rotate", value: "20deg" },
]);
await expect(saved).rejects.toThrow("offline");
expect(element.style.getPropertyValue("display")).toBe("inline");
});
});
describe("readShownRotation", () => {
it("shows an element's own CSS turn, and GSAP's legacy reading once GSAP turns it", () => {
const element = document.createElement("div");
document.body.append(element);
element.style.setProperty("rotate", "25deg");
expect(readShownRotation(element)).toEqual({ angle: 25 });
Object.assign(element, { _gsap: { renderTransform: () => {} } });
applyStudioRotation(element, { angle: 15 });
expect(readShownRotation(element)).toEqual({ angle: 15 });
});
});