diff --git a/build/build-plugins.js b/build/build-plugins.js index bee6aa51fffd..cc9226c00094 100644 --- a/build/build-plugins.js +++ b/build/build-plugins.js @@ -34,7 +34,7 @@ const bsPlugins = { Alert: path.resolve(__dirname, '../js/src/alert/alert.js'), Button: path.resolve(__dirname, '../js/src/button/button.js'), Carousel: path.resolve(__dirname, '../js/src/carousel/carousel.js'), - Collapse: path.resolve(__dirname, '../js/src/collapse.js'), + Collapse: path.resolve(__dirname, '../js/src/collapse/collapse.js'), Dropdown: path.resolve(__dirname, '../js/src/dropdown.js'), Modal: path.resolve(__dirname, '../js/src/modal.js'), Popover: path.resolve(__dirname, '../js/src/popover.js'), diff --git a/js/index.esm.js b/js/index.esm.js index 4f5058560da7..bb8b7509e656 100644 --- a/js/index.esm.js +++ b/js/index.esm.js @@ -8,7 +8,7 @@ import Alert from './src/alert/alert' import Button from './src/button/button' import Carousel from './src/carousel/carousel' -import Collapse from './src/collapse' +import Collapse from './src/collapse/collapse' import Dropdown from './src/dropdown' import Modal from './src/modal' import Popover from './src/popover' diff --git a/js/index.umd.js b/js/index.umd.js index f3b81377e5a6..56f1a32a7db3 100644 --- a/js/index.umd.js +++ b/js/index.umd.js @@ -8,7 +8,7 @@ import Alert from './src/alert/alert' import Button from './src/button/button' import Carousel from './src/carousel/carousel' -import Collapse from './src/collapse' +import Collapse from './src/collapse/collapse' import Dropdown from './src/dropdown' import Modal from './src/modal' import Popover from './src/popover' diff --git a/js/src/collapse.js b/js/src/collapse/collapse.js similarity index 98% rename from js/src/collapse.js rename to js/src/collapse/collapse.js index 372869b6923e..0e4e29d7f3a4 100644 --- a/js/src/collapse.js +++ b/js/src/collapse/collapse.js @@ -15,11 +15,11 @@ import { makeArray, reflow, typeCheckConfig -} from './util/index' -import Data from './dom/data' -import EventHandler from './dom/eventHandler' -import Manipulator from './dom/manipulator' -import SelectorEngine from './dom/selectorEngine' +} from '../util/index' +import Data from '../dom/data' +import EventHandler from '../dom/eventHandler' +import Manipulator from '../dom/manipulator' +import SelectorEngine from '../dom/selectorEngine' /** * ------------------------------------------------------------------------ @@ -431,7 +431,7 @@ EventHandler.on(document, Event.CLICK_DATA_API, Selector.DATA_TOGGLE, function ( * ------------------------------------------------------------------------ * add .collapse to jQuery only if jQuery is present */ - +/* istanbul ignore if */ if (typeof $ !== 'undefined') { const JQUERY_NO_CONFLICT = $.fn[NAME] $.fn[NAME] = Collapse._jQueryInterface diff --git a/js/src/collapse/collapse.spec.js b/js/src/collapse/collapse.spec.js new file mode 100644 index 000000000000..a4d5c58b4ae5 --- /dev/null +++ b/js/src/collapse/collapse.spec.js @@ -0,0 +1,222 @@ +import Collapse from './collapse' +import EventHandler from '../dom/eventHandler' + +/** Test helpers */ +import { getFixture, clearFixture } from '../../tests/helpers/fixture' + +describe('Collapse', () => { + let fixtureEl + + beforeAll(() => { + fixtureEl = getFixture() + }) + + afterEach(() => { + clearFixture() + }) + + describe('VERSION', () => { + it('should return plugin version', () => { + expect(Collapse.VERSION).toEqual(jasmine.any(String)) + }) + }) + + describe('Default', () => { + it('should return plugin default config', () => { + expect(Collapse.Default).toEqual(jasmine.any(Object)) + }) + }) + + describe('toggle', () => { + it('should call show method if show class is not present', () => { + fixtureEl.innerHTML = '
' + + const collapseEl = fixtureEl.querySelector('div') + const collapse = new Collapse(collapseEl) + + spyOn(collapse, 'show') + + collapse.toggle() + + expect(collapse.show).toHaveBeenCalled() + }) + + it('should call hide method if show class is present', () => { + fixtureEl.innerHTML = '' + + const collapseEl = fixtureEl.querySelector('.show') + const collapse = new Collapse(collapseEl, { + toggle: false + }) + + spyOn(collapse, 'hide') + + collapse.toggle() + + expect(collapse.hide).toHaveBeenCalled() + }) + }) + + describe('show', () => { + it('should do nothing if is transitioning', () => { + fixtureEl.innerHTML = '' + + spyOn(EventHandler, 'trigger') + + const collapseEl = fixtureEl.querySelector('div') + const collapse = new Collapse(collapseEl, { + toggle: false + }) + + collapse._isTransitioning = true + collapse.show() + + expect(EventHandler.trigger).not.toHaveBeenCalled() + }) + + it('should do nothing if already shown', () => { + fixtureEl.innerHTML = '' + + spyOn(EventHandler, 'trigger') + + const collapseEl = fixtureEl.querySelector('div') + const collapse = new Collapse(collapseEl, { + toggle: false + }) + + collapse.show() + + expect(EventHandler.trigger).not.toHaveBeenCalled() + }) + + it('should show a collapsed element', done => { + fixtureEl.innerHTML = '' + + const collapseEl = fixtureEl.querySelector('div') + const collapse = new Collapse(collapseEl, { + toggle: false + }) + + collapseEl.addEventListener('show.bs.collapse', () => { + expect(collapseEl.style.height).toEqual('0px') + }) + collapseEl.addEventListener('shown.bs.collapse', () => { + expect(collapseEl.classList.contains('show')).toEqual(true) + expect(collapseEl.style.height).toEqual('') + done() + }) + + collapse.show() + }) + + it('should collapse only the first collapse', done => { + fixtureEl.innerHTML = [ + '