import { describe, it, expect, beforeEach } from "vitest"; import { escapeHtml, raw, html, fragment, renderInto } from "../src/ui/render.js"; describe("ui/render helpers", () => { beforeEach(() => { document.body.innerHTML = ""; }); it("escapeHtml escapes special characters", () => { expect(escapeHtml(`'`)).toBe("<a href="x&y">'"); }); it("html tag escapes by default and allows raw opt-out", () => { const user = `bold&spicy`; const safe = html`Hello ${user}!`; expect(safe).toBe("Hello <b>bold&spicy</b>!"); const unsafe = html`Hello ${raw(user)}!`; expect(unsafe).toBe("Hello bold&spicy!"); }); it("fragment creates nodes", () => { const frag = fragment(""); expect(frag.childNodes.length).toBe(1); expect(frag.querySelectorAll("li").length).toBe(2); }); it("renderInto replaces by default and can append/prepend", () => { const host = document.createElement("div"); document.body.appendChild(host); renderInto(host, "

first

"); expect(host.querySelector("p")?.textContent).toBe("first"); renderInto(host, "

second

", "append"); const ps = host.querySelectorAll("p"); expect(ps.length).toBe(2); expect(ps[0].textContent).toBe("first"); expect(ps[1].textContent).toBe("second"); renderInto(host, "

third

", "prepend"); const ps2 = host.querySelectorAll("p"); expect(ps2.length).toBe(3); expect(ps2[0].textContent).toBe("third"); }); });