[FIX] hooks: useSubEnv will not erase previous useChildSubEnv

This commit is contained in:
Bruno Boi
2022-01-31 14:38:00 +01:00
committed by Aaron Bohy
parent 722abd6d5f
commit 4d68dac24d
3 changed files with 50 additions and 2 deletions
+1 -2
View File
@@ -45,9 +45,8 @@ function extendEnv(currentEnv: Object, extension: Object): Object {
*/
export function useSubEnv(envExtension: Env) {
const node = getCurrent()!;
const newEnv = extendEnv(node.component.env as any, envExtension);
node.component.env = extendEnv(node.component.env as any, envExtension);
node.childEnv = newEnv;
useChildSubEnv(envExtension);
}
export function useChildSubEnv(envExtension: Env) {
@@ -103,6 +103,37 @@ exports[`hooks mounted callbacks should be called in reverse order from willUnmo
}"
`;
exports[`hooks parent and child env (with useChildSubEnv then useSubEnv) 1`] = `
"function anonymous(bdom, helpers
) {
let { text, createBlock, list, multi, html, toggler, component, comment } = bdom;
return function template(ctx, node, key = \\"\\") {
let b2 = text(ctx['env'].val);
let b3 = component(\`Child\`, {}, key + \`__1\`, node, ctx);
return multi([b2, b3]);
}
}"
`;
exports[`hooks parent and child env (with useChildSubEnv then useSubEnv) 2`] = `
"function anonymous(bdom, helpers
) {
let { text, createBlock, list, multi, html, toggler, component, comment } = bdom;
let block2 = createBlock(\`<div><block-text-0/></div>\`);
return function template(ctx, node, key = \\"\\") {
let b2;
if (ctx['env'].hasParent) {
let txt1 = ctx['env'].val;
b2 = block2([txt1]);
}
return multi([b2]);
}
}"
`;
exports[`hooks parent and child env (with useChildSubEnv) 1`] = `
"function anonymous(bdom, helpers
) {
+18
View File
@@ -322,6 +322,24 @@ describe("hooks", () => {
expect(fixture.innerHTML).toBe("3<div>5</div>");
});
test("parent and child env (with useChildSubEnv then useSubEnv)", async () => {
class Child extends Component {
static template = xml`<div t-if="env.hasParent"><t t-esc="env.val"/></div>`;
}
class Parent extends Component {
static template = xml`<t t-esc="env.val"/><Child/>`;
static components = { Child };
setup() {
useChildSubEnv({ hasParent: true });
useSubEnv({ val: 5 });
}
}
const env = { val: 3 };
await mount(Parent, fixture, { env });
expect(fixture.innerHTML).toBe("5<div>5</div>");
});
test("can use onWillStart, onWillUpdateProps", async () => {
const steps: string[] = [];
async function slow(): Promise<string> {