|
| 1 | +/** |
| 2 | + * Unit tests for resolveNearestHfElement (pure resolver — no browser needed). |
| 3 | + * |
| 4 | + * elementFromPoint itself requires a real browser layout engine. The adapter's |
| 5 | + * elementAtPoint() method is therefore NOT tested here; cover it with an |
| 6 | + * integration test that mounts a real same-origin iframe (WS-A1 follow-on). |
| 7 | + */ |
| 8 | + |
| 9 | +import { describe, it, expect, vi } from "vitest"; |
| 10 | +import { resolveNearestHfElement } from "./iframe.js"; |
| 11 | +import type { ElementAtPointResult } from "./types.js"; |
| 12 | + |
| 13 | +// ─── Minimal fake element ──────────────────────────────────────────────────── |
| 14 | + |
| 15 | +interface FakeEl { |
| 16 | + attrs: Record<string, string>; |
| 17 | + tagName: string; |
| 18 | + parentElement: FakeEl | null; |
| 19 | + getAttribute(name: string): string | null; |
| 20 | + hasAttribute(name: string): boolean; |
| 21 | +} |
| 22 | + |
| 23 | +function fakeEl( |
| 24 | + attrs: Record<string, string>, |
| 25 | + tagName: string, |
| 26 | + parent: FakeEl | null = null, |
| 27 | +): FakeEl { |
| 28 | + return { |
| 29 | + attrs, |
| 30 | + tagName, |
| 31 | + parentElement: parent, |
| 32 | + getAttribute(name) { |
| 33 | + return Object.prototype.hasOwnProperty.call(this.attrs, name) ? this.attrs[name] : null; |
| 34 | + }, |
| 35 | + hasAttribute(name) { |
| 36 | + return Object.prototype.hasOwnProperty.call(this.attrs, name); |
| 37 | + }, |
| 38 | + }; |
| 39 | +} |
| 40 | + |
| 41 | +const visible = () => true; |
| 42 | +const invisible = () => false; |
| 43 | + |
| 44 | +// ─── Tests ──────────────────────────────────────────────────────────────────── |
| 45 | + |
| 46 | +describe("resolveNearestHfElement", () => { |
| 47 | + it("returns null for a null input", () => { |
| 48 | + expect(resolveNearestHfElement(null, visible)).toBeNull(); |
| 49 | + }); |
| 50 | + |
| 51 | + it("returns the element itself when it carries data-hf-id", () => { |
| 52 | + const el = fakeEl({ "data-hf-id": "hf-abc" }, "div"); |
| 53 | + const result = resolveNearestHfElement(el as unknown as Element, visible); |
| 54 | + expect(result).toEqual<ElementAtPointResult>({ id: "hf-abc", tag: "div" }); |
| 55 | + }); |
| 56 | + |
| 57 | + it("walks up to a parent that carries data-hf-id", () => { |
| 58 | + const parent = fakeEl({ "data-hf-id": "hf-parent" }, "section"); |
| 59 | + const child = fakeEl({}, "span", parent); |
| 60 | + const result = resolveNearestHfElement(child as unknown as Element, visible); |
| 61 | + expect(result).toEqual<ElementAtPointResult>({ id: "hf-parent", tag: "section" }); |
| 62 | + }); |
| 63 | + |
| 64 | + it("returns null when the nearest data-hf-id node is data-hf-root", () => { |
| 65 | + const root = fakeEl({ "data-hf-id": "hf-stage", "data-hf-root": "" }, "div"); |
| 66 | + const child = fakeEl({}, "p", root); |
| 67 | + expect(resolveNearestHfElement(child as unknown as Element, visible)).toBeNull(); |
| 68 | + }); |
| 69 | + |
| 70 | + it("returns null when the element itself is data-hf-root", () => { |
| 71 | + const root = fakeEl({ "data-hf-id": "hf-stage", "data-hf-root": "" }, "div"); |
| 72 | + expect(resolveNearestHfElement(root as unknown as Element, visible)).toBeNull(); |
| 73 | + }); |
| 74 | + |
| 75 | + it("returns null when isVisible returns false for the matching element", () => { |
| 76 | + const el = fakeEl({ "data-hf-id": "hf-abc" }, "div"); |
| 77 | + expect(resolveNearestHfElement(el as unknown as Element, invisible)).toBeNull(); |
| 78 | + }); |
| 79 | + |
| 80 | + it("skips an opacity-0 element and returns null (isVisible called on the resolved node)", () => { |
| 81 | + // isVisible is only checked on the RESOLVED node, not intermediary nodes. |
| 82 | + const parent = fakeEl({ "data-hf-id": "hf-parent" }, "div"); |
| 83 | + const child = fakeEl({}, "span", parent); |
| 84 | + // Make parent invisible |
| 85 | + const isVisible = vi.fn((el: Element) => { |
| 86 | + const fe = el as unknown as FakeEl; |
| 87 | + return fe.attrs["data-hf-id"] !== "hf-parent"; |
| 88 | + }); |
| 89 | + expect(resolveNearestHfElement(child as unknown as Element, isVisible)).toBeNull(); |
| 90 | + // isVisible was called once (on the resolved parent node) |
| 91 | + expect(isVisible).toHaveBeenCalledTimes(1); |
| 92 | + }); |
| 93 | + |
| 94 | + it("returns null when no data-hf-id found in any ancestor", () => { |
| 95 | + const grandparent = fakeEl({}, "body"); |
| 96 | + const parent = fakeEl({}, "div", grandparent); |
| 97 | + const child = fakeEl({}, "span", parent); |
| 98 | + expect(resolveNearestHfElement(child as unknown as Element, visible)).toBeNull(); |
| 99 | + }); |
| 100 | + |
| 101 | + it("tag is lowercased", () => { |
| 102 | + const el = fakeEl({ "data-hf-id": "hf-xyz" }, "DIV"); |
| 103 | + const result = resolveNearestHfElement(el as unknown as Element, visible); |
| 104 | + expect(result?.tag).toBe("div"); |
| 105 | + }); |
| 106 | + |
| 107 | + it("stops at the nearest ancestor — does not continue past first data-hf-id", () => { |
| 108 | + const outer = fakeEl({ "data-hf-id": "hf-outer" }, "section"); |
| 109 | + const inner = fakeEl({ "data-hf-id": "hf-inner" }, "div", outer); |
| 110 | + const child = fakeEl({}, "span", inner); |
| 111 | + const result = resolveNearestHfElement(child as unknown as Element, visible); |
| 112 | + expect(result?.id).toBe("hf-inner"); |
| 113 | + }); |
| 114 | +}); |
| 115 | + |
| 116 | +// ─── select + on('selection') wiring ───────────────────────────────────────── |
| 117 | +// These cover the adapter-level selection state without needing a real iframe. |
| 118 | +// We import createIframePreviewAdapter and pass a stub iframe. |
| 119 | + |
| 120 | +import { createIframePreviewAdapter } from "./iframe.js"; |
| 121 | + |
| 122 | +function stubIframe() { |
| 123 | + return {} as HTMLIFrameElement; |
| 124 | +} |
| 125 | + |
| 126 | +describe("IframePreviewAdapter selection", () => { |
| 127 | + it("on('selection') fires when select() is called", () => { |
| 128 | + const adapter = createIframePreviewAdapter(stubIframe()); |
| 129 | + const cb = vi.fn(); |
| 130 | + adapter.on("selection", cb); |
| 131 | + adapter.select(["hf-abc"]); |
| 132 | + expect(cb).toHaveBeenCalledWith(["hf-abc"]); |
| 133 | + }); |
| 134 | + |
| 135 | + it("off unsubscribes the handler", () => { |
| 136 | + const adapter = createIframePreviewAdapter(stubIframe()); |
| 137 | + const cb = vi.fn(); |
| 138 | + const off = adapter.on("selection", cb); |
| 139 | + off(); |
| 140 | + adapter.select(["hf-abc"]); |
| 141 | + expect(cb).not.toHaveBeenCalled(); |
| 142 | + }); |
| 143 | + |
| 144 | + it("additive select merges with prior selection", () => { |
| 145 | + const adapter = createIframePreviewAdapter(stubIframe()); |
| 146 | + const cb = vi.fn(); |
| 147 | + adapter.on("selection", cb); |
| 148 | + adapter.select(["hf-a"]); |
| 149 | + adapter.select(["hf-b"], { additive: true }); |
| 150 | + expect(cb).toHaveBeenLastCalledWith(expect.arrayContaining(["hf-a", "hf-b"])); |
| 151 | + }); |
| 152 | + |
| 153 | + it("non-additive select replaces prior selection", () => { |
| 154 | + const adapter = createIframePreviewAdapter(stubIframe()); |
| 155 | + const cb = vi.fn(); |
| 156 | + adapter.on("selection", cb); |
| 157 | + adapter.select(["hf-a"]); |
| 158 | + adapter.select(["hf-b"]); |
| 159 | + expect(cb).toHaveBeenLastCalledWith(["hf-b"]); |
| 160 | + }); |
| 161 | + |
| 162 | + it("multiple handlers all fire", () => { |
| 163 | + const adapter = createIframePreviewAdapter(stubIframe()); |
| 164 | + const cb1 = vi.fn(); |
| 165 | + const cb2 = vi.fn(); |
| 166 | + adapter.on("selection", cb1); |
| 167 | + adapter.on("selection", cb2); |
| 168 | + adapter.select(["hf-abc"]); |
| 169 | + expect(cb1).toHaveBeenCalledOnce(); |
| 170 | + expect(cb2).toHaveBeenCalledOnce(); |
| 171 | + }); |
| 172 | +}); |
0 commit comments