From d74b5a03db221d578f54bb789d5e6aca84df77ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9ry=20Debongnie?= Date: Fri, 1 Nov 2019 07:58:25 +0100 Subject: [PATCH] [IMP] component: add props on root components This is a partial revert of commit 7d249d6f093c9e97771bbb085c1aeda439d5ac36. The reason is that this changes made it much harder to unit test components. Before, we could simply instantiate a component like this: const my|Comp = new MyComponent(null, props); and then simply test it. This commit reestablish that possibility. --- doc/reference/component.md | 6 +++++- src/component/component.ts | 22 +++++++++++----------- tests/component/component.test.ts | 6 +++--- 3 files changed, 19 insertions(+), 15 deletions(-) diff --git a/doc/reference/component.md b/doc/reference/component.md index d21f8138..b1d2e63b 100644 --- a/doc/reference/component.md +++ b/doc/reference/component.md @@ -443,10 +443,14 @@ const app = new App(); app.mount(document.body); ``` -The root component does not have a parent nor props. It will be setup with an +The root component does not have a parent nor `props` (see note below). It will be setup with an [environment](environment.md) (either the `env` defined on its class, or a default empty environment). +Note: a root component can however be given a `props` object in its constructor, +like this: `new App(null, {some: 'object'});`. It will not be a true `props` +object, managed by Owl (so, for example, it will never be updated). + ### Composition The example above shows a QWeb template with a sub component. In a template, diff --git a/src/component/component.ts b/src/component/component.ts index 62be517f..f974b1dc 100644 --- a/src/component/component.ts +++ b/src/component/component.ts @@ -111,21 +111,22 @@ export class Component { * hand. Other components should be created automatically by the framework (with * the t-component directive in a template) */ - constructor(parent?: Component, props?: Props) { + constructor(parent?: Component | null, props?: Props) { Component.current = this; - const id: number = nextId++; let constr = this.constructor as any; + const defaultProps = constr.defaultProps; + if (defaultProps) { + props = this.__applyDefaultProps(props, defaultProps); + } + this.props = props; + if (QWeb.dev) { + QWeb.utils.validateProps(constr, this.props); + } + + const id: number = nextId++; let depth; if (parent) { - const defaultProps = constr.defaultProps; - if (defaultProps) { - props = this.__applyDefaultProps(props, defaultProps); - } - this.props = props; - if (QWeb.dev) { - QWeb.utils.validateProps(constr, this.props); - } this.env = parent.env; const __powl__ = parent.__owl__; __powl__.children[id] = this; @@ -136,7 +137,6 @@ export class Component { if (!this.env.qweb) { this.env.qweb = new QWeb(); } - this.props = (undefined as unknown) as Props; this.env.qweb.on("update", this, () => { if (this.__owl__.isMounted) { this.render(true); diff --git a/tests/component/component.test.ts b/tests/component/component.test.ts index a6270dca..1cf1653e 100644 --- a/tests/component/component.test.ts +++ b/tests/component/component.test.ts @@ -71,9 +71,9 @@ class WidgetA extends Widget { //------------------------------------------------------------------------------ describe("basic widget properties", () => { - test("props is not defined on root components", async () => { - const widget = new Widget(); - expect(widget.props).toBe(undefined); + test("props is set on root components", async () => { + const widget = new Widget(null, {}); + expect(widget.props).toEqual({}); }); test("has no el after creation", async () => {