import Modal from '../../src/modal' import EventHandler from '../../src/dom/event-handler' /** Test helpers */ import { getFixture, clearFixture, createEvent, jQueryMock } from '../helpers/fixture' describe('Modal', () => { let fixtureEl let style beforeAll(() => { fixtureEl = getFixture() // Enable the scrollbar measurer const css = '.modal-scrollbar-measure { position: absolute; top: -9999px; width: 50px; height: 50px; overflow: scroll; }' style = document.createElement('style') style.type = 'text/css' style.appendChild(document.createTextNode(css)) document.head.appendChild(style) // Simulate scrollbars document.documentElement.style.paddingRight = '16px' }) afterEach(() => { clearFixture() document.body.classList.remove('modal-open') document.body.removeAttribute('style') document.body.removeAttribute('data-padding-right') document.querySelectorAll('.modal-backdrop') .forEach(backdrop => { document.body.removeChild(backdrop) }) document.body.style.paddingRight = '0px' }) afterAll(() => { document.head.removeChild(style) document.documentElement.style.paddingRight = '0px' }) 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)) }) }) describe('toggle', () => { it('should toggle a modal', done => { fixtureEl.innerHTML = '' const modalEl = fixtureEl.querySelector('.modal') const modal = new Modal(modalEl) const originalPadding = '0px' document.body.style.paddingRight = originalPadding modalEl.addEventListener('shown.bs.modal', () => { expect(document.body.getAttribute('data-padding-right')).toEqual(originalPadding, 'original body padding should be stored in data-padding-right') modal.toggle() }) modalEl.addEventListener('hidden.bs.modal', () => { expect(document.body.getAttribute('data-padding-right')).toBeNull() expect().nothing() done() }) modal.toggle() }) it('should adjust the inline padding of fixed elements when opening and restore when closing', done => { fixtureEl.innerHTML = [ '
', '' ].join('') const fixedEl = fixtureEl.querySelector('.fixed-top') const originalPadding = parseInt(window.getComputedStyle(fixedEl).paddingRight, 10) const modalEl = fixtureEl.querySelector('.modal') const modal = new Modal(modalEl) modalEl.addEventListener('shown.bs.modal', () => { const expectedPadding = originalPadding + modal._getScrollbarWidth() const currentPadding = parseInt(window.getComputedStyle(modalEl).paddingRight, 10) expect(fixedEl.getAttribute('data-padding-right')).toEqual('0px', 'original fixed element padding should be stored in data-padding-right') expect(currentPadding).toEqual(expectedPadding, 'fixed element padding should be adjusted while opening') modal.toggle() }) modalEl.addEventListener('hidden.bs.modal', () => { const currentPadding = parseInt(window.getComputedStyle(modalEl).paddingRight, 10) expect(fixedEl.getAttribute('data-padding-right')).toEqual(null, 'data-padding-right should be cleared after closing') expect(currentPadding).toEqual(originalPadding, 'fixed element padding should be reset after closing') done() }) modal.toggle() }) it('should adjust the inline margin of sticky elements when opening and restore when closing', done => { fixtureEl.innerHTML = [ '
', '' ].join('') const stickyTopEl = fixtureEl.querySelector('.sticky-top') const originalMargin = parseInt(window.getComputedStyle(stickyTopEl).marginRight, 10) const modalEl = fixtureEl.querySelector('.modal') const modal = new Modal(modalEl) modalEl.addEventListener('shown.bs.modal', () => { const expectedMargin = originalMargin - modal._getScrollbarWidth() const currentMargin = parseInt(window.getComputedStyle(stickyTopEl).marginRight, 10) expect(stickyTopEl.getAttribute('data-margin-right')).toEqual('0px', 'original sticky element margin should be stored in data-margin-right') expect(currentMargin).toEqual(expectedMargin, 'sticky element margin should be adjusted while opening') modal.toggle() }) modalEl.addEventListener('hidden.bs.modal', () => { const currentMargin = parseInt(window.getComputedStyle(stickyTopEl).marginRight, 10) expect(stickyTopEl.getAttribute('data-margin-right')).toEqual(null, 'data-margin-right should be cleared after closing') expect(currentMargin).toEqual(originalMargin, 'sticky element margin should be reset after closing') done() }) modal.toggle() }) it('should ignore values set via CSS when trying to restore body padding after closing', done => { fixtureEl.innerHTML = '' const styleTest = document.createElement('style') styleTest.type = 'text/css' styleTest.appendChild(document.createTextNode('body { padding-right: 7px; }')) document.head.appendChild(styleTest) const modalEl = fixtureEl.querySelector('.modal') const modal = new Modal(modalEl) modalEl.addEventListener('shown.bs.modal', () => { modal.toggle() }) modalEl.addEventListener('hidden.bs.modal', () => { expect(window.getComputedStyle(document.body).paddingLeft).toEqual('0px', 'body does not have inline padding set') document.head.removeChild(styleTest) done() }) modal.toggle() }) it('should ignore other inline styles when trying to restore body padding after closing', done => { fixtureEl.innerHTML = '' const styleTest = document.createElement('style') styleTest.type = 'text/css' styleTest.appendChild(document.createTextNode('body { padding-right: 7px; }')) document.head.appendChild(styleTest) document.body.style.color = 'red' const modalEl = fixtureEl.querySelector('.modal') const modal = new Modal(modalEl) modalEl.addEventListener('shown.bs.modal', () => { modal.toggle() }) modalEl.addEventListener('hidden.bs.modal', () => { const bodyPaddingRight = document.body.style.paddingRight expect(bodyPaddingRight === '0px' || bodyPaddingRight === '').toEqual(true, 'body does not have inline padding set') expect(document.body.style.color).toEqual('red', 'body still has other inline styles set') document.head.removeChild(styleTest) document.body.removeAttribute('style') done() }) modal.toggle() }) it('should properly restore non-pixel inline body padding after closing', done => { fixtureEl.innerHTML = '' document.body.style.paddingRight = '5%' const modalEl = fixtureEl.querySelector('.modal') const modal = new Modal(modalEl) modalEl.addEventListener('shown.bs.modal', () => { modal.toggle() }) modalEl.addEventListener('hidden.bs.modal', () => { expect(document.body.style.paddingRight).toEqual('5%') document.body.removeAttribute('style') done() }) modal.toggle() }) }) describe('show', () => { it('should show a modal', done => { fixtureEl.innerHTML = '' const modalEl = fixtureEl.querySelector('.modal') const modal = new Modal(modalEl) modalEl.addEventListener('show.bs.modal', e => { expect(e).toBeDefined() }) modalEl.addEventListener('shown.bs.modal', () => { expect(modalEl.getAttribute('aria-modal')).toEqual('true') expect(modalEl.getAttribute('role')).toEqual('dialog') expect(modalEl.getAttribute('aria-hidden')).toEqual(null) expect(modalEl.style.display).toEqual('block') expect(document.querySelector('.modal-backdrop')).toBeDefined() done() }) modal.show() }) it('should show a modal without backdrop', done => { fixtureEl.innerHTML = '' const modalEl = fixtureEl.querySelector('.modal') const modal = new Modal(modalEl, { backdrop: false }) modalEl.addEventListener('show.bs.modal', e => { expect(e).toBeDefined() }) modalEl.addEventListener('shown.bs.modal', () => { expect(modalEl.getAttribute('aria-modal')).toEqual('true') expect(modalEl.getAttribute('role')).toEqual('dialog') expect(modalEl.getAttribute('aria-hidden')).toEqual(null) expect(modalEl.style.display).toEqual('block') expect(document.querySelector('.modal-backdrop')).toBeNull() done() }) modal.show() }) it('should show a modal and append the element', done => { const modalEl = document.createElement('div') const id = 'dynamicModal' modalEl.setAttribute('id', id) modalEl.classList.add('modal') modalEl.innerHTML = '' const modal = new Modal(modalEl) modalEl.addEventListener('shown.bs.modal', () => { const dynamicModal = document.getElementById(id) expect(dynamicModal).toBeDefined() dynamicModal.parentNode.removeChild(dynamicModal) done() }) modal.show() }) it('should do nothing if a modal is shown', () => { fixtureEl.innerHTML = '' const modalEl = fixtureEl.querySelector('.modal') const modal = new Modal(modalEl) spyOn(EventHandler, 'trigger') modal._isShown = true modal.show() expect(EventHandler.trigger).not.toHaveBeenCalled() }) it('should do nothing if a modal is transitioning', () => { fixtureEl.innerHTML = '' const modalEl = fixtureEl.querySelector('.modal') const modal = new Modal(modalEl) spyOn(EventHandler, 'trigger') modal._isTransitioning = true modal.show() expect(EventHandler.trigger).not.toHaveBeenCalled() }) it('should not fire shown event when show is prevented', done => { fixtureEl.innerHTML = '' const modalEl = fixtureEl.querySelector('.modal') const modal = new Modal(modalEl) modalEl.addEventListener('show.bs.modal', e => { e.preventDefault() const expectedDone = () => { expect().nothing() done() } setTimeout(expectedDone, 10) }) modalEl.addEventListener('shown.bs.modal', () => { throw new Error('shown event triggered') }) modal.show() }) it('should set is transitioning if fade class is present', done => { fixtureEl.innerHTML = '