From 88a006a6034c73394722ddbd75923d766652b1a1 Mon Sep 17 00:00:00 2001 From: louismaximepiton Date: Mon, 17 Jul 2023 11:46:23 +0200 Subject: [PATCH] Add tests --- js/src/modal.js | 4 ++-- js/tests/unit/modal.spec.js | 45 +++++++++++++++++++++++++++++++------ 2 files changed, 40 insertions(+), 9 deletions(-) diff --git a/js/src/modal.js b/js/src/modal.js index 33e525d235dc..0c02431fe814 100644 --- a/js/src/modal.js +++ b/js/src/modal.js @@ -139,11 +139,11 @@ class Modal extends BaseComponent { } dispose() { - this._element.addEventListener(EVENT_HIDDEN, () => { + EventHandler.on(this._element, EVENT_HIDDEN, () => { EventHandler.off(window, EVENT_KEY) EventHandler.off(this._dialog, EVENT_KEY) - this._backdrop.dispose() + this._backdrop?.dispose() this._focustrap.deactivate() super.dispose() diff --git a/js/tests/unit/modal.spec.js b/js/tests/unit/modal.spec.js index 6434d8b3cca2..3e6a0d46ab58 100644 --- a/js/tests/unit/modal.spec.js +++ b/js/tests/unit/modal.spec.js @@ -1,3 +1,4 @@ +import BaseComponent from '../../src/base-component.js' import EventHandler from '../../src/dom/event-handler.js' import Modal from '../../src/modal.js' import ScrollBarHelper from '../../src/util/scrollbar.js' @@ -846,18 +847,48 @@ describe('Modal', () => { const modalEl = fixtureEl.querySelector('.modal') const modal = new Modal(modalEl) - const focustrap = modal._focustrap - const spyDeactivate = spyOn(focustrap, 'deactivate').and.callThrough() - - expect(Modal.getInstance(modalEl)).toEqual(modal) - + const spyHideModal = spyOn(modal, '_hideModal').and.callThrough() + const spyDeactivate = spyOn(modal._focustrap, 'deactivate') + const spyBackdropDispose = spyOn(modal._backdrop, 'dispose') + const spySuperDispose = spyOn(BaseComponent.prototype, 'dispose') const spyOff = spyOn(EventHandler, 'off') modal.dispose() - expect(Modal.getInstance(modalEl)).toBeNull() - expect(spyOff).toHaveBeenCalledTimes(3) + expect(spyHideModal).toHaveBeenCalled() + expect(spyOff).toHaveBeenCalledTimes(2) expect(spyDeactivate).toHaveBeenCalled() + expect(spyBackdropDispose).toHaveBeenCalled() + expect(spySuperDispose).toHaveBeenCalled() + }) + + it('should dispose a shown modal', () => { + return new Promise(resolve => { + fixtureEl.innerHTML = '' + + const modalEl = fixtureEl.querySelector('.modal') + const modal = new Modal(modalEl) + + modal.show() + + const spyHideModal = spyOn(modal, '_hideModal').and.callThrough() + const spyDeactivate = spyOn(modal._focustrap, 'deactivate') + const spyBackdropDispose = spyOn(modal._backdrop, 'dispose') + const spySuperDispose = spyOn(BaseComponent.prototype, 'dispose') + const spyOff = spyOn(EventHandler, 'off') + + modal.dispose() + + expect(spyHideModal).toHaveBeenCalled() + + setTimeout(() => { + expect(spyOff).toHaveBeenCalledTimes(2) + expect(spyDeactivate).toHaveBeenCalled() + expect(spyBackdropDispose).toHaveBeenCalled() + expect(spySuperDispose).toHaveBeenCalled() + resolve() + }, 20) + }) }) })