diff --git a/core/src/core-api/config.js b/core/src/core-api/config.js index 4e0494f917..2dadc375f9 100644 --- a/core/src/core-api/config.js +++ b/core/src/core-api/config.js @@ -219,6 +219,7 @@ class LuigiConfig { */ unload() { this.initialized = false; + window.Luigi._store.clear(); AuthLayerSvc.unload(); EventListenerHelpers.removeAllEventListeners(); const container = LuigiElements.getLuigiContainer(); diff --git a/core/src/main.js b/core/src/main.js index d6a4836a1b..e5c5decaaf 100644 --- a/core/src/main.js +++ b/core/src/main.js @@ -8,8 +8,12 @@ import { AuthLayerSvc } from './services'; const createConfigStore = () => { const { subscribe, update, reset } = writable({}); const scopeSubscribers = {}; + let unSubscriptions = []; return { - subscribe, + subscribe: fn => { + //subscribe fn returns unsubscription fn + unSubscriptions.push(subscribe(fn)); + }, update, reset, subscribeToScope: (fn, scope) => { @@ -27,6 +31,12 @@ const createConfigStore = () => { fn(data); }); } + }, + clear: () => { + unSubscriptions.forEach(sub => { + sub(); + }); + unSubscriptions = []; } }; }; diff --git a/core/test/core-api/config.spec.js b/core/test/core-api/config.spec.js index 4f3829467e..cd1f09adcb 100644 --- a/core/test/core-api/config.spec.js +++ b/core/test/core-api/config.spec.js @@ -1,5 +1,7 @@ import { IframeHelpers } from '../../src/utilities/helpers'; -import { LuigiConfig } from '../../src/core-api'; +import { LuigiConfig, LuigiElements } from '../../src/core-api'; +import { AuthLayerSvc } from '../../src/services'; +import { EventListenerHelpers } from '../../src/utilities/helpers'; const sinon = require('sinon'); const chai = require('chai'); const assert = chai.assert; @@ -92,4 +94,31 @@ describe('updateContextValues', () => { assert.deepEqual(component.context, { initialContext: 'initial', updatedContext: 'updated' }); }); }); + + it('Luigi unload', () => { + window.Luigi = { + _store: { + clear: sinon.spy() + } + }; + let containerStub = sinon.stub(LuigiElements, 'getLuigiContainer').returns({ + firstChild: {}, + removeChild: sinon.spy(function() { + this.firstChild = null; + }) + }); + let authUnloadStub = sinon.stub(AuthLayerSvc, 'unload'); + let removeAllListenersStub = sinon.stub(EventListenerHelpers, 'removeAllEventListeners'); + + LuigiConfig.unload(); + sinon.assert.called(containerStub.returnValues[0].removeChild); + sinon.assert.calledOnce(containerStub.returnValues[0].removeChild); + sinon.assert.calledOnce(window.Luigi._store.clear); + sinon.assert.calledOnce(authUnloadStub); + sinon.assert.calledOnce(removeAllListenersStub); + + authUnloadStub.restore(); + removeAllListenersStub.restore(); + containerStub.restore(); + }); });