Skip to content

Commit

Permalink
Clean Luigi store (#3964)
Browse files Browse the repository at this point in the history
  • Loading branch information
JohannesDoberer authored Oct 1, 2024
1 parent 98d4caf commit fa3a695
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
1 change: 1 addition & 0 deletions core/src/core-api/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ class LuigiConfig {
*/
unload() {
this.initialized = false;
window.Luigi._store.clear();
AuthLayerSvc.unload();
EventListenerHelpers.removeAllEventListeners();
const container = LuigiElements.getLuigiContainer();
Expand Down
12 changes: 11 additions & 1 deletion core/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -27,6 +31,12 @@ const createConfigStore = () => {
fn(data);
});
}
},
clear: () => {
unSubscriptions.forEach(sub => {
sub();
});
unSubscriptions = [];
}
};
};
Expand Down
31 changes: 30 additions & 1 deletion core/test/core-api/config.spec.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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();
});
});

0 comments on commit fa3a695

Please sign in to comment.