import { renderToString, renderToBdom, snapshotEverything, TestContext, makeTestFixture, } from "../helpers"; import { mount, patch } from "../../src/runtime/blockdom"; import { createBlock } from "../../src/runtime/blockdom/index"; import { markup } from "../../src/runtime/utils"; snapshotEverything(); // ----------------------------------------------------------------------------- // t-out // ----------------------------------------------------------------------------- describe("t-out", () => { let fixture: HTMLElement; beforeEach(() => { fixture = makeTestFixture(); }); test("literal", () => { const template = ``; expect(renderToString(template)).toBe("ok"); }); test("number literal", () => { const template = ``; expect(renderToString(template)).toBe("1"); }); test("literal, no outside html element", () => { const template = ``; expect(renderToString(template)).toBe("ok"); }); test("variable", () => { const template = ``; expect(renderToString(template, { var: "ok" })).toBe("ok"); }); test("with a String class", () => { const template = ``; expect(renderToString(template, { var: new String("ok") })).toBe("ok"); }); test("t-out with the 0 number", () => { const template = ``; expect(renderToString(template, { var: 0 })).toBe("0"); }); test("t-out with the 0 number, in a p", () => { const template = `

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

0

"); }); test("top level t-out with undefined", () => { const template = ``; expect(renderToString(template, { var: undefined })).toBe(""); }); test("top level t-out with null", () => { const template = ``; expect(renderToString(template, { var: null })).toBe(""); }); test("with an extended String class", () => { class LoveString extends String { valueOf(): string { return `<3 ${super.valueOf()} <3`; } toString(): string { return this.valueOf(); } } const template = ``; expect(renderToString(template, { var: new LoveString("ok") })).toBe( "<3 ok <3" ); }); test("not escaping", () => { const template = `
`; expect(renderToString(template, { var: markup("") })).toBe("
"); }); test("t-out and another sibling node", () => { const template = `hello`; expect(renderToString(template, { var: markup("world") })).toBe( "helloworld" ); }); test("t-out with comment", () => { const template = ``; expect(renderToString(template, { var: markup("

text

") })).toBe( "

text

" ); }); test("t-out on a node with a body, as a default", () => { const template = `nope`; expect(renderToString(template)).toBe("nope"); }); test("t-out with a in body", () => { const template = ``; expect(renderToString(template, { var: "coucou" })).toBe("coucou"); }); test("t-out with just a t-set t-value in body", () => { const template = ``; expect(renderToString(template, { var: "coucou" })).toBe("coucou"); }); test("t-out on a node with a dom node in body, as a default", () => { const template = `
nope
`; expect(renderToString(template)).toBe("
nope
"); }); test("multiple calls to t-out", () => { const context = new TestContext(); const sub = `
Greeter
`; const main = `
coucou
`; context.addTemplate("sub", sub); context.addTemplate("main", main); const expected = "
coucou
Greeter
coucou
"; expect(context.renderToString("main")).toBe(expected); }); test("t-out escaped", () => { const template = ``; expect(renderToString(template, { var: "
nope
" })).toBe( "<div>nope</div>" ); }); test("t-out markedup", () => { const template = ``; expect(renderToString(template, { var: markup("
nope
") })).toBe( "
nope
" ); }); test("t-out switch escaped", () => { const template = ``; const node = renderToBdom(template, { var: "
nope
" }); mount(node, fixture); expect(fixture.innerHTML).toBe("<div>nope</div>"); patch(node, renderToBdom(template, { var: "
  • yep
  • " })); expect(fixture.innerHTML).toBe("<li>yep</li>"); }); test("t-out switch markup", () => { const template = ``; const node = renderToBdom(template, { var: markup("
    nope
    ") }); mount(node, fixture); expect(fixture.innerHTML).toBe("
    nope
    "); patch(node, renderToBdom(template, { var: markup("
  • yep
  • ") })); expect(fixture.innerHTML).toBe("
  • yep
  • "); }); test("t-out switch markup on escaped", () => { const template = ``; const node = renderToBdom(template, { var: "
    nope
    " }); mount(node, fixture); expect(fixture.innerHTML).toBe("<div>nope</div>"); patch(node, renderToBdom(template, { var: markup("
  • yep
  • ") })); expect(fixture.innerHTML).toBe("
  • yep
  • "); }); test("t-out switch escaped on markup", () => { const template = ``; const node = renderToBdom(template, { var: markup("
    nope
    ") }); mount(node, fixture); expect(fixture.innerHTML).toBe("
    nope
    "); patch(node, renderToBdom(template, { var: "
  • yep
  • " })); expect(fixture.innerHTML).toBe("<li>yep</li>"); }); test("t-out block", () => { const block = createBlock("
    block
    "); const template = ``; expect(renderToString(template, { bdom: block() })).toBe("
    block
    "); }); test("t-out bdom", () => { const template = `
      set
    `; expect(renderToString(template)).toBe("
      set
    "); }); test("t-out switch markup on bdom", () => { const template = `
      set
    `; const node = renderToBdom(template, { hasBdom: true, var: markup("
  • yep
  • ") }); mount(node, fixture); expect(fixture.innerHTML).toBe("
      set
    "); patch(node, renderToBdom(template, { hasBdom: false, var: markup("
  • yep
  • ") })); expect(fixture.innerHTML).toBe("
  • yep
  • "); }); test("t-out 0", () => { const context = new TestContext(); context.addTemplate("_basic-callee", `
    `); context.addTemplate("caller", `
    zero
    `); const node = context.getTemplate("caller")({}, null, ""); mount(node, fixture); expect(fixture.innerHTML).toBe("
    zero
    "); }); test("t-out with arbitrary object", () => { const template = `
    `; const node = renderToBdom(template, { var: { someKey: "someValue" } }); expect(() => mount(node, fixture)).toThrow(); }); test("t-out with arbitrary object 2", () => { const template = `
    `; const node = renderToBdom(template, { var: ["someValue"] }); expect(() => mount(node, fixture)).toThrow(); }); }); describe("t-raw is deprecated", () => { test("should warn", () => { const template = `
    `; const warn = console.warn; const steps: string[] = []; console.warn = (msg: any) => steps.push(msg); expect(renderToString(template, { var: "
    escaped
    " })).toBe( "
    <div>escaped</div>
    " ); expect(steps).toEqual([ 't-raw has been deprecated in favor of t-out. If the value to render is not wrapped by the "markup" function, it will be escaped', ]); console.warn = warn; }); test("t-out is actually called in t-raw's place", () => { const template = `
    `; const warn = console.warn; console.warn = (msg: any) => msg; expect(renderToString(template, { var: markup("
    raw
    ") })).toBe( "
    raw
    " ); console.warn = warn; }); });