/* MIT License Copyright 2023 - Present, Shopify Inc. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ import { describe, expect, it } from "vitest"; import { createTemplate, css, html, safe, type SafeMarkup } from "./utils"; describe("safe", () => { it("returns the input unchanged at runtime", () => { expect(safe("hi")).toBe("hi"); }); }); describe("html / css tagged templates", () => { it("concatenates static strings only", () => { expect(html`
hello
`).toBe("hello
"); }); it("interpolates SafeMarkup values between segments", () => { const name = safe("Westin"); expect(html`hello ${name}!`).toBe("hello Westin!"); }); it("handles multiple interpolations", () => { const a = safe("A"); const b = safe("B"); expect(html`${a}-${b}-${a}`).toBe("A-B-A"); }); it("falls back to empty strings when the template array has missing slots", () => { const emptyStrings = Object.assign([], { raw: [] }) as unknown as TemplateStringsArray; expect(html(emptyStrings)).toBe(""); }); it("css is the same function as html", () => { expect(css).toBe(html); }); describe("unsafe interpolation", () => { it("rejects raw strings at the type level", () => { const raw = ""; // @ts-expect-error — raw strings must go through `safe()` first. const result = html`prefix ${raw} suffix`; // The directive above asserts the compile-time guard. At runtime the // call still succeeds; this assertion exists so the test exercises // something other than the type-system check. expect(typeof result).toBe("string"); }); it("does not sanitize values at runtime if the type system is bypassed", () => { const escaped = "" as unknown as SafeMarkup; expect(html`prefix ${escaped} suffix`).toBe("prefix suffix"); }); }); }); describe("createTemplate", () => { it("returns an HTMLTemplateElement whose content holds the parsed fragment", () => { const template = createTemplate(html`