Skip to content

Modal: fix dispose function #38857

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions js/src/modal.js
Copy link
Member

@GeoSot GeoSot Jul 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be great to add a test too, supporting your solution

NOTE for reviewers:
The proposed change is valid.
My only consideration is, whether we should handle such situations or not. In case we decide to handle them, we should also think about the other components too

Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,17 @@ class Modal extends BaseComponent {
}

dispose() {
EventHandler.off(window, EVENT_KEY)
EventHandler.off(this._dialog, EVENT_KEY)
EventHandler.on(this._element, EVENT_HIDDEN, () => {
EventHandler.off(window, EVENT_KEY)
EventHandler.off(this._dialog, EVENT_KEY)

this._backdrop.dispose()
this._focustrap.deactivate()
this._backdrop?.dispose()
this._focustrap.deactivate()

super.dispose()
})

super.dispose()
this._hideModal()
}

handleUpdate() {
Expand Down
45 changes: 38 additions & 7 deletions js/tests/unit/modal.spec.js
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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 = '<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)
})
})
})

Expand Down