123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419 |
- $(function () {
- 'use strict'
- 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.bootstrapDropdown()
- try {
- $el.bootstrapDropdown('noMethod')
- } catch (err) {
- assert.strictEqual(err.message, 'No method named "noMethod"')
- }
- })
- QUnit.test('should return jquery collection containing the element', function (assert) {
- assert.expect(2)
- var $el = $('<div/>')
- 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 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.trigger('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.trigger('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()
- $dropdown
- .parent('.dropdown')
- .on('shown.bs.dropdown', function () {
- assert.strictEqual($dropdown.attr('aria-expanded'), 'true', 'aria-expanded is set to string "true" on click')
- done()
- })
- $dropdown.trigger('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()
- $dropdown
- .parent('.dropdown')
- .on('hidden.bs.dropdown', function () {
- assert.strictEqual($dropdown.attr('aria-expanded'), 'false', 'aria-expanded is set to string "false" on hide')
- done()
- })
- $dropdown.trigger('click')
- $(document.body).trigger('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>'
- var $dropdown = $(dropdownHTML).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.trigger('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.trigger('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 e = $.Event('keyup')
- e.which = 9 // Tab
- $(document.body).trigger(e)
- }).on('hidden.bs.dropdown', function () {
- assert.ok(!$dropdown.parent('.dropdown').hasClass('show'), '"show" class removed')
- done()
- })
- $dropdown.trigger('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.trigger('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.trigger('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 e = $.Event('keyup')
- e.which = 9 // Tab
- $(document.body).trigger(e)
- }).on('hidden.bs.dropdown', function () {
- assert.strictEqual($('#qunit-fixture .dropdown-menu.show').length, 0, '"show" class removed')
- $last.trigger('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 e = $.Event('keyup')
- e.which = 9 // Tab
- $(document.body).trigger(e)
- }).on('hidden.bs.dropdown', function () {
- assert.strictEqual($('#qunit-fixture .dropdown-menu.show').length, 0, '"show" class removed')
- done()
- })
- $first.trigger('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.trigger('click')
- $(document.body).trigger('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.trigger('click')
- $(document.body).trigger('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.trigger('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.trigger('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')
- $dropdown.trigger($.Event('keydown', {
- which: 27
- }))
- })
- $dropdown.trigger('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.trigger('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.trigger('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')
- $dropdown.trigger($.Event('keydown', {
- which: 40
- }))
- assert.ok($(document.activeElement).is($('#item1')), 'item1 is focused')
- $(document.activeElement).trigger($.Event('keydown', {
- which: 40
- }))
- assert.ok($(document.activeElement).is($('#item2')), 'item2 is focused')
- $(document.activeElement).trigger($.Event('keydown', {
- which: 38
- }))
- assert.ok($(document.activeElement).is($('#item1')), 'item1 is focused')
- done()
- })
- $dropdown.trigger('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.trigger($.Event('click'))
- })
- $dropdown.trigger('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.trigger($.Event('click'))
- })
- $dropdown.trigger('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-haspopup="true" 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.trigger($.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').trigger($.Event('keydown', {
- which: 27
- }))
- assert.ok(!$dropdown.parent('.dropdown').hasClass('show'), 'dropdown menu is not shown')
- done()
- })
- $dropdown.trigger('click')
- })
- QUnit.test('should ignore space key events for <input>s within dropdown, and accept up, down and escape', function (assert) {
- assert.expect(6)
- 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').trigger($.Event('keydown', {
- which: 27
- }))
- assert.ok(!$dropdown.parent('.dropdown').hasClass('show'), 'dropdown menu is not shown')
- $dropdown
- .parent('.dropdown')
- .one('shown.bs.dropdown', function () {
- // Key down
- $input.trigger('focus').trigger($.Event('keydown', {
- which: 40
- }))
- assert.ok(document.activeElement === $('#item1')[0], 'item1 is focused')
- $dropdown
- .parent('.dropdown')
- .one('shown.bs.dropdown', function () {
- // Key up
- $input.trigger('focus').trigger($.Event('keydown', {
- which: 38
- }))
- assert.ok(document.activeElement === $('#item1')[0], 'item1 is focused')
- done()
- }).bootstrapDropdown('toggle')
- $input.trigger('click')
- })
- $input.trigger('click')
- })
- $input.trigger('click')
- })
- QUnit.test('should ignore space key events for <textarea>s within dropdown, and accept up, down and escape', function (assert) {
- assert.expect(6)
- 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').trigger($.Event('keydown', {
- which: 27
- }))
- assert.ok(!$dropdown.parent('.dropdown').hasClass('show'), 'dropdown menu is not shown')
- $dropdown
- .parent('.dropdown')
- .one('shown.bs.dropdown', function () {
- // Key down
- $textarea.trigger('focus').trigger($.Event('keydown', {
- which: 40
- }))
- assert.ok(document.activeElement === $('#item1')[0], 'item1 is focused')
- $dropdown
- .parent('.dropdown')
- .one('shown.bs.dropdown', function () {
- // Key up
- $textarea.trigger('focus').trigger($.Event('keydown', {
- which: 38
- }))
- assert.ok(document.activeElement === $('#item1')[0], 'item1 is focused')
- done()
- }).bootstrapDropdown('toggle')
- $textarea.trigger('click')
- })
- $textarea.trigger('click')
- })
- $textarea.trigger('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.trigger('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.data('bs.dropdown')
- 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.data('bs.dropdown')
- 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.data('bs.dropdown')
- 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.data('bs.dropdown')
- 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 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.data('bs.dropdown')
- 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 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.data('bs.dropdown')
- var done = assert.async()
- $dropdown.trigger('click')
- $dropdown
- .parent('.dropdown')
- .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.hide()
- })
- 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.data('bs.dropdown')
- $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.data('bs.dropdown')
- 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.data('bs.dropdown')
- 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.data('bs.dropdown')
- 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.data('bs.dropdown')
- var done = assert.async()
- $dropdown.trigger('click')
- $dropdown
- .parent('.dropdown')
- .on('hide.bs.dropdown', function (event) {
- event.preventDefault()
- done()
- })
- dropdown.hide()
- assert.ok($dropdown.parent('.dropdown').hasClass('show'), 'dropdown menu is shown')
- })
- 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.data('bs.dropdown')
- 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.data('bs.dropdown')
- var offset = dropdown._getOffset()
- assert.strictEqual(offset.offset, myOffset)
- assert.ok(typeof offset.fn === 'undefined')
- })
- })
|