diff --git a/build/build-plugins.js b/build/build-plugins.js
index d60c1f53d4..c071f7f28c 100644
--- a/build/build-plugins.js
+++ b/build/build-plugins.js
@@ -36,7 +36,7 @@ const bsPlugins = {
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/collapse.js'),
- Dropdown: path.resolve(__dirname, '../js/src/dropdown.js'),
+ Dropdown: path.resolve(__dirname, '../js/src/dropdown/dropdown.js'),
Modal: path.resolve(__dirname, '../js/src/modal.js'),
Popover: path.resolve(__dirname, '../js/src/popover.js'),
ScrollSpy: path.resolve(__dirname, '../js/src/scrollspy.js'),
diff --git a/js/index.esm.js b/js/index.esm.js
index bb8b7509e6..28c1ecd484 100644
--- a/js/index.esm.js
+++ b/js/index.esm.js
@@ -9,7 +9,7 @@ import Alert from './src/alert/alert'
import Button from './src/button/button'
import Carousel from './src/carousel/carousel'
import Collapse from './src/collapse/collapse'
-import Dropdown from './src/dropdown'
+import Dropdown from './src/dropdown/dropdown'
import Modal from './src/modal'
import Popover from './src/popover'
import ScrollSpy from './src/scrollspy'
diff --git a/js/index.umd.js b/js/index.umd.js
index 56f1a32a7d..5066a75312 100644
--- a/js/index.umd.js
+++ b/js/index.umd.js
@@ -9,7 +9,7 @@ import Alert from './src/alert/alert'
import Button from './src/button/button'
import Carousel from './src/carousel/carousel'
import Collapse from './src/collapse/collapse'
-import Dropdown from './src/dropdown'
+import Dropdown from './src/dropdown/dropdown'
import Modal from './src/modal'
import Popover from './src/popover'
import ScrollSpy from './src/scrollspy'
diff --git a/js/src/dropdown.js b/js/src/dropdown/dropdown.js
similarity index 97%
rename from js/src/dropdown.js
rename to js/src/dropdown/dropdown.js
index 729b64732a..8607afe7f5 100644
--- a/js/src/dropdown.js
+++ b/js/src/dropdown/dropdown.js
@@ -12,12 +12,12 @@ import {
makeArray,
noop,
typeCheckConfig
-} from './util/index'
-import Data from './dom/data'
-import EventHandler from './dom/event-handler'
-import Manipulator from './dom/manipulator'
+} from '../util/index'
+import Data from '../dom/data'
+import EventHandler from '../dom/event-handler'
+import Manipulator from '../dom/manipulator'
import Popper from 'popper.js'
-import SelectorEngine from './dom/selector-engine'
+import SelectorEngine from '../dom/selector-engine'
/**
* ------------------------------------------------------------------------
@@ -289,15 +289,9 @@ class Dropdown {
}
_getMenuElement() {
- if (!this._menu) {
- const parent = Dropdown._getParentFromElement(this._element)
+ const parent = Dropdown._getParentFromElement(this._element)
- if (parent) {
- this._menu = SelectorEngine.findOne(Selector.MENU, parent)
- }
- }
-
- return this._menu
+ return SelectorEngine.findOne(Selector.MENU, parent)
}
_getPlacement() {
@@ -545,7 +539,7 @@ EventHandler
* ------------------------------------------------------------------------
* add .dropdown to jQuery only if jQuery is present
*/
-
+/* istanbul ignore if */
if (typeof $ !== 'undefined') {
const JQUERY_NO_CONFLICT = $.fn[NAME]
$.fn[NAME] = Dropdown._jQueryInterface
diff --git a/js/src/dropdown/dropdown.spec.js b/js/src/dropdown/dropdown.spec.js
new file mode 100644
index 0000000000..692b270908
--- /dev/null
+++ b/js/src/dropdown/dropdown.spec.js
@@ -0,0 +1,1494 @@
+import Popper from 'popper.js'
+
+import Dropdown from './dropdown'
+import EventHandler from '../dom/event-handler'
+
+/** Test helpers */
+import { getFixture, clearFixture, createEvent, jQueryMock } from '../../tests/helpers/fixture'
+
+describe('Dropdown', () => {
+ let fixtureEl
+
+ beforeAll(() => {
+ fixtureEl = getFixture()
+ })
+
+ afterEach(() => {
+ clearFixture()
+ })
+
+ describe('VERSION', () => {
+ it('should return plugin version', () => {
+ expect(Dropdown.VERSION).toEqual(jasmine.any(String))
+ })
+ })
+
+ describe('Default', () => {
+ it('should return plugin default config', () => {
+ expect(Dropdown.Default).toEqual(jasmine.any(Object))
+ })
+ })
+
+ describe('DefaultType', () => {
+ it('should return plugin default type config', () => {
+ expect(Dropdown.DefaultType).toEqual(jasmine.any(Object))
+ })
+ })
+
+ describe('constructor', () => {
+ it('should create offset modifier correctly when offset option is a function', () => {
+ fixtureEl.innerHTML = [
+ '
',
+ ' ',
+ ' ',
+ '
'
+ ].join('')
+
+ const getOffset = offsets => offsets
+ const btnDropdown = fixtureEl.querySelector('[data-toggle="dropdown"]')
+ const dropdown = new Dropdown(btnDropdown, {
+ offset: getOffset
+ })
+
+ const offset = dropdown._getOffset()
+
+ expect(offset.offset).toBeUndefined()
+ expect(typeof offset.fn).toEqual('function')
+ })
+
+ it('should create offset modifier correctly when offset option is not a function', () => {
+ fixtureEl.innerHTML = [
+ '',
+ ' ',
+ ' ',
+ '
'
+ ].join('')
+
+ const myOffset = 7
+ const btnDropdown = fixtureEl.querySelector('[data-toggle="dropdown"]')
+ const dropdown = new Dropdown(btnDropdown, {
+ offset: myOffset
+ })
+
+ const offset = dropdown._getOffset()
+
+ expect(offset.offset).toEqual(myOffset)
+ expect(offset.fn).toBeUndefined()
+ })
+
+ it('should add a listener on trigger which do not have data-toggle="dropdown"', () => {
+ fixtureEl.innerHTML = [
+ '',
+ ' ',
+ ' ',
+ '
'
+ ].join('')
+
+ const btnDropdown = fixtureEl.querySelector('.btn')
+ const dropdown = new Dropdown(btnDropdown)
+
+ spyOn(dropdown, 'toggle')
+
+ btnDropdown.click()
+
+ expect(dropdown.toggle).toHaveBeenCalled()
+ })
+ })
+
+ describe('toggle', () => {
+ it('should toggle a dropdown', done => {
+ fixtureEl.innerHTML = [
+ '',
+ ' ',
+ ' ',
+ '
'
+ ].join('')
+
+ const btnDropdown = fixtureEl.querySelector('[data-toggle="dropdown"]')
+ const dropdownEl = fixtureEl.querySelector('.dropdown')
+ const dropdown = new Dropdown(btnDropdown)
+
+ dropdownEl.addEventListener('shown.bs.dropdown', () => {
+ expect(dropdownEl.classList.contains('show')).toEqual(true)
+ expect(btnDropdown.getAttribute('aria-expanded')).toEqual('true')
+ done()
+ })
+
+ dropdown.toggle()
+ })
+
+ it('should toggle a dropdown and add/remove event listener on mobile', done => {
+ fixtureEl.innerHTML = [
+ '',
+ ' ',
+ ' ',
+ '
'
+ ].join('')
+
+ const defaultValueOnTouchStart = document.documentElement.ontouchstart
+ const btnDropdown = fixtureEl.querySelector('[data-toggle="dropdown"]')
+ const dropdownEl = fixtureEl.querySelector('.dropdown')
+ const dropdown = new Dropdown(btnDropdown)
+
+ document.documentElement.ontouchstart = () => {}
+ spyOn(EventHandler, 'on')
+ spyOn(EventHandler, 'off')
+
+ dropdownEl.addEventListener('shown.bs.dropdown', () => {
+ expect(dropdownEl.classList.contains('show')).toEqual(true)
+ expect(btnDropdown.getAttribute('aria-expanded')).toEqual('true')
+ expect(EventHandler.on).toHaveBeenCalled()
+
+ dropdown.toggle()
+ })
+
+ dropdownEl.addEventListener('hidden.bs.dropdown', () => {
+ expect(dropdownEl.classList.contains('show')).toEqual(false)
+ expect(btnDropdown.getAttribute('aria-expanded')).toEqual('false')
+ expect(EventHandler.off).toHaveBeenCalled()
+
+ document.documentElement.ontouchstart = defaultValueOnTouchStart
+ done()
+ })
+
+ dropdown.toggle()
+ })
+
+ it('should toggle a dropdown at the right', done => {
+ fixtureEl.innerHTML = [
+ '',
+ ' ',
+ ' ',
+ '
'
+ ].join('')
+
+ const btnDropdown = fixtureEl.querySelector('[data-toggle="dropdown"]')
+ const dropdownEl = fixtureEl.querySelector('.dropdown')
+ const dropdown = new Dropdown(btnDropdown)
+
+ dropdownEl.addEventListener('shown.bs.dropdown', () => {
+ expect(dropdownEl.classList.contains('show')).toEqual(true)
+ expect(btnDropdown.getAttribute('aria-expanded')).toEqual('true')
+ done()
+ })
+
+ dropdown.toggle()
+ })
+
+ it('should toggle a dropup', done => {
+ fixtureEl.innerHTML = [
+ '',
+ ' ',
+ ' ',
+ '
'
+ ].join('')
+
+ const btnDropdown = fixtureEl.querySelector('[data-toggle="dropdown"]')
+ const dropupEl = fixtureEl.querySelector('.dropup')
+ const dropdown = new Dropdown(btnDropdown)
+
+ dropupEl.addEventListener('shown.bs.dropdown', () => {
+ expect(dropupEl.classList.contains('show')).toEqual(true)
+ expect(btnDropdown.getAttribute('aria-expanded')).toEqual('true')
+ done()
+ })
+
+ dropdown.toggle()
+ })
+
+ it('should toggle a dropup at the right', done => {
+ fixtureEl.innerHTML = [
+ '',
+ ' ',
+ ' ',
+ '
'
+ ].join('')
+
+ const btnDropdown = fixtureEl.querySelector('[data-toggle="dropdown"]')
+ const dropupEl = fixtureEl.querySelector('.dropup')
+ const dropdown = new Dropdown(btnDropdown)
+
+ dropupEl.addEventListener('shown.bs.dropdown', () => {
+ expect(dropupEl.classList.contains('show')).toEqual(true)
+ expect(btnDropdown.getAttribute('aria-expanded')).toEqual('true')
+ done()
+ })
+
+ dropdown.toggle()
+ })
+
+ it('should toggle a dropright', done => {
+ fixtureEl.innerHTML = [
+ '',
+ ' ',
+ ' ',
+ '
'
+ ].join('')
+
+ const btnDropdown = fixtureEl.querySelector('[data-toggle="dropdown"]')
+ const droprightEl = fixtureEl.querySelector('.dropright')
+ const dropdown = new Dropdown(btnDropdown)
+
+ droprightEl.addEventListener('shown.bs.dropdown', () => {
+ expect(droprightEl.classList.contains('show')).toEqual(true)
+ expect(btnDropdown.getAttribute('aria-expanded')).toEqual('true')
+ done()
+ })
+
+ dropdown.toggle()
+ })
+
+ it('should toggle a dropleft', done => {
+ fixtureEl.innerHTML = [
+ '',
+ ' ',
+ ' ',
+ '
'
+ ].join('')
+
+ const btnDropdown = fixtureEl.querySelector('[data-toggle="dropdown"]')
+ const dropleftEl = fixtureEl.querySelector('.dropleft')
+ const dropdown = new Dropdown(btnDropdown)
+
+ dropleftEl.addEventListener('shown.bs.dropdown', () => {
+ expect(dropleftEl.classList.contains('show')).toEqual(true)
+ expect(btnDropdown.getAttribute('aria-expanded')).toEqual('true')
+ done()
+ })
+
+ dropdown.toggle()
+ })
+
+ it('should toggle a dropdown with parent reference', done => {
+ fixtureEl.innerHTML = [
+ '',
+ ' ',
+ ' ',
+ '
'
+ ].join('')
+
+ const btnDropdown = fixtureEl.querySelector('[data-toggle="dropdown"]')
+ const dropdownEl = fixtureEl.querySelector('.dropdown')
+ const dropdown = new Dropdown(btnDropdown, {
+ reference: 'parent'
+ })
+
+ dropdownEl.addEventListener('shown.bs.dropdown', () => {
+ expect(dropdownEl.classList.contains('show')).toEqual(true)
+ expect(btnDropdown.getAttribute('aria-expanded')).toEqual('true')
+ done()
+ })
+
+ dropdown.toggle()
+ })
+
+ it('should toggle a dropdown with a dom node reference', done => {
+ fixtureEl.innerHTML = [
+ '',
+ ' ',
+ ' ',
+ '
'
+ ].join('')
+
+ const btnDropdown = fixtureEl.querySelector('[data-toggle="dropdown"]')
+ const dropdownEl = fixtureEl.querySelector('.dropdown')
+ const dropdown = new Dropdown(btnDropdown, {
+ reference: fixtureEl
+ })
+
+ dropdownEl.addEventListener('shown.bs.dropdown', () => {
+ expect(dropdownEl.classList.contains('show')).toEqual(true)
+ expect(btnDropdown.getAttribute('aria-expanded')).toEqual('true')
+ done()
+ })
+
+ dropdown.toggle()
+ })
+
+ it('should toggle a dropdown with a jquery object reference', done => {
+ fixtureEl.innerHTML = [
+ '',
+ ' ',
+ ' ',
+ '
'
+ ].join('')
+
+ const btnDropdown = fixtureEl.querySelector('[data-toggle="dropdown"]')
+ const dropdownEl = fixtureEl.querySelector('.dropdown')
+ const dropdown = new Dropdown(btnDropdown, {
+ reference: { 0: fixtureEl, jquery: 'jQuery' }
+ })
+
+ dropdownEl.addEventListener('shown.bs.dropdown', () => {
+ expect(dropdownEl.classList.contains('show')).toEqual(true)
+ expect(btnDropdown.getAttribute('aria-expanded')).toEqual('true')
+ done()
+ })
+
+ dropdown.toggle()
+ })
+
+ it('should not toggle a dropdown if the element is disabled', done => {
+ fixtureEl.innerHTML = [
+ '',
+ ' ',
+ ' ',
+ '
'
+ ].join('')
+
+ const btnDropdown = fixtureEl.querySelector('[data-toggle="dropdown"]')
+ const dropdownEl = fixtureEl.querySelector('.dropdown')
+ const dropdown = new Dropdown(btnDropdown)
+
+ dropdownEl.addEventListener('shown.bs.dropdown', () => {
+ throw new Error('should not throw shown.bs.dropdown event')
+ })
+
+ dropdown.toggle()
+
+ setTimeout(() => {
+ expect().nothing()
+ done()
+ })
+ })
+
+ it('should not toggle a dropdown if the element contains .disabled', done => {
+ fixtureEl.innerHTML = [
+ '',
+ ' ',
+ ' ',
+ '
'
+ ].join('')
+
+ const btnDropdown = fixtureEl.querySelector('[data-toggle="dropdown"]')
+ const dropdownEl = fixtureEl.querySelector('.dropdown')
+ const dropdown = new Dropdown(btnDropdown)
+
+ dropdownEl.addEventListener('shown.bs.dropdown', () => {
+ throw new Error('should not throw shown.bs.dropdown event')
+ })
+
+ dropdown.toggle()
+
+ setTimeout(() => {
+ expect().nothing()
+ done()
+ })
+ })
+
+ it('should not toggle a dropdown if the menu is shown', done => {
+ fixtureEl.innerHTML = [
+ '',
+ ' ',
+ ' ',
+ '
'
+ ].join('')
+
+ const btnDropdown = fixtureEl.querySelector('[data-toggle="dropdown"]')
+ const dropdownEl = fixtureEl.querySelector('.dropdown')
+ const dropdown = new Dropdown(btnDropdown)
+
+ dropdownEl.addEventListener('shown.bs.dropdown', () => {
+ throw new Error('should not throw shown.bs.dropdown event')
+ })
+
+ dropdown.toggle()
+
+ setTimeout(() => {
+ expect().nothing()
+ done()
+ })
+ })
+
+ it('should not toggle a dropdown if show event is prevented', done => {
+ fixtureEl.innerHTML = [
+ '',
+ ' ',
+ ' ',
+ '
'
+ ].join('')
+
+ const btnDropdown = fixtureEl.querySelector('[data-toggle="dropdown"]')
+ const dropdownEl = fixtureEl.querySelector('.dropdown')
+ const dropdown = new Dropdown(btnDropdown)
+
+ dropdownEl.addEventListener('show.bs.dropdown', e => {
+ e.preventDefault()
+ })
+
+ dropdownEl.addEventListener('shown.bs.dropdown', () => {
+ throw new Error('should not throw shown.bs.dropdown event')
+ })
+
+ dropdown.toggle()
+
+ setTimeout(() => {
+ expect().nothing()
+ done()
+ })
+ })
+ })
+
+ describe('show', () => {
+ it('should show a dropdown', done => {
+ fixtureEl.innerHTML = [
+ '',
+ ' ',
+ ' ',
+ '
'
+ ].join('')
+
+ const btnDropdown = fixtureEl.querySelector('[data-toggle="dropdown"]')
+ const dropdownEl = fixtureEl.querySelector('.dropdown')
+ const dropdown = new Dropdown(btnDropdown)
+
+ dropdownEl.addEventListener('shown.bs.dropdown', () => {
+ expect(dropdownEl.classList.contains('show')).toEqual(true)
+ done()
+ })
+
+ dropdown.show()
+ })
+
+ it('should not show a dropdown if the element is disabled', done => {
+ fixtureEl.innerHTML = [
+ '',
+ ' ',
+ ' ',
+ '
'
+ ].join('')
+
+ const btnDropdown = fixtureEl.querySelector('[data-toggle="dropdown"]')
+ const dropdownEl = fixtureEl.querySelector('.dropdown')
+ const dropdown = new Dropdown(btnDropdown)
+
+ dropdownEl.addEventListener('shown.bs.dropdown', () => {
+ throw new Error('should not throw shown.bs.dropdown event')
+ })
+
+ dropdown.show()
+
+ setTimeout(() => {
+ expect().nothing()
+ done()
+ }, 10)
+ })
+
+ it('should not show a dropdown if the element contains .disabled', done => {
+ fixtureEl.innerHTML = [
+ '',
+ ' ',
+ ' ',
+ '
'
+ ].join('')
+
+ const btnDropdown = fixtureEl.querySelector('[data-toggle="dropdown"]')
+ const dropdownEl = fixtureEl.querySelector('.dropdown')
+ const dropdown = new Dropdown(btnDropdown)
+
+ dropdownEl.addEventListener('shown.bs.dropdown', () => {
+ throw new Error('should not throw shown.bs.dropdown event')
+ })
+
+ dropdown.show()
+
+ setTimeout(() => {
+ expect().nothing()
+ done()
+ }, 10)
+ })
+
+ it('should not show a dropdown if the menu is shown', done => {
+ fixtureEl.innerHTML = [
+ '',
+ ' ',
+ ' ',
+ '
'
+ ].join('')
+
+ const btnDropdown = fixtureEl.querySelector('[data-toggle="dropdown"]')
+ const dropdownEl = fixtureEl.querySelector('.dropdown')
+ const dropdown = new Dropdown(btnDropdown)
+
+ dropdownEl.addEventListener('shown.bs.dropdown', () => {
+ throw new Error('should not throw shown.bs.dropdown event')
+ })
+
+ dropdown.show()
+
+ setTimeout(() => {
+ expect().nothing()
+ done()
+ }, 10)
+ })
+
+ it('should not show a dropdown if show event is prevented', done => {
+ fixtureEl.innerHTML = [
+ '',
+ ' ',
+ ' ',
+ '
'
+ ].join('')
+
+ const btnDropdown = fixtureEl.querySelector('[data-toggle="dropdown"]')
+ const dropdownEl = fixtureEl.querySelector('.dropdown')
+ const dropdown = new Dropdown(btnDropdown)
+
+ dropdownEl.addEventListener('show.bs.dropdown', e => {
+ e.preventDefault()
+ })
+
+ dropdownEl.addEventListener('shown.bs.dropdown', () => {
+ throw new Error('should not throw shown.bs.dropdown event')
+ })
+
+ dropdown.show()
+
+ setTimeout(() => {
+ expect().nothing()
+ done()
+ }, 10)
+ })
+ })
+
+ describe('hide', () => {
+ it('should hide a dropdown', done => {
+ fixtureEl.innerHTML = [
+ '',
+ ' ',
+ ' ',
+ '
'
+ ].join('')
+
+ const btnDropdown = fixtureEl.querySelector('[data-toggle="dropdown"]')
+ const dropdownEl = fixtureEl.querySelector('.dropdown')
+ const dropdownMenu = fixtureEl.querySelector('.dropdown-menu')
+ const dropdown = new Dropdown(btnDropdown)
+
+ dropdownEl.addEventListener('hidden.bs.dropdown', () => {
+ expect(dropdownMenu.classList.contains('show')).toEqual(false)
+ done()
+ })
+
+ dropdown.hide()
+ })
+
+ it('should not hide a dropdown if the element is disabled', done => {
+ fixtureEl.innerHTML = [
+ '',
+ ' ',
+ ' ',
+ '
'
+ ].join('')
+
+ const btnDropdown = fixtureEl.querySelector('[data-toggle="dropdown"]')
+ const dropdownEl = fixtureEl.querySelector('.dropdown')
+ const dropdownMenu = fixtureEl.querySelector('.dropdown-menu')
+ const dropdown = new Dropdown(btnDropdown)
+
+ dropdownEl.addEventListener('hidden.bs.dropdown', () => {
+ throw new Error('should not throw hidden.bs.dropdown event')
+ })
+
+ dropdown.hide()
+
+ setTimeout(() => {
+ expect(dropdownMenu.classList.contains('show')).toEqual(true)
+ done()
+ }, 10)
+ })
+
+ it('should not hide a dropdown if the element contains .disabled', done => {
+ fixtureEl.innerHTML = [
+ '',
+ ' ',
+ ' ',
+ '
'
+ ].join('')
+
+ const btnDropdown = fixtureEl.querySelector('[data-toggle="dropdown"]')
+ const dropdownEl = fixtureEl.querySelector('.dropdown')
+ const dropdownMenu = fixtureEl.querySelector('.dropdown-menu')
+ const dropdown = new Dropdown(btnDropdown)
+
+ dropdownEl.addEventListener('hidden.bs.dropdown', () => {
+ throw new Error('should not throw hidden.bs.dropdown event')
+ })
+
+ dropdown.hide()
+
+ setTimeout(() => {
+ expect(dropdownMenu.classList.contains('show')).toEqual(true)
+ done()
+ }, 10)
+ })
+
+ it('should not hide a dropdown if the menu is not shown', done => {
+ fixtureEl.innerHTML = [
+ '',
+ ' ',
+ ' ',
+ '
'
+ ].join('')
+
+ const btnDropdown = fixtureEl.querySelector('[data-toggle="dropdown"]')
+ const dropdownEl = fixtureEl.querySelector('.dropdown')
+ const dropdown = new Dropdown(btnDropdown)
+
+ dropdownEl.addEventListener('hidden.bs.dropdown', () => {
+ throw new Error('should not throw hidden.bs.dropdown event')
+ })
+
+ dropdown.hide()
+
+ setTimeout(() => {
+ expect().nothing()
+ done()
+ }, 10)
+ })
+
+ it('should not hide a dropdown if hide event is prevented', done => {
+ fixtureEl.innerHTML = [
+ '',
+ ' ',
+ ' ',
+ '
'
+ ].join('')
+
+ const btnDropdown = fixtureEl.querySelector('[data-toggle="dropdown"]')
+ const dropdownEl = fixtureEl.querySelector('.dropdown')
+ const dropdownMenu = fixtureEl.querySelector('.dropdown-menu')
+ const dropdown = new Dropdown(btnDropdown)
+
+ dropdownEl.addEventListener('hide.bs.dropdown', e => {
+ e.preventDefault()
+ })
+
+ dropdownEl.addEventListener('hidden.bs.dropdown', () => {
+ throw new Error('should not throw hidden.bs.dropdown event')
+ })
+
+ dropdown.hide()
+
+ setTimeout(() => {
+ expect(dropdownMenu.classList.contains('show')).toEqual(true)
+ done()
+ })
+ })
+ })
+
+ describe('dispose', () => {
+ it('should dispose dropdown', () => {
+ fixtureEl.innerHTML = [
+ '',
+ ' ',
+ ' ',
+ '
'
+ ].join('')
+
+ const btnDropdown = fixtureEl.querySelector('[data-toggle="dropdown"]')
+ const dropdown = new Dropdown(btnDropdown)
+
+ expect(dropdown._popper).toBeNull()
+ expect(dropdown._menu).toBeDefined()
+ expect(dropdown._element).toBeDefined()
+
+ dropdown.dispose()
+
+ expect(dropdown._menu).toBeNull()
+ expect(dropdown._element).toBeNull()
+ })
+
+ it('should dispose dropdown with popper.js', () => {
+ fixtureEl.innerHTML = [
+ '',
+ ' ',
+ ' ',
+ '
'
+ ].join('')
+
+ const btnDropdown = fixtureEl.querySelector('[data-toggle="dropdown"]')
+ const dropdown = new Dropdown(btnDropdown)
+
+ dropdown.toggle()
+
+ expect(dropdown._popper).toBeDefined()
+ expect(dropdown._menu).toBeDefined()
+ expect(dropdown._element).toBeDefined()
+
+ spyOn(Popper.prototype, 'destroy')
+
+ dropdown.dispose()
+
+ expect(dropdown._popper).toBeNull()
+ expect(dropdown._menu).toBeNull()
+ expect(dropdown._element).toBeNull()
+ expect(Popper.prototype.destroy).toHaveBeenCalled()
+ })
+ })
+
+ describe('update', () => {
+ it('should call popper.js and detect navbar on update', () => {
+ fixtureEl.innerHTML = [
+ '',
+ ' ',
+ ' ',
+ '
'
+ ].join('')
+
+ const btnDropdown = fixtureEl.querySelector('[data-toggle="dropdown"]')
+ const dropdown = new Dropdown(btnDropdown)
+
+ dropdown.toggle()
+
+ expect(dropdown._popper).toBeDefined()
+
+ spyOn(dropdown._popper, 'scheduleUpdate')
+ spyOn(dropdown, '_detectNavbar')
+
+ dropdown.update()
+
+ expect(dropdown._popper.scheduleUpdate).toHaveBeenCalled()
+ expect(dropdown._detectNavbar).toHaveBeenCalled()
+ })
+
+ it('should just detect navbar on update', () => {
+ fixtureEl.innerHTML = [
+ '',
+ ' ',
+ ' ',
+ '
'
+ ].join('')
+
+ const btnDropdown = fixtureEl.querySelector('[data-toggle="dropdown"]')
+ const dropdown = new Dropdown(btnDropdown)
+
+ spyOn(dropdown, '_detectNavbar')
+
+ dropdown.update()
+
+ expect(dropdown._popper).toBeNull()
+ expect(dropdown._detectNavbar).toHaveBeenCalled()
+ })
+ })
+
+ describe('data-api', () => {
+ it('should not add class position-static to dropdown if boundary not set', done => {
+ fixtureEl.innerHTML = [
+ '',
+ ' ',
+ ' ',
+ '
'
+ ].join('')
+
+ const btnDropdown = fixtureEl.querySelector('[data-toggle="dropdown"]')
+ const dropdownEl = fixtureEl.querySelector('.dropdown')
+
+ dropdownEl.addEventListener('shown.bs.dropdown', () => {
+ expect(dropdownEl.classList.contains('position-static')).toEqual(false)
+ done()
+ })
+
+ btnDropdown.click()
+ })
+
+ it('should add class position-static to dropdown if boundary not scrollParent', done => {
+ fixtureEl.innerHTML = [
+ '',
+ ' ',
+ ' ',
+ '
'
+ ].join('')
+
+ const btnDropdown = fixtureEl.querySelector('[data-toggle="dropdown"]')
+ const dropdownEl = fixtureEl.querySelector('.dropdown')
+
+ dropdownEl.addEventListener('shown.bs.dropdown', () => {
+ expect(dropdownEl.classList.contains('position-static')).toEqual(true)
+ done()
+ })
+
+ btnDropdown.click()
+ })
+
+ it('should show and hide a dropdown', done => {
+ fixtureEl.innerHTML = [
+ '',
+ ' ',
+ ' ',
+ '
'
+ ].join('')
+
+ const btnDropdown = fixtureEl.querySelector('[data-toggle="dropdown"]')
+ const dropdownEl = fixtureEl.querySelector('.dropdown')
+ let showEventTriggered = false
+ let hideEventTriggered = false
+
+ dropdownEl.addEventListener('show.bs.dropdown', () => {
+ showEventTriggered = true
+ })
+
+ dropdownEl.addEventListener('shown.bs.dropdown', e => {
+ expect(dropdownEl.classList.contains('show')).toEqual(true)
+ expect(btnDropdown.getAttribute('aria-expanded')).toEqual('true')
+ expect(showEventTriggered).toEqual(true)
+ expect(e.relatedTarget).toEqual(btnDropdown)
+ document.body.click()
+ })
+
+ dropdownEl.addEventListener('hide.bs.dropdown', () => {
+ hideEventTriggered = true
+ })
+
+ dropdownEl.addEventListener('hidden.bs.dropdown', e => {
+ expect(dropdownEl.classList.contains('show')).toEqual(false)
+ expect(btnDropdown.getAttribute('aria-expanded')).toEqual('false')
+ expect(hideEventTriggered).toEqual(true)
+ expect(e.relatedTarget).toEqual(btnDropdown)
+ done()
+ })
+
+ btnDropdown.click()
+ })
+
+ it('should not use popper.js in navbar', done => {
+ fixtureEl.innerHTML = [
+ ''
+ ].join('')
+
+ const btnDropdown = fixtureEl.querySelector('[data-toggle="dropdown"]')
+ const dropdownEl = fixtureEl.querySelector('.dropdown')
+ const dropdownMenu = fixtureEl.querySelector('.dropdown-menu')
+
+ dropdownEl.addEventListener('shown.bs.dropdown', () => {
+ expect(dropdownMenu.getAttribute('style')).toEqual(null, 'no inline style applied by popper.js')
+ done()
+ })
+
+ btnDropdown.click()
+ })
+
+ it('should not use popper.js if display set to static', done => {
+ fixtureEl.innerHTML = [
+ '',
+ ' ',
+ ' ',
+ '
'
+ ].join('')
+
+ const btnDropdown = fixtureEl.querySelector('[data-toggle="dropdown"]')
+ const dropdownEl = fixtureEl.querySelector('.dropdown')
+ const dropdownMenu = fixtureEl.querySelector('.dropdown-menu')
+
+ dropdownEl.addEventListener('shown.bs.dropdown', () => {
+ // popper.js add this attribute when we use it
+ expect(dropdownMenu.getAttribute('x-placement')).toEqual(null)
+ done()
+ })
+
+ btnDropdown.click()
+ })
+
+ it('should remove "show" class if tabbing outside of menu', done => {
+ fixtureEl.innerHTML = [
+ '',
+ ' ',
+ ' ',
+ '
'
+ ].join('')
+
+ const btnDropdown = fixtureEl.querySelector('[data-toggle="dropdown"]')
+ const dropdownEl = fixtureEl.querySelector('.dropdown')
+
+ dropdownEl.addEventListener('shown.bs.dropdown', () => {
+ expect(dropdownEl.classList.contains('show')).toEqual(true)
+
+ const keyUp = createEvent('keyup')
+
+ keyUp.which = 9 // Tab
+ document.dispatchEvent(keyUp)
+ })
+
+ dropdownEl.addEventListener('hidden.bs.dropdown', () => {
+ expect(dropdownEl.classList.contains('show')).toEqual(false)
+ done()
+ })
+
+ btnDropdown.click()
+ })
+
+ it('should remove "show" class if body is clicked, with multiple dropdowns', done => {
+ fixtureEl.innerHTML = [
+ '',
+ ' ',
+ '
',
+ '',
+ ' ',
+ ' ',
+ ' ',
+ '
'
+ ].join('')
+
+ const triggerDropdownList = fixtureEl.querySelectorAll('[data-toggle="dropdown"]')
+
+ expect(triggerDropdownList.length).toEqual(2)
+
+ const first = triggerDropdownList[0]
+ const last = triggerDropdownList[1]
+ const dropdownTestMenu = first.parentNode
+ const btnGroup = last.parentNode
+
+ dropdownTestMenu.addEventListener('shown.bs.dropdown', () => {
+ expect(dropdownTestMenu.classList.contains('show')).toEqual(true)
+ expect(fixtureEl.querySelectorAll('.dropdown-menu.show').length).toEqual(1)
+ document.body.click()
+ })
+
+ dropdownTestMenu.addEventListener('hidden.bs.dropdown', () => {
+ expect(fixtureEl.querySelectorAll('.dropdown-menu.show').length).toEqual(0)
+ last.click()
+ })
+
+ btnGroup.addEventListener('shown.bs.dropdown', () => {
+ expect(btnGroup.classList.contains('show')).toEqual(true)
+ expect(fixtureEl.querySelectorAll('.dropdown-menu.show').length).toEqual(1)
+ document.body.click()
+ })
+
+ btnGroup.addEventListener('hidden.bs.dropdown', () => {
+ expect(fixtureEl.querySelectorAll('.dropdown-menu.show').length).toEqual(0)
+ done()
+ })
+
+ first.click()
+ })
+
+ it('should remove "show" class if body if tabbing outside of menu, with multiple dropdowns', done => {
+ fixtureEl.innerHTML = [
+ '',
+ '',
+ ' ',
+ ' ',
+ ' ',
+ '
'
+ ].join('')
+
+ const triggerDropdownList = fixtureEl.querySelectorAll('[data-toggle="dropdown"]')
+
+ expect(triggerDropdownList.length).toEqual(2)
+
+ const first = triggerDropdownList[0]
+ const last = triggerDropdownList[1]
+ const dropdownTestMenu = first.parentNode
+ const btnGroup = last.parentNode
+
+ dropdownTestMenu.addEventListener('shown.bs.dropdown', () => {
+ expect(dropdownTestMenu.classList.contains('show')).toEqual(true, '"show" class added on click')
+ expect(fixtureEl.querySelectorAll('.dropdown-menu.show').length).toEqual(1, 'only one dropdown is shown')
+
+ const keyUp = createEvent('keyup')
+ keyUp.which = 9 // Tab
+
+ document.dispatchEvent(keyUp)
+ })
+
+ dropdownTestMenu.addEventListener('hidden.bs.dropdown', () => {
+ expect(fixtureEl.querySelectorAll('.dropdown-menu.show').length).toEqual(0, '"show" class removed')
+ last.click()
+ })
+
+ btnGroup.addEventListener('shown.bs.dropdown', () => {
+ expect(btnGroup.classList.contains('show')).toEqual(true, '"show" class added on click')
+ expect(fixtureEl.querySelectorAll('.dropdown-menu.show').length).toEqual(1, 'only one dropdown is shown')
+
+ const keyUp = createEvent('keyup')
+ keyUp.which = 9 // Tab
+
+ document.dispatchEvent(keyUp)
+ })
+
+ btnGroup.addEventListener('hidden.bs.dropdown', () => {
+ expect(fixtureEl.querySelectorAll('.dropdown-menu.show').length).toEqual(0, '"show" class removed')
+ done()
+ })
+
+ first.click()
+ })
+
+ it('should fire hide and hidden event without a clickEvent if event type is not click', done => {
+ fixtureEl.innerHTML = [
+ '',
+ ' ',
+ ' ',
+ '
'
+ ].join('')
+
+ const triggerDropdown = fixtureEl.querySelector('[data-toggle="dropdown"]')
+ const dropdown = fixtureEl.querySelector('.dropdown')
+
+ dropdown.addEventListener('hide.bs.dropdown', e => {
+ expect(e.clickEvent).toBeUndefined()
+ })
+
+ dropdown.addEventListener('hidden.bs.dropdown', e => {
+ expect(e.clickEvent).toBeUndefined()
+ done()
+ })
+
+ dropdown.addEventListener('shown.bs.dropdown', () => {
+ const keyDown = createEvent('keydown')
+
+ keyDown.which = 27
+ triggerDropdown.dispatchEvent(keyDown)
+ })
+
+ triggerDropdown.click()
+ })
+
+ it('should ignore keyboard events within s and