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; }; 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..f4391b4c04e97 --- /dev/null +++ b/test/unit/specs/state_management/app_state.js @@ -0,0 +1,58 @@ +define(function (require) { + var sinon = require('test_utils/auto_release_sinon'); + require('components/state_management/app_state'); + + describe('State Management', function () { + var $rootScope, AppState; + + beforeEach(function () { + module('kibana'); + + inject(function (_$rootScope_, _$location_, Private) { + $rootScope = _$rootScope_; + AppState = Private(require('components/state_management/app_state')); + }); + }); + + describe('App State', function () { + var appState; + + beforeEach(function () { + appState = new AppState(); + }); + + 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 () { + 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]); + }); + }); + + 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); + }); + }); + }); +});