From 45ea6a195e223991c1b22658b9f79998b9004de3 Mon Sep 17 00:00:00 2001 From: Bruno Boi Date: Mon, 31 Jan 2022 14:38:00 +0100 Subject: [PATCH] [FIX] hooks: useSubEnv will not erase previous useChildSubEnv --- src/hooks.ts | 3 +- .../__snapshots__/hooks.test.ts.snap | 31 +++++++++++++++++++ tests/components/hooks.test.ts | 18 +++++++++++ 3 files changed, 50 insertions(+), 2 deletions(-) diff --git a/src/hooks.ts b/src/hooks.ts index ff259cc8..3eb865b7 100644 --- a/src/hooks.ts +++ b/src/hooks.ts @@ -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) { diff --git a/tests/components/__snapshots__/hooks.test.ts.snap b/tests/components/__snapshots__/hooks.test.ts.snap index d5edf3cb..2f610f5c 100644 --- a/tests/components/__snapshots__/hooks.test.ts.snap +++ b/tests/components/__snapshots__/hooks.test.ts.snap @@ -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(\`
\`); + + 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 ) { diff --git a/tests/components/hooks.test.ts b/tests/components/hooks.test.ts index fc08ba42..23515c7b 100644 --- a/tests/components/hooks.test.ts +++ b/tests/components/hooks.test.ts @@ -322,6 +322,24 @@ describe("hooks", () => { expect(fixture.innerHTML).toBe("3
5
"); }); + test("parent and child env (with useChildSubEnv then useSubEnv)", async () => { + class Child extends Component { + static template = xml`
`; + } + + class Parent extends Component { + static template = xml``; + static components = { Child }; + setup() { + useChildSubEnv({ hasParent: true }); + useSubEnv({ val: 5 }); + } + } + const env = { val: 3 }; + await mount(Parent, fixture, { env }); + expect(fixture.innerHTML).toBe("5
5
"); + }); + test("can use onWillStart, onWillUpdateProps", async () => { const steps: string[] = []; async function slow(): Promise {