2019-10-02 11:43:54 +02:00
|
|
|
import Modal from '../../src/modal'
|
|
|
|
import EventHandler from '../../src/dom/event-handler'
|
2021-06-06 08:26:36 +02:00
|
|
|
import ScrollBarHelper from '../../src/util/scrollbar'
|
2021-04-25 05:50:16 +02:00
|
|
|
import { clearBodyAndDocument, clearFixture, createEvent, getFixture, jQueryMock } from '../helpers/fixture'
|
2019-05-01 15:43:40 +02:00
|
|
|
|
|
|
|
describe('Modal', () => {
|
|
|
|
let fixtureEl
|
|
|
|
|
|
|
|
beforeAll(() => {
|
|
|
|
fixtureEl = getFixture()
|
|
|
|
})
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
clearFixture()
|
2021-04-25 05:50:16 +02:00
|
|
|
clearBodyAndDocument()
|
2019-05-01 15:43:40 +02:00
|
|
|
document.body.classList.remove('modal-open')
|
|
|
|
|
2021-07-30 08:28:51 +02:00
|
|
|
for (const backdrop of document.querySelectorAll('.modal-backdrop')) {
|
|
|
|
backdrop.remove()
|
|
|
|
}
|
2019-05-01 15:43:40 +02:00
|
|
|
})
|
|
|
|
|
2021-04-12 01:57:53 +02:00
|
|
|
beforeEach(() => {
|
2021-04-25 05:50:16 +02:00
|
|
|
clearBodyAndDocument()
|
2019-05-01 15:43:40 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('VERSION', () => {
|
|
|
|
it('should return plugin version', () => {
|
|
|
|
expect(Modal.VERSION).toEqual(jasmine.any(String))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('Default', () => {
|
|
|
|
it('should return plugin default config', () => {
|
|
|
|
expect(Modal.Default).toEqual(jasmine.any(Object))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-02-16 07:58:08 +01:00
|
|
|
describe('DATA_KEY', () => {
|
|
|
|
it('should return plugin data key', () => {
|
|
|
|
expect(Modal.DATA_KEY).toEqual('bs.modal')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-02-22 08:01:04 +01:00
|
|
|
describe('constructor', () => {
|
|
|
|
it('should take care of element either passed as a CSS selector or DOM element', () => {
|
|
|
|
fixtureEl.innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
|
|
|
|
|
|
|
|
const modalEl = fixtureEl.querySelector('.modal')
|
|
|
|
const modalBySelector = new Modal('.modal')
|
|
|
|
const modalByElement = new Modal(modalEl)
|
|
|
|
|
|
|
|
expect(modalBySelector._element).toEqual(modalEl)
|
|
|
|
expect(modalByElement._element).toEqual(modalEl)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-05-01 15:43:40 +02:00
|
|
|
describe('toggle', () => {
|
2022-01-30 13:30:04 +01:00
|
|
|
it('should call ScrollBarHelper to handle scrollBar on body', () => {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
fixtureEl.innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
|
|
|
|
|
2022-05-31 10:18:32 +02:00
|
|
|
const spyHide = spyOn(ScrollBarHelper.prototype, 'hide').and.callThrough()
|
|
|
|
const spyReset = spyOn(ScrollBarHelper.prototype, 'reset').and.callThrough()
|
2022-01-30 13:30:04 +01:00
|
|
|
const modalEl = fixtureEl.querySelector('.modal')
|
|
|
|
const modal = new Modal(modalEl)
|
|
|
|
|
|
|
|
modalEl.addEventListener('shown.bs.modal', () => {
|
2022-05-31 10:18:32 +02:00
|
|
|
expect(spyHide).toHaveBeenCalled()
|
2022-01-30 13:30:04 +01:00
|
|
|
modal.toggle()
|
|
|
|
})
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modalEl.addEventListener('hidden.bs.modal', () => {
|
2022-05-31 10:18:32 +02:00
|
|
|
expect(spyReset).toHaveBeenCalled()
|
2022-01-30 13:30:04 +01:00
|
|
|
resolve()
|
|
|
|
})
|
2019-05-01 15:43:40 +02:00
|
|
|
|
|
|
|
modal.toggle()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('show', () => {
|
2022-01-30 13:30:04 +01:00
|
|
|
it('should show a modal', () => {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
fixtureEl.innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
const modalEl = fixtureEl.querySelector('.modal')
|
|
|
|
const modal = new Modal(modalEl)
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modalEl.addEventListener('show.bs.modal', event => {
|
|
|
|
expect(event).toBeDefined()
|
|
|
|
})
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modalEl.addEventListener('shown.bs.modal', () => {
|
|
|
|
expect(modalEl.getAttribute('aria-modal')).toEqual('true')
|
|
|
|
expect(modalEl.getAttribute('role')).toEqual('dialog')
|
|
|
|
expect(modalEl.getAttribute('aria-hidden')).toBeNull()
|
|
|
|
expect(modalEl.style.display).toEqual('block')
|
|
|
|
expect(document.querySelector('.modal-backdrop')).not.toBeNull()
|
|
|
|
resolve()
|
|
|
|
})
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modal.show()
|
|
|
|
})
|
2019-05-01 15:43:40 +02:00
|
|
|
})
|
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
it('should show a modal without backdrop', () => {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
fixtureEl.innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
const modalEl = fixtureEl.querySelector('.modal')
|
|
|
|
const modal = new Modal(modalEl, {
|
|
|
|
backdrop: false
|
|
|
|
})
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modalEl.addEventListener('show.bs.modal', event => {
|
|
|
|
expect(event).toBeDefined()
|
|
|
|
})
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modalEl.addEventListener('shown.bs.modal', () => {
|
|
|
|
expect(modalEl.getAttribute('aria-modal')).toEqual('true')
|
|
|
|
expect(modalEl.getAttribute('role')).toEqual('dialog')
|
|
|
|
expect(modalEl.getAttribute('aria-hidden')).toBeNull()
|
|
|
|
expect(modalEl.style.display).toEqual('block')
|
|
|
|
expect(document.querySelector('.modal-backdrop')).toBeNull()
|
|
|
|
resolve()
|
|
|
|
})
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modal.show()
|
|
|
|
})
|
2019-05-01 15:43:40 +02:00
|
|
|
})
|
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
it('should show a modal and append the element', () => {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
const modalEl = document.createElement('div')
|
|
|
|
const id = 'dynamicModal'
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modalEl.setAttribute('id', id)
|
|
|
|
modalEl.classList.add('modal')
|
|
|
|
modalEl.innerHTML = '<div class="modal-dialog"></div>'
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
const modal = new Modal(modalEl)
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modalEl.addEventListener('shown.bs.modal', () => {
|
|
|
|
const dynamicModal = document.getElementById(id)
|
|
|
|
expect(dynamicModal).not.toBeNull()
|
|
|
|
dynamicModal.remove()
|
|
|
|
resolve()
|
|
|
|
})
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modal.show()
|
|
|
|
})
|
2019-05-01 15:43:40 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should do nothing if a modal is shown', () => {
|
2020-05-11 15:54:56 +02:00
|
|
|
fixtureEl.innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
|
2019-05-01 15:43:40 +02:00
|
|
|
|
|
|
|
const modalEl = fixtureEl.querySelector('.modal')
|
|
|
|
const modal = new Modal(modalEl)
|
|
|
|
|
2022-05-31 10:18:32 +02:00
|
|
|
const spy = spyOn(EventHandler, 'trigger')
|
2019-05-01 15:43:40 +02:00
|
|
|
modal._isShown = true
|
|
|
|
|
|
|
|
modal.show()
|
|
|
|
|
2022-05-31 10:18:32 +02:00
|
|
|
expect(spy).not.toHaveBeenCalled()
|
2019-05-01 15:43:40 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should do nothing if a modal is transitioning', () => {
|
2020-05-11 15:54:56 +02:00
|
|
|
fixtureEl.innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
|
2019-05-01 15:43:40 +02:00
|
|
|
|
|
|
|
const modalEl = fixtureEl.querySelector('.modal')
|
|
|
|
const modal = new Modal(modalEl)
|
|
|
|
|
2022-05-31 10:18:32 +02:00
|
|
|
const spy = spyOn(EventHandler, 'trigger')
|
2019-05-01 15:43:40 +02:00
|
|
|
modal._isTransitioning = true
|
|
|
|
|
|
|
|
modal.show()
|
|
|
|
|
2022-05-31 10:18:32 +02:00
|
|
|
expect(spy).not.toHaveBeenCalled()
|
2019-05-01 15:43:40 +02:00
|
|
|
})
|
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
it('should not fire shown event when show is prevented', () => {
|
2022-02-19 15:08:16 +01:00
|
|
|
return new Promise((resolve, reject) => {
|
2022-01-30 13:30:04 +01:00
|
|
|
fixtureEl.innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
const modalEl = fixtureEl.querySelector('.modal')
|
|
|
|
const modal = new Modal(modalEl)
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modalEl.addEventListener('show.bs.modal', event => {
|
|
|
|
event.preventDefault()
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
const expectedDone = () => {
|
|
|
|
expect().nothing()
|
|
|
|
resolve()
|
|
|
|
}
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
setTimeout(expectedDone, 10)
|
|
|
|
})
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modalEl.addEventListener('shown.bs.modal', () => {
|
2022-02-19 15:08:16 +01:00
|
|
|
reject(new Error('shown event triggered'))
|
2022-01-30 13:30:04 +01:00
|
|
|
})
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modal.show()
|
|
|
|
})
|
2019-05-01 15:43:40 +02:00
|
|
|
})
|
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
it('should be shown after the first call to show() has been prevented while fading is enabled ', () => {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
fixtureEl.innerHTML = '<div class="modal fade"><div class="modal-dialog"></div></div>'
|
2021-05-24 17:52:36 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
const modalEl = fixtureEl.querySelector('.modal')
|
|
|
|
const modal = new Modal(modalEl)
|
2021-05-24 17:52:36 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
let prevented = false
|
|
|
|
modalEl.addEventListener('show.bs.modal', event => {
|
|
|
|
if (!prevented) {
|
|
|
|
event.preventDefault()
|
|
|
|
prevented = true
|
2021-05-24 17:52:36 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
setTimeout(() => {
|
|
|
|
modal.show()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
2021-05-24 17:52:36 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modalEl.addEventListener('shown.bs.modal', () => {
|
|
|
|
expect(prevented).toBeTrue()
|
|
|
|
expect(modal._isAnimated()).toBeTrue()
|
|
|
|
resolve()
|
|
|
|
})
|
2021-05-24 17:52:36 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modal.show()
|
|
|
|
})
|
2021-05-24 17:52:36 +02:00
|
|
|
})
|
2022-01-30 13:30:04 +01:00
|
|
|
it('should set is transitioning if fade class is present', () => {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
fixtureEl.innerHTML = '<div class="modal fade"><div class="modal-dialog"></div></div>'
|
2021-05-24 17:52:36 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
const modalEl = fixtureEl.querySelector('.modal')
|
|
|
|
const modal = new Modal(modalEl)
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modalEl.addEventListener('show.bs.modal', () => {
|
|
|
|
setTimeout(() => {
|
|
|
|
expect(modal._isTransitioning).toBeTrue()
|
|
|
|
})
|
|
|
|
})
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modalEl.addEventListener('shown.bs.modal', () => {
|
|
|
|
expect(modal._isTransitioning).toBeFalse()
|
|
|
|
resolve()
|
2021-05-24 17:52:36 +02:00
|
|
|
})
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modal.show()
|
2019-05-01 15:43:40 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
it('should close modal when a click occurred on data-bs-dismiss="modal" inside modal', () => {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
fixtureEl.innerHTML = [
|
|
|
|
'<div class="modal fade">',
|
|
|
|
' <div class="modal-dialog">',
|
|
|
|
' <div class="modal-header">',
|
|
|
|
' <button type="button" data-bs-dismiss="modal"></button>',
|
|
|
|
' </div>',
|
|
|
|
' </div>',
|
|
|
|
'</div>'
|
|
|
|
].join('')
|
|
|
|
|
|
|
|
const modalEl = fixtureEl.querySelector('.modal')
|
|
|
|
const btnClose = fixtureEl.querySelector('[data-bs-dismiss="modal"]')
|
|
|
|
const modal = new Modal(modalEl)
|
|
|
|
|
2022-05-31 10:18:32 +02:00
|
|
|
const spy = spyOn(modal, 'hide').and.callThrough()
|
2022-01-30 13:30:04 +01:00
|
|
|
|
|
|
|
modalEl.addEventListener('shown.bs.modal', () => {
|
|
|
|
btnClose.click()
|
|
|
|
})
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modalEl.addEventListener('hidden.bs.modal', () => {
|
2022-05-31 10:18:32 +02:00
|
|
|
expect(spy).toHaveBeenCalled()
|
2022-01-30 13:30:04 +01:00
|
|
|
resolve()
|
|
|
|
})
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modal.show()
|
2019-05-01 15:43:40 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
it('should close modal when a click occurred on a data-bs-dismiss="modal" with "bs-target" outside of modal element', () => {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
fixtureEl.innerHTML = [
|
|
|
|
'<button type="button" data-bs-dismiss="modal" data-bs-target="#modal1"></button>',
|
|
|
|
'<div id="modal1" class="modal fade">',
|
|
|
|
' <div class="modal-dialog"></div>',
|
|
|
|
'</div>'
|
|
|
|
].join('')
|
2021-07-19 15:56:05 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
const modalEl = fixtureEl.querySelector('.modal')
|
|
|
|
const btnClose = fixtureEl.querySelector('[data-bs-dismiss="modal"]')
|
|
|
|
const modal = new Modal(modalEl)
|
2021-07-19 15:56:05 +02:00
|
|
|
|
2022-05-31 10:18:32 +02:00
|
|
|
const spy = spyOn(modal, 'hide').and.callThrough()
|
2021-07-19 15:56:05 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modalEl.addEventListener('shown.bs.modal', () => {
|
|
|
|
btnClose.click()
|
|
|
|
})
|
2021-07-19 15:56:05 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modalEl.addEventListener('hidden.bs.modal', () => {
|
2022-05-31 10:18:32 +02:00
|
|
|
expect(spy).toHaveBeenCalled()
|
2022-01-30 13:30:04 +01:00
|
|
|
resolve()
|
|
|
|
})
|
2021-07-19 15:56:05 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modal.show()
|
|
|
|
})
|
2021-07-19 15:56:05 +02:00
|
|
|
})
|
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
it('should set .modal\'s scroll top to 0', () => {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
fixtureEl.innerHTML = [
|
|
|
|
'<div class="modal fade">',
|
|
|
|
' <div class="modal-dialog"></div>',
|
|
|
|
'</div>'
|
|
|
|
].join('')
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
const modalEl = fixtureEl.querySelector('.modal')
|
|
|
|
const modal = new Modal(modalEl)
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modalEl.addEventListener('shown.bs.modal', () => {
|
|
|
|
expect(modalEl.scrollTop).toEqual(0)
|
|
|
|
resolve()
|
|
|
|
})
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modal.show()
|
|
|
|
})
|
2019-05-01 15:43:40 +02:00
|
|
|
})
|
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
it('should set modal body scroll top to 0 if modal body do not exists', () => {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
fixtureEl.innerHTML = [
|
|
|
|
'<div class="modal fade">',
|
|
|
|
' <div class="modal-dialog">',
|
|
|
|
' <div class="modal-body"></div>',
|
|
|
|
' </div>',
|
|
|
|
'</div>'
|
|
|
|
].join('')
|
|
|
|
|
|
|
|
const modalEl = fixtureEl.querySelector('.modal')
|
|
|
|
const modalBody = modalEl.querySelector('.modal-body')
|
|
|
|
const modal = new Modal(modalEl)
|
|
|
|
|
|
|
|
modalEl.addEventListener('shown.bs.modal', () => {
|
|
|
|
expect(modalBody.scrollTop).toEqual(0)
|
|
|
|
resolve()
|
|
|
|
})
|
2019-07-28 11:49:44 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modal.show()
|
2019-07-28 11:49:44 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
it('should not trap focus if focus equal to false', () => {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
fixtureEl.innerHTML = '<div class="modal fade"><div class="modal-dialog"></div></div>'
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
const modalEl = fixtureEl.querySelector('.modal')
|
|
|
|
const modal = new Modal(modalEl, {
|
|
|
|
focus: false
|
|
|
|
})
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-05-31 10:18:32 +02:00
|
|
|
const spy = spyOn(modal._focustrap, 'activate').and.callThrough()
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modalEl.addEventListener('shown.bs.modal', () => {
|
2022-05-31 10:18:32 +02:00
|
|
|
expect(spy).not.toHaveBeenCalled()
|
2022-01-30 13:30:04 +01:00
|
|
|
resolve()
|
|
|
|
})
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modal.show()
|
|
|
|
})
|
2019-05-01 15:43:40 +02:00
|
|
|
})
|
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
it('should add listener when escape touch is pressed', () => {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
fixtureEl.innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
const modalEl = fixtureEl.querySelector('.modal')
|
|
|
|
const modal = new Modal(modalEl)
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-05-31 10:18:32 +02:00
|
|
|
const spy = spyOn(modal, 'hide').and.callThrough()
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modalEl.addEventListener('shown.bs.modal', () => {
|
|
|
|
const keydownEscape = createEvent('keydown')
|
|
|
|
keydownEscape.key = 'Escape'
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modalEl.dispatchEvent(keydownEscape)
|
|
|
|
})
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modalEl.addEventListener('hidden.bs.modal', () => {
|
2022-05-31 10:18:32 +02:00
|
|
|
expect(spy).toHaveBeenCalled()
|
2022-01-30 13:30:04 +01:00
|
|
|
resolve()
|
|
|
|
})
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modal.show()
|
|
|
|
})
|
2019-05-01 15:43:40 +02:00
|
|
|
})
|
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
it('should do nothing when the pressed key is not escape', () => {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
fixtureEl.innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
const modalEl = fixtureEl.querySelector('.modal')
|
|
|
|
const modal = new Modal(modalEl)
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-05-31 10:18:32 +02:00
|
|
|
const spy = spyOn(modal, 'hide')
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
const expectDone = () => {
|
2022-05-31 10:18:32 +02:00
|
|
|
expect(spy).not.toHaveBeenCalled()
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
resolve()
|
|
|
|
}
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modalEl.addEventListener('shown.bs.modal', () => {
|
|
|
|
const keydownTab = createEvent('keydown')
|
|
|
|
keydownTab.key = 'Tab'
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modalEl.dispatchEvent(keydownTab)
|
|
|
|
setTimeout(expectDone, 30)
|
|
|
|
})
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modal.show()
|
|
|
|
})
|
2019-05-01 15:43:40 +02:00
|
|
|
})
|
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
it('should adjust dialog on resize', () => {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
fixtureEl.innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
const modalEl = fixtureEl.querySelector('.modal')
|
|
|
|
const modal = new Modal(modalEl)
|
|
|
|
|
2022-03-01 16:08:12 +01:00
|
|
|
const spy = spyOn(modal, '_adjustDialog').and.callThrough()
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
const expectDone = () => {
|
2022-03-01 16:08:12 +01:00
|
|
|
expect(spy).toHaveBeenCalled()
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
resolve()
|
|
|
|
}
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modalEl.addEventListener('shown.bs.modal', () => {
|
|
|
|
const resizeEvent = createEvent('resize')
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
window.dispatchEvent(resizeEvent)
|
|
|
|
setTimeout(expectDone, 10)
|
|
|
|
})
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modal.show()
|
2019-05-01 15:43:40 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
it('should not close modal when clicking on modal-content', () => {
|
2022-02-19 15:08:16 +01:00
|
|
|
return new Promise((resolve, reject) => {
|
2022-01-30 13:30:04 +01:00
|
|
|
fixtureEl.innerHTML = [
|
|
|
|
'<div class="modal">',
|
|
|
|
' <div class="modal-dialog">',
|
|
|
|
' <div class="modal-content"></div>',
|
|
|
|
' </div>',
|
|
|
|
'</div>'
|
|
|
|
].join('')
|
2021-10-08 02:23:23 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
const modalEl = fixtureEl.querySelector('.modal')
|
|
|
|
const modal = new Modal(modalEl)
|
2021-10-08 02:23:23 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
const shownCallback = () => {
|
|
|
|
setTimeout(() => {
|
|
|
|
expect(modal._isShown).toEqual(true)
|
|
|
|
resolve()
|
|
|
|
}, 10)
|
|
|
|
}
|
2021-10-08 02:23:23 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modalEl.addEventListener('shown.bs.modal', () => {
|
|
|
|
fixtureEl.querySelector('.modal-dialog').click()
|
|
|
|
fixtureEl.querySelector('.modal-content').click()
|
|
|
|
shownCallback()
|
|
|
|
})
|
2021-10-08 02:23:23 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modalEl.addEventListener('hidden.bs.modal', () => {
|
2022-02-19 15:08:16 +01:00
|
|
|
reject(new Error('Should not hide a modal'))
|
2022-01-30 13:30:04 +01:00
|
|
|
})
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modal.show()
|
2019-05-01 15:43:40 +02:00
|
|
|
})
|
2022-01-30 13:30:04 +01:00
|
|
|
})
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
it('should not close modal when clicking outside of modal-content if backdrop = false', () => {
|
2022-02-19 15:08:16 +01:00
|
|
|
return new Promise((resolve, reject) => {
|
2022-01-30 13:30:04 +01:00
|
|
|
fixtureEl.innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
const modalEl = fixtureEl.querySelector('.modal')
|
|
|
|
const modal = new Modal(modalEl, {
|
|
|
|
backdrop: false
|
|
|
|
})
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
const shownCallback = () => {
|
|
|
|
setTimeout(() => {
|
|
|
|
expect(modal._isShown).toBeTrue()
|
|
|
|
resolve()
|
|
|
|
}, 10)
|
|
|
|
}
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modalEl.addEventListener('shown.bs.modal', () => {
|
|
|
|
modalEl.click()
|
|
|
|
shownCallback()
|
|
|
|
})
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modalEl.addEventListener('hidden.bs.modal', () => {
|
2022-02-19 15:08:16 +01:00
|
|
|
reject(new Error('Should not hide a modal'))
|
2022-01-30 13:30:04 +01:00
|
|
|
})
|
2019-10-25 20:12:09 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modal.show()
|
2019-10-25 20:12:09 +02:00
|
|
|
})
|
2022-01-30 13:30:04 +01:00
|
|
|
})
|
2019-10-25 20:12:09 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
it('should not close modal when clicking outside of modal-content if backdrop = static', () => {
|
2022-02-19 15:08:16 +01:00
|
|
|
return new Promise((resolve, reject) => {
|
2022-01-30 13:30:04 +01:00
|
|
|
fixtureEl.innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
|
2019-10-25 20:12:09 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
const modalEl = fixtureEl.querySelector('.modal')
|
|
|
|
const modal = new Modal(modalEl, {
|
|
|
|
backdrop: 'static'
|
|
|
|
})
|
2019-10-25 20:12:09 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
const shownCallback = () => {
|
|
|
|
setTimeout(() => {
|
|
|
|
expect(modal._isShown).toBeTrue()
|
|
|
|
resolve()
|
|
|
|
}, 10)
|
|
|
|
}
|
2019-10-25 20:12:09 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modalEl.addEventListener('shown.bs.modal', () => {
|
|
|
|
modalEl.click()
|
|
|
|
shownCallback()
|
|
|
|
})
|
2019-10-25 20:12:09 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modalEl.addEventListener('hidden.bs.modal', () => {
|
2022-02-19 15:08:16 +01:00
|
|
|
reject(new Error('Should not hide a modal'))
|
2022-01-30 13:30:04 +01:00
|
|
|
})
|
2020-01-10 10:06:12 +01:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modal.show()
|
2020-01-10 10:06:12 +01:00
|
|
|
})
|
2022-01-30 13:30:04 +01:00
|
|
|
})
|
|
|
|
it('should close modal when escape key is pressed with keyboard = true and backdrop is static', () => {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
fixtureEl.innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
|
|
|
|
|
|
|
|
const modalEl = fixtureEl.querySelector('.modal')
|
|
|
|
const modal = new Modal(modalEl, {
|
|
|
|
backdrop: 'static',
|
|
|
|
keyboard: true
|
|
|
|
})
|
2020-01-10 10:06:12 +01:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
const shownCallback = () => {
|
|
|
|
setTimeout(() => {
|
|
|
|
expect(modal._isShown).toBeFalse()
|
|
|
|
resolve()
|
|
|
|
}, 10)
|
|
|
|
}
|
2020-01-10 10:06:12 +01:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modalEl.addEventListener('shown.bs.modal', () => {
|
|
|
|
const keydownEscape = createEvent('keydown')
|
|
|
|
keydownEscape.key = 'Escape'
|
2020-01-10 10:06:12 +01:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modalEl.dispatchEvent(keydownEscape)
|
|
|
|
shownCallback()
|
|
|
|
})
|
2020-01-10 10:06:12 +01:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modal.show()
|
|
|
|
})
|
2020-01-10 10:06:12 +01:00
|
|
|
})
|
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
it('should not close modal when escape key is pressed with keyboard = false', () => {
|
2022-02-19 15:08:16 +01:00
|
|
|
return new Promise((resolve, reject) => {
|
2022-01-30 13:30:04 +01:00
|
|
|
fixtureEl.innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
|
2020-01-10 10:06:12 +01:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
const modalEl = fixtureEl.querySelector('.modal')
|
|
|
|
const modal = new Modal(modalEl, {
|
|
|
|
keyboard: false
|
|
|
|
})
|
2020-01-10 10:06:12 +01:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
const shownCallback = () => {
|
|
|
|
setTimeout(() => {
|
|
|
|
expect(modal._isShown).toBeTrue()
|
|
|
|
resolve()
|
|
|
|
}, 10)
|
|
|
|
}
|
2020-01-10 10:06:12 +01:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modalEl.addEventListener('shown.bs.modal', () => {
|
|
|
|
const keydownEscape = createEvent('keydown')
|
|
|
|
keydownEscape.key = 'Escape'
|
2020-01-10 10:06:12 +01:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modalEl.dispatchEvent(keydownEscape)
|
|
|
|
shownCallback()
|
|
|
|
})
|
2020-01-10 10:06:12 +01:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modalEl.addEventListener('hidden.bs.modal', () => {
|
2022-02-19 15:08:16 +01:00
|
|
|
reject(new Error('Should not hide a modal'))
|
2022-01-30 13:30:04 +01:00
|
|
|
})
|
2020-01-10 10:06:12 +01:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modal.show()
|
|
|
|
})
|
2020-01-10 10:06:12 +01:00
|
|
|
})
|
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
it('should not overflow when clicking outside of modal-content if backdrop = static', () => {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
fixtureEl.innerHTML = '<div class="modal"><div class="modal-dialog" style="transition-duration: 20ms;"></div></div>'
|
2020-06-25 10:35:53 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
const modalEl = fixtureEl.querySelector('.modal')
|
|
|
|
const modal = new Modal(modalEl, {
|
|
|
|
backdrop: 'static'
|
|
|
|
})
|
2020-06-25 10:35:53 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modalEl.addEventListener('shown.bs.modal', () => {
|
|
|
|
modalEl.click()
|
|
|
|
setTimeout(() => {
|
|
|
|
expect(modalEl.clientHeight).toEqual(modalEl.scrollHeight)
|
|
|
|
resolve()
|
|
|
|
}, 20)
|
|
|
|
})
|
2020-06-25 10:35:53 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modal.show()
|
|
|
|
})
|
2020-06-25 10:35:53 +02:00
|
|
|
})
|
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
it('should not queue multiple callbacks when clicking outside of modal-content and backdrop = static', () => {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
fixtureEl.innerHTML = '<div class="modal"><div class="modal-dialog" style="transition-duration: 50ms;"></div></div>'
|
2021-06-03 13:44:16 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
const modalEl = fixtureEl.querySelector('.modal')
|
|
|
|
const modal = new Modal(modalEl, {
|
|
|
|
backdrop: 'static'
|
|
|
|
})
|
2021-06-03 13:44:16 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modalEl.addEventListener('shown.bs.modal', () => {
|
|
|
|
const spy = spyOn(modal, '_queueCallback').and.callThrough()
|
2021-06-03 13:44:16 +02:00
|
|
|
|
2022-05-13 06:20:04 +02:00
|
|
|
modalEl.click()
|
|
|
|
modalEl.click()
|
2021-06-03 13:44:16 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
setTimeout(() => {
|
|
|
|
expect(spy).toHaveBeenCalledTimes(1)
|
|
|
|
resolve()
|
|
|
|
}, 20)
|
|
|
|
})
|
2021-06-03 13:44:16 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modal.show()
|
|
|
|
})
|
2021-06-03 13:44:16 +02:00
|
|
|
})
|
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
it('should trap focus', () => {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
fixtureEl.innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
const modalEl = fixtureEl.querySelector('.modal')
|
|
|
|
const modal = new Modal(modalEl)
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-05-31 10:18:32 +02:00
|
|
|
const spy = spyOn(modal._focustrap, 'activate').and.callThrough()
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modalEl.addEventListener('shown.bs.modal', () => {
|
2022-05-31 10:18:32 +02:00
|
|
|
expect(spy).toHaveBeenCalled()
|
2022-01-30 13:30:04 +01:00
|
|
|
resolve()
|
|
|
|
})
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modal.show()
|
|
|
|
})
|
2019-05-01 15:43:40 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('hide', () => {
|
2022-01-30 13:30:04 +01:00
|
|
|
it('should hide a modal', () => {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
fixtureEl.innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
const modalEl = fixtureEl.querySelector('.modal')
|
|
|
|
const modal = new Modal(modalEl)
|
|
|
|
const backdropSpy = spyOn(modal._backdrop, 'hide').and.callThrough()
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modalEl.addEventListener('shown.bs.modal', () => {
|
|
|
|
modal.hide()
|
|
|
|
})
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modalEl.addEventListener('hide.bs.modal', event => {
|
|
|
|
expect(event).toBeDefined()
|
|
|
|
})
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modalEl.addEventListener('hidden.bs.modal', () => {
|
|
|
|
expect(modalEl.getAttribute('aria-modal')).toBeNull()
|
|
|
|
expect(modalEl.getAttribute('role')).toBeNull()
|
|
|
|
expect(modalEl.getAttribute('aria-hidden')).toEqual('true')
|
|
|
|
expect(modalEl.style.display).toEqual('none')
|
|
|
|
expect(backdropSpy).toHaveBeenCalled()
|
|
|
|
resolve()
|
|
|
|
})
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modal.show()
|
|
|
|
})
|
2019-05-01 15:43:40 +02:00
|
|
|
})
|
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
it('should close modal when clicking outside of modal-content', () => {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
fixtureEl.innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
const modalEl = fixtureEl.querySelector('.modal')
|
|
|
|
const modal = new Modal(modalEl)
|
|
|
|
modalEl.addEventListener('shown.bs.modal', () => {
|
2022-05-13 06:20:04 +02:00
|
|
|
modalEl.click()
|
2022-01-30 13:30:04 +01:00
|
|
|
})
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modalEl.addEventListener('hidden.bs.modal', () => {
|
|
|
|
expect(document.querySelector('.modal-backdrop')).toBeNull()
|
|
|
|
resolve()
|
|
|
|
})
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modal.show()
|
|
|
|
})
|
2019-05-01 15:43:40 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should do nothing is the modal is not shown', () => {
|
2020-05-11 15:54:56 +02:00
|
|
|
fixtureEl.innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
|
2019-05-01 15:43:40 +02:00
|
|
|
|
|
|
|
const modalEl = fixtureEl.querySelector('.modal')
|
|
|
|
const modal = new Modal(modalEl)
|
|
|
|
|
|
|
|
modal.hide()
|
|
|
|
|
|
|
|
expect().nothing()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should do nothing is the modal is transitioning', () => {
|
2020-05-11 15:54:56 +02:00
|
|
|
fixtureEl.innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
|
2019-05-01 15:43:40 +02:00
|
|
|
|
|
|
|
const modalEl = fixtureEl.querySelector('.modal')
|
|
|
|
const modal = new Modal(modalEl)
|
|
|
|
|
|
|
|
modal._isTransitioning = true
|
|
|
|
modal.hide()
|
|
|
|
|
|
|
|
expect().nothing()
|
|
|
|
})
|
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
it('should not hide a modal if hide is prevented', () => {
|
2022-02-19 15:08:16 +01:00
|
|
|
return new Promise((resolve, reject) => {
|
2022-01-30 13:30:04 +01:00
|
|
|
fixtureEl.innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
const modalEl = fixtureEl.querySelector('.modal')
|
|
|
|
const modal = new Modal(modalEl)
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modalEl.addEventListener('shown.bs.modal', () => {
|
|
|
|
modal.hide()
|
|
|
|
})
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
const hideCallback = () => {
|
|
|
|
setTimeout(() => {
|
|
|
|
expect(modal._isShown).toBeTrue()
|
|
|
|
resolve()
|
|
|
|
}, 10)
|
|
|
|
}
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modalEl.addEventListener('hide.bs.modal', event => {
|
|
|
|
event.preventDefault()
|
|
|
|
hideCallback()
|
|
|
|
})
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modalEl.addEventListener('hidden.bs.modal', () => {
|
2022-02-19 15:08:16 +01:00
|
|
|
reject(new Error('should not trigger hidden'))
|
2022-01-30 13:30:04 +01:00
|
|
|
})
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modal.show()
|
|
|
|
})
|
2019-05-01 15:43:40 +02:00
|
|
|
})
|
2021-07-27 07:01:04 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
it('should release focus trap', () => {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
fixtureEl.innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
|
2021-07-27 07:01:04 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
const modalEl = fixtureEl.querySelector('.modal')
|
|
|
|
const modal = new Modal(modalEl)
|
2022-05-31 10:18:32 +02:00
|
|
|
const spy = spyOn(modal._focustrap, 'deactivate').and.callThrough()
|
2021-07-27 07:01:04 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modalEl.addEventListener('shown.bs.modal', () => {
|
|
|
|
modal.hide()
|
|
|
|
})
|
2021-07-27 07:01:04 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modalEl.addEventListener('hidden.bs.modal', () => {
|
2022-05-31 10:18:32 +02:00
|
|
|
expect(spy).toHaveBeenCalled()
|
2022-01-30 13:30:04 +01:00
|
|
|
resolve()
|
|
|
|
})
|
2021-07-27 07:01:04 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modal.show()
|
|
|
|
})
|
2021-07-27 07:01:04 +02:00
|
|
|
})
|
2019-05-01 15:43:40 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('dispose', () => {
|
|
|
|
it('should dispose a modal', () => {
|
2020-11-20 19:10:15 +01:00
|
|
|
fixtureEl.innerHTML = '<div id="exampleModal" class="modal"><div class="modal-dialog"></div></div>'
|
2019-05-01 15:43:40 +02:00
|
|
|
|
|
|
|
const modalEl = fixtureEl.querySelector('.modal')
|
|
|
|
const modal = new Modal(modalEl)
|
2021-07-27 07:01:04 +02:00
|
|
|
const focustrap = modal._focustrap
|
2022-05-31 10:18:32 +02:00
|
|
|
const spyDeactivate = spyOn(focustrap, 'deactivate').and.callThrough()
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2019-07-28 15:24:46 +02:00
|
|
|
expect(Modal.getInstance(modalEl)).toEqual(modal)
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-05-31 10:18:32 +02:00
|
|
|
const spyOff = spyOn(EventHandler, 'off')
|
2019-05-01 15:43:40 +02:00
|
|
|
|
|
|
|
modal.dispose()
|
|
|
|
|
2021-05-11 07:45:57 +02:00
|
|
|
expect(Modal.getInstance(modalEl)).toBeNull()
|
2022-05-31 10:18:32 +02:00
|
|
|
expect(spyOff).toHaveBeenCalledTimes(3)
|
|
|
|
expect(spyDeactivate).toHaveBeenCalled()
|
2019-05-01 15:43:40 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('handleUpdate', () => {
|
|
|
|
it('should call adjust dialog', () => {
|
2020-11-20 19:10:15 +01:00
|
|
|
fixtureEl.innerHTML = '<div id="exampleModal" class="modal"><div class="modal-dialog"></div></div>'
|
2019-05-01 15:43:40 +02:00
|
|
|
|
|
|
|
const modalEl = fixtureEl.querySelector('.modal')
|
|
|
|
const modal = new Modal(modalEl)
|
|
|
|
|
2022-05-31 10:18:32 +02:00
|
|
|
const spy = spyOn(modal, '_adjustDialog')
|
2019-05-01 15:43:40 +02:00
|
|
|
|
|
|
|
modal.handleUpdate()
|
|
|
|
|
2022-05-31 10:18:32 +02:00
|
|
|
expect(spy).toHaveBeenCalled()
|
2019-05-01 15:43:40 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('data-api', () => {
|
2022-01-30 13:30:04 +01:00
|
|
|
it('should toggle modal', () => {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
fixtureEl.innerHTML = [
|
|
|
|
'<button type="button" data-bs-toggle="modal" data-bs-target="#exampleModal"></button>',
|
|
|
|
'<div id="exampleModal" class="modal"><div class="modal-dialog"></div></div>'
|
|
|
|
].join('')
|
|
|
|
|
|
|
|
const modalEl = fixtureEl.querySelector('.modal')
|
|
|
|
const trigger = fixtureEl.querySelector('[data-bs-toggle="modal"]')
|
|
|
|
|
|
|
|
modalEl.addEventListener('shown.bs.modal', () => {
|
|
|
|
expect(modalEl.getAttribute('aria-modal')).toEqual('true')
|
|
|
|
expect(modalEl.getAttribute('role')).toEqual('dialog')
|
|
|
|
expect(modalEl.getAttribute('aria-hidden')).toBeNull()
|
|
|
|
expect(modalEl.style.display).toEqual('block')
|
|
|
|
expect(document.querySelector('.modal-backdrop')).not.toBeNull()
|
|
|
|
setTimeout(() => trigger.click(), 10)
|
|
|
|
})
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modalEl.addEventListener('hidden.bs.modal', () => {
|
|
|
|
expect(modalEl.getAttribute('aria-modal')).toBeNull()
|
|
|
|
expect(modalEl.getAttribute('role')).toBeNull()
|
|
|
|
expect(modalEl.getAttribute('aria-hidden')).toEqual('true')
|
|
|
|
expect(modalEl.style.display).toEqual('none')
|
|
|
|
expect(document.querySelector('.modal-backdrop')).toBeNull()
|
|
|
|
resolve()
|
|
|
|
})
|
2021-01-13 20:59:47 +01:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
trigger.click()
|
2019-05-01 15:43:40 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
it('should not recreate a new modal', () => {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
fixtureEl.innerHTML = [
|
|
|
|
'<button type="button" data-bs-toggle="modal" data-bs-target="#exampleModal"></button>',
|
|
|
|
'<div id="exampleModal" class="modal"><div class="modal-dialog"></div></div>'
|
|
|
|
].join('')
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
const modalEl = fixtureEl.querySelector('.modal')
|
|
|
|
const modal = new Modal(modalEl)
|
|
|
|
const trigger = fixtureEl.querySelector('[data-bs-toggle="modal"]')
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-05-31 10:18:32 +02:00
|
|
|
const spy = spyOn(modal, 'show').and.callThrough()
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modalEl.addEventListener('shown.bs.modal', () => {
|
2022-05-31 10:18:32 +02:00
|
|
|
expect(spy).toHaveBeenCalled()
|
2022-01-30 13:30:04 +01:00
|
|
|
resolve()
|
|
|
|
})
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
trigger.click()
|
|
|
|
})
|
2019-05-01 15:43:40 +02:00
|
|
|
})
|
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
it('should prevent default when the trigger is <a> or <area>', () => {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
fixtureEl.innerHTML = [
|
|
|
|
'<a data-bs-toggle="modal" href="#" data-bs-target="#exampleModal"></a>',
|
|
|
|
'<div id="exampleModal" class="modal"><div class="modal-dialog"></div></div>'
|
|
|
|
].join('')
|
|
|
|
|
|
|
|
const modalEl = fixtureEl.querySelector('.modal')
|
|
|
|
const trigger = fixtureEl.querySelector('[data-bs-toggle="modal"]')
|
|
|
|
|
2022-05-31 10:18:32 +02:00
|
|
|
const spy = spyOn(Event.prototype, 'preventDefault').and.callThrough()
|
2022-01-30 13:30:04 +01:00
|
|
|
|
|
|
|
modalEl.addEventListener('shown.bs.modal', () => {
|
|
|
|
expect(modalEl.getAttribute('aria-modal')).toEqual('true')
|
|
|
|
expect(modalEl.getAttribute('role')).toEqual('dialog')
|
|
|
|
expect(modalEl.getAttribute('aria-hidden')).toBeNull()
|
|
|
|
expect(modalEl.style.display).toEqual('block')
|
|
|
|
expect(document.querySelector('.modal-backdrop')).not.toBeNull()
|
2022-05-31 10:18:32 +02:00
|
|
|
expect(spy).toHaveBeenCalled()
|
2022-01-30 13:30:04 +01:00
|
|
|
resolve()
|
|
|
|
})
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
trigger.click()
|
2019-05-01 15:43:40 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
it('should focus the trigger on hide', () => {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
fixtureEl.innerHTML = [
|
|
|
|
'<a data-bs-toggle="modal" href="#" data-bs-target="#exampleModal"></a>',
|
|
|
|
'<div id="exampleModal" class="modal"><div class="modal-dialog"></div></div>'
|
|
|
|
].join('')
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
const modalEl = fixtureEl.querySelector('.modal')
|
|
|
|
const trigger = fixtureEl.querySelector('[data-bs-toggle="modal"]')
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-05-31 10:18:32 +02:00
|
|
|
const spy = spyOn(trigger, 'focus')
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modalEl.addEventListener('shown.bs.modal', () => {
|
|
|
|
const modal = Modal.getInstance(modalEl)
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modal.hide()
|
|
|
|
})
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
const hideListener = () => {
|
|
|
|
setTimeout(() => {
|
2022-05-31 10:18:32 +02:00
|
|
|
expect(spy).toHaveBeenCalled()
|
2022-01-30 13:30:04 +01:00
|
|
|
resolve()
|
|
|
|
}, 20)
|
|
|
|
}
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modalEl.addEventListener('hidden.bs.modal', () => {
|
|
|
|
hideListener()
|
|
|
|
})
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
trigger.click()
|
|
|
|
})
|
2019-05-01 15:43:40 +02:00
|
|
|
})
|
2022-01-30 13:30:04 +01:00
|
|
|
it('should not prevent default when a click occurred on data-bs-dismiss="modal" where tagName is DIFFERENT than <a> or <area>', () => {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
fixtureEl.innerHTML = [
|
|
|
|
'<div class="modal">',
|
|
|
|
' <div class="modal-dialog">',
|
|
|
|
' <button type="button" data-bs-dismiss="modal"></button>',
|
|
|
|
' </div>',
|
|
|
|
'</div>'
|
|
|
|
].join('')
|
|
|
|
|
|
|
|
const modalEl = fixtureEl.querySelector('.modal')
|
|
|
|
const btnClose = fixtureEl.querySelector('button[data-bs-dismiss="modal"]')
|
|
|
|
const modal = new Modal(modalEl)
|
|
|
|
|
2022-05-31 10:18:32 +02:00
|
|
|
const spy = spyOn(Event.prototype, 'preventDefault').and.callThrough()
|
2022-01-30 13:30:04 +01:00
|
|
|
|
|
|
|
modalEl.addEventListener('shown.bs.modal', () => {
|
|
|
|
btnClose.click()
|
|
|
|
})
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modalEl.addEventListener('hidden.bs.modal', () => {
|
2022-05-31 10:18:32 +02:00
|
|
|
expect(spy).not.toHaveBeenCalled()
|
2022-01-30 13:30:04 +01:00
|
|
|
resolve()
|
|
|
|
})
|
2021-05-18 08:26:22 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modal.show()
|
2021-05-18 08:26:22 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
it('should prevent default when a click occurred on data-bs-dismiss="modal" where tagName is <a> or <area>', () => {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
fixtureEl.innerHTML = [
|
|
|
|
'<div class="modal">',
|
|
|
|
' <div class="modal-dialog">',
|
|
|
|
' <a type="button" data-bs-dismiss="modal"></a>',
|
|
|
|
' </div>',
|
|
|
|
'</div>'
|
|
|
|
].join('')
|
2021-05-18 08:26:22 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
const modalEl = fixtureEl.querySelector('.modal')
|
|
|
|
const btnClose = fixtureEl.querySelector('a[data-bs-dismiss="modal"]')
|
|
|
|
const modal = new Modal(modalEl)
|
2021-05-18 08:26:22 +02:00
|
|
|
|
2022-05-31 10:18:32 +02:00
|
|
|
const spy = spyOn(Event.prototype, 'preventDefault').and.callThrough()
|
2021-05-18 08:26:22 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modalEl.addEventListener('shown.bs.modal', () => {
|
|
|
|
btnClose.click()
|
|
|
|
})
|
2021-05-18 08:26:22 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modalEl.addEventListener('hidden.bs.modal', () => {
|
2022-05-31 10:18:32 +02:00
|
|
|
expect(spy).toHaveBeenCalled()
|
2022-01-30 13:30:04 +01:00
|
|
|
resolve()
|
|
|
|
})
|
2021-05-18 08:26:22 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modal.show()
|
|
|
|
})
|
2021-05-18 08:26:22 +02:00
|
|
|
})
|
2022-01-30 13:30:04 +01:00
|
|
|
it('should not focus the trigger if the modal is not visible', () => {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
fixtureEl.innerHTML = [
|
|
|
|
'<a data-bs-toggle="modal" href="#" data-bs-target="#exampleModal" style="display: none;"></a>',
|
|
|
|
'<div id="exampleModal" class="modal" style="display: none;"><div class="modal-dialog"></div></div>'
|
|
|
|
].join('')
|
2021-05-18 08:26:22 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
const modalEl = fixtureEl.querySelector('.modal')
|
|
|
|
const trigger = fixtureEl.querySelector('[data-bs-toggle="modal"]')
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-05-31 10:18:32 +02:00
|
|
|
const spy = spyOn(trigger, 'focus')
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modalEl.addEventListener('shown.bs.modal', () => {
|
|
|
|
const modal = Modal.getInstance(modalEl)
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modal.hide()
|
|
|
|
})
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
const hideListener = () => {
|
|
|
|
setTimeout(() => {
|
2022-05-31 10:18:32 +02:00
|
|
|
expect(spy).not.toHaveBeenCalled()
|
2022-01-30 13:30:04 +01:00
|
|
|
resolve()
|
|
|
|
}, 20)
|
|
|
|
}
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modalEl.addEventListener('hidden.bs.modal', () => {
|
|
|
|
hideListener()
|
|
|
|
})
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
trigger.click()
|
2019-05-01 15:43:40 +02:00
|
|
|
})
|
|
|
|
})
|
2022-01-30 13:30:04 +01:00
|
|
|
it('should not focus the trigger if the modal is not shown', () => {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
fixtureEl.innerHTML = [
|
|
|
|
'<a data-bs-toggle="modal" href="#" data-bs-target="#exampleModal"></a>',
|
|
|
|
'<div id="exampleModal" class="modal"><div class="modal-dialog"></div></div>'
|
|
|
|
].join('')
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
const modalEl = fixtureEl.querySelector('.modal')
|
|
|
|
const trigger = fixtureEl.querySelector('[data-bs-toggle="modal"]')
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-05-31 10:18:32 +02:00
|
|
|
const spy = spyOn(trigger, 'focus')
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
const showListener = () => {
|
|
|
|
setTimeout(() => {
|
2022-05-31 10:18:32 +02:00
|
|
|
expect(spy).not.toHaveBeenCalled()
|
2022-01-30 13:30:04 +01:00
|
|
|
resolve()
|
|
|
|
}, 10)
|
|
|
|
}
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
modalEl.addEventListener('show.bs.modal', event => {
|
|
|
|
event.preventDefault()
|
|
|
|
showListener()
|
|
|
|
})
|
2019-05-01 15:43:40 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
trigger.click()
|
2019-05-01 15:43:40 +02:00
|
|
|
})
|
|
|
|
})
|
2021-08-10 16:55:34 +02:00
|
|
|
|
2022-01-30 13:30:04 +01:00
|
|
|
it('should call hide first, if another modal is open', () => {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
fixtureEl.innerHTML = [
|
|
|
|
'<button data-bs-toggle="modal" data-bs-target="#modal2"></button>',
|
|
|
|
'<div id="modal1" class="modal fade"><div class="modal-dialog"></div></div>',
|
|
|
|
'<div id="modal2" class="modal"><div class="modal-dialog"></div></div>'
|
|
|
|
].join('')
|
|
|
|
|
|
|
|
const trigger2 = fixtureEl.querySelector('button')
|
|
|
|
const modalEl1 = document.querySelector('#modal1')
|
|
|
|
const modalEl2 = document.querySelector('#modal2')
|
|
|
|
const modal1 = new Modal(modalEl1)
|
|
|
|
|
|
|
|
modalEl1.addEventListener('shown.bs.modal', () => {
|
|
|
|
trigger2.click()
|
|
|
|
})
|
|
|
|
modalEl1.addEventListener('hidden.bs.modal', () => {
|
|
|
|
expect(Modal.getInstance(modalEl2)).not.toBeNull()
|
|
|
|
expect(modalEl2).toHaveClass('show')
|
|
|
|
resolve()
|
|
|
|
})
|
|
|
|
modal1.show()
|
2021-08-10 16:55:34 +02:00
|
|
|
})
|
|
|
|
})
|
2019-05-01 15:43:40 +02:00
|
|
|
})
|
2019-07-28 15:24:46 +02:00
|
|
|
describe('jQueryInterface', () => {
|
2019-05-01 15:43:40 +02:00
|
|
|
it('should create a modal', () => {
|
2020-05-11 15:54:56 +02:00
|
|
|
fixtureEl.innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
|
2019-05-01 15:43:40 +02:00
|
|
|
|
|
|
|
const div = fixtureEl.querySelector('div')
|
|
|
|
|
2019-07-28 15:24:46 +02:00
|
|
|
jQueryMock.fn.modal = Modal.jQueryInterface
|
2019-05-01 15:43:40 +02:00
|
|
|
jQueryMock.elements = [div]
|
|
|
|
|
|
|
|
jQueryMock.fn.modal.call(jQueryMock)
|
|
|
|
|
2021-05-11 07:45:57 +02:00
|
|
|
expect(Modal.getInstance(div)).not.toBeNull()
|
2019-05-01 15:43:40 +02:00
|
|
|
})
|
|
|
|
|
2021-03-03 01:17:48 +01:00
|
|
|
it('should create a modal with given config', () => {
|
|
|
|
fixtureEl.innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
|
|
|
|
|
|
|
|
const div = fixtureEl.querySelector('div')
|
|
|
|
|
|
|
|
jQueryMock.fn.modal = Modal.jQueryInterface
|
|
|
|
jQueryMock.elements = [div]
|
|
|
|
|
|
|
|
jQueryMock.fn.modal.call(jQueryMock, { keyboard: false })
|
2022-05-31 10:18:32 +02:00
|
|
|
const spy = spyOn(Modal.prototype, 'constructor')
|
|
|
|
expect(spy).not.toHaveBeenCalledWith(div, { keyboard: false })
|
2021-03-03 01:17:48 +01:00
|
|
|
|
|
|
|
const modal = Modal.getInstance(div)
|
2021-05-11 07:45:57 +02:00
|
|
|
expect(modal).not.toBeNull()
|
2021-10-14 17:16:54 +02:00
|
|
|
expect(modal._config.keyboard).toBeFalse()
|
2021-03-03 01:17:48 +01:00
|
|
|
})
|
|
|
|
|
2019-05-01 15:43:40 +02:00
|
|
|
it('should not re create a modal', () => {
|
2020-05-11 15:54:56 +02:00
|
|
|
fixtureEl.innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
|
2019-05-01 15:43:40 +02:00
|
|
|
|
|
|
|
const div = fixtureEl.querySelector('div')
|
|
|
|
const modal = new Modal(div)
|
|
|
|
|
2019-07-28 15:24:46 +02:00
|
|
|
jQueryMock.fn.modal = Modal.jQueryInterface
|
2019-05-01 15:43:40 +02:00
|
|
|
jQueryMock.elements = [div]
|
|
|
|
|
|
|
|
jQueryMock.fn.modal.call(jQueryMock)
|
|
|
|
|
2019-07-28 15:24:46 +02:00
|
|
|
expect(Modal.getInstance(div)).toEqual(modal)
|
2019-05-01 15:43:40 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should throw error on undefined method', () => {
|
2020-05-11 15:54:56 +02:00
|
|
|
fixtureEl.innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
|
2019-05-01 15:43:40 +02:00
|
|
|
|
|
|
|
const div = fixtureEl.querySelector('div')
|
|
|
|
const action = 'undefinedMethod'
|
|
|
|
|
2019-07-28 15:24:46 +02:00
|
|
|
jQueryMock.fn.modal = Modal.jQueryInterface
|
2019-05-01 15:43:40 +02:00
|
|
|
jQueryMock.elements = [div]
|
|
|
|
|
2021-01-13 21:13:30 +01:00
|
|
|
expect(() => {
|
2019-05-01 15:43:40 +02:00
|
|
|
jQueryMock.fn.modal.call(jQueryMock, action)
|
2021-01-13 21:13:30 +01:00
|
|
|
}).toThrowError(TypeError, `No method named "${action}"`)
|
2019-05-01 15:43:40 +02:00
|
|
|
})
|
|
|
|
|
2020-11-20 19:10:15 +01:00
|
|
|
it('should call show method', () => {
|
2020-05-11 15:54:56 +02:00
|
|
|
fixtureEl.innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
|
2019-05-01 15:43:40 +02:00
|
|
|
|
|
|
|
const div = fixtureEl.querySelector('div')
|
|
|
|
const modal = new Modal(div)
|
|
|
|
|
2019-07-28 15:24:46 +02:00
|
|
|
jQueryMock.fn.modal = Modal.jQueryInterface
|
2019-05-01 15:43:40 +02:00
|
|
|
jQueryMock.elements = [div]
|
|
|
|
|
2022-05-31 10:18:32 +02:00
|
|
|
const spy = spyOn(modal, 'show')
|
2019-05-01 15:43:40 +02:00
|
|
|
|
|
|
|
jQueryMock.fn.modal.call(jQueryMock, 'show')
|
|
|
|
|
2022-05-31 10:18:32 +02:00
|
|
|
expect(spy).toHaveBeenCalled()
|
2019-05-01 15:43:40 +02:00
|
|
|
})
|
|
|
|
|
2020-11-20 19:10:15 +01:00
|
|
|
it('should not call show method', () => {
|
|
|
|
fixtureEl.innerHTML = '<div class="modal" data-bs-show="false"><div class="modal-dialog"></div></div>'
|
2019-05-01 15:43:40 +02:00
|
|
|
|
|
|
|
const div = fixtureEl.querySelector('div')
|
|
|
|
|
2019-07-28 15:24:46 +02:00
|
|
|
jQueryMock.fn.modal = Modal.jQueryInterface
|
2019-05-01 15:43:40 +02:00
|
|
|
jQueryMock.elements = [div]
|
|
|
|
|
2022-05-31 10:18:32 +02:00
|
|
|
const spy = spyOn(Modal.prototype, 'show')
|
2019-05-01 15:43:40 +02:00
|
|
|
|
|
|
|
jQueryMock.fn.modal.call(jQueryMock)
|
|
|
|
|
2022-05-31 10:18:32 +02:00
|
|
|
expect(spy).not.toHaveBeenCalled()
|
2019-05-01 15:43:40 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-07-28 15:24:46 +02:00
|
|
|
describe('getInstance', () => {
|
2019-05-01 15:43:40 +02:00
|
|
|
it('should return modal instance', () => {
|
2020-05-11 15:54:56 +02:00
|
|
|
fixtureEl.innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
|
2019-05-01 15:43:40 +02:00
|
|
|
|
|
|
|
const div = fixtureEl.querySelector('div')
|
|
|
|
const modal = new Modal(div)
|
|
|
|
|
2019-07-28 15:24:46 +02:00
|
|
|
expect(Modal.getInstance(div)).toEqual(modal)
|
2020-11-16 16:23:09 +01:00
|
|
|
expect(Modal.getInstance(div)).toBeInstanceOf(Modal)
|
2019-05-01 15:43:40 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should return null when there is no modal instance', () => {
|
2020-05-11 15:54:56 +02:00
|
|
|
fixtureEl.innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
|
2019-05-01 15:43:40 +02:00
|
|
|
|
|
|
|
const div = fixtureEl.querySelector('div')
|
|
|
|
|
2021-05-11 07:45:57 +02:00
|
|
|
expect(Modal.getInstance(div)).toBeNull()
|
2019-05-01 15:43:40 +02:00
|
|
|
})
|
|
|
|
})
|
2021-06-03 17:53:27 +02:00
|
|
|
|
|
|
|
describe('getOrCreateInstance', () => {
|
|
|
|
it('should return modal instance', () => {
|
|
|
|
fixtureEl.innerHTML = '<div></div>'
|
|
|
|
|
|
|
|
const div = fixtureEl.querySelector('div')
|
|
|
|
const modal = new Modal(div)
|
|
|
|
|
|
|
|
expect(Modal.getOrCreateInstance(div)).toEqual(modal)
|
|
|
|
expect(Modal.getInstance(div)).toEqual(Modal.getOrCreateInstance(div, {}))
|
|
|
|
expect(Modal.getOrCreateInstance(div)).toBeInstanceOf(Modal)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should return new instance when there is no modal instance', () => {
|
|
|
|
fixtureEl.innerHTML = '<div></div>'
|
|
|
|
|
|
|
|
const div = fixtureEl.querySelector('div')
|
|
|
|
|
2021-10-14 17:16:54 +02:00
|
|
|
expect(Modal.getInstance(div)).toBeNull()
|
2021-06-03 17:53:27 +02:00
|
|
|
expect(Modal.getOrCreateInstance(div)).toBeInstanceOf(Modal)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should return new instance when there is no modal instance with given configuration', () => {
|
|
|
|
fixtureEl.innerHTML = '<div></div>'
|
|
|
|
|
|
|
|
const div = fixtureEl.querySelector('div')
|
|
|
|
|
2021-10-14 17:16:54 +02:00
|
|
|
expect(Modal.getInstance(div)).toBeNull()
|
2021-06-03 17:53:27 +02:00
|
|
|
const modal = Modal.getOrCreateInstance(div, {
|
|
|
|
backdrop: true
|
|
|
|
})
|
|
|
|
expect(modal).toBeInstanceOf(Modal)
|
|
|
|
|
2021-10-14 17:16:54 +02:00
|
|
|
expect(modal._config.backdrop).toBeTrue()
|
2021-06-03 17:53:27 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should return the instance when exists without given configuration', () => {
|
|
|
|
fixtureEl.innerHTML = '<div></div>'
|
|
|
|
|
|
|
|
const div = fixtureEl.querySelector('div')
|
|
|
|
const modal = new Modal(div, {
|
|
|
|
backdrop: true
|
|
|
|
})
|
|
|
|
expect(Modal.getInstance(div)).toEqual(modal)
|
|
|
|
|
|
|
|
const modal2 = Modal.getOrCreateInstance(div, {
|
|
|
|
backdrop: false
|
|
|
|
})
|
|
|
|
expect(modal).toBeInstanceOf(Modal)
|
|
|
|
expect(modal2).toEqual(modal)
|
|
|
|
|
2021-10-14 17:16:54 +02:00
|
|
|
expect(modal2._config.backdrop).toBeTrue()
|
2021-06-03 17:53:27 +02:00
|
|
|
})
|
|
|
|
})
|
2019-05-01 15:43:40 +02:00
|
|
|
})
|