2022-05-26 23:45:40 +02:00
|
|
|
import { TemplateSet } from "../../src/runtime/template_set";
|
2021-12-20 11:19:59 +01:00
|
|
|
import { renderToString, snapshotTemplate, TestContext } from "../helpers";
|
2020-11-26 16:45:25 +01:00
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
// basic validation
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
describe("basic validation", () => {
|
|
|
|
|
test("error if no template with given name", () => {
|
|
|
|
|
const context = new TemplateSet();
|
|
|
|
|
expect(() => context.getTemplate("invalidname")).toThrow("Missing template");
|
|
|
|
|
});
|
|
|
|
|
|
2023-08-25 09:03:39 +02:00
|
|
|
test("cannot add a different template with the same name in dev mode", () => {
|
|
|
|
|
const context = new TemplateSet({ dev: true });
|
2022-04-27 10:27:57 +02:00
|
|
|
context.addTemplate("test", `<t/>`);
|
|
|
|
|
// Same template with the same name is fine
|
|
|
|
|
expect(() => context.addTemplate("test", "<t/>")).not.toThrow();
|
|
|
|
|
// Different template with the same name crashes
|
2020-11-26 16:45:25 +01:00
|
|
|
expect(() => context.addTemplate("test", "<div/>")).toThrow("already defined");
|
|
|
|
|
});
|
|
|
|
|
|
2023-08-25 09:03:39 +02:00
|
|
|
test("adding different template with same name outside dev mode silently ignores it", () => {
|
|
|
|
|
const context = new TemplateSet({ dev: false });
|
|
|
|
|
context.addTemplate("test", `<t/>`);
|
|
|
|
|
expect(() => context.addTemplate("test", "<div/>")).not.toThrow();
|
|
|
|
|
expect(context.rawTemplates.test).toBe("<t/>");
|
|
|
|
|
});
|
|
|
|
|
|
2020-11-26 16:45:25 +01:00
|
|
|
test("invalid xml", () => {
|
|
|
|
|
const template = "<div>";
|
2021-12-20 11:19:59 +01:00
|
|
|
expect(() => snapshotTemplate(template)).toThrow("Invalid XML in template");
|
2020-11-26 16:45:25 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test("missing template in template set", () => {
|
|
|
|
|
const context = new TestContext();
|
|
|
|
|
const template = `<t t-call="othertemplate" />`;
|
|
|
|
|
|
|
|
|
|
context.addTemplate("template", template);
|
|
|
|
|
expect(() => context.renderToString("template")).toThrowError("Missing");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test("error when unknown directive", () => {
|
|
|
|
|
const template = `<div t-best-beer="rochefort 10">test</div>`;
|
|
|
|
|
expect(() => renderToString(template)).toThrow("Unknown QWeb directive: 't-best-beer'");
|
|
|
|
|
});
|
2023-07-20 09:38:40 +02:00
|
|
|
|
|
|
|
|
test("compilation error", () => {
|
|
|
|
|
const template = `<div t-att-class="a b">test</div>`;
|
|
|
|
|
expect(() => renderToString(template))
|
|
|
|
|
.toThrow(`Failed to compile anonymous template: Unexpected identifier
|
|
|
|
|
|
|
|
|
|
generated code:
|
|
|
|
|
function(app, bdom, helpers) {
|
|
|
|
|
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
|
|
|
|
|
|
|
|
|
|
let block1 = createBlock(\`<div block-attribute-0="class">test</div>\`);
|
|
|
|
|
|
|
|
|
|
return function template(ctx, node, key = "") {
|
|
|
|
|
let attr1 = ctx['a']ctx['b'];
|
|
|
|
|
return block1([attr1]);
|
|
|
|
|
}
|
|
|
|
|
}`);
|
|
|
|
|
});
|
2020-11-26 16:45:25 +01:00
|
|
|
});
|