From a236ae396a467b23a37fc9362bdde772d9fcc4d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9ry=20Debongnie?= Date: Fri, 22 Mar 2019 09:28:56 +0100 Subject: [PATCH] fix: make sure props are always defined fix #7 --- src/component.ts | 4 ++-- tests/component.test.ts | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/component.ts b/src/component.ts index d46d834c..638a96a1 100644 --- a/src/component.ts +++ b/src/component.ts @@ -48,7 +48,7 @@ export interface Type extends Function { export class Component< T extends Env, - Props, + Props extends {}, State extends {} > extends EventBus { readonly __widget__: Meta; @@ -96,7 +96,7 @@ export class Component< // Pro: if props is empty, we can create easily a widget // Con: this is not really safe // Pro: but creating widget (by a template) is always unsafe anyway - this.props = props; + this.props = props || {}; let id: number = getId(); let p: Component | null = null; if (parent instanceof Component) { diff --git a/tests/component.test.ts b/tests/component.test.ts index d15c43ef..b9c21c5e 100644 --- a/tests/component.test.ts +++ b/tests/component.test.ts @@ -70,6 +70,12 @@ class WidgetB extends Widget { //------------------------------------------------------------------------------ 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 () => { const widget = new Widget(env); expect(widget.el).toBe(null);