Files
hyperframes/packages/studio/src/components/editor/domEditingLayers.test.ts
T

54 lines
1.6 KiB
TypeScript

// @vitest-environment jsdom
import { describe, expect, it } from "vitest";
import { resolveDomEditSelection, buildDomEditPatchTarget } from "./domEditingLayers";
const opts = { activeCompositionPath: "index.html", isMasterView: true, skipSourceProbe: true };
describe("buildDomEditPatchTarget", () => {
it("includes hfId when selection has hfId", () => {
const target = buildDomEditPatchTarget({
id: undefined,
hfId: "hf-abc",
selector: ".foo",
selectorIndex: 0,
});
expect(target.hfId).toBe("hf-abc");
});
it("includes id and selector when hfId absent", () => {
const target = buildDomEditPatchTarget({
id: "hero",
hfId: undefined,
selector: "#hero",
selectorIndex: undefined,
});
expect(target.id).toBe("hero");
expect(target.hfId).toBeUndefined();
});
});
describe("resolveDomEditSelection — hfId from data-hf-id", () => {
it("populates hfId from the element data-hf-id attribute", async () => {
const el = document.createElement("div");
el.id = "hero";
el.setAttribute("data-hf-id", "hf-x7k2");
document.body.appendChild(el);
const selection = await resolveDomEditSelection(el, opts);
document.body.removeChild(el);
expect(selection?.hfId).toBe("hf-x7k2");
});
it("leaves hfId undefined when element has no data-hf-id", async () => {
const el = document.createElement("div");
el.id = "no-hfid-el";
document.body.appendChild(el);
const selection = await resolveDomEditSelection(el, opts);
document.body.removeChild(el);
expect(selection?.hfId).toBeUndefined();
});
});