/**
* Unit tests for admin.js escapeHtml function.
*/
import { describe, test, expect, beforeAll, afterAll } from "vitest";
import { loadAdminJs, cleanupAdminJs } from "./helpers/admin-env.js";
let escapeHtml;
beforeAll(() => {
const win = loadAdminJs();
escapeHtml = win.escapeHtml;
});
afterAll(() => {
cleanupAdminJs();
});
describe("escapeHtml", () => {
describe("Happy Path", () => {
test("should escape basic HTML tags", () => {
expect(escapeHtml('<script>alert("XSS")</script>')).toBe(
"<script>alert("XSS")</script>",
);
});
test("should escape special characters", () => {
expect(escapeHtml("'\"&`/")).toBe("'"&`/");
});
test("should return plain text unchanged", () => {
expect(escapeHtml("Hello World")).toBe("Hello World");
});
});
describe("Null, Undefined and Empty Strings Handling", () => {
test("should return empty string for null and undefined", () => {
expect(escapeHtml(null)).toBe("");
expect(escapeHtml(undefined)).toBe("");
});
test("should return empty string for empty input", () => {
expect(escapeHtml("")).toBe("");
});
test("should preserve whitespace-only strings", () => {
expect(escapeHtml(" ")).toBe(" ");
expect(escapeHtml("\t")).toBe("\t");
expect(escapeHtml("\n")).toBe("\n");
});
});
describe("Type Coercion", () => {
test("should convert numbers to strings", () => {
expect(escapeHtml(123)).toBe("123");
expect(escapeHtml(0)).toBe("0");
expect(escapeHtml(-456)).toBe("-456");
});
test("should convert booleans to strings", () => {
expect(escapeHtml(true)).toBe("true");
expect(escapeHtml(false)).toBe("false");
});
test("should handle NaN", () => {
expect(escapeHtml(NaN)).toBe("NaN");
});
test("should handle Infinity", () => {
expect(escapeHtml(Infinity)).toBe("Infinity");
expect(escapeHtml(-Infinity)).toBe("-Infinity");
});
test("should convert objects to strings", () => {
expect(escapeHtml({})).toBe("[object Object]");
expect(escapeHtml({ key: "value" })).toBe("[object Object]");
});
test("should convert arrays to strings", () => {
expect(escapeHtml([1, 2, 3])).toBe("1,2,3");
expect(escapeHtml(["<", ">"])).toBe("<,>");
});
});
describe("Edge Cases", () => {
test("should handle Unicode characters", () => {
expect(escapeHtml("Hello ไธ็ ๐")).toBe("Hello ไธ็ ๐");
});
});
});