mirror of
https://github.com/odoo/owl.git
synced 2025-10-06 19:59:41 +07:00
[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:
committed by
Aaron Bohy
parent
b66d5231d3
commit
463eb4bb86
+3
-1
@@ -38,7 +38,9 @@ export function useEnv<E extends Env>(): E {
|
|||||||
*/
|
*/
|
||||||
export function useSubEnv(envExtension: Env) {
|
export function useSubEnv(envExtension: Env) {
|
||||||
const node = getCurrent()!;
|
const node = getCurrent()!;
|
||||||
node.childEnv = Object.freeze(Object.assign({}, node.childEnv, envExtension));
|
const env = Object.create(node.childEnv);
|
||||||
|
const descrs = Object.getOwnPropertyDescriptors(envExtension);
|
||||||
|
node.childEnv = Object.freeze(Object.defineProperties(env, descrs));
|
||||||
}
|
}
|
||||||
|
|
||||||
// -----------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------
|
||||||
|
|||||||
@@ -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`] = `
|
exports[`hooks useEffect hook dependencies prevent effects from rerunning when unchanged 1`] = `
|
||||||
"function anonymous(bdom, helpers
|
"function anonymous(bdom, helpers
|
||||||
) {
|
) {
|
||||||
|
|||||||
@@ -194,6 +194,40 @@ describe("hooks", () => {
|
|||||||
expect(component.env).toHaveProperty("val");
|
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 () => {
|
test("can use useComponent", async () => {
|
||||||
expect.assertions(2);
|
expect.assertions(2);
|
||||||
class Test extends Component {
|
class Test extends Component {
|
||||||
|
|||||||
Reference in New Issue
Block a user