diff options
-rw-r--r-- | build/build-plugins.js | 2 | ||||
-rw-r--r-- | js/index.esm.js | 2 | ||||
-rw-r--r-- | js/index.umd.js | 2 | ||||
-rw-r--r-- | js/src/dropdown/dropdown.js (renamed from js/src/dropdown.js) | 22 | ||||
-rw-r--r-- | js/src/dropdown/dropdown.spec.js | 1494 | ||||
-rw-r--r-- | js/tests/karma.conf.js | 4 | ||||
-rw-r--r-- | js/tests/unit/dropdown.js | 1496 |
7 files changed, 1508 insertions, 1514 deletions
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 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) - - if (parent) { - this._menu = SelectorEngine.findOne(Selector.MENU, parent) - } - } + const parent = Dropdown._getParentFromElement(this._element) - 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 = [ + '<div class="dropdown">', + ' <button href="#" class="btn dropdown-toggle" data-toggle="dropdown">Dropdown</button>', + ' <div class="dropdown-menu">', + ' <a class="dropdown-item" href="#">Secondary link</a>', + ' </div>', + '</div>' + ].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 = [ + '<div class="dropdown">', + ' <button href="#" class="btn dropdown-toggle" data-toggle="dropdown">Dropdown</button>', + ' <div class="dropdown-menu">', + ' <a class="dropdown-item" href="#">Secondary link</a>', + ' </div>', + '</div>' + ].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 = [ + '<div class="dropdown">', + ' <button href="#" class="btn">Dropdown</button>', + ' <div class="dropdown-menu">', + ' <a class="dropdown-item" href="#">Secondary link</a>', + ' </div>', + '</div>' + ].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 = [ + '<div class="dropdown">', + ' <button href="#" class="btn dropdown-toggle" data-toggle="dropdown" aria-expanded="false">Dropdown</button>', + ' <div class="dropdown-menu">', + ' <a class="dropdown-item" href="#">Secondary link</a>', + ' </div>', + '</div>' + ].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 = [ + '<div class="dropdown">', + ' <button href="#" class="btn dropdown-toggle" data-toggle="dropdown" aria-expanded="false">Dropdown</button>', + ' <div class="dropdown-menu">', + ' <a class="dropdown-item" href="#">Secondary link</a>', + ' </div>', + '</div>' + ].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 = [ + '<div class="dropdown">', + ' <button href="#" class="btn dropdown-toggle" data-toggle="dropdown" aria-expanded="false">Dropdown</button>', + ' <div class="dropdown-menu dropdown-menu-right">', + ' <a class="dropdown-item" href="#">Secondary link</a>', + ' </div>', + '</div>' + ].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 = [ + '<div class="dropup">', + ' <button href="#" class="btn dropdown-toggle" data-toggle="dropdown" aria-expanded="false">Dropdown</button>', + ' <div class="dropdown-menu">', + ' <a class="dropdown-item" href="#">Secondary link</a>', + ' </div>', + '</div>' + ].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 = [ + '<div class="dropup">', + ' <button href="#" class="btn dropdown-toggle" data-toggle="dropdown" aria-expanded="false">Dropdown</button>', + ' <div class="dropdown-menu dropdown-menu-right">', + ' <a class="dropdown-item" href="#">Secondary link</a>', + ' </div>', + '</div>' + ].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 = [ + '<div class="dropright">', + ' <button href="#" class="btn dropdown-toggle" data-toggle="dropdown" aria-expanded="false">Dropdown</button>', + ' <div class="dropdown-menu">', + ' <a class="dropdown-item" href="#">Secondary link</a>', + ' </div>', + '</div>' + ].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 = [ + '<div class="dropleft">', + ' <button href="#" class="btn dropdown-toggle" data-toggle="dropdown" aria-expanded="false">Dropdown</button>', + ' <div class="dropdown-menu">', + ' <a class="dropdown-item" href="#">Secondary link</a>', + ' </div>', + '</div>' + ].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 = [ + '<div class="dropdown">', + ' <button href="#" class="btn dropdown-toggle" data-toggle="dropdown" aria-expanded="false">Dropdown</button>', + ' <div class="dropdown-menu">', + ' <a class="dropdown-item" href="#">Secondary link</a>', + ' </div>', + '</div>' + ].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 = [ + '<div class="dropdown">', + ' <button href="#" class="btn dropdown-toggle" data-toggle="dropdown" aria-expanded="false">Dropdown</button>', + ' <div class="dropdown-menu">', + ' <a class="dropdown-item" href="#">Secondary link</a>', + ' </div>', + '</div>' + ].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 = [ + '<div class="dropdown">', + ' <button href="#" class="btn dropdown-toggle" data-toggle="dropdown" aria-expanded="false">Dropdown</button>', + ' <div class="dropdown-menu">', + ' <a class="dropdown-item" href="#">Secondary link</a>', + ' </div>', + '</div>' + ].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 = [ + '<div class="dropdown">', + ' <button disabled href="#" class="btn dropdown-toggle" data-toggle="dropdown">Dropdown</button>', + ' <div class="dropdown-menu">', + ' <a class="dropdown-item" href="#">Secondary link</a>', + ' </div>', + '</div>' + ].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 = [ + '<div class="dropdown">', + ' <button href="#" class="btn dropdown-toggle disabled" data-toggle="dropdown">Dropdown</button>', + ' <div class="dropdown-menu">', + ' <a class="dropdown-item" href="#">Secondary link</a>', + ' </div>', + '</div>' + ].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 = [ + '<div class="dropdown">', + ' <button href="#" class="btn dropdown-toggle" data-toggle="dropdown">Dropdown</button>', + ' <div class="dropdown-menu show">', + ' <a class="dropdown-item" href="#">Secondary link</a>', + ' </div>', + '</div>' + ].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 = [ + '<div class="dropdown">', + ' <button href="#" class="btn dropdown-toggle" data-toggle="dropdown">Dropdown</button>', + ' <div class="dropdown-menu">', + ' <a class="dropdown-item" href="#">Secondary link</a>', + ' </div>', + '</div>' + ].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 = [ + '<div class="dropdown">', + ' <button href="#" class="btn dropdown-toggle" data-toggle="dropdown">Dropdown</button>', + ' <div class="dropdown-menu">', + ' <a class="dropdown-item" href="#">Secondary link</a>', + ' </div>', + '</div>' + ].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 = [ + '<div class="dropdown">', + ' <button disabled href="#" class="btn dropdown-toggle" data-toggle="dropdown">Dropdown</button>', + ' <div class="dropdown-menu">', + ' <a class="dropdown-item" href="#">Secondary link</a>', + ' </div>', + '</div>' + ].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 = [ + '<div class="dropdown">', + ' <button href="#" class="btn dropdown-toggle disabled" data-toggle="dropdown">Dropdown</button>', + ' <div class="dropdown-menu">', + ' <a class="dropdown-item" href="#">Secondary link</a>', + ' </div>', + '</div>' + ].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 = [ + '<div class="dropdown">', + ' <button href="#" class="btn dropdown-toggle" data-toggle="dropdown">Dropdown</button>', + ' <div class="dropdown-menu show">', + ' <a class="dropdown-item" href="#">Secondary link</a>', + ' </div>', + '</div>' + ].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 = [ + '<div class="dropdown">', + ' <button href="#" class="btn dropdown-toggle" data-toggle="dropdown">Dropdown</button>', + ' <div class="dropdown-menu">', + ' <a class="dropdown-item" href="#">Secondary link</a>', + ' </div>', + '</div>' + ].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 = [ + '<div class="dropdown">', + ' <button href="#" class="btn dropdown-toggle" data-toggle="dropdown">Dropdown</button>', + ' <div class="dropdown-menu show">', + ' <a class="dropdown-item" href="#">Secondary link</a>', + ' </div>', + '</div>' + ].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 = [ + '<div class="dropdown">', + ' <button disabled href="#" class="btn dropdown-toggle" data-toggle="dropdown">Dropdown</button>', + ' <div class="dropdown-menu show">', + ' <a class="dropdown-item" href="#">Secondary link</a>', + ' </div>', + '</div>' + ].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 = [ + '<div class="dropdown">', + ' <button href="#" class="btn dropdown-toggle disabled" data-toggle="dropdown">Dropdown</button>', + ' <div class="dropdown-menu show">', + ' <a class="dropdown-item" href="#">Secondary link</a>', + ' </div>', + '</div>' + ].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 = [ + '<div class="dropdown">', + ' <button href="#" class="btn dropdown-toggle" data-toggle="dropdown">Dropdown</button>', + ' <div class="dropdown-menu">', + ' <a class="dropdown-item" href="#">Secondary link</a>', + ' </div>', + '</div>' + ].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 = [ + '<div class="dropdown">', + ' <button href="#" class="btn dropdown-toggle" data-toggle="dropdown">Dropdown</button>', + ' <div class="dropdown-menu show">', + ' <a class="dropdown-item" href="#">Secondary link</a>', + ' </div>', + '</div>' + ].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 = [ + '<div class="dropdown">', + ' <button href="#" class="btn dropdown-toggle" data-toggle="dropdown">Dropdown</button>', + ' <div class="dropdown-menu">', + ' <a class="dropdown-item" href="#">Secondary link</a>', + ' </div>', + '</div>' + ].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 = [ + '<div class="dropdown">', + ' <button href="#" class="btn dropdown-toggle" data-toggle="dropdown">Dropdown</button>', + ' <div class="dropdown-menu">', + ' <a class="dropdown-item" href="#">Secondary link</a>', + ' </div>', + '</div>' + ].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 = [ + '<div class="dropdown">', + ' <button href="#" class="btn dropdown-toggle" data-toggle="dropdown">Dropdown</button>', + ' <div class="dropdown-menu">', + ' <a class="dropdown-item" href="#">Secondary link</a>', + ' </div>', + '</div>' + ].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 = [ + '<div class="dropdown">', + ' <button href="#" class="btn dropdown-toggle" data-toggle="dropdown">Dropdown</button>', + ' <div class="dropdown-menu">', + ' <a class="dropdown-item" href="#">Secondary link</a>', + ' </div>', + '</div>' + ].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 = [ + '<div class="dropdown">', + ' <button href="#" class="btn dropdown-toggle" data-toggle="dropdown">Dropdown</button>', + ' <div class="dropdown-menu">', + ' <a class="dropdown-item" href="#">Secondary link</a>', + ' </div>', + '</div>' + ].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 = [ + '<div class="dropdown">', + ' <button href="#" class="btn dropdown-toggle" data-toggle="dropdown" data-boundary="viewport">Dropdown</button>', + ' <div class="dropdown-menu">', + ' <a class="dropdown-item" href="#">Secondary link</a>', + ' </div>', + '</div>' + ].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 = [ + '<div class="dropdown">', + ' <button href="#" class="btn dropdown-toggle" data-toggle="dropdown" aria-expanded="false">Dropdown</button>', + ' <div class="dropdown-menu">', + ' <a class="dropdown-item" href="#">Secondary link</a>', + ' </div>', + '</div>' + ].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 = [ + '<nav class="navbar navbar-expand-md navbar-light bg-light">', + ' <div class="dropdown">', + ' <button href="#" class="btn dropdown-toggle" data-toggle="dropdown" aria-expanded="false">Dropdown</button>', + ' <div class="dropdown-menu">', + ' <a class="dropdown-item" href="#">Secondary link</a>', + ' </div>', + ' </div>', + '</nav>' + ].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 = [ + '<div class="dropdown">', + ' <button href="#" class="btn dropdown-toggle" data-toggle="dropdown" data-display="static">Dropdown</button>', + ' <div class="dropdown-menu">', + ' <a class="dropdown-item" href="#">Secondary link</a>', + ' </div>', + '</div>' + ].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 = [ + '<div class="dropdown">', + ' <button href="#" class="btn dropdown-toggle" data-toggle="dropdown">Dropdown</button>', + ' <div class="dropdown-menu">', + ' <a class="dropdown-item" href="#">Secondary link</a>', + ' </div>', + '</div>' + ].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 = [ + '<div class="nav">', + ' <div class="dropdown" id="testmenu">', + ' <a class="dropdown-toggle" data-toggle="dropdown" href="#testmenu">Test menu <span class="caret"/></a>', + ' <div class="dropdown-menu">', + ' <a class="dropdown-item" href="#sub1">Submenu 1</a>', + ' </div>', + ' </div>', + '</div>', + '<div class="btn-group">', + ' <button class="btn">Actions</button>', + ' <button class="btn dropdown-toggle" data-toggle="dropdown"></button>', + ' <div class="dropdown-menu">', + ' <a class="dropdown-item" href="#">Action 1</a>', + ' </div>', + '</div>' + ].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 = [ + '<div class="dropdown">', + ' <a class="dropdown-toggle" data-toggle="dropdown" href="#testmenu">Test menu</a>', + ' <div class="dropdown-menu">', + ' <a class="dropdown-item" href="#sub1">Submenu 1</a>', + ' </div>', + '</div>', + '<div class="btn-group">', + ' <button class="btn">Actions</button>', + ' <button class="btn dropdown-toggle" data-toggle="dropdown"></button>', + ' <div class="dropdown-menu">', + ' <a class="dropdown-item" href="#">Action 1</a>', + ' </div>', + '</div>' + ].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 = [ + '<div class="dropdown">', + ' <button href="#" class="btn dropdown-toggle" data-toggle="dropdown">Dropdown</button>', + ' <div class="dropdown-menu">', + ' <a class="dropdown-item" href="#sub1">Submenu 1</a>', + ' </div>', + '</div>' + ].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 <input>s and <textarea>s', done => { + fixtureEl.innerHTML = [ + '<div class="dropdown">', + ' <button href="#" class="btn dropdown-toggle" data-toggle="dropdown">Dropdown</button>', + ' <div class="dropdown-menu">', + ' <a class="dropdown-item" href="#sub1">Submenu 1</a>', + ' <input type="text" />', + ' <textarea></textarea>', + ' </div>', + '</div>' + ].join('') + + // the element must be displayed, without that activeElement won't change + fixtureEl.style.display = 'block' + + const triggerDropdown = fixtureEl.querySelector('[data-toggle="dropdown"]') + const dropdown = fixtureEl.querySelector('.dropdown') + const input = fixtureEl.querySelector('input') + const textarea = fixtureEl.querySelector('textarea') + + dropdown.addEventListener('shown.bs.dropdown', () => { + input.focus() + const keyDown = createEvent('keydown') + + keyDown.which = 38 + input.dispatchEvent(keyDown) + + expect(document.activeElement).toEqual(input, 'input still focused') + + textarea.focus() + textarea.dispatchEvent(keyDown) + + expect(document.activeElement).toEqual(textarea, 'textarea still focused') + fixtureEl.style.display = 'none' + done() + }) + + triggerDropdown.click() + }) + + it('should skip disabled element when using keyboard navigation', done => { + fixtureEl.innerHTML = [ + '<div class="dropdown">', + ' <button href="#" class="btn dropdown-toggle" data-toggle="dropdown">Dropdown</button>', + ' <div class="dropdown-menu">', + ' <a class="dropdown-item disabled" href="#sub1">Submenu 1</a>', + ' <button class="dropdown-item" type="button" disabled>Disabled button</button>', + ' <a id="item1" class="dropdown-item" href="#">Another link</a>', + ' </div>', + '</div>' + ].join('') + + const triggerDropdown = fixtureEl.querySelector('[data-toggle="dropdown"]') + const dropdown = fixtureEl.querySelector('.dropdown') + + // the element must be displayed, without that activeElement won't change + fixtureEl.style.display = 'block' + + dropdown.addEventListener('shown.bs.dropdown', () => { + const keyDown = createEvent('keydown') + keyDown.which = 40 + + triggerDropdown.dispatchEvent(keyDown) + triggerDropdown.dispatchEvent(keyDown) + + expect(document.activeElement.classList.contains('disabled')).toEqual(false, '.disabled not focused') + expect(document.activeElement.hasAttribute('disabled')).toEqual(false, ':disabled not focused') + fixtureEl.style.display = 'none' + done() + }) + + triggerDropdown.click() + }) + + it('should focus next/previous element when using keyboard navigation', done => { + fixtureEl.innerHTML = [ + '<div class="dropdown">', + ' <button href="#" class="btn dropdown-toggle" data-toggle="dropdown">Dropdown</button>', + ' <div class="dropdown-menu">', + ' <a id="item1" class="dropdown-item" href="#">A link</a>', + ' <a id="item2" class="dropdown-item" href="#">Another link</a>', + ' </div>', + '</div>' + ].join('') + + const triggerDropdown = fixtureEl.querySelector('[data-toggle="dropdown"]') + const dropdown = fixtureEl.querySelector('.dropdown') + const item1 = fixtureEl.querySelector('#item1') + const item2 = fixtureEl.querySelector('#item2') + + // the element must be displayed, without that activeElement won't change + fixtureEl.style.display = 'block' + + dropdown.addEventListener('shown.bs.dropdown', () => { + const keyDown40 = createEvent('keydown') + keyDown40.which = 40 + + triggerDropdown.dispatchEvent(keyDown40) + expect(document.activeElement).toEqual(item1, 'item1 is focused') + + document.activeElement.dispatchEvent(keyDown40) + expect(document.activeElement).toEqual(item2, 'item2 is focused') + + const keyDown38 = createEvent('keydown') + keyDown38.which = 38 + + document.activeElement.dispatchEvent(keyDown38) + expect(document.activeElement).toEqual(item1, 'item1 is focused') + + fixtureEl.style.display = 'none' + done() + }) + + triggerDropdown.click() + }) + + it('should not close the dropdown if the user clicks on a text field', done => { + fixtureEl.innerHTML = [ + '<div class="dropdown">', + ' <button href="#" class="btn dropdown-toggle" data-toggle="dropdown">Dropdown</button>', + ' <div class="dropdown-menu">', + ' <input type="text" />', + ' </div>', + '</div>' + ].join('') + + const triggerDropdown = fixtureEl.querySelector('[data-toggle="dropdown"]') + const dropdown = fixtureEl.querySelector('.dropdown') + const input = fixtureEl.querySelector('input') + + input.addEventListener('click', () => { + expect(dropdown.classList.contains('show')).toEqual(true, 'dropdown menu is shown') + done() + }) + + dropdown.addEventListener('shown.bs.dropdown', () => { + expect(dropdown.classList.contains('show')).toEqual(true, 'dropdown menu is shown') + input.dispatchEvent(createEvent('click')) + }) + + triggerDropdown.click() + }) + + it('should not close the dropdown if the user clicks on a textarea', done => { + fixtureEl.innerHTML = [ + '<div class="dropdown">', + ' <button href="#" class="btn dropdown-toggle" data-toggle="dropdown">Dropdown</button>', + ' <div class="dropdown-menu">', + ' <textarea></textarea>', + ' </div>', + '</div>' + ].join('') + + const triggerDropdown = fixtureEl.querySelector('[data-toggle="dropdown"]') + const dropdown = fixtureEl.querySelector('.dropdown') + const textarea = fixtureEl.querySelector('textarea') + + textarea.addEventListener('click', () => { + expect(dropdown.classList.contains('show')).toEqual(true, 'dropdown menu is shown') + done() + }) + + dropdown.addEventListener('shown.bs.dropdown', () => { + expect(dropdown.classList.contains('show')).toEqual(true, 'dropdown menu is shown') + textarea.dispatchEvent(createEvent('click')) + }) + + triggerDropdown.click() + }) + + it('should ignore keyboard events for <input>s and <textarea>s within dropdown-menu, except for escape key', done => { + fixtureEl.innerHTML = [ + '<div class="dropdown">', + ' <button href="#" class="btn dropdown-toggle" data-toggle="dropdown">Dropdown</button>', + ' <div class="dropdown-menu">', + ' <a class="dropdown-item" href="#sub1">Submenu 1</a>', + ' <input type="text" />', + ' <textarea></textarea>', + ' </div>', + '</div>' + ].join('') + + // the element must be displayed, without that activeElement won't change + fixtureEl.style.display = 'block' + + const triggerDropdown = fixtureEl.querySelector('[data-toggle="dropdown"]') + const dropdown = fixtureEl.querySelector('.dropdown') + const input = fixtureEl.querySelector('input') + const textarea = fixtureEl.querySelector('textarea') + + // Space key + const keyDownSpace = createEvent('keydown') + keyDownSpace.which = 32 + + // Key up + const keyDownUp = createEvent('keydown') + keyDownSpace.which = 38 + + // Key down + const keyDown = createEvent('keydown') + keyDownSpace.which = 40 + + // Key escape + const keyDownEscape = createEvent('keydown') + keyDownEscape.which = 27 + + dropdown.addEventListener('shown.bs.dropdown', () => { + // Space key + input.focus() + input.dispatchEvent(keyDownSpace) + + expect(document.activeElement).toEqual(input, 'input still focused') + + textarea.focus() + textarea.dispatchEvent(keyDownSpace) + + expect(document.activeElement).toEqual(textarea, 'textarea still focused') + + // Key up + input.focus() + input.dispatchEvent(keyDownUp) + + expect(document.activeElement).toEqual(input, 'input still focused') + + textarea.focus() + textarea.dispatchEvent(keyDownUp) + + expect(document.activeElement).toEqual(textarea, 'textarea still focused') + + // Key down + input.focus() + input.dispatchEvent(keyDown) + + expect(document.activeElement).toEqual(input, 'input still focused') + + textarea.focus() + textarea.dispatchEvent(keyDown) + + expect(document.activeElement).toEqual(textarea, 'textarea still focused') + + // Key escape + input.focus() + input.dispatchEvent(keyDownEscape) + + expect(dropdown.classList.contains('show')).toEqual(false, 'dropdown menu is not shown') + fixtureEl.style.display = 'none' + done() + }) + + triggerDropdown.click() + }) + + it('should not open dropdown if escape key was pressed on the toggle', done => { + fixtureEl.innerHTML = [ + '<div class="tabs">', + ' <div class="dropdown">', + ' <button disabled href="#" class="btn dropdown-toggle" data-toggle="dropdown">Dropdown</button>', + ' <div class="dropdown-menu">', + ' <a class="dropdown-item" href="#">Secondary link</a>', + ' <a class="dropdown-item" href="#">Something else here</a>', + ' <div class="divider"/>', + ' <a class="dropdown-item" href="#">Another link</a>', + ' </div>', + ' </div>', + '</div>' + ] + + const triggerDropdown = fixtureEl.querySelector('[data-toggle="dropdown"]') + const dropdown = new Dropdown(triggerDropdown) + const button = fixtureEl.querySelector('button[data-toggle="dropdown"]') + + spyOn(dropdown, 'toggle') + + // Key escape + button.focus() + // Key escape + const keyDownEscape = createEvent('keydown') + keyDownEscape.which = 27 + button.dispatchEvent(keyDownEscape) + + setTimeout(() => { + expect(dropdown.toggle).not.toHaveBeenCalled() + expect(triggerDropdown.parentNode.classList.contains('show')).toEqual(false) + done() + }, 20) + }) + }) + + describe('_jQueryInterface', () => { + it('should create a dropdown', () => { + fixtureEl.innerHTML = '<div></div>' + + const div = fixtureEl.querySelector('div') + + jQueryMock.fn.dropdown = Dropdown._jQueryInterface + jQueryMock.elements = [div] + + jQueryMock.fn.dropdown.call(jQueryMock) + + expect(Dropdown._getInstance(div)).toBeDefined() + }) + + it('should not re create a dropdown', () => { + fixtureEl.innerHTML = '<div></div>' + + const div = fixtureEl.querySelector('div') + const dropdown = new Dropdown(div) + + jQueryMock.fn.dropdown = Dropdown._jQueryInterface + jQueryMock.elements = [div] + + jQueryMock.fn.dropdown.call(jQueryMock) + + expect(Dropdown._getInstance(div)).toEqual(dropdown) + }) + + it('should throw error on undefined method', () => { + fixtureEl.innerHTML = '<div></div>' + + const div = fixtureEl.querySelector('div') + const action = 'undefinedMethod' + + jQueryMock.fn.dropdown = Dropdown._jQueryInterface + jQueryMock.elements = [div] + + try { + jQueryMock.fn.dropdown.call(jQueryMock, action) + } catch (error) { + expect(error.message).toEqual(`No method named "${action}"`) + } + }) + }) + + describe('_getInstance', () => { + it('should return dropdown instance', () => { + fixtureEl.innerHTML = '<div></div>' + + const div = fixtureEl.querySelector('div') + const dropdown = new Dropdown(div) + + expect(Dropdown._getInstance(div)).toEqual(dropdown) + }) + + it('should return null when there is no dropdown instance', () => { + fixtureEl.innerHTML = '<div></div>' + + const div = fixtureEl.querySelector('div') + + expect(Dropdown._getInstance(div)).toEqual(null) + }) + }) +}) diff --git a/js/tests/karma.conf.js b/js/tests/karma.conf.js index 00bf5d8d35..5d4b6ed1de 100644 --- a/js/tests/karma.conf.js +++ b/js/tests/karma.conf.js @@ -9,6 +9,7 @@ const { } = require('./browsers') const babel = require('rollup-plugin-babel') const istanbul = require('rollup-plugin-istanbul') +const resolve = require('rollup-plugin-node-resolve') const { env } = process const browserStack = env.BROWSER === 'true' @@ -65,7 +66,8 @@ const rollupPreprocessor = { plugins: [ '@babel/plugin-proposal-object-rest-spread' ] - }) + }), + resolve() ], output: { format: 'iife', diff --git a/js/tests/unit/dropdown.js b/js/tests/unit/dropdown.js deleted file mode 100644 index f4327959b7..0000000000 --- a/js/tests/unit/dropdown.js +++ /dev/null @@ -1,1496 +0,0 @@ -$(function () { - 'use strict' - - var Dropdown = typeof window.bootstrap === 'undefined' ? window.Dropdown : window.bootstrap.Dropdown - - QUnit.module('dropdowns plugin') - - QUnit.test('should be defined on jquery object', function (assert) { - assert.expect(1) - assert.ok($(document.body).dropdown, 'dropdown method is defined') - }) - - QUnit.module('dropdowns', { - beforeEach: function () { - // Run all tests in noConflict mode -- it's the only way to ensure that the plugin works in noConflict mode - $.fn.bootstrapDropdown = $.fn.dropdown.noConflict() - }, - afterEach: function () { - $.fn.dropdown = $.fn.bootstrapDropdown - delete $.fn.bootstrapDropdown - $('#qunit-fixture').html('') - } - }) - - QUnit.test('should provide no conflict', function (assert) { - assert.expect(1) - assert.strictEqual(typeof $.fn.dropdown, 'undefined', 'dropdown was set back to undefined (org value)') - }) - - QUnit.test('should throw explicit error on undefined method', function (assert) { - assert.expect(1) - var $el = $('<div/>') - $el.appendTo('#qunit-fixture') - $el.bootstrapDropdown() - try { - $el.bootstrapDropdown('noMethod') - } catch (error) { - assert.strictEqual(error.message, 'No method named "noMethod"') - } - }) - - QUnit.test('should return jquery collection containing the element', function (assert) { - assert.expect(2) - var $el = $('<div/>') - $el.appendTo('#qunit-fixture') - var $dropdown = $el.bootstrapDropdown() - assert.ok($dropdown instanceof $, 'returns jquery collection') - assert.strictEqual($dropdown[0], $el[0], 'collection contains element') - }) - - QUnit.test('should not open dropdown if target is disabled via attribute', function (assert) { - assert.expect(1) - var done = assert.async() - var dropdownHTML = '<div class="tabs">' + - '<div class="dropdown">' + - '<button disabled href="#" class="btn dropdown-toggle" data-toggle="dropdown">Dropdown</button>' + - '<div class="dropdown-menu">' + - '<a class="dropdown-item" href="#">Secondary link</a>' + - '<a class="dropdown-item" href="#">Something else here</a>' + - '<div class="divider"/>' + - '<a class="dropdown-item" href="#">Another link</a>' + - '</div>' + - '</div>' + - '</div>' - $(dropdownHTML).appendTo('#qunit-fixture') - var $dropdown = $('#qunit-fixture').find('[data-toggle="dropdown"]').bootstrapDropdown() - $dropdown.on('click', function () { - assert.ok(!$dropdown.parent('.dropdown').hasClass('show')) - done() - }) - $dropdown.trigger($.Event('click')) - }) - - QUnit.test('should not open dropdown if escape key was pressed on the toggle', function (assert) { - assert.expect(1) - var done = assert.async() - var dropdownHTML = '<div class="tabs">' + - '<div class="dropdown">' + - '<button disabled href="#" class="btn dropdown-toggle" data-toggle="dropdown">Dropdown</button>' + - '<div class="dropdown-menu">' + - '<a class="dropdown-item" href="#">Secondary link</a>' + - '<a class="dropdown-item" href="#">Something else here</a>' + - '<div class="divider"/>' + - '<a class="dropdown-item" href="#">Another link</a>' + - '</div>' + - '</div>' + - '</div>' - $(dropdownHTML).appendTo('#qunit-fixture') - var $dropdown = $('#qunit-fixture').find('[data-toggle="dropdown"]').bootstrapDropdown() - var $button = $('button[data-toggle="dropdown"]') - $button[0].focus() - // Key escape - var keydown = new Event('keydown') - keydown.which = 27 - $button[0].dispatchEvent(keydown) - assert.ok(!$dropdown.parent('.dropdown').hasClass('show'), 'dropdown menu is not shown after escape pressed') - done() - }) - - QUnit.test('should not add class position-static to dropdown if boundary not set', function (assert) { - assert.expect(1) - var done = assert.async() - var dropdownHTML = '<div class="tabs">' + - '<div class="dropdown">' + - '<a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown</a>' + - '<div class="dropdown-menu">' + - '<a class="dropdown-item" href="#">Secondary link</a>' + - '<a class="dropdown-item" href="#">Something else here</a>' + - '</div>' + - '</div>' + - '</div>' - var $dropdown = $(dropdownHTML).find('[data-toggle="dropdown"]').bootstrapDropdown() - $dropdown - .parent('.dropdown') - .on('shown.bs.dropdown', function () { - assert.ok(!$dropdown.parent('.dropdown').hasClass('position-static'), '"position-static" class not added') - done() - }) - $dropdown[0].dispatchEvent(new Event('click')) - }) - - QUnit.test('should add class position-static to dropdown if boundary not scrollParent', function (assert) { - assert.expect(1) - var done = assert.async() - var dropdownHTML = '<div class="tabs">' + - '<div class="dropdown">' + - '<a href="#" class="dropdown-toggle" data-toggle="dropdown" data-boundary="viewport">Dropdown</a>' + - '<div class="dropdown-menu">' + - '<a class="dropdown-item" href="#">Secondary link</a>' + - '<a class="dropdown-item" href="#">Something else here</a>' + - '</div>' + - '</div>' + - '</div>' - var $dropdown = $(dropdownHTML).find('[data-toggle="dropdown"]').bootstrapDropdown() - $dropdown - .parent('.dropdown') - .on('shown.bs.dropdown', function () { - assert.ok($dropdown.parent('.dropdown').hasClass('position-static'), '"position-static" class added') - done() - }) - $dropdown[0].dispatchEvent(new Event('click')) - }) - - QUnit.test('should set aria-expanded="true" on target when dropdown menu is shown', function (assert) { - assert.expect(1) - var done = assert.async() - var dropdownHTML = '<div class="tabs">' + - '<div class="dropdown">' + - '<a href="#" class="dropdown-toggle" data-toggle="dropdown" aria-expanded="false">Dropdown</a>' + - '<div class="dropdown-menu">' + - '<a class="dropdown-item" href="#">Secondary link</a>' + - '<a class="dropdown-item" href="#">Something else here</a>' + - '<div class="divider"/>' + - '<a class="dropdown-item" href="#">Another link</a>' + - '</div>' + - '</div>' + - '</div>' - var $dropdown = $(dropdownHTML) - .appendTo('#qunit-fixture') - .find('[data-toggle="dropdown"]') - .bootstrapDropdown() - - var dropdownParent = $dropdown.parent('.dropdown')[0] - - dropdownParent.addEventListener('shown.bs.dropdown', function () { - assert.strictEqual($dropdown.attr('aria-expanded'), 'true', 'aria-expanded is set to string "true" on click') - done() - }) - $dropdown[0].dispatchEvent(new Event('click')) - }) - - QUnit.test('should set aria-expanded="false" on target when dropdown menu is hidden', function (assert) { - assert.expect(1) - var done = assert.async() - var dropdownHTML = '<div class="tabs">' + - '<div class="dropdown">' + - '<a href="#" class="dropdown-toggle" aria-expanded="false" data-toggle="dropdown">Dropdown</a>' + - '<div class="dropdown-menu">' + - '<a class="dropdown-item" href="#">Secondary link</a>' + - '<a class="dropdown-item" href="#">Something else here</a>' + - '<div class="divider"/>' + - '<a class="dropdown-item" href="#">Another link</a>' + - '</div>' + - '</div>' + - '</div>' - var $dropdown = $(dropdownHTML) - .appendTo('#qunit-fixture') - .find('[data-toggle="dropdown"]') - .bootstrapDropdown() - - var dropdownParent = $dropdown.parent('.dropdown')[0] - - dropdownParent.addEventListener('hidden.bs.dropdown', function () { - assert.strictEqual($dropdown.attr('aria-expanded'), 'false', 'aria-expanded is set to string "false" on hide') - done() - }) - - $dropdown[0].dispatchEvent(new Event('click')) - document.body.click() - }) - - QUnit.test('should not open dropdown if target is disabled via class', function (assert) { - assert.expect(1) - var done = assert.async() - var dropdownHTML = '<div class="tabs">' + - '<div class="dropdown">' + - '<button href="#" class="btn dropdown-toggle disabled" data-toggle="dropdown">Dropdown</button>' + - '<div class="dropdown-menu">' + - '<a class="dropdown-item" href="#">Secondary link</a>' + - '<a class="dropdown-item" href="#">Something else here</a>' + - '<div class="divider"/>' + - '<a class="dropdown-item" href="#">Another link</a>' + - '</div>' + - '</div>' + - '</div>' - - $(dropdownHTML).appendTo('#qunit-fixture') - var $dropdown = $('#qunit-fixture').find('[data-toggle="dropdown"]').bootstrapDropdown() - $dropdown.on('click', function () { - assert.ok(!$dropdown.parent('.dropdown').hasClass('show')) - done() - }) - $dropdown.trigger($.Event('click')) - }) - - QUnit.test('should add class show to menu if clicked', function (assert) { - assert.expect(1) - var done = assert.async() - var dropdownHTML = '<div class="tabs">' + - '<div class="dropdown">' + - ' <a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown</a>' + - ' <div class="dropdown-menu">' + - ' <a class="dropdown-item" href="#">Secondary link</a>' + - ' <a class="dropdown-item" href="#">Something else here</a>' + - ' <div class="divider"/>' + - ' <a class="dropdown-item" href="#">Another link</a>' + - ' </div>' + - ' </div>' + - '</div>' - - $(dropdownHTML).appendTo('#qunit-fixture') - var $dropdown = $('#qunit-fixture').find('[data-toggle="dropdown"]').bootstrapDropdown() - - $dropdown - .parent('.dropdown') - .on('shown.bs.dropdown', function () { - assert.ok($dropdown.parent('.dropdown').hasClass('show'), '"show" class added on click') - done() - }) - $dropdown[0].dispatchEvent(new Event('click')) - }) - - QUnit.test('should remove "show" class if body is clicked', function (assert) { - assert.expect(2) - var done = assert.async() - var dropdownHTML = '<div class="tabs">' + - '<div class="dropdown">' + - '<a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown</a>' + - '<div class="dropdown-menu">' + - '<a class="dropdown-item" href="#">Secondary link</a>' + - '<a class="dropdown-item" href="#">Something else here</a>' + - '<div class="divider"/>' + - '<a class="dropdown-item" href="#">Another link</a>' + - '</div>' + - '</div>' + - '</div>' - var $dropdown = $(dropdownHTML) - .appendTo('#qunit-fixture') - .find('[data-toggle="dropdown"]') - .bootstrapDropdown() - - $dropdown - .parent('.dropdown') - .on('shown.bs.dropdown', function () { - assert.ok($dropdown.parent('.dropdown').hasClass('show'), '"show" class added on click') - $(document.body).trigger('click') - }).on('hidden.bs.dropdown', function () { - assert.ok(!$dropdown.parent('.dropdown').hasClass('show'), '"show" class removed') - done() - }) - - $dropdown[0].dispatchEvent(new Event('click')) - }) - - QUnit.test('should remove "show" class if tabbing outside of menu', function (assert) { - assert.expect(2) - var done = assert.async() - var dropdownHTML = '<div class="tabs">' + - '<div class="dropdown">' + - '<a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown</a>' + - '<div class="dropdown-menu">' + - '<a class="dropdown-item" href="#">Secondary link</a>' + - '<a class="dropdown-item" href="#">Something else here</a>' + - '<div class="dropdown-divider"/>' + - '<a class="dropdown-item" href="#">Another link</a>' + - '</div>' + - '</div>' + - '</div>' - var $dropdown = $(dropdownHTML) - .appendTo('#qunit-fixture') - .find('[data-toggle="dropdown"]') - .bootstrapDropdown() - $dropdown - .parent('.dropdown') - .on('shown.bs.dropdown', function () { - assert.ok($dropdown.parent('.dropdown').hasClass('show'), '"show" class added on click') - - var keyup9 = new Event('keyup') - keyup9.which = 9 // Tab - document.dispatchEvent(keyup9) - }) - .on('hidden.bs.dropdown', function () { - assert.ok(!$dropdown.parent('.dropdown').hasClass('show'), '"show" class removed') - done() - }) - - $dropdown[0].dispatchEvent(new Event('click')) - }) - - QUnit.test('should remove "show" class if body is clicked, with multiple dropdowns', function (assert) { - assert.expect(7) - var done = assert.async() - var dropdownHTML = '<div class="nav">' + - '<div class="dropdown" id="testmenu">' + - '<a class="dropdown-toggle" data-toggle="dropdown" href="#testmenu">Test menu <span class="caret"/></a>' + - '<div class="dropdown-menu">' + - '<a class="dropdown-item" href="#sub1">Submenu 1</a>' + - '</div>' + - '</div>' + - '</div>' + - '<div class="btn-group">' + - '<button class="btn">Actions</button>' + - '<button class="btn dropdown-toggle" data-toggle="dropdown"></button>' + - '<div class="dropdown-menu">' + - '<a class="dropdown-item" href="#">Action 1</a>' + - '</div>' + - '</div>' - var $dropdowns = $(dropdownHTML).appendTo('#qunit-fixture').find('[data-toggle="dropdown"]') - var $first = $dropdowns.first() - var $last = $dropdowns.last() - - assert.strictEqual($dropdowns.length, 2, 'two dropdowns') - - $first.parent('.dropdown') - .on('shown.bs.dropdown', function () { - assert.strictEqual($first.parents('.show').length, 1, '"show" class added on click') - assert.strictEqual($('#qunit-fixture .dropdown-menu.show').length, 1, 'only one dropdown is shown') - $(document.body).trigger('click') - }).on('hidden.bs.dropdown', function () { - assert.strictEqual($('#qunit-fixture .dropdown-menu.show').length, 0, '"show" class removed') - $last[0].dispatchEvent(new Event('click')) - }) - - $last.parent('.btn-group') - .on('shown.bs.dropdown', function () { - assert.strictEqual($last.parent('.show').length, 1, '"show" class added on click') - assert.strictEqual($('#qunit-fixture .dropdown-menu.show').length, 1, 'only one dropdown is shown') - $(document.body).trigger('click') - }).on('hidden.bs.dropdown', function () { - assert.strictEqual($('#qunit-fixture .dropdown-menu.show').length, 0, '"show" class removed') - done() - }) - $first[0].dispatchEvent(new Event('click')) - }) - - QUnit.test('should remove "show" class if body if tabbing outside of menu, with multiple dropdowns', function (assert) { - assert.expect(7) - var done = assert.async() - var dropdownHTML = '<div class="nav">' + - '<div class="dropdown" id="testmenu">' + - '<a class="dropdown-toggle" data-toggle="dropdown" href="#testmenu">Test menu <span class="caret"/></a>' + - '<div class="dropdown-menu">' + - '<a class="dropdown-item" href="#sub1">Submenu 1</a>' + - '</div>' + - '</div>' + - '</div>' + - '<div class="btn-group">' + - '<button class="btn">Actions</button>' + - '<button class="btn dropdown-toggle" data-toggle="dropdown"><span class="caret"/></button>' + - '<div class="dropdown-menu">' + - '<a class="dropdown-item" href="#">Action 1</a>' + - '</div>' + - '</div>' - var $dropdowns = $(dropdownHTML).appendTo('#qunit-fixture').find('[data-toggle="dropdown"]') - var $first = $dropdowns.first() - var $last = $dropdowns.last() - - assert.strictEqual($dropdowns.length, 2, 'two dropdowns') - - $first.parent('.dropdown') - .on('shown.bs.dropdown', function () { - assert.strictEqual($first.parents('.show').length, 1, '"show" class added on click') - assert.strictEqual($('#qunit-fixture .dropdown-menu.show').length, 1, 'only one dropdown is shown') - var keyup = new Event('keyup') - keyup.which = 9 // Tab - document.dispatchEvent(keyup) - }).on('hidden.bs.dropdown', function () { - assert.strictEqual($('#qunit-fixture .dropdown-menu.show').length, 0, '"show" class removed') - $last[0].dispatchEvent(new Event('click')) - }) - - $last.parent('.btn-group') - .on('shown.bs.dropdown', function () { - assert.strictEqual($last.parent('.show').length, 1, '"show" class added on click') - assert.strictEqual($('#qunit-fixture .dropdown-menu.show').length, 1, 'only one dropdown is shown') - var keyup = new Event('keyup') - keyup.which = 9 // Tab - document.dispatchEvent(keyup) - }).on('hidden.bs.dropdown', function () { - assert.strictEqual($('#qunit-fixture .dropdown-menu.show').length, 0, '"show" class removed') - done() - }) - $first[0].dispatchEvent(new Event('click')) - }) - - QUnit.test('should fire show and hide event', function (assert) { - assert.expect(2) - var dropdownHTML = '<div class="tabs">' + - '<div class="dropdown">' + - '<a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown</a>' + - '<div class="dropdown-menu">' + - '<a class="dropdown-item" href="#">Secondary link</a>' + - '<a class="dropdown-item" href="#">Something else here</a>' + - '<div class="divider"/>' + - '<a class="dropdown-item" href="#">Another link</a>' + - '</div>' + - '</div>' + - '</div>' - var $dropdown = $(dropdownHTML) - .appendTo('#qunit-fixture') - .find('[data-toggle="dropdown"]') - .bootstrapDropdown() - - var done = assert.async() - - $dropdown - .parent('.dropdown') - .on('show.bs.dropdown', function () { - assert.ok(true, 'show was fired') - }) - .on('hide.bs.dropdown', function () { - assert.ok(true, 'hide was fired') - done() - }) - - $dropdown[0].dispatchEvent(new Event('click')) - document.body.click() - }) - - QUnit.test('should fire shown and hidden event', function (assert) { - assert.expect(2) - var dropdownHTML = '<div class="tabs">' + - '<div class="dropdown">' + - '<a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown</a>' + - '<div class="dropdown-menu">' + - '<a class="dropdown-item" href="#">Secondary link</a>' + - '<a class="dropdown-item" href="#">Something else here</a>' + - '<div class="divider"/>' + - '<a class="dropdown-item" href="#">Another link</a>' + - '</div>' + - '</div>' + - '</div>' - var $dropdown = $(dropdownHTML) - .appendTo('#qunit-fixture') - .find('[data-toggle="dropdown"]') - .bootstrapDropdown() - - var done = assert.async() - - $dropdown - .parent('.dropdown') - .on('shown.bs.dropdown', function () { - assert.ok(true, 'shown was fired') - }) - .on('hidden.bs.dropdown', function () { - assert.ok(true, 'hidden was fired') - done() - }) - - $dropdown[0].dispatchEvent(new Event('click')) - document.body.click() - }) - - QUnit.test('should fire shown and hidden event with a relatedTarget', function (assert) { - assert.expect(2) - var dropdownHTML = '<div class="tabs">' + - '<div class="dropdown">' + - '<a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown</a>' + - '<div class="dropdown-menu">' + - '<a class="dropdown-item" href="#">Secondary link</a>' + - '<a class="dropdown-item" href="#">Something else here</a>' + - '<div class="divider"/>' + - '<a class="dropdown-item" href="#">Another link</a>' + - '</div>' + - '</div>' + - '</div>' - var $dropdown = $(dropdownHTML) - .appendTo('#qunit-fixture') - .find('[data-toggle="dropdown"]') - .bootstrapDropdown() - var done = assert.async() - - $dropdown.parent('.dropdown') - .on('hidden.bs.dropdown', function (e) { - assert.strictEqual(e.relatedTarget, $dropdown[0]) - done() - }) - .on('shown.bs.dropdown', function (e) { - assert.strictEqual(e.relatedTarget, $dropdown[0]) - $(document.body).trigger('click') - }) - - $dropdown[0].dispatchEvent(new Event('click')) - }) - - QUnit.test('should fire hide and hidden event with a clickEvent', function (assert) { - assert.expect(3) - var dropdownHTML = '<div class="tabs">' + - '<div class="dropdown">' + - '<a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown</a>' + - '<div class="dropdown-menu">' + - '<a class="dropdown-item" href="#">Secondary link</a>' + - '<a class="dropdown-item" href="#">Something else here</a>' + - '<div class="divider"/>' + - '<a class="dropdown-item" href="#">Another link</a>' + - '</div>' + - '</div>' + - '</div>' - var $dropdown = $(dropdownHTML) - .appendTo('#qunit-fixture') - .find('[data-toggle="dropdown"]') - .bootstrapDropdown() - - $dropdown.parent('.dropdown') - .on('hide.bs.dropdown', function (e) { - assert.ok(e.clickEvent) - }) - .on('hidden.bs.dropdown', function (e) { - assert.ok(e.clickEvent) - }) - .on('shown.bs.dropdown', function () { - assert.ok(true, 'shown was fired') - $(document.body).trigger('click') - }) - - $dropdown[0].click() - }) - - QUnit.test('should fire hide and hidden event without a clickEvent if event type is not click', function (assert) { - assert.expect(3) - var dropdownHTML = '<div class="tabs">' + - '<div class="dropdown">' + - '<a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown</a>' + - '<div class="dropdown-menu">' + - '<a class="dropdown-item" href="#">Secondary link</a>' + - '<a class="dropdown-item" href="#">Something else here</a>' + - '<div class="divider"/>' + - '<a class="dropdown-item" href="#">Another link</a>' + - '</div>' + - '</div>' + - '</div>' - var $dropdown = $(dropdownHTML) - .appendTo('#qunit-fixture') - .find('[data-toggle="dropdown"]') - .bootstrapDropdown() - - $dropdown.parent('.dropdown') - .on('hide.bs.dropdown', function (e) { - assert.notOk(e.clickEvent) - }) - .on('hidden.bs.dropdown', function (e) { - assert.notOk(e.clickEvent) - }) - .on('shown.bs.dropdown', function () { - assert.ok(true, 'shown was fired') - - var keyDown = new Event('keydown') - keyDown.which = 27 - $dropdown[0].dispatchEvent(keyDown) - }) - - $dropdown[0].click() - }) - - QUnit.test('should ignore keyboard events within <input>s and <textarea>s', function (assert) { - assert.expect(3) - var done = assert.async() - - var dropdownHTML = '<div class="tabs">' + - '<div class="dropdown">' + - '<a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown</a>' + - '<div class="dropdown-menu">' + - '<a class="dropdown-item" href="#">Secondary link</a>' + - '<a class="dropdown-item" href="#">Something else here</a>' + - '<div class="divider"/>' + - '<a class="dropdown-item" href="#">Another link</a>' + - '<input type="text" id="input">' + - '<textarea id="textarea"/>' + - '</div>' + - '</div>' + - '</div>' - var $dropdown = $(dropdownHTML) - .appendTo('#qunit-fixture') - .find('[data-toggle="dropdown"]') - .bootstrapDropdown() - - var $input = $('#input') - var $textarea = $('#textarea') - - $dropdown - .parent('.dropdown') - .on('shown.bs.dropdown', function () { - assert.ok(true, 'shown was fired') - - $input.trigger('focus').trigger($.Event('keydown', { - which: 38 - })) - assert.ok($(document.activeElement).is($input), 'input still focused') - - $textarea.trigger('focus').trigger($.Event('keydown', { - which: 38 - })) - assert.ok($(document.activeElement).is($textarea), 'textarea still focused') - - done() - }) - - $dropdown[0].dispatchEvent(new Event('click')) - }) - - QUnit.test('should skip disabled element when using keyboard navigation', function (assert) { - assert.expect(3) - var done = assert.async() - var dropdownHTML = '<div class="tabs">' + - '<div class="dropdown">' + - '<a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown</a>' + - '<div class="dropdown-menu">' + - '<a class="dropdown-item disabled" href="#">Disabled link</a>' + - '<button class="dropdown-item" type="button" disabled>Disabled button</button>' + - '<a id="item1" class="dropdown-item" href="#">Another link</a>' + - '</div>' + - '</div>' + - '</div>' - var $dropdown = $(dropdownHTML) - .appendTo('#qunit-fixture') - .find('[data-toggle="dropdown"]') - .bootstrapDropdown() - - $dropdown - .parent('.dropdown') - .on('shown.bs.dropdown', function () { - assert.ok(true, 'shown was fired') - $dropdown.trigger($.Event('keydown', { - which: 40 - })) - $dropdown.trigger($.Event('keydown', { - which: 40 - })) - assert.ok(!$(document.activeElement).is('.disabled'), '.disabled is not focused') - assert.ok(!$(document.activeElement).is(':disabled'), ':disabled is not focused') - done() - }) - $dropdown[0].dispatchEvent(new Event('click')) - }) - - QUnit.test('should focus next/previous element when using keyboard navigation', function (assert) { - assert.expect(4) - var done = assert.async() - var dropdownHTML = '<div class="tabs">' + - '<div class="dropdown">' + - '<a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown</a>' + - '<div class="dropdown-menu">' + - '<a id="item1" class="dropdown-item" href="#">A link</a>' + - '<a id="item2" class="dropdown-item" href="#">Another link</a>' + - '</div>' + - '</div>' + - '</div>' - var $dropdown = $(dropdownHTML) - .appendTo('#qunit-fixture') - .find('[data-toggle="dropdown"]') - .bootstrapDropdown() - - $dropdown - .parent('.dropdown') - .on('shown.bs.dropdown', function () { - assert.ok(true, 'shown was fired') - - var keydown40 = new Event('keydown') - keydown40.which = 40 - $dropdown[0].dispatchEvent(keydown40) - assert.ok($(document.activeElement).is($('#item1')), 'item1 is focused') - - keydown40 = new Event('keydown') - keydown40.which = 40 - document.activeElement.dispatchEvent(keydown40) - assert.ok($(document.activeElement).is($('#item2')), 'item2 is focused') - - var keydown38 = new Event('keydown') - keydown38.which = 38 - document.activeElement.dispatchEvent(keydown38) - assert.ok($(document.activeElement).is($('#item1')), 'item1 is focused') - done() - }) - $dropdown[0].dispatchEvent(new Event('click')) - }) - - QUnit.test('should not close the dropdown if the user clicks on a text field', function (assert) { - assert.expect(2) - var done = assert.async() - var dropdownHTML = '<div class="dropdown">' + - '<button type="button" data-toggle="dropdown">Dropdown</button>' + - '<div class="dropdown-menu">' + - '<input id="textField" type="text" />' + - '</div>' + - '</div>' - var $dropdown = $(dropdownHTML) - .appendTo('#qunit-fixture') - .find('[data-toggle="dropdown"]') - .bootstrapDropdown() - - var $textfield = $('#textField') - $textfield.on('click', function () { - assert.ok($dropdown.parent('.dropdown').hasClass('show'), 'dropdown menu is shown') - done() - }) - - $dropdown - .parent('.dropdown') - .on('shown.bs.dropdown', function () { - assert.ok($dropdown.parent('.dropdown').hasClass('show'), 'dropdown menu is shown') - $textfield[0].dispatchEvent(new Event('click')) - }) - $dropdown[0].dispatchEvent(new Event('click')) - }) - - QUnit.test('should not close the dropdown if the user clicks on a textarea', function (assert) { - assert.expect(2) - var done = assert.async() - var dropdownHTML = '<div class="dropdown">' + - '<button type="button" data-toggle="dropdown">Dropdown</button>' + - '<div class="dropdown-menu">' + - '<textarea id="textArea"></textarea>' + - '</div>' + - '</div>' - var $dropdown = $(dropdownHTML) - .appendTo('#qunit-fixture') - .find('[data-toggle="dropdown"]') - .bootstrapDropdown() - - var $textarea = $('#textArea') - $textarea.on('click', function () { - assert.ok($dropdown.parent('.dropdown').hasClass('show'), 'dropdown menu is shown') - done() - }) - - $dropdown - .parent('.dropdown') - .on('shown.bs.dropdown', function () { - assert.ok($dropdown.parent('.dropdown').hasClass('show'), 'dropdown menu is shown') - $textarea[0].dispatchEvent(new Event('click')) - }) - $dropdown[0].dispatchEvent(new Event('click')) - }) - - QUnit.test('Dropdown should not use Popper.js in navbar', function (assert) { - assert.expect(1) - var done = assert.async() - var html = '<nav class="navbar navbar-expand-md navbar-light bg-light">' + - '<div class="dropdown">' + - ' <a class="nav-link dropdown-toggle" href="#" id="dropdown" data-toggle="dropdown" aria-expanded="false">Dropdown</a>' + - ' <div class="dropdown-menu" aria-labelledby="dropdown">' + - ' <a class="dropdown-item" href="#">Action</a>' + - ' <a class="dropdown-item" href="#">Another action</a>' + - ' <a class="dropdown-item" href="#">Something else here</a>' + - ' </div>' + - '</div>' + - '</nav>' - - $(html).appendTo('#qunit-fixture') - var $triggerDropdown = $('#qunit-fixture') - .find('[data-toggle="dropdown"]') - .bootstrapDropdown() - var $dropdownMenu = $triggerDropdown.next() - - $triggerDropdown - .parent('.dropdown') - .on('shown.bs.dropdown', function () { - assert.ok(typeof $dropdownMenu.attr('style') === 'undefined', 'No inline style applied by Popper.js') - done() - }) - $triggerDropdown[0].dispatchEvent(new Event('click')) - }) - - QUnit.test('should close dropdown and set focus back to toggle when escape is pressed while focused on a dropdown item', function (assert) { - assert.expect(3) - var done = assert.async() - - var dropdownHTML = '<div class="tabs">' + - '<div class="dropdown">' + - '<a href="#" class="dropdown-toggle" id="toggle" data-toggle="dropdown">Dropdown</a>' + - '<div class="dropdown-menu">' + - '<a class="dropdown-item" id="item" href="#">Menu item</a>' + - '</div>' + - '</div>' - var $dropdown = $(dropdownHTML) - .appendTo('#qunit-fixture') - .find('[data-toggle="dropdown"]') - .bootstrapDropdown() - - var $item = $('#item') - var $toggle = $('#toggle') - - $dropdown - .parent('.dropdown') - .on('shown.bs.dropdown', function () { - // Forcibly focus first item - $item.focus() - assert.ok($(document.activeElement)[0] === $item[0], 'menu item initial focus set') - - // Key escape - var keydown = new Event('keydown') - keydown.which = 27 - $item[0].dispatchEvent(keydown) - - assert.ok(!$dropdown.parent('.dropdown').hasClass('show'), 'dropdown menu was closed after escape') - assert.ok($(document.activeElement)[0] === $toggle[0], 'toggle has focus again once menu was closed after escape') - done() - }) - - $dropdown[0].dispatchEvent(new Event('click')) - }) - - QUnit.test('should ignore keyboard events for <input>s and <textarea>s within dropdown-menu, except for escape key', function (assert) { - assert.expect(7) - var done = assert.async() - - var dropdownHTML = '<div class="tabs">' + - '<div class="dropdown">' + - '<a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown</a>' + - '<div class="dropdown-menu">' + - '<a class="dropdown-item" href="#">Secondary link</a>' + - '<a class="dropdown-item" href="#">Something else here</a>' + - '<div class="divider"/>' + - '<a class="dropdown-item" href="#">Another link</a>' + - '<input type="text" id="input">' + - '<textarea id="textarea"/>' + - '</div>' + - '</div>' + - '</div>' - var $dropdown = $(dropdownHTML) - .appendTo('#qunit-fixture') - .find('[data-toggle="dropdown"]') - .bootstrapDropdown() - - var $input = $('#input') - var $textarea = $('#textarea') - - $dropdown - .parent('.dropdown') - .on('shown.bs.dropdown', function () { - // Space key - $input.trigger('focus').trigger($.Event('keydown', { - which: 32 - })) - assert.ok($(document.activeElement)[0] === $input[0], 'input still focused') - $textarea.trigger('focus').trigger($.Event('keydown', { - which: 32 - })) - assert.ok($(document.activeElement)[0] === $textarea[0], 'textarea still focused') - - // Key up - $input.trigger('focus').trigger($.Event('keydown', { - which: 38 - })) - assert.ok($(document.activeElement)[0] === $input[0], 'input still focused') - $textarea.trigger('focus').trigger($.Event('keydown', { - which: 38 - })) - assert.ok($(document.activeElement)[0] === $textarea[0], 'textarea still focused') - - // Key down - $input.trigger('focus').trigger($.Event('keydown', { - which: 40 - })) - assert.ok($(document.activeElement)[0] === $input[0], 'input still focused') - $textarea.trigger('focus').trigger($.Event('keydown', { - which: 40 - })) - assert.ok($(document.activeElement)[0] === $textarea[0], 'textarea still focused') - - // Key escape - $input.trigger('focus') - var keydown = new Event('keydown') - keydown.which = 27 - $input[0].dispatchEvent(keydown) - - assert.ok(!$dropdown.parent('.dropdown').hasClass('show'), 'dropdown menu is not shown') - done() - }) - - $dropdown[0].dispatchEvent(new Event('click')) - }) - - QUnit.test('should ignore space key events for <input>s within dropdown, and accept up, down and escape', function (assert) { - assert.expect(7) - var done = assert.async() - - var dropdownHTML = - '<ul class="nav tabs">' + - ' <li class="dropdown">' + - ' <input type="text" id="input" data-toggle="dropdown">' + - ' <div class="dropdown-menu" role="menu">' + - ' <a id="item1" class="dropdown-item" href="#">Secondary link</a>' + - ' <a id="item2" class="dropdown-item" href="#">Something else here</a>' + - ' <div class="divider"></div>' + - ' <a class="dropdown-item" href="#">Another link</a>' + - ' </div>' + - ' </li>' + - '</ul>' - - var $dropdown = $(dropdownHTML) - .appendTo('#qunit-fixture') - .find('[data-toggle="dropdown"]') - .bootstrapDropdown() - - var $input = $('#input') - - $dropdown - .parent('.dropdown') - .one('shown.bs.dropdown', function () { - assert.ok(true, 'shown was fired') - - // Key space - $input.trigger('focus').trigger($.Event('keydown', { - which: 32 - })) - assert.ok($dropdown.parent('.dropdown').hasClass('show'), 'dropdown menu is shown') - assert.ok($(document.activeElement).is($input), 'input is still focused') - - // Key escape - $input.trigger('focus') - var keydown = new Event('keydown') - keydown.which = 27 - $input[0].dispatchEvent(keydown) - assert.ok(!$dropdown.parent('.dropdown').hasClass('show'), 'dropdown menu is not shown') - - $dropdown - .parent('.dropdown') - .one('shown.bs.dropdown', function () { - assert.ok(true, 'shown was fired') - - // Key down - $input.trigger('focus') - var keydown40 = new Event('keydown') - keydown40.which = 40 - $input[0].dispatchEvent(keydown40) - assert.ok(document.activeElement === $('#item1')[0], 'item1 is focused') - - $dropdown - .parent('.dropdown') - .one('shown.bs.dropdown', function () { - // Key up - $input.trigger('focus') - var keydown38 = new Event('keydown') - keydown38.which = 38 - $input[0].dispatchEvent(keydown38) - - assert.ok(document.activeElement === $('#item1')[0], 'item1 is focused') - done() - }) - .bootstrapDropdown('toggle') - - $input.bootstrapDropdown('toggle') - }) - - $input.bootstrapDropdown('toggle') - }) - - $input[0].dispatchEvent(new Event('click')) - }) - - QUnit.test('should ignore space key events for <textarea>s within dropdown, and accept up, down and escape', function (assert) { - assert.expect(7) - var done = assert.async() - - var dropdownHTML = - '<ul class="nav tabs">' + - ' <li class="dropdown">' + - ' <textarea id="textarea" data-toggle="dropdown"></textarea>' + - ' <div class="dropdown-menu" role="menu">' + - ' <a id="item1" class="dropdown-item" href="#">Secondary link</a>' + - ' <a id="item2" class="dropdown-item" href="#">Something else here</a>' + - ' <div class="divider"></div>' + - ' <a class="dropdown-item" href="#">Another link</a>' + - ' </div>' + - ' </li>' + - '</ul>' - - var $dropdown = $(dropdownHTML) - .appendTo('#qunit-fixture') - .find('[data-toggle="dropdown"]') - .bootstrapDropdown() - - var $textarea = $('#textarea') - - $dropdown - .parent('.dropdown') - .one('shown.bs.dropdown', function () { - assert.ok(true, 'shown was fired') - - // Key space - $textarea.trigger('focus').trigger($.Event('keydown', { - which: 32 - })) - assert.ok($dropdown.parent('.dropdown').hasClass('show'), 'dropdown menu is shown') - assert.ok($(document.activeElement).is($textarea), 'textarea is still focused') - - // Key escape - $textarea.trigger('focus') - var keydown27 = new Event('keydown') - keydown27.which = 27 - $textarea[0].dispatchEvent(keydown27) - assert.ok(!$dropdown.parent('.dropdown').hasClass('show'), 'dropdown menu is not shown') - - $dropdown - .parent('.dropdown') - .one('shown.bs.dropdown', function () { - assert.ok(true, 'shown was fired') - - // Key down - $textarea.trigger('focus') - var keydown40 = new Event('keydown') - keydown40.which = 40 - $textarea[0].dispatchEvent(keydown40) - assert.ok(document.activeElement === $('#item1')[0], 'item1 is focused') - - $dropdown - .parent('.dropdown') - .one('shown.bs.dropdown', function () { - // Key up - $textarea.trigger('focus') - var keydown38 = new Event('keydown') - keydown38.which = 38 - $textarea[0].dispatchEvent(keydown38) - - assert.ok(document.activeElement === $('#item1')[0], 'item1 is focused') - done() - }) - .bootstrapDropdown('toggle') - - $textarea.bootstrapDropdown('toggle') - }) - - $textarea.bootstrapDropdown('toggle') - }) - $textarea[0].dispatchEvent(new Event('click')) - }) - - QUnit.test('should not use Popper.js if display set to static', function (assert) { - assert.expect(1) - var dropdownHTML = - '<div class="dropdown">' + - '<a href="#" class="dropdown-toggle" data-toggle="dropdown" data-display="static">Dropdown</a>' + - '<div class="dropdown-menu">' + - '<a class="dropdown-item" href="#">Secondary link</a>' + - '<a class="dropdown-item" href="#">Something else here</a>' + - '<div class="divider"/>' + - '<a class="dropdown-item" href="#">Another link</a>' + - '</div>' + - '</div>' - - var $dropdown = $(dropdownHTML) - .appendTo('#qunit-fixture') - .find('[data-toggle="dropdown"]') - .bootstrapDropdown() - var done = assert.async() - var dropdownMenu = $dropdown.next()[0] - - $dropdown.parent('.dropdown') - .on('shown.bs.dropdown', function () { - // Popper.js add this attribute when we use it - assert.strictEqual(dropdownMenu.getAttribute('x-placement'), null) - done() - }) - - $dropdown[0].dispatchEvent(new Event('click')) - }) - - QUnit.test('should call Popper.js and detect navbar on update', function (assert) { - assert.expect(3) - - var dropdownHTML = - '<div class="dropdown">' + - ' <a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown</a>' + - ' <div class="dropdown-menu">' + - ' <a class="dropdown-item" href="#">Another link</a>' + - ' </div>' + - '</div>' - - var $dropdown = $(dropdownHTML) - .appendTo('#qunit-fixture') - .find('[data-toggle="dropdown"]') - .bootstrapDropdown() - - var dropdown = Dropdown._getInstance($dropdown[0]) - dropdown.toggle() - assert.ok(dropdown._popper) - - var spyPopper = sinon.spy(dropdown._popper, 'scheduleUpdate') - var spyDetectNavbar = sinon.spy(dropdown, '_detectNavbar') - dropdown.update() - - assert.ok(spyPopper.called) - assert.ok(spyDetectNavbar.called) - }) - - QUnit.test('should just detect navbar on update', function (assert) { - assert.expect(2) - - var dropdownHTML = - '<div class="dropdown">' + - ' <a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown</a>' + - ' <div class="dropdown-menu">' + - ' <a class="dropdown-item" href="#">Another link</a>' + - ' </div>' + - '</div>' - - var $dropdown = $(dropdownHTML) - .appendTo('#qunit-fixture') - .find('[data-toggle="dropdown"]') - .bootstrapDropdown() - - var dropdown = Dropdown._getInstance($dropdown[0]) - var spyDetectNavbar = sinon.spy(dropdown, '_detectNavbar') - - dropdown.update() - - assert.notOk(dropdown._popper) - assert.ok(spyDetectNavbar.called) - }) - - QUnit.test('should dispose dropdown with Popper', function (assert) { - assert.expect(6) - - var dropdownHTML = - '<div class="dropdown">' + - ' <a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown</a>' + - ' <div class="dropdown-menu">' + - ' <a class="dropdown-item" href="#">Another link</a>' + - ' </div>' + - '</div>' - - var $dropdown = $(dropdownHTML) - .appendTo('#qunit-fixture') - .find('[data-toggle="dropdown"]') - .bootstrapDropdown() - - var dropdown = Dropdown._getInstance($dropdown[0]) - dropdown.toggle() - - assert.ok(dropdown._popper) - assert.ok(dropdown._menu !== null) - assert.ok(dropdown._element !== null) - var spyDestroy = sinon.spy(dropdown._popper, 'destroy') - - dropdown.dispose() - - assert.ok(spyDestroy.called) - assert.ok(dropdown._menu === null) - assert.ok(dropdown._element === null) - }) - - QUnit.test('should dispose dropdown', function (assert) { - assert.expect(5) - - var dropdownHTML = - '<div class="dropdown">' + - ' <a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown</a>' + - ' <div class="dropdown-menu">' + - ' <a class="dropdown-item" href="#">Another link</a>' + - ' </div>' + - '</div>' - - var $dropdown = $(dropdownHTML) - .appendTo('#qunit-fixture') - .find('[data-toggle="dropdown"]') - .bootstrapDropdown() - - var dropdown = Dropdown._getInstance($dropdown[0]) - - assert.notOk(dropdown._popper) - assert.ok(dropdown._menu !== null) - assert.ok(dropdown._element !== null) - - dropdown.dispose() - - assert.ok(dropdown._menu === null) - assert.ok(dropdown._element === null) - }) - - QUnit.test('should hide dropdown', function (assert) { - assert.expect(2) - - var dropdownHTML = - '<div class="dropdown">' + - ' <a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown</a>' + - ' <div class="dropdown-menu">' + - ' <a class="dropdown-item" href="#">Another link</a>' + - ' </div>' + - '</div>' - - var $dropdown = $(dropdownHTML) - .appendTo('#qunit-fixture') - .find('[data-toggle="dropdown"]') - .bootstrapDropdown() - - var dropdown = Dropdown._getInstance($dropdown[0]) - var done = assert.async() - - $dropdown - .parent('.dropdown') - .on('shown.bs.dropdown', function () { - dropdown.hide() - }) - .on('hide.bs.dropdown', function () { - assert.ok(true, 'hide was fired') - }) - .on('hidden.bs.dropdown', function () { - assert.ok(!$dropdown.parent('.dropdown').hasClass('show'), 'dropdown menu is hidden') - done() - }) - - dropdown.show() - }) - - QUnit.test('should not hide dropdown', function (assert) { - assert.expect(1) - - var dropdownHTML = - '<div class="dropdown">' + - ' <a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown</a>' + - ' <div class="dropdown-menu">' + - ' <a class="dropdown-item" href="#">Another link</a>' + - ' </div>' + - '</div>' - - var $dropdown = $(dropdownHTML) - .appendTo('#qunit-fixture') - .find('[data-toggle="dropdown"]') - .bootstrapDropdown() - - var dropdown = Dropdown._getInstance($dropdown[0]) - $dropdown.trigger('click') - dropdown.show() - - assert.ok($dropdown.parent('.dropdown').hasClass('show'), 'dropdown menu is still shown') - }) - - QUnit.test('should not show dropdown', function (assert) { - assert.expect(1) - - var dropdownHTML = - '<div class="dropdown">' + - ' <a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown</a>' + - ' <div class="dropdown-menu">' + - ' <a class="dropdown-item" href="#">Another link</a>' + - ' </div>' + - '</div>' - - var $dropdown = $(dropdownHTML) - .appendTo('#qunit-fixture') - .find('[data-toggle="dropdown"]') - .bootstrapDropdown() - - var dropdown = Dropdown._getInstance($dropdown[0]) - dropdown.hide() - assert.ok(!$dropdown.parent('.dropdown').hasClass('show'), 'dropdown menu is still hidden') - }) - - QUnit.test('should show dropdown', function (assert) { - assert.expect(2) - - var dropdownHTML = - '<div class="dropdown">' + - ' <a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown</a>' + - ' <div class="dropdown-menu">' + - ' <a class="dropdown-item" href="#">Another link</a>' + - ' </div>' + - '</div>' - - var $dropdown = $(dropdownHTML) - .appendTo('#qunit-fixture') - .find('[data-toggle="dropdown"]') - .bootstrapDropdown() - - var dropdown = Dropdown._getInstance($dropdown[0]) - var done = assert.async() - - $dropdown - .parent('.dropdown') - .on('show.bs.dropdown', function () { - assert.ok(true, 'show was fired') - }) - .on('shown.bs.dropdown', function () { - assert.ok($dropdown.parent('.dropdown').hasClass('show'), 'dropdown menu is shown') - done() - }) - - dropdown.show() - }) - - QUnit.test('should prevent default event on show method call', function (assert) { - assert.expect(1) - - var dropdownHTML = - '<div class="dropdown">' + - ' <a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown</a>' + - ' <div class="dropdown-menu">' + - ' <a class="dropdown-item" href="#">Another link</a>' + - ' </div>' + - '</div>' - - var $dropdown = $(dropdownHTML) - .appendTo('#qunit-fixture') - .find('[data-toggle="dropdown"]') - .bootstrapDropdown() - - var dropdown = Dropdown._getInstance($dropdown[0]) - var done = assert.async() - - $dropdown - .parent('.dropdown') - .on('show.bs.dropdown', function (event) { - event.preventDefault() - done() - }) - - dropdown.show() - assert.ok(!$dropdown.parent('.dropdown').hasClass('show'), 'dropdown menu is hidden') - }) - - QUnit.test('should prevent default event on hide method call', function (assert) { - assert.expect(1) - - var dropdownHTML = - '<div class="dropdown">' + - ' <a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown</a>' + - ' <div class="dropdown-menu">' + - ' <a class="dropdown-item" href="#">Another link</a>' + - ' </div>' + - '</div>' - - var $dropdown = $(dropdownHTML) - .appendTo('#qunit-fixture') - .find('[data-toggle="dropdown"]') - .bootstrapDropdown() - - var dropdown = Dropdown._getInstance($dropdown[0]) - var done = assert.async() - - $dropdown - .parent('.dropdown') - .on('shown.bs.dropdown', function () { - dropdown.hide() - }) - .on('hide.bs.dropdown', function (event) { - event.preventDefault() - assert.ok($dropdown.parent('.dropdown').hasClass('show'), 'dropdown menu is shown') - done() - }) - - dropdown.show() - }) - - QUnit.test('should not open dropdown via show method if target is disabled via attribute', function (assert) { - assert.expect(1) - var dropdownHTML = - '<div class="dropdown">' + - ' <button disabled href="#" class="btn dropdown-toggle" data-toggle="dropdown">Dropdown</button>' + - ' <div class="dropdown-menu">' + - ' <a class="dropdown-item" href="#">Another link</a>' + - ' </div>' + - '</div>' - $(dropdownHTML).appendTo('#qunit-fixture') - var $dropdown = $('#qunit-fixture').find('[data-toggle="dropdown"]').bootstrapDropdown() - $dropdown.show() - assert.ok(!$dropdown.parent('.dropdown').hasClass('show')) - }) - - QUnit.test('should not open dropdown via show method if target is disabled via class', function (assert) { - assert.expect(1) - var dropdownHTML = - '<div class="dropdown">' + - ' <button href="#" class="btn dropdown-toggle disabled" data-toggle="dropdown">Dropdown</button>' + - ' <div class="dropdown-menu">' + - ' <a class="dropdown-item" href="#">Another link</a>' + - ' </div>' + - '</div>' - - $(dropdownHTML).appendTo('#qunit-fixture') - var $dropdown = $('#qunit-fixture').find('[data-toggle="dropdown"]').bootstrapDropdown() - $dropdown.show() - assert.ok(!$dropdown.parent('.dropdown').hasClass('show')) - }) - - QUnit.test('should not hide dropdown via hide method if target is disabled via attribute', function (assert) { - assert.expect(1) - var dropdownHTML = - '<div class="dropdown show">' + - ' <button disabled href="#" class="btn dropdown-toggle" data-toggle="dropdown">Dropdown</button>' + - ' <div class="dropdown-menu">' + - ' <a class="dropdown-item" href="#">Another link</a>' + - ' </div>' + - '</div>' - $(dropdownHTML).appendTo('#qunit-fixture') - var $dropdown = $('#qunit-fixture').find('[data-toggle="dropdown"]').bootstrapDropdown() - $dropdown.hide() - assert.ok($dropdown.parent('.dropdown').hasClass('show')) - }) - - QUnit.test('should not hide dropdown via hide method if target is disabled via class', function (assert) { - assert.expect(1) - var dropdownHTML = - '<div class="dropdown show">' + - ' <button href="#" class="btn dropdown-toggle disabled" data-toggle="dropdown">Dropdown</button>' + - ' <div class="dropdown-menu">' + - ' <a class="dropdown-item" href="#">Another link</a>' + - ' </div>' + - '</div>' - - $(dropdownHTML).appendTo('#qunit-fixture') - var $dropdown = $('#qunit-fixture').find('[data-toggle="dropdown"]').bootstrapDropdown() - $dropdown.hide() - assert.ok($dropdown.parent('.dropdown').hasClass('show')) - }) - - QUnit.test('should create offset modifier correctly when offset option is a function', function (assert) { - assert.expect(2) - - var getOffset = function (offsets) { - return offsets - } - - var dropdownHTML = - '<div class="dropdown">' + - ' <a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown</a>' + - ' <div class="dropdown-menu">' + - ' <a class="dropdown-item" href="#">Another link</a>' + - ' </div>' + - '</div>' - - var $dropdown = $(dropdownHTML) - .appendTo('#qunit-fixture') - .find('[data-toggle="dropdown"]') - .bootstrapDropdown({ - offset: getOffset - }) - - var dropdown = Dropdown._getInstance($dropdown[0]) - var offset = dropdown._getOffset() - - assert.ok(typeof offset.offset === 'undefined') - assert.ok(typeof offset.fn === 'function') - }) - - QUnit.test('should create offset modifier correctly when offset option is not a function', function (assert) { - assert.expect(2) - - var dropdownHTML = - '<div class="dropdown">' + - ' <a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown</a>' + - ' <div class="dropdown-menu">' + - ' <a class="dropdown-item" href="#">Another link</a>' + - ' </div>' + - '</div>' - - var myOffset = 42 - var $dropdown = $(dropdownHTML) - .appendTo('#qunit-fixture') - .find('[data-toggle="dropdown"]') - .bootstrapDropdown({ - offset: myOffset - }) - - var dropdown = Dropdown._getInstance($dropdown[0]) - var offset = dropdown._getOffset() - - assert.strictEqual(offset.offset, myOffset) - assert.ok(typeof offset.fn === 'undefined') - }) - - QUnit.test('should return the version', function (assert) { - assert.expect(1) - assert.strictEqual(typeof Dropdown.VERSION, 'string') - }) -}) |