From e8f94fd37c16c3d36ea7ea6ad6dece7a922f7954 Mon Sep 17 00:00:00 2001 From: Andy Hume Date: Mon, 20 Jul 2015 10:46:42 +0100 Subject: [PATCH] Define empty state if initialState function not called this.state, by definiton, is an object of key/values. It should never be undefined. If no state has been set yet, this.state should be an empty object. --- lib/with-state.js | 2 +- package.json | 2 +- test/spec/with-state.spec.js | 11 +++++++++++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/with-state.js b/lib/with-state.js index 39adf19..92331e5 100644 --- a/lib/with-state.js +++ b/lib/with-state.js @@ -146,7 +146,7 @@ define(function (require) { this.stateChanged = function () {}; this.after('initialize', function () { - this._stateDef = (this._stateDef || function () {}); + this._stateDef = (this._stateDef || function () { return {} }); this.replaceState(this._stateDef.call(this)); }); } diff --git a/package.json b/package.json index 3f3366e..d3bd5e6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "flight-with-state", - "version": "2.0.1", + "version": "2.1.0", "devDependencies": { "bower": "^1.3.3", "grunt": "~0.4.5", diff --git a/test/spec/with-state.spec.js b/test/spec/with-state.spec.js index 985b609..670dba9 100644 --- a/test/spec/with-state.spec.js +++ b/test/spec/with-state.spec.js @@ -16,9 +16,11 @@ define(function (require) { var ComponentA; var ComponentB; + var ComponentC; var instanceAofA; var instanceBofA; var instanceAofB; + var instanceAofC; // Initialize a component and attach it to the DOM beforeEach(function () { @@ -48,14 +50,18 @@ define(function (require) { }); }); + ComponentC = makeComponent(function () {}); + instanceAofA = initializeComponent(ComponentA); instanceBofA = initializeComponent(ComponentA); instanceAofB = initializeComponent(ComponentB); + instanceAofC = initializeComponent(ComponentC); }); afterEach(function () { ComponentA && ComponentA.teardownAll(); ComponentB && ComponentB.teardownAll(); + ComponentC && ComponentC.teardownAll(); }); describe('initialState', function () { @@ -64,6 +70,11 @@ define(function (require) { expect(instanceAofA.state).toBeDefined(); }); + it('should add empty state if initialState is not called', function () { + expect(instanceAofC).toBeDefined(); + expect(instanceAofC.state).toEqual({}); + }); + it('propagates initialState to this.state', function () { expect(instanceAofA.state.alive).toBe(true); });