This commit is contained in:
Louis-Maxime Piton 2025-05-02 08:45:42 +00:00 committed by GitHub
commit 915e2d604a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 47 additions and 12 deletions

View File

@ -141,13 +141,17 @@ class Modal extends BaseComponent {
} }
dispose() { dispose() {
EventHandler.off(window, EVENT_KEY) EventHandler.on(this._element, EVENT_HIDDEN, () => {
EventHandler.off(this._dialog, EVENT_KEY) EventHandler.off(window, EVENT_KEY)
EventHandler.off(this._dialog, EVENT_KEY)
this._backdrop.dispose() this._backdrop?.dispose()
this._focustrap.deactivate() this._focustrap.deactivate()
super.dispose() super.dispose()
})
this._hideModal()
} }
handleUpdate() { handleUpdate() {

View File

@ -1,3 +1,4 @@
import BaseComponent from '../../src/base-component.js'
import EventHandler from '../../src/dom/event-handler.js' import EventHandler from '../../src/dom/event-handler.js'
import Modal from '../../src/modal.js' import Modal from '../../src/modal.js'
import ScrollBarHelper from '../../src/util/scrollbar.js' import ScrollBarHelper from '../../src/util/scrollbar.js'
@ -848,18 +849,48 @@ describe('Modal', () => {
const modalEl = fixtureEl.querySelector('.modal') const modalEl = fixtureEl.querySelector('.modal')
const modal = new Modal(modalEl) const modal = new Modal(modalEl)
const focustrap = modal._focustrap const spyHideModal = spyOn(modal, '_hideModal').and.callThrough()
const spyDeactivate = spyOn(focustrap, 'deactivate').and.callThrough() const spyDeactivate = spyOn(modal._focustrap, 'deactivate')
const spyBackdropDispose = spyOn(modal._backdrop, 'dispose')
expect(Modal.getInstance(modalEl)).toEqual(modal) const spySuperDispose = spyOn(BaseComponent.prototype, 'dispose')
const spyOff = spyOn(EventHandler, 'off') const spyOff = spyOn(EventHandler, 'off')
modal.dispose() modal.dispose()
expect(Modal.getInstance(modalEl)).toBeNull() expect(spyHideModal).toHaveBeenCalled()
expect(spyOff).toHaveBeenCalledTimes(3) expect(spyOff).toHaveBeenCalledTimes(2)
expect(spyDeactivate).toHaveBeenCalled() expect(spyDeactivate).toHaveBeenCalled()
expect(spyBackdropDispose).toHaveBeenCalled()
expect(spySuperDispose).toHaveBeenCalled()
})
it('should dispose a shown modal', () => {
return new Promise(resolve => {
fixtureEl.innerHTML = '<div id="exampleModal" class="modal"><div class="modal-dialog"></div></div>'
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)
})
}) })
}) })