From 4ebe419c56cd5bd2a9f4f261d649fbf13d9b4c97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9ry=20Debongnie?= Date: Sat, 26 Oct 2019 22:06:33 +0200 Subject: [PATCH] [FIX] component: make sure props are validated in all cases closes #379 --- src/component/component.ts | 8 +++----- src/component/props_validation.ts | 2 +- tests/component/props_validation.test.ts | 22 ++++++++++++++++++++++ 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/component/component.ts b/src/component/component.ts index e13c1b2d..03b2b842 100644 --- a/src/component/component.ts +++ b/src/component/component.ts @@ -134,6 +134,9 @@ export class Component { // Con: this is not really safe // Pro: but creating component (by a template) is always unsafe anyway this.props = props || {}; + if (QWeb.dev) { + QWeb.utils.validateProps(this.constructor, this.props); + } let id: number = nextId++; let p: Component | null = null; if (parent instanceof Component) { @@ -142,11 +145,6 @@ export class Component { parent.__owl__.children[id] = this; } else { this.env = parent; - if (QWeb.dev) { - // we only validate props for root widgets here. "Regular" widget - // props are validated by the t-component directive - QWeb.utils.validateProps(this.constructor, this.props); - } this.env.qweb.on("update", this, () => { if (this.__owl__.isMounted) { this.render(true); diff --git a/src/component/props_validation.ts b/src/component/props_validation.ts index b073a8db..9973223b 100644 --- a/src/component/props_validation.ts +++ b/src/component/props_validation.ts @@ -21,7 +21,7 @@ QWeb.utils.validateProps = function(Widget, props: Object) { // optional prop break; } - if (!props[propName]) { + if (!(propName in props)) { throw new Error(`Missing props '${propsDef[i]}' (component '${Widget.name}')`); } } diff --git a/tests/component/props_validation.test.ts b/tests/component/props_validation.test.ts index 63994bfe..8afb6411 100644 --- a/tests/component/props_validation.test.ts +++ b/tests/component/props_validation.test.ts @@ -322,6 +322,28 @@ describe("props validation", () => { QWeb.utils.validateProps(TestWidget, { message: null }); }).toThrow(); }); + + test("can set default required boolean values", async () => { + class TestWidget extends Widget { + static props = ["p"]; + static template = xml`hey`; + } + + class App extends Widget { + static template = xml`
`; + static components = { TestWidget }; + } + + const w = new App(env, {}); + let error; + try { + await w.mount(fixture); + } catch (e) { + error = e; + } + expect(error).toBeDefined(); + expect(error.message).toBe("Missing props 'p' (component 'TestWidget')"); + }); }); describe("default props", () => {