[FIX] hooks: useSubEnv supports arbitrary descriptors in env

Before this commit, when defining a getter in the env passed to useSubEnv,
the value was read, losing the definition of the property.

After this commit, declaring a getter in the env works as expected:
the property stays a getter.
This commit is contained in:
Lucas Perais (lpe)
2021-12-02 13:28:53 +01:00
committed by Géry Debongnie
parent 9d48bda227
commit 144d4a253c
3 changed files with 63 additions and 1 deletions
@@ -158,6 +158,32 @@ exports[`hooks use sub env does not pollute user env 1`] = `
}"
`;
exports[`hooks use sub env supports arbitrary descriptor 1`] = `
"function anonymous(bdom, helpers
) {
let { text, createBlock, list, multi, html, toggler, component, comment } = bdom;
let block1 = createBlock(\`<div><block-text-0/> <block-text-1/></div>\`);
return function template(ctx, node, key = \\"\\") {
let d1 = ctx['env'].someVal;
let d2 = ctx['env'].someVal2;
return block1([d1, d2]);
}
}"
`;
exports[`hooks use sub env supports arbitrary descriptor 2`] = `
"function anonymous(bdom, helpers
) {
let { text, createBlock, list, multi, html, toggler, component, comment } = bdom;
return function template(ctx, node, key = \\"\\") {
return component(\`Child\`, {}, key + \`__1\`, node, ctx);
}
}"
`;
exports[`hooks useEffect hook dependencies prevent effects from rerunning when unchanged 1`] = `
"function anonymous(bdom, helpers
) {
+34
View File
@@ -194,6 +194,40 @@ describe("hooks", () => {
expect(component.env).toHaveProperty("val");
});
test("use sub env supports arbitrary descriptor", async () => {
let someVal = "maggot";
let someVal2 = "brain";
class Child extends Component {
static template = xml`<div><t t-esc="env.someVal" /> <t t-esc="env.someVal2" /></div>`;
}
class Test extends Component {
static template = xml`<Child />`;
static components = { Child };
setup() {
useSubEnv({
get someVal2() {
return someVal2;
},
});
}
}
someVal = "maggot";
const env = {
get someVal() {
return someVal;
},
};
const component = await new App(Test).configure({ env }).mount(fixture);
expect(fixture.innerHTML).toBe("<div>maggot brain</div>");
someVal = "brain";
someVal2 = "maggot";
component.render();
await nextTick();
expect(fixture.innerHTML).toBe("<div>brain maggot</div>");
});
test("can use useComponent", async () => {
expect.assertions(2);
class Test extends Component {