Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
louismaximepiton committed Jul 17, 2023
1 parent b17e3b1 commit 88a006a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
4 changes: 2 additions & 2 deletions js/src/modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
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

0 comments on commit 88a006a

Please sign in to comment.