0
0
mirror of https://github.com/twbs/bootstrap.git synced 2025-01-17 09:52:29 +01:00

Clean dropdown unit tests up

This commit is contained in:
Heinrich Fenkart 2014-06-18 21:34:12 +02:00
parent 77d0557468
commit 9ff66a12bc

View File

@ -19,46 +19,48 @@ $(function () {
}) })
test('should provide no conflict', function () { test('should provide no conflict', function () {
ok(!$.fn.dropdown, 'dropdown was set back to undefined (org value)') strictEqual($.fn.dropdown, undefined, 'dropdown was set back to undefined (org value)')
}) })
test('should return element', function () { test('should return jquery collection containing the element', function () {
var el = $('<div />') var $el = $('<div/>')
ok(el.bootstrapDropdown()[0] === el[0], 'same element returned') var $dropdown = $el.bootstrapDropdown()
ok($dropdown instanceof $, 'returns jquery collection')
strictEqual($dropdown[0], $el[0], 'collection contains element')
}) })
test('should not open dropdown if target is disabled', function () { test('should not open dropdown if target is disabled via attribute', function () {
var dropdownHTML = '<ul class="tabs">' + var dropdownHTML = '<ul class="tabs">' +
'<li class="dropdown">' + '<li class="dropdown">' +
'<button disabled href="#" class="btn dropdown-toggle" data-toggle="dropdown">Dropdown</button>' + '<button disabled href="#" class="btn dropdown-toggle" data-toggle="dropdown">Dropdown</button>' +
'<ul class="dropdown-menu">' + '<ul class="dropdown-menu">' +
'<li><a href="#">Secondary link</a></li>' + '<li><a href="#">Secondary link</a></li>' +
'<li><a href="#">Something else here</a></li>' + '<li><a href="#">Something else here</a></li>' +
'<li class="divider"></li>' + '<li class="divider"/>' +
'<li><a href="#">Another link</a></li>' + '<li><a href="#">Another link</a></li>' +
'</ul>' + '</ul>' +
'</li>' + '</li>' +
'</ul>' '</ul>'
var dropdown = $(dropdownHTML).find('[data-toggle="dropdown"]').bootstrapDropdown().click() var $dropdown = $(dropdownHTML).find('[data-toggle="dropdown"]').bootstrapDropdown().click()
ok(!dropdown.parent('.dropdown').hasClass('open'), 'open class added on click') ok(!$dropdown.parent('.dropdown').hasClass('open'), '"open" class added on click')
}) })
test('should not open dropdown if target is disabled', function () { test('should not open dropdown if target is disabled via class', function () {
var dropdownHTML = '<ul class="tabs">' + var dropdownHTML = '<ul class="tabs">' +
'<li class="dropdown">' + '<li class="dropdown">' +
'<button href="#" class="btn dropdown-toggle disabled" data-toggle="dropdown">Dropdown</button>' + '<button href="#" class="btn dropdown-toggle disabled" data-toggle="dropdown">Dropdown</button>' +
'<ul class="dropdown-menu">' + '<ul class="dropdown-menu">' +
'<li><a href="#">Secondary link</a></li>' + '<li><a href="#">Secondary link</a></li>' +
'<li><a href="#">Something else here</a></li>' + '<li><a href="#">Something else here</a></li>' +
'<li class="divider"></li>' + '<li class="divider"/>' +
'<li><a href="#">Another link</a></li>' + '<li><a href="#">Another link</a></li>' +
'</ul>' + '</ul>' +
'</li>' + '</li>' +
'</ul>' '</ul>'
var dropdown = $(dropdownHTML).find('[data-toggle="dropdown"]').bootstrapDropdown().click() var $dropdown = $(dropdownHTML).find('[data-toggle="dropdown"]').bootstrapDropdown().click()
ok(!dropdown.parent('.dropdown').hasClass('open'), 'open class added on click') ok(!$dropdown.parent('.dropdown').hasClass('open'), '"open" class added on click')
}) })
test('should add class open to menu if clicked', function () { test('should add class open to menu if clicked', function () {
@ -68,14 +70,14 @@ $(function () {
'<ul class="dropdown-menu">' + '<ul class="dropdown-menu">' +
'<li><a href="#">Secondary link</a></li>' + '<li><a href="#">Secondary link</a></li>' +
'<li><a href="#">Something else here</a></li>' + '<li><a href="#">Something else here</a></li>' +
'<li class="divider"></li>' + '<li class="divider"/>' +
'<li><a href="#">Another link</a></li>' + '<li><a href="#">Another link</a></li>' +
'</ul>' + '</ul>' +
'</li>' + '</li>' +
'</ul>' '</ul>'
var dropdown = $(dropdownHTML).find('[data-toggle="dropdown"]').bootstrapDropdown().click() var $dropdown = $(dropdownHTML).find('[data-toggle="dropdown"]').bootstrapDropdown().click()
ok(dropdown.parent('.dropdown').hasClass('open'), 'open class added on click') ok($dropdown.parent('.dropdown').hasClass('open'), '"open" class added on click')
}) })
test('should test if element has a # before assuming it\'s a selector', function () { test('should test if element has a # before assuming it\'s a selector', function () {
@ -85,77 +87,74 @@ $(function () {
'<ul class="dropdown-menu">' + '<ul class="dropdown-menu">' +
'<li><a href="#">Secondary link</a></li>' + '<li><a href="#">Secondary link</a></li>' +
'<li><a href="#">Something else here</a></li>' + '<li><a href="#">Something else here</a></li>' +
'<li class="divider"></li>' + '<li class="divider"/>' +
'<li><a href="#">Another link</a></li>' + '<li><a href="#">Another link</a></li>' +
'</ul>' + '</ul>' +
'</li>' + '</li>' +
'</ul>' '</ul>'
var dropdown = $(dropdownHTML).find('[data-toggle="dropdown"]').bootstrapDropdown().click() var $dropdown = $(dropdownHTML).find('[data-toggle="dropdown"]').bootstrapDropdown().click()
ok(dropdown.parent('.dropdown').hasClass('open'), 'open class added on click') ok($dropdown.parent('.dropdown').hasClass('open'), '"open" class added on click')
}) })
test('should remove open class if body clicked', function () { test('should remove "open" class if body is clicked', function () {
var dropdownHTML = '<ul class="tabs">' + var dropdownHTML = '<ul class="tabs">' +
'<li class="dropdown">' + '<li class="dropdown">' +
'<a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown</a>' + '<a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown</a>' +
'<ul class="dropdown-menu">' + '<ul class="dropdown-menu">' +
'<li><a href="#">Secondary link</a></li>' + '<li><a href="#">Secondary link</a></li>' +
'<li><a href="#">Something else here</a></li>' + '<li><a href="#">Something else here</a></li>' +
'<li class="divider"></li>' + '<li class="divider"/>' +
'<li><a href="#">Another link</a></li>' + '<li><a href="#">Another link</a></li>' +
'</ul>' + '</ul>' +
'</li>' + '</li>' +
'</ul>' '</ul>'
var dropdown = $(dropdownHTML) var $dropdown = $(dropdownHTML)
.appendTo('#qunit-fixture') .appendTo('#qunit-fixture')
.find('[data-toggle="dropdown"]') .find('[data-toggle="dropdown"]')
.bootstrapDropdown() .bootstrapDropdown()
.click() .click()
ok(dropdown.parent('.dropdown').hasClass('open'), 'open class added on click') ok($dropdown.parent('.dropdown').hasClass('open'), '"open" class added on click')
$('body').click() $(document.body).click()
ok(!dropdown.parent('.dropdown').hasClass('open'), 'open class removed') ok(!$dropdown.parent('.dropdown').hasClass('open'), '"open" class removed')
dropdown.remove()
}) })
test('should remove open class if body clicked, with multiple drop downs', function () { test('should remove "open" class if body is clicked, with multiple dropdowns', function () {
var dropdownHTML = '<ul class="nav">' + var dropdownHTML = '<ul class="nav">' +
' <li><a href="#menu1">Menu 1</a></li>' + '<li><a href="#menu1">Menu 1</a></li>' +
' <li class="dropdown" id="testmenu">' + '<li class="dropdown" id="testmenu">' +
' <a class="dropdown-toggle" data-toggle="dropdown" href="#testmenu">Test menu <b class="caret"></b></a>' + '<a class="dropdown-toggle" data-toggle="dropdown" href="#testmenu">Test menu <span class="caret"/></a>' +
' <ul class="dropdown-menu" role="menu">' + '<ul class="dropdown-menu" role="menu">' +
' <li><a href="#sub1">Submenu 1</a></li>' + '<li><a href="#sub1">Submenu 1</a></li>' +
' </ul>' + '</ul>' +
' </li>' + '</li>' +
'</ul>' + '</ul>' +
'<div class="btn-group">' + '<div class="btn-group">' +
' <button class="btn">Actions</button>' + '<button class="btn">Actions</button>' +
' <button class="btn dropdown-toggle" data-toggle="dropdown"><span class="caret"></span></button>' + '<button class="btn dropdown-toggle" data-toggle="dropdown"><span class="caret"/></button>' +
' <ul class="dropdown-menu">' + '<ul class="dropdown-menu">' +
' <li><a href="#">Action 1</a></li>' + '<li><a href="#">Action 1</a></li>' +
' </ul>' + '</ul>' +
'</div>' '</div>'
var dropdowns = $(dropdownHTML).appendTo('#qunit-fixture').find('[data-toggle="dropdown"]') var $dropdowns = $(dropdownHTML).appendTo('#qunit-fixture').find('[data-toggle="dropdown"]')
var first = dropdowns.first() var $first = $dropdowns.first()
var last = dropdowns.last() var $last = $dropdowns.last()
ok(dropdowns.length == 2, 'Should be two dropdowns') strictEqual($dropdowns.length, 2, 'two dropdowns')
first.click() $first.click()
ok(first.parents('.open').length == 1, 'open class added on click') strictEqual($first.parents('.open').length, 1, '"open" class added on click')
ok($('#qunit-fixture .open').length == 1, 'only one object is open') strictEqual($('#qunit-fixture .open').length, 1, 'only one dropdown is open')
$('body').click() $(document.body).click()
ok($('#qunit-fixture .open').length === 0, 'open class removed') strictEqual($('#qunit-fixture .open').length, 0, '"open" class removed')
last.click() $last.click()
ok(last.parent('.open').length == 1, 'open class added on click') strictEqual($last.parent('.open').length, 1, '"open" class added on click')
ok($('#qunit-fixture .open').length == 1, 'only one object is open') strictEqual($('#qunit-fixture .open').length, 1, 'only one dropdown is open')
$('body').click() $(document.body).click()
ok($('#qunit-fixture .open').length === 0, 'open class removed') strictEqual($('#qunit-fixture .open').length, 0, '"open" class removed')
$('#qunit-fixture').html('')
}) })
test('should fire show and hide event', function () { test('should fire show and hide event', function () {
@ -165,63 +164,63 @@ $(function () {
'<ul class="dropdown-menu">' + '<ul class="dropdown-menu">' +
'<li><a href="#">Secondary link</a></li>' + '<li><a href="#">Secondary link</a></li>' +
'<li><a href="#">Something else here</a></li>' + '<li><a href="#">Something else here</a></li>' +
'<li class="divider"></li>' + '<li class="divider"/>' +
'<li><a href="#">Another link</a></li>' + '<li><a href="#">Another link</a></li>' +
'</ul>' + '</ul>' +
'</li>' + '</li>' +
'</ul>' '</ul>'
var dropdown = $(dropdownHTML) var $dropdown = $(dropdownHTML)
.appendTo('#qunit-fixture') .appendTo('#qunit-fixture')
.find('[data-toggle="dropdown"]') .find('[data-toggle="dropdown"]')
.bootstrapDropdown() .bootstrapDropdown()
stop() stop()
dropdown $dropdown
.parent('.dropdown') .parent('.dropdown')
.on('show.bs.dropdown', function () { .on('show.bs.dropdown', function () {
ok(true, 'show was called') ok(true, 'show was fired')
}) })
.on('hide.bs.dropdown', function () { .on('hide.bs.dropdown', function () {
ok(true, 'hide was called') ok(true, 'hide was fired')
start() start()
}) })
dropdown.click() $dropdown.click()
$(document.body).click() $(document.body).click()
}) })
test('should fire shown and hiden event', function () { test('should fire shown and hidden event', function () {
var dropdownHTML = '<ul class="tabs">' + var dropdownHTML = '<ul class="tabs">' +
'<li class="dropdown">' + '<li class="dropdown">' +
'<a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown</a>' + '<a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown</a>' +
'<ul class="dropdown-menu">' + '<ul class="dropdown-menu">' +
'<li><a href="#">Secondary link</a></li>' + '<li><a href="#">Secondary link</a></li>' +
'<li><a href="#">Something else here</a></li>' + '<li><a href="#">Something else here</a></li>' +
'<li class="divider"></li>' + '<li class="divider"/>' +
'<li><a href="#">Another link</a></li>' + '<li><a href="#">Another link</a></li>' +
'</ul>' + '</ul>' +
'</li>' + '</li>' +
'</ul>' '</ul>'
var dropdown = $(dropdownHTML) var $dropdown = $(dropdownHTML)
.appendTo('#qunit-fixture') .appendTo('#qunit-fixture')
.find('[data-toggle="dropdown"]') .find('[data-toggle="dropdown"]')
.bootstrapDropdown() .bootstrapDropdown()
stop() stop()
dropdown $dropdown
.parent('.dropdown') .parent('.dropdown')
.on('shown.bs.dropdown', function () { .on('shown.bs.dropdown', function () {
ok(true, 'show was called') ok(true, 'shown was fired')
}) })
.on('hidden.bs.dropdown', function () { .on('hidden.bs.dropdown', function () {
ok(true, 'hide was called') ok(true, 'hidden was fired')
start() start()
}) })
dropdown.click() $dropdown.click()
$(document.body).click() $(document.body).click()
}) })