import { renderToString, snapshotEverything, TestContext } from "../helpers"; snapshotEverything(); // ----------------------------------------------------------------------------- // t-set // ----------------------------------------------------------------------------- describe("t-set", () => { test("set from attribute literal", () => { const template = `
`; expect(renderToString(template)).toBe("
ok
"); }); test("t-set does not modify render context existing key values", () => { const template = `
`; const ctx = { value: 17 }; expect(renderToString(template, ctx)).toBe("
35
"); expect(ctx.value).toBe(17); }); test("set from attribute literal (no outside div)", () => { const template = ``; expect(renderToString(template)).toBe("ok"); }); test("t-set and t-if", () => { const template = `
grimbergen
`; expect(renderToString(template, { value: "ok" })).toBe("
grimbergen
"); }); test("t-set, multiple t-ifs, and a specific configuration", () => { const template = `

First div
Second

`; expect(renderToString(template)).toBe( "

First div
Second

" ); }); test("set from body literal", () => { const template = `ok`; expect(renderToString(template)).toBe("ok"); }); test("body with backslash at top level", () => { const template = '\\'; expect(renderToString(template)).toBe("\\"); }); test("body with backtick at top-level", () => { const template = '`'; expect(renderToString(template)).toBe("`"); }); test("body with interpolation sigil at top level", () => { const template = '${very cool}'; expect(renderToString(template)).toBe("${very cool}"); }); test("set from body literal (with t-if/t-else", () => { const template = ` true false `; expect(renderToString(template, { condition: true })).toBe("true"); expect(renderToString(template, { condition: false })).toBe("false"); }); test("set from attribute lookup", () => { const template = `
`; expect(renderToString(template, { value: "ok" })).toBe("
ok
"); }); test("t-set evaluates an expression only once", () => { const template = `
`; expect(renderToString(template, { value: "stella" })).toBe( "
stella artoisstella artois
" ); }); test("set from body lookup", () => { const template = `
`; expect(renderToString(template, { value: "ok" })).toBe("
ok
"); }); test("set from empty body", () => { const template = `
`; expect(renderToString(template)).toBe("
"); }); test("value priority", () => { const template = `
2
`; expect(renderToString(template)).toBe("
1
"); }); test("value priority (with non text body", () => { const template = `
2
`; expect(renderToString(template)).toBe("
1
"); }); test("evaluate value expression", () => { const template = `
`; expect(renderToString(template)).toBe("
3
"); }); test("t-set should reuse variable if possible", () => { const template = `
v
`; const expected = "
v1
va
"; expect(renderToString(template, { list: ["a", "b"] })).toBe(expected); }); test("t-set with content and sub t-esc", () => { const template = `
boop
`; expect(renderToString(template, { beep: "beep" })).toBe("
beep boop
"); }); test("evaluate value expression, part 2", () => { const template = `
`; expect(renderToString(template, { somevariable: 43 })).toBe("
45
"); }); test("t-set, t-if, and mix of expression/body lookup, 1", () => { const template = `
1
`; expect(renderToString(template, { flag: true })).toBe("
1
"); expect(renderToString(template, { flag: false })).toBe("
0
"); }); test("t-set, t-if, and mix of expression/body lookup, 2", () => { const template = `
0
`; expect(renderToString(template, { flag: true })).toBe("
1
"); expect(renderToString(template, { flag: false })).toBe("
0
"); }); test("t-set, t-if, and mix of expression/body lookup, 3", () => { const template = ` 0 `; expect(renderToString(template, { flag: true })).toBe("1"); expect(renderToString(template, { flag: false })).toBe("0"); }); test("t-set body is evaluated immediately", () => { const template = `
`; expect(renderToString(template)).toBe("
before
"); }); test("t-set with t-value (falsy) and body", () => { const template = `
`; expect(renderToString(template)).toBe("
before
"); }); test("t-set with t-value (truthy) and body", () => { const template = `
`; expect(renderToString(template)).toBe("
Truthy
"); }); test("t-set outside modified in t-foreach", async () => { const template = `

InLoop:

EndLoop:

`; expect(renderToString(template)).toBe( "

InLoop: 0

InLoop: 1

EndLoop: 2

" ); }); test("t-set outside modified in t-foreach increment-after operator", async () => { const template = `

InLoop:

EndLoop:

`; expect(renderToString(template)).toBe( "

InLoop: 0

InLoop: 0

EndLoop: 0

" ); }); test("t-set outside modified in t-foreach increment-before operator", async () => { const template = `

InLoop:

EndLoop:

`; expect(renderToString(template)).toBe( "

InLoop: 0

InLoop: 1

EndLoop: 0

" ); }); test("t-set can't alter from within callee", async () => { const context = new TestContext(); const sub = `
`; const main = `

`; context.addTemplate("sub", sub); context.addTemplate("main", main); expect(context.renderToString("main")).toBe( "

source

sourcecalled

source

" ); }); test("t-set can't alter in t-call body", async () => { const context = new TestContext(); const sub = `
`; const main = `

`; context.addTemplate("sub", sub); context.addTemplate("main", main); expect(context.renderToString("main")).toBe( "

source

inCallcalled

source

" ); }); });