import { mount } from "../../src/runtime/blockdom"; import { makeTestFixture, renderToBdom, renderToString, snapshotEverything, TestContext, } from "../helpers"; snapshotEverything(); // ----------------------------------------------------------------------------- // t-esc // ----------------------------------------------------------------------------- describe("t-esc", () => { test("literal", () => { const template = ``; expect(renderToString(template)).toBe("ok"); }); test("variable", () => { const template = ``; expect(renderToString(template, { var: "ok" })).toBe("ok"); }); test("escaping", () => { const template = ``; expect(renderToString(template, { var: "abc" })).toBe( "<ok>abc</ok>" ); }); test("escaping on a node", () => { const template = ``; expect(renderToString(template)).toBe("ok"); }); test("escaping on a node with a body", () => { const template = `nope`; expect(renderToString(template)).toBe("ok"); }); test("escaping on a node with a body, as a default", () => { const template = `nope`; expect(renderToString(template)).toBe("nope"); }); test("div with falsy values", () => { const template = `

`; const vals = { v1: false, v2: undefined, v3: null, v4: 0, v5: "", }; expect(renderToString(template, vals)).toBe( "

false

0

" ); }); test("t-esc with the 0 number", () => { const template = ``; expect(renderToString(template, { var: 0 })).toBe("0"); }); test("t-esc with the 0 number, in a p", () => { const template = `

`; expect(renderToString(template, { var: 0 })).toBe("

0

"); }); test("top level t-esc with undefined", () => { const template = ``; expect(renderToString(template, { var: undefined })).toBe(""); }); test("falsy values in text nodes", () => { const template = ` ::::`; const vals = { v1: false, v2: undefined, v3: null, v4: 0, v5: "", }; expect(renderToString(template, vals)).toBe("false:::0:"); }); test("t-esc work with spread operator", () => { const template = ``; expect(renderToString(template, { state: { list: [1, 2] } })).toBe("1,2"); }); test("t-esc is escaped", () => { const template = `

escaped

`; const bdom = renderToBdom(template); const fixture = makeTestFixture(); mount(bdom, fixture); expect(fixture.textContent).toBe("

escaped

"); }); test("t-esc=0 is escaped", () => { const context = new TestContext(); const sub = ''; const main = `

escaped

`; context.addTemplate("sub", sub); context.addTemplate("main", main); const bdom = context.getTemplate("main")({}, {}); const fixture = makeTestFixture(); mount(bdom, fixture); expect(fixture.querySelector("span")!.textContent).toBe("

escaped

"); }); });