diff --git a/frontend/src/services/util-registry/util-registry.js b/frontend/src/services/util-registry/util-registry.js index b342d812a..b8dea6ec3 100644 --- a/frontend/src/services/util-registry/util-registry.js +++ b/frontend/src/services/util-registry/util-registry.js @@ -103,8 +103,10 @@ export class UtilRegistry { utilsInScope.forEach((util) => { //if(DEBUG_MODE > 2) { console.log('Destroying Util: ', {util}); - //} + //}# + let utilIndex = this._activeUtilInstancesWrapped.indexOf(util); util.destroy(); + this._activeUtilInstancesWrapped.splice(utilIndex, 1); }); } diff --git a/frontend/src/services/util-registry/util-registry.spec.js b/frontend/src/services/util-registry/util-registry.spec.js index 00f8806b3..18506be99 100644 --- a/frontend/src/services/util-registry/util-registry.spec.js +++ b/frontend/src/services/util-registry/util-registry.spec.js @@ -199,6 +199,44 @@ describe('UtilRegistry', () => { }); }); }); + + describe('destroyAll()', () => { + let testScope; + let testElement; + let firstUtil; + + beforeEach( () => { + testScope = document.createElement('div'); + testElement = document.createElement('div'); + testElement.classList.add('util3'); + testScope.appendChild(testElement); + + utilRegistry.register(TestUtil3); + utilRegistry.initAll(testScope); + + firstUtil = utilRegistry._activeUtilInstancesWrapped[0]; + spyOn(firstUtil.instance, 'destroy'); + }); + + it('Util should be destroyed', () => { + utilRegistry.destroyAll(testScope); + expect(utilRegistry._activeUtilInstancesWrapped.length).toBe(0); + expect(firstUtil.instance.destroy).toHaveBeenCalled(); + }); + + it('Util out of scope should not be destroyed', () => { + let outOfScope = document.createElement('div'); + expect(utilRegistry._activeUtilInstancesWrapped.length).toEqual(1); + + utilRegistry.destroyAll(outOfScope); + + expect(utilRegistry._activeUtilInstancesWrapped.length).toEqual(1); + expect(utilRegistry._activeUtilInstancesWrapped[0]).toBe(firstUtil); + expect(firstUtil.instance.destroy).not.toHaveBeenCalled(); + + }); + }); + }); // test utilities @@ -219,6 +257,7 @@ class TestUtil2 { } class TestUtil3 { constructor() {} start() {} + destroy() {} } @Utility({ selector: '#throws' })