mirror of
https://github.com/odoo/owl.git
synced 2025-10-06 19:59:41 +07:00
[FIX] component: make sure props are validated in all cases
closes #379
This commit is contained in:
committed by
Aaron Bohy
parent
9779cd196c
commit
4ebe419c56
@@ -134,6 +134,9 @@ export class Component<T extends Env, Props extends {}> {
|
|||||||
// Con: this is not really safe
|
// Con: this is not really safe
|
||||||
// Pro: but creating component (by a template) is always unsafe anyway
|
// Pro: but creating component (by a template) is always unsafe anyway
|
||||||
this.props = <Props>props || <Props>{};
|
this.props = <Props>props || <Props>{};
|
||||||
|
if (QWeb.dev) {
|
||||||
|
QWeb.utils.validateProps(this.constructor, this.props);
|
||||||
|
}
|
||||||
let id: number = nextId++;
|
let id: number = nextId++;
|
||||||
let p: Component<T, any> | null = null;
|
let p: Component<T, any> | null = null;
|
||||||
if (parent instanceof Component) {
|
if (parent instanceof Component) {
|
||||||
@@ -142,11 +145,6 @@ export class Component<T extends Env, Props extends {}> {
|
|||||||
parent.__owl__.children[id] = this;
|
parent.__owl__.children[id] = this;
|
||||||
} else {
|
} else {
|
||||||
this.env = parent;
|
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, () => {
|
this.env.qweb.on("update", this, () => {
|
||||||
if (this.__owl__.isMounted) {
|
if (this.__owl__.isMounted) {
|
||||||
this.render(true);
|
this.render(true);
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ QWeb.utils.validateProps = function(Widget, props: Object) {
|
|||||||
// optional prop
|
// optional prop
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (!props[propName]) {
|
if (!(propName in props)) {
|
||||||
throw new Error(`Missing props '${propsDef[i]}' (component '${Widget.name}')`);
|
throw new Error(`Missing props '${propsDef[i]}' (component '${Widget.name}')`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -322,6 +322,28 @@ describe("props validation", () => {
|
|||||||
QWeb.utils.validateProps(TestWidget, { message: null });
|
QWeb.utils.validateProps(TestWidget, { message: null });
|
||||||
}).toThrow();
|
}).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", () => {
|
describe("default props", () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user