import { describe, expect, test } from "@odoo/hoot";
import { patchWithCleanup } from "@web/../tests/web_test_helpers";
import { createElement } from "@web/core/utils/xml";
import { FormCompiler } from "@web/views/form/form_compiler";
describe.current.tags("headless");
function compileTemplate(arch) {
const parser = new DOMParser();
const xml = parser.parseFromString(arch, "text/xml");
const compiler = new FormCompiler({ form: xml.documentElement });
return compiler.compile("form");
}
test("properly compile simple div", () => {
const arch = /*xml*/ `
`;
const expected = /*xml*/ `
`;
expect(compileTemplate(arch)).toHaveOuterHTML(expected);
});
test("label with empty string compiles to FormLabel with empty string", () => {
const arch = /*xml*/ ``;
const expected = /*xml*/ `
`;
expect(compileTemplate(arch)).toHaveOuterHTML(expected);
});
test("properly compile simple div with field", () => {
const arch = /*xml*/ ``;
const expected = /*xml*/ `
`;
expect(compileTemplate(arch)).toHaveOuterHTML(expected);
});
test("properly compile inner groups", () => {
const arch = /*xml*/ `
`;
const expected = /*xml*/ `
`;
expect(compileTemplate(arch)).toHaveOuterHTML(expected);
});
test("properly compile attributes with nested forms", () => {
const arch = /*xml*/ `
`;
const expected = /*xml*/ `
`;
expect(compileTemplate(arch)).toHaveOuterHTML(expected);
});
test("properly compile notebook", () => {
const arch = /*xml*/ `
`;
const expected = /*xml*/ `
`;
expect(compileTemplate(arch)).toHaveOuterHTML(expected);
});
test("properly compile field without placeholder", () => {
const arch = /*xml*/ `
`;
const expected = /*xml*/ `
`;
expect(compileTemplate(arch)).toHaveOuterHTML(expected);
});
test("properly compile no sheet", () => {
const arch = /*xml*/ `
someDiv
`;
const expected = /*xml*/ `
`;
expect(compileTemplate(arch)).toHaveOuterHTML(expected);
});
test("properly compile sheet", () => {
const arch = /*xml*/ `
someDiv
inside sheet
after sheet
`;
const expected = /*xml*/ `
`;
expect(compileTemplate(arch)).toHaveOuterHTML(expected);
});
test("properly compile buttonBox invisible in sheet", () => {
const arch = /*xml*/ `
`;
const expected = /*xml*/ `
`;
expect(compileTemplate(arch)).toHaveOuterHTML(expected);
});
test("properly compile invisible", () => {
// cf python side: def transfer_node_to_modifiers
// modifiers' string are evaluated to their boolean or array form
// So the following arch may actually be written as:
// ```
//
//
//
// ````
const arch = /*xml*/ `
`;
const expected = /*xml*/ `
`;
expect(compileTemplate(arch)).toHaveOuterHTML(expected);
});
test("compile invisible containing string as domain", () => {
const arch = /*xml*/ `
`;
const expected = /*xml*/ `
`;
expect(compileTemplate(arch)).toHaveOuterHTML(expected);
});
test("properly compile status bar with content", () => {
const arch = /*xml*/ `
`;
const expected = /*xml*/ `
`;
expect(compileTemplate(arch)).toHaveOuterHTML(expected);
});
test("properly compile status bar without content", () => {
const arch = /*xml*/ `
`;
const expected = /*xml*/ `
`;
expect(compileTemplate(arch)).toHaveOuterHTML(expected);
});
test("properly compile settings", () => {
const arch = /*xml*/ `
label with content
`;
const expected = /*xml*/ `
label with content
`;
expect(compileTemplate(arch)).toHaveOuterHTML(expected);
});
test("properly compile empty ButtonBox", () => {
const arch = /*xml*/ `
`;
const expected = /*xml*/ `
`;
expect(compileTemplate(arch)).toHaveOuterHTML(expected);
});
test("invisible is correctly computed with another t-if", () => {
patchWithCleanup(FormCompiler.prototype, {
setup() {
super.setup();
this.compilers.push({
selector: "myNode",
fn: () => {
const div = createElement("div");
div.className = "myNode";
div.setAttribute("t-if", "myCondition or myOtherCondition");
return div;
},
});
},
});
const arch = ` `;
const expected = `
`;
expect(compileTemplate(arch)).toHaveOuterHTML(expected);
});
test("keep nosheet style if a sheet is part of a nested form", () => {
const arch = `
`;
const expected = `
`;
expect(compileTemplate(arch)).toHaveOuterHTML(expected);
});
test("form with t-translation directive", () => {
patchWithCleanup(console, { warn: (message) => expect.step(message) });
const arch = `
Hello
`;
const expected = `
`;
expect(compileTemplate(arch)).toHaveOuterHTML(expected);
expect.verifySteps([]); // should no log any warning
});