From 82870ec4a4a3870b5c2625d51b1c5278acf0d87b Mon Sep 17 00:00:00 2001 From: Joe Fleming Date: Tue, 30 Sep 2014 11:30:42 -0700 Subject: [PATCH 1/5] app state destroys itself when the route changes --- src/kibana/components/state_management/app_state.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/kibana/components/state_management/app_state.js b/src/kibana/components/state_management/app_state.js index 2c40e168e67f4..f5e6e089d6f16 100644 --- a/src/kibana/components/state_management/app_state.js +++ b/src/kibana/components/state_management/app_state.js @@ -1,14 +1,16 @@ define(function (require) { var _ = require('lodash'); - return function AppStateProvider(Private) { + return function AppStateProvider(Private, $rootScope) { var State = Private(require('components/state_management/state')); _(AppState).inherits(State); function AppState(defaults) { AppState.Super.call(this, '_a', defaults); - } + // When we have a route change, destroy the app state + $rootScope.$on('$routeChangeStart', _.bindKey(this, 'destroy')); + } return AppState; }; From fa394f6d700e4082f4527e539e44c44372c1ff3d Mon Sep 17 00:00:00 2001 From: Joe Fleming Date: Tue, 30 Sep 2014 14:37:14 -0700 Subject: [PATCH 2/5] add app state test --- test/unit/index.html | 1 + test/unit/specs/state_management/app_state.js | 33 +++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 test/unit/specs/state_management/app_state.js diff --git a/test/unit/index.html b/test/unit/index.html index 474255c5b3cbf..d89eaaf654192 100644 --- a/test/unit/index.html +++ b/test/unit/index.html @@ -106,6 +106,7 @@ 'specs/factories/base_object', 'specs/state_management/state', 'specs/state_management/global_state', + 'specs/state_management/app_state', 'specs/utils/diff_object', 'specs/factories/events', 'specs/vislib/color', diff --git a/test/unit/specs/state_management/app_state.js b/test/unit/specs/state_management/app_state.js new file mode 100644 index 0000000000000..b5932fb35c90a --- /dev/null +++ b/test/unit/specs/state_management/app_state.js @@ -0,0 +1,33 @@ +define(function (require) { + var sinon = require('test_utils/auto_release_sinon'); + require('components/state_management/app_state'); + + describe('State Management', function () { + var $rootScope, $location, AppState; + + beforeEach(function () { + module('kibana'); + + inject(function (_$location_, Private) { + $location = _$location_; + AppState = Private(require('components/state_management/app_state')); + }); + }); + + describe('App State', function () { + var appState; + + beforeEach(function () { + appState = new AppState(); + }); + + it('should have a destroy method', function () { + expect(appState).to.have.property('destroy'); + }); + + it('should use passed in params'); + + it('should be destroyed on route change'); + }); + }); +}); From 6f8d258ccc78cfb497ff5374bc32d39e4625bf32 Mon Sep 17 00:00:00 2001 From: Joe Fleming Date: Tue, 30 Sep 2014 16:15:44 -0700 Subject: [PATCH 3/5] add test for --- test/unit/specs/state_management/app_state.js | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/test/unit/specs/state_management/app_state.js b/test/unit/specs/state_management/app_state.js index b5932fb35c90a..b2a4900f4c87e 100644 --- a/test/unit/specs/state_management/app_state.js +++ b/test/unit/specs/state_management/app_state.js @@ -3,13 +3,13 @@ define(function (require) { require('components/state_management/app_state'); describe('State Management', function () { - var $rootScope, $location, AppState; + var $rootScope, AppState; beforeEach(function () { module('kibana'); - inject(function (_$location_, Private) { - $location = _$location_; + inject(function (_$rootScope_, _$location_, Private) { + $rootScope = _$rootScope_; AppState = Private(require('components/state_management/app_state')); }); }); @@ -25,9 +25,16 @@ define(function (require) { expect(appState).to.have.property('destroy'); }); - it('should use passed in params'); + it('should be destroyed on $routeChangeStart', function () { + var destroySpy = sinon.spy(appState, 'destroy'); + var url = '/test/path'; + + $rootScope.$emit('$routeChangeStart'); - it('should be destroyed on route change'); + expect(destroySpy.callCount).to.be(1); + }); + + it('should use passed in params'); }); }); }); From fb3bf65d6b45680a511cfc9ebf28fd7f3a61906b Mon Sep 17 00:00:00 2001 From: Joe Fleming Date: Tue, 30 Sep 2014 16:18:30 -0700 Subject: [PATCH 4/5] add test for passed in params --- test/unit/specs/state_management/app_state.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/test/unit/specs/state_management/app_state.js b/test/unit/specs/state_management/app_state.js index b2a4900f4c87e..068571891aa87 100644 --- a/test/unit/specs/state_management/app_state.js +++ b/test/unit/specs/state_management/app_state.js @@ -34,7 +34,20 @@ define(function (require) { expect(destroySpy.callCount).to.be(1); }); - it('should use passed in params'); + it('should use passed in params', function () { + var params = { + test: true, + mock: false + }; + + appState = new AppState(params); + expect(appState).to.have.property('_defaults'); + + Object.keys(params).forEach(function (key) { + expect(appState._defaults).to.have.property(key); + expect(appState._defaults[key]).to.equal(params[key]); + }); + }); }); }); }); From f28099cb8d69ac7aa73cd25a37f857057738b515 Mon Sep 17 00:00:00 2001 From: Joe Fleming Date: Tue, 30 Sep 2014 16:19:56 -0700 Subject: [PATCH 5/5] add test for _urlParam --- test/unit/specs/state_management/app_state.js | 27 +++++++++++-------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/test/unit/specs/state_management/app_state.js b/test/unit/specs/state_management/app_state.js index 068571891aa87..f4391b4c04e97 100644 --- a/test/unit/specs/state_management/app_state.js +++ b/test/unit/specs/state_management/app_state.js @@ -21,17 +21,9 @@ define(function (require) { appState = new AppState(); }); - it('should have a destroy method', function () { - expect(appState).to.have.property('destroy'); - }); - - it('should be destroyed on $routeChangeStart', function () { - var destroySpy = sinon.spy(appState, 'destroy'); - var url = '/test/path'; - - $rootScope.$emit('$routeChangeStart'); - - expect(destroySpy.callCount).to.be(1); + it('should have _urlParam of _a', function () { + expect(appState).to.have.property('_urlParam'); + expect(appState._urlParam).to.equal('_a'); }); it('should use passed in params', function () { @@ -48,6 +40,19 @@ define(function (require) { expect(appState._defaults[key]).to.equal(params[key]); }); }); + + it('should have a destroy method', function () { + expect(appState).to.have.property('destroy'); + }); + + it('should be destroyed on $routeChangeStart', function () { + var destroySpy = sinon.spy(appState, 'destroy'); + var url = '/test/path'; + + $rootScope.$emit('$routeChangeStart'); + + expect(destroySpy.callCount).to.be(1); + }); }); }); });