From d08ea63565e0eaade1988b410dca080edf99e995 Mon Sep 17 00:00:00 2001 From: "Michael Mattiello (mcm)" Date: Fri, 2 Jul 2021 11:00:30 +0200 Subject: [PATCH] [IMP] directive: make props override t-props Before this commit, props and t-props were computed like `Object.assign(props, t_props)`. Now, it computes like `Object.assign(t_props, props)` so props will override t-props. closes https://github.com/odoo/owl/issues/886 --- src/component/directive.ts | 2 +- .../__snapshots__/component.test.ts.snap | 2 +- tests/component/component.test.ts | 24 +++++++++++++++++++ .../route_component.test.ts.snap | 2 +- 4 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/component/directive.ts b/src/component/directive.ts index 3712dc0b..23c349ea 100644 --- a/src/component/directive.ts +++ b/src/component/directive.ts @@ -336,7 +336,7 @@ QWeb.addDirective({ } if (hasDynamicProps) { const dynamicProp = ctx.formatExpression(node.getAttribute("t-props")!); - ctx.addLine(`let props${componentID} = Object.assign({${propStr}}, ${dynamicProp});`); + ctx.addLine(`let props${componentID} = Object.assign({}, ${dynamicProp}, {${propStr}});`); } else { ctx.addLine(`let props${componentID} = {${propStr}};`); } diff --git a/tests/component/__snapshots__/component.test.ts.snap b/tests/component/__snapshots__/component.test.ts.snap index 17760c9e..267d4f16 100644 --- a/tests/component/__snapshots__/component.test.ts.snap +++ b/tests/component/__snapshots__/component.test.ts.snap @@ -467,7 +467,7 @@ exports[`dynamic t-props basic use 1`] = ` let vn1 = h('div', p1, c1); // Component 'Child' let w2 = '__3__' in parent.__owl__.cmap ? parent.__owl__.children[parent.__owl__.cmap['__3__']] : false; - let props2 = Object.assign({}, scope['some'].obj); + let props2 = Object.assign({}, scope['some'].obj, {}); if (w2 && w2.__owl__.currentFiber && !w2.__owl__.vnode) { w2.destroy(); w2 = false; diff --git a/tests/component/component.test.ts b/tests/component/component.test.ts index 1610541e..bc5a34f4 100644 --- a/tests/component/component.test.ts +++ b/tests/component/component.test.ts @@ -4040,6 +4040,30 @@ describe("dynamic t-props", () => { expect(fixture.innerHTML).toBe("
3
"); expect(env.qweb.templates[Parent.template].fn.toString()).toMatchSnapshot(); }); + + test("t-props with props", async () => { + expect.assertions(1); + + class Child extends Component { + static template = xml`
`; + setup() { + expect(this.props).toEqual({ a: 1, b: 2, c: "c" }); + } + } + class Parent extends Component { + static template = xml` +
+ +
+ `; + static components = { Child }; + + props = { a: "a", c: "c" }; + } + + const widget = new Parent(); + await widget.mount(fixture); + }); }); describe("support svg components", () => { diff --git a/tests/router/__snapshots__/route_component.test.ts.snap b/tests/router/__snapshots__/route_component.test.ts.snap index a166319b..e1ef351d 100644 --- a/tests/router/__snapshots__/route_component.test.ts.snap +++ b/tests/router/__snapshots__/route_component.test.ts.snap @@ -18,7 +18,7 @@ exports[`RouteComponent can render simple cases 1`] = ` let w4 = k5 in parent.__owl__.cmap ? parent.__owl__.children[parent.__owl__.cmap[k5]] : false; let vn6 = {}; result = vn6; - let props4 = Object.assign({}, scope['env'].router.currentParams); + let props4 = Object.assign({}, scope['env'].router.currentParams, {}); if (w4 && w4.__owl__.currentFiber && !w4.__owl__.vnode) { w4.destroy(); w4 = false;