From 1ac07a66ce56e6ee3d2b48a649e957417f56c83e Mon Sep 17 00:00:00 2001 From: Johann-S Date: Wed, 1 May 2019 15:43:40 +0200 Subject: [PATCH] rewrite modal unit tests --- build/build-plugins.js | 2 +- js/index.esm.js | 2 +- js/index.umd.js | 2 +- js/src/{ => modal}/modal.js | 53 +- js/src/modal/modal.spec.js | 974 ++++++++++++++++++++++++++++++++++++ js/tests/unit/modal.js | 816 ------------------------------ 6 files changed, 997 insertions(+), 852 deletions(-) rename js/src/{ => modal}/modal.js (95%) create mode 100644 js/src/modal/modal.spec.js delete mode 100644 js/tests/unit/modal.js diff --git a/build/build-plugins.js b/build/build-plugins.js index 6989e4f4db..d0d473f666 100644 --- a/build/build-plugins.js +++ b/build/build-plugins.js @@ -37,7 +37,7 @@ const bsPlugins = { Carousel: path.resolve(__dirname, '../js/src/carousel/carousel.js'), Collapse: path.resolve(__dirname, '../js/src/collapse/collapse.js'), Dropdown: path.resolve(__dirname, '../js/src/dropdown/dropdown.js'), - Modal: path.resolve(__dirname, '../js/src/modal.js'), + Modal: path.resolve(__dirname, '../js/src/modal/modal.js'), Popover: path.resolve(__dirname, '../js/src/popover.js'), ScrollSpy: path.resolve(__dirname, '../js/src/scrollspy.js'), Tab: path.resolve(__dirname, '../js/src/tab.js'), diff --git a/js/index.esm.js b/js/index.esm.js index 3c3def944f..c2912d1910 100644 --- a/js/index.esm.js +++ b/js/index.esm.js @@ -10,7 +10,7 @@ import Button from './src/button/button' import Carousel from './src/carousel/carousel' import Collapse from './src/collapse/collapse' import Dropdown from './src/dropdown/dropdown' -import Modal from './src/modal' +import Modal from './src/modal/modal' import Popover from './src/popover' import ScrollSpy from './src/scrollspy' import Tab from './src/tab' diff --git a/js/index.umd.js b/js/index.umd.js index ed6ba1e1d3..eafc90141d 100644 --- a/js/index.umd.js +++ b/js/index.umd.js @@ -10,7 +10,7 @@ import Button from './src/button/button' import Carousel from './src/carousel/carousel' import Collapse from './src/collapse/collapse' import Dropdown from './src/dropdown/dropdown' -import Modal from './src/modal' +import Modal from './src/modal/modal' import Popover from './src/popover' import ScrollSpy from './src/scrollspy' import Tab from './src/tab' diff --git a/js/src/modal.js b/js/src/modal/modal.js similarity index 95% rename from js/src/modal.js rename to js/src/modal/modal.js index 9943f93e09..4c430a01f5 100644 --- a/js/src/modal.js +++ b/js/src/modal/modal.js @@ -15,11 +15,11 @@ import { makeArray, reflow, typeCheckConfig -} from './util/index' -import Data from './dom/data' -import EventHandler from './dom/event-handler' -import Manipulator from './dom/manipulator' -import SelectorEngine from './dom/selector-engine' +} from '../util/index' +import Data from '../dom/data' +import EventHandler from '../dom/event-handler' +import Manipulator from '../dom/manipulator' +import SelectorEngine from '../dom/selector-engine' /** * ------------------------------------------------------------------------ @@ -171,7 +171,7 @@ class Modal { const hideEvent = EventHandler.trigger(this._element, Event.HIDE) - if (!this._isShown || hideEvent.defaultPrevented) { + if (hideEvent.defaultPrevented) { return } @@ -310,14 +310,14 @@ class Modal { this.hide() } }) - } else if (!this._isShown) { + } else { EventHandler.off(this._element, Event.KEYDOWN_DISMISS) } } _setResizeEvent() { if (this._isShown) { - EventHandler.on(window, Event.RESIZE, event => this.handleUpdate(event)) + EventHandler.on(window, Event.RESIZE, () => this._adjustDialog()) } else { EventHandler.off(window, Event.RESIZE) } @@ -337,10 +337,8 @@ class Modal { } _removeBackdrop() { - if (this._backdrop) { - this._backdrop.parentNode.removeChild(this._backdrop) - this._backdrop = null - } + this._backdrop.parentNode.removeChild(this._backdrop) + this._backdrop = null } _showBackdrop(callback) { @@ -381,10 +379,6 @@ class Modal { this._backdrop.classList.add(ClassName.SHOW) - if (!callback) { - return - } - if (!animate) { callback() return @@ -399,9 +393,7 @@ class Modal { const callbackRemove = () => { this._removeBackdrop() - if (callback) { - callback() - } + callback() } if (this._element.classList.contains(ClassName.FADE)) { @@ -411,7 +403,7 @@ class Modal { } else { callbackRemove() } - } else if (callback) { + } else { callback() } } @@ -557,19 +549,8 @@ class Modal { */ EventHandler.on(document, Event.CLICK_DATA_API, Selector.DATA_TOGGLE, function (event) { - let target const selector = getSelectorFromElement(this) - - if (selector) { - target = SelectorEngine.findOne(selector) - } - - const config = Data.getData(target, DATA_KEY) ? - 'toggle' : - { - ...Manipulator.getDataAttributes(target), - ...Manipulator.getDataAttributes(this) - } + const target = SelectorEngine.findOne(selector) if (this.tagName === 'A' || this.tagName === 'AREA') { event.preventDefault() @@ -590,6 +571,11 @@ EventHandler.on(document, Event.CLICK_DATA_API, Selector.DATA_TOGGLE, function ( let data = Data.getData(target, DATA_KEY) if (!data) { + const config = { + ...Manipulator.getDataAttributes(target), + ...Manipulator.getDataAttributes(this) + } + data = new Modal(target, config) } @@ -600,8 +586,9 @@ EventHandler.on(document, Event.CLICK_DATA_API, Selector.DATA_TOGGLE, function ( * ------------------------------------------------------------------------ * jQuery * ------------------------------------------------------------------------ + * add .modal to jQuery only if jQuery is present */ - +/* istanbul ignore if */ if (typeof $ !== 'undefined') { const JQUERY_NO_CONFLICT = $.fn[NAME] $.fn[NAME] = Modal._jQueryInterface diff --git a/js/src/modal/modal.spec.js b/js/src/modal/modal.spec.js new file mode 100644 index 0000000000..f1311c5918 --- /dev/null +++ b/js/src/modal/modal.spec.js @@ -0,0 +1,974 @@ +import Modal from './modal' +import EventHandler from '../dom/event-handler' +import { makeArray } from '../util/index' + +/** Test helpers */ +import { getFixture, clearFixture, createEvent, jQueryMock } from '../../tests/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') + const backdropList = makeArray(document.querySelectorAll('.modal-backdrop')) + + backdropList.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 = '