[FIX] component: make sure props are validated in all cases

closes #379
This commit is contained in:
Géry Debongnie
2019-10-26 22:06:33 +02:00
committed by Aaron Bohy
parent 9779cd196c
commit 4ebe419c56
3 changed files with 26 additions and 6 deletions
+3 -5
View File
@@ -134,6 +134,9 @@ export class Component<T extends Env, Props extends {}> {
// Con: this is not really safe
// Pro: but creating component (by a template) is always unsafe anyway
this.props = <Props>props || <Props>{};
if (QWeb.dev) {
QWeb.utils.validateProps(this.constructor, this.props);
}
let id: number = nextId++;
let p: Component<T, any> | null = null;
if (parent instanceof Component) {
@@ -142,11 +145,6 @@ export class Component<T extends Env, Props extends {}> {
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);
+1 -1
View File
@@ -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}')`);
}
}
+22
View File
@@ -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`<span><t t-if="props.p">hey</t></span>`;
}
class App extends Widget {
static template = xml`<div><TestWidget/></div>`;
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", () => {