import { renderToString, snapshotEverything, TestContext } from "../helpers"; snapshotEverything(); // ----------------------------------------------------------------------------- // t-foreach // ----------------------------------------------------------------------------- describe("t-foreach", () => { test("simple iteration", () => { const template = ``; expect(renderToString(template)).toBe("321"); }); test("simple iteration with two nodes inside", () => { const template = ` a b `; const expected = "a3b3a2b2a1b1"; expect(renderToString(template)).toBe(expected); }); test.skip("t-key on t-foreach", async () => { // qweb.addTemplate( // "test", // ` //
// // // //
` // ); // let vnode = qweb.render("test", { things: [1, 2] }); // vnode = patch(document.createElement("div"), vnode); // let elm = vnode.elm as HTMLElement; // expect(elm.outerHTML).toBe("
"); // const first = elm.querySelectorAll("span")[0]; // const second = elm.querySelectorAll("span")[1]; // patch(vnode, qweb.render("test", { things: [2, 1] })); // expect(elm.outerHTML).toBe("
"); // expect(first).toBe(elm.querySelectorAll("span")[1]); // expect(second).toBe(elm.querySelectorAll("span")[0]); }); test("simple iteration (in a node)", () => { const template = `
`; expect(renderToString(template)).toBe("
321
"); }); test("iterate on items", () => { const template = `
[: ]
`; expect(renderToString(template)).toBe("
[0: 3 3] [1: 2 2] [2: 1 1]
"); }); test("iterate on items (on a element node)", () => { const template = `
`; const expected = `
12
`; expect(renderToString(template)).toBe(expected); }); test("iterate, position", () => { const template = `
- first last ()
`; const expected = `
- first (0) - (1) - (2) - (3) - last (4)
`; expect(renderToString(template)).toBe(expected); }); test("iterate, dict param", () => { const template = `
[: ]
`; const expected = `
[0: a 1] [1: b 2] [2: c 3]
`; const context = { value: { a: 1, b: 2, c: 3 } }; expect(renderToString(template, context)).toBe(expected); }); test("does not pollute the rendering context", () => { const template = `
`; const context = { __owl__: {} }; renderToString(template, context); expect(Object.keys(context)).toEqual(["__owl__"]); }); test("t-foreach in t-foreach", () => { const template = `
[]
`; const context = { numbers: [1, 2, 3], letters: ["a", "b"] }; const expected = "
[1a] [1b] [2a] [2b] [3a] [3b]
"; expect(renderToString(template, context)).toBe(expected); }); test("t-call without body in t-foreach in t-foreach", () => { const context = new TestContext(); const sub = ` [] [] [] `; const main = `
[][][]
`; context.addTemplate("sub", sub); context.addTemplate("main", main); const ctx = { numbers: [1, 2, 3], letters: ["a", "b"] }; const expected = "
[1] [a] [x_1_a] [1] [b] [x_1_b] [2] [a] [x_2_a] [2] [b] [x_2_b] [3] [a] [x_3_a] [3] [b] [x_3_b] [][][]
"; expect(context.renderToString("main", ctx)).toBe(expected); }); test("t-call with body in t-foreach in t-foreach", () => { const context = new TestContext(); const sub = ` [] [] [] `; const main = `
[][][]
`; context.addTemplate("sub", sub); context.addTemplate("main", main); const ctx = { numbers: [1, 2, 3], letters: ["a", "b"] }; const expected = "
[1] [a] [x_1_a] [1] [b] [x_1_b] [2] [a] [x_2_a] [2] [b] [x_2_b] [3] [a] [x_3_a] [3] [b] [x_3_b] [][][]
"; expect(context.renderToString("main", ctx)).toBe(expected); }); test("throws error if invalid loop expression", () => { const test = `
`; expect(() => renderToString(test)).toThrow("Invalid loop expression"); }); test("warn if no key in some case", () => { const consoleWarn = console.warn; console.warn = jest.fn(); const template = `
`; renderToString(template); expect(console.warn).toHaveBeenCalledTimes(1); expect(console.warn).toHaveBeenCalledWith( `\"Directive t-foreach should always be used with a t-key! (in template: '
')\"` ); console.warn = consoleWarn; }); test("t-foreach with t-if inside", () => { const template = `
`; const ctx = { elems: [ { id: 1, text: "a" }, { id: 2, text: "b" }, { id: 3, text: "c" }, ], }; expect(renderToString(template, ctx)).toBe("
ab
"); }); test("t-foreach with t-if inside (no external node)", () => { const template = ` `; const ctx = { elems: [ { id: 1, text: "a" }, { id: 2, text: "b" }, { id: 3, text: "c" }, ], }; expect(renderToString(template, ctx)).toBe("ab"); }); test("with t-memo", () => { const items = [ { id: 1, x: 1, y: 1 }, { id: 2, x: 1, y: 1 }, ]; const template = `

`; const expected = `

11

11

`; expect(renderToString(template, { items })).toBe(expected); }); });