fix: make sure props are always defined

fix #7
This commit is contained in:
Géry Debongnie
2019-03-22 09:28:56 +01:00
parent add380f59b
commit a236ae396a
2 changed files with 8 additions and 2 deletions
+2 -2
View File
@@ -48,7 +48,7 @@ export interface Type<T> extends Function {
export class Component< export class Component<
T extends Env, T extends Env,
Props, Props extends {},
State extends {} State extends {}
> extends EventBus { > extends EventBus {
readonly __widget__: Meta<Env, Props>; readonly __widget__: Meta<Env, Props>;
@@ -96,7 +96,7 @@ export class Component<
// Pro: if props is empty, we can create easily a widget // Pro: if props is empty, we can create easily a widget
// Con: this is not really safe // Con: this is not really safe
// Pro: but creating widget (by a template) is always unsafe anyway // Pro: but creating widget (by a template) is always unsafe anyway
this.props = <Props>props; this.props = <Props>props || <Props>{};
let id: number = getId(); let id: number = getId();
let p: Component<T, any, any> | null = null; let p: Component<T, any, any> | null = null;
if (parent instanceof Component) { if (parent instanceof Component) {
+6
View File
@@ -70,6 +70,12 @@ class WidgetB extends Widget {
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
describe("basic widget properties", () => { describe("basic widget properties", () => {
test("props and state are properly defined", async () => {
const widget = new Widget(env);
expect(widget.props).toEqual({});
expect(widget.state).toEqual({});
});
test("has no el after creation", async () => { test("has no el after creation", async () => {
const widget = new Widget(env); const widget = new Widget(env);
expect(widget.el).toBe(null); expect(widget.el).toBe(null);