0
0
mirror of https://github.com/twbs/bootstrap.git synced 2024-12-10 22:24:19 +01:00
Bootstrap/js/tests/unit/dropdown.js

228 lines
8.1 KiB
JavaScript
Raw Normal View History

$(function () {
2014-03-17 08:12:55 +01:00
'use strict';
module('dropdowns plugin')
2014-02-13 08:55:12 +01:00
test('should be defined on jquery object', function () {
ok($(document.body).dropdown, 'dropdown method is defined')
})
module('dropdowns', {
2014-03-17 08:12:55 +01:00
setup: 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()
},
2014-03-17 08:12:55 +01:00
teardown: function () {
$.fn.dropdown = $.fn.bootstrapDropdown
delete $.fn.bootstrapDropdown
}
})
test('should provide no conflict', function () {
2014-06-18 21:34:12 +02:00
strictEqual($.fn.dropdown, undefined, 'dropdown was set back to undefined (org value)')
})
2014-06-18 21:34:12 +02:00
test('should return jquery collection containing the element', function () {
var $el = $('<div/>')
var $dropdown = $el.bootstrapDropdown()
ok($dropdown instanceof $, 'returns jquery collection')
strictEqual($dropdown[0], $el[0], 'collection contains element')
2014-02-13 08:55:12 +01:00
})
2014-06-18 21:34:12 +02:00
test('should not open dropdown if target is disabled via attribute', function () {
2014-07-06 11:56:12 +02:00
var dropdownHTML = '<ul class="tabs">'
+ '<li class="dropdown">'
+ '<button disabled href="#" class="btn dropdown-toggle" data-toggle="dropdown">Dropdown</button>'
+ '<ul class="dropdown-menu">'
+ '<li><a href="#">Secondary link</a></li>'
+ '<li><a href="#">Something else here</a></li>'
+ '<li class="divider"/>'
+ '<li><a href="#">Another link</a></li>'
+ '</ul>'
+ '</li>'
+ '</ul>'
2014-06-18 21:34:12 +02:00
var $dropdown = $(dropdownHTML).find('[data-toggle="dropdown"]').bootstrapDropdown().click()
2014-02-13 08:55:12 +01:00
2014-06-18 21:34:12 +02:00
ok(!$dropdown.parent('.dropdown').hasClass('open'), '"open" class added on click')
2014-02-13 08:55:12 +01:00
})
2014-06-18 21:34:12 +02:00
test('should not open dropdown if target is disabled via class', function () {
2014-07-06 11:56:12 +02:00
var dropdownHTML = '<ul class="tabs">'
+ '<li class="dropdown">'
+ '<button href="#" class="btn dropdown-toggle disabled" data-toggle="dropdown">Dropdown</button>'
+ '<ul class="dropdown-menu">'
+ '<li><a href="#">Secondary link</a></li>'
+ '<li><a href="#">Something else here</a></li>'
+ '<li class="divider"/>'
+ '<li><a href="#">Another link</a></li>'
+ '</ul>'
+ '</li>'
+ '</ul>'
2014-06-18 21:34:12 +02:00
var $dropdown = $(dropdownHTML).find('[data-toggle="dropdown"]').bootstrapDropdown().click()
2014-02-13 08:55:12 +01:00
2014-06-18 21:34:12 +02:00
ok(!$dropdown.parent('.dropdown').hasClass('open'), '"open" class added on click')
2014-02-13 08:55:12 +01:00
})
test('should add class open to menu if clicked', function () {
2014-07-06 11:56:12 +02:00
var dropdownHTML = '<ul class="tabs">'
+ '<li class="dropdown">'
+ '<a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown</a>'
+ '<ul class="dropdown-menu">'
+ '<li><a href="#">Secondary link</a></li>'
+ '<li><a href="#">Something else here</a></li>'
+ '<li class="divider"/>'
+ '<li><a href="#">Another link</a></li>'
+ '</ul>'
+ '</li>'
+ '</ul>'
2014-06-18 21:34:12 +02:00
var $dropdown = $(dropdownHTML).find('[data-toggle="dropdown"]').bootstrapDropdown().click()
2014-02-13 08:55:12 +01:00
2014-06-18 21:34:12 +02:00
ok($dropdown.parent('.dropdown').hasClass('open'), '"open" class added on click')
2014-02-13 08:55:12 +01:00
})
test('should test if element has a # before assuming it\'s a selector', function () {
2014-07-06 11:56:12 +02:00
var dropdownHTML = '<ul class="tabs">'
+ '<li class="dropdown">'
+ '<a href="/foo/" class="dropdown-toggle" data-toggle="dropdown">Dropdown</a>'
+ '<ul class="dropdown-menu">'
+ '<li><a href="#">Secondary link</a></li>'
+ '<li><a href="#">Something else here</a></li>'
+ '<li class="divider"/>'
+ '<li><a href="#">Another link</a></li>'
+ '</ul>'
+ '</li>'
+ '</ul>'
2014-06-18 21:34:12 +02:00
var $dropdown = $(dropdownHTML).find('[data-toggle="dropdown"]').bootstrapDropdown().click()
2014-02-13 08:55:12 +01:00
2014-06-18 21:34:12 +02:00
ok($dropdown.parent('.dropdown').hasClass('open'), '"open" class added on click')
2014-02-13 08:55:12 +01:00
})
2014-06-18 21:34:12 +02:00
test('should remove "open" class if body is clicked', function () {
2014-07-06 11:56:12 +02:00
var dropdownHTML = '<ul class="tabs">'
+ '<li class="dropdown">'
+ '<a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown</a>'
+ '<ul class="dropdown-menu">'
+ '<li><a href="#">Secondary link</a></li>'
+ '<li><a href="#">Something else here</a></li>'
+ '<li class="divider"/>'
+ '<li><a href="#">Another link</a></li>'
+ '</ul>'
+ '</li>'
+ '</ul>'
2014-06-18 21:34:12 +02:00
var $dropdown = $(dropdownHTML)
.appendTo('#qunit-fixture')
.find('[data-toggle="dropdown"]')
.bootstrapDropdown()
.click()
ok($dropdown.parent('.dropdown').hasClass('open'), '"open" class added on click')
$(document.body).click()
ok(!$dropdown.parent('.dropdown').hasClass('open'), '"open" class removed')
2014-02-13 08:55:12 +01:00
})
2014-06-18 21:34:12 +02:00
test('should remove "open" class if body is clicked, with multiple dropdowns', function () {
2014-07-06 11:56:12 +02:00
var dropdownHTML = '<ul class="nav">'
+ '<li><a href="#menu1">Menu 1</a></li>'
+ '<li class="dropdown" id="testmenu">'
+ '<a class="dropdown-toggle" data-toggle="dropdown" href="#testmenu">Test menu <span class="caret"/></a>'
+ '<ul class="dropdown-menu" role="menu">'
+ '<li><a href="#sub1">Submenu 1</a></li>'
+ '</ul>'
+ '</li>'
+ '</ul>'
+ '<div class="btn-group">'
+ '<button class="btn">Actions</button>'
+ '<button class="btn dropdown-toggle" data-toggle="dropdown"><span class="caret"/></button>'
+ '<ul class="dropdown-menu">'
+ '<li><a href="#">Action 1</a></li>'
+ '</ul>'
+ '</div>'
2014-06-18 21:34:12 +02:00
var $dropdowns = $(dropdownHTML).appendTo('#qunit-fixture').find('[data-toggle="dropdown"]')
var $first = $dropdowns.first()
var $last = $dropdowns.last()
2014-02-13 08:55:12 +01:00
2014-06-18 21:34:12 +02:00
strictEqual($dropdowns.length, 2, 'two dropdowns')
2014-02-13 08:55:12 +01:00
2014-06-18 21:34:12 +02:00
$first.click()
strictEqual($first.parents('.open').length, 1, '"open" class added on click')
strictEqual($('#qunit-fixture .open').length, 1, 'only one dropdown is open')
$(document.body).click()
strictEqual($('#qunit-fixture .open').length, 0, '"open" class removed')
2014-02-13 08:55:12 +01:00
2014-06-18 21:34:12 +02:00
$last.click()
strictEqual($last.parent('.open').length, 1, '"open" class added on click')
strictEqual($('#qunit-fixture .open').length, 1, 'only one dropdown is open')
$(document.body).click()
strictEqual($('#qunit-fixture .open').length, 0, '"open" class removed')
2014-02-13 08:55:12 +01:00
})
test('should fire show and hide event', function () {
2014-07-06 11:56:12 +02:00
var dropdownHTML = '<ul class="tabs">'
+ '<li class="dropdown">'
+ '<a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown</a>'
+ '<ul class="dropdown-menu">'
+ '<li><a href="#">Secondary link</a></li>'
+ '<li><a href="#">Something else here</a></li>'
+ '<li class="divider"/>'
+ '<li><a href="#">Another link</a></li>'
+ '</ul>'
+ '</li>'
+ '</ul>'
2014-06-18 21:34:12 +02:00
var $dropdown = $(dropdownHTML)
.appendTo('#qunit-fixture')
.find('[data-toggle="dropdown"]')
.bootstrapDropdown()
2014-02-13 08:55:12 +01:00
stop()
2014-06-18 21:34:12 +02:00
$dropdown
2014-02-13 08:55:12 +01:00
.parent('.dropdown')
.on('show.bs.dropdown', function () {
2014-06-18 21:34:12 +02:00
ok(true, 'show was fired')
})
.on('hide.bs.dropdown', function () {
2014-06-18 21:34:12 +02:00
ok(true, 'hide was fired')
2014-02-13 08:55:12 +01:00
start()
})
2014-06-18 21:34:12 +02:00
$dropdown.click()
2014-02-13 08:55:12 +01:00
$(document.body).click()
})
2014-06-18 21:34:12 +02:00
test('should fire shown and hidden event', function () {
2014-07-06 11:56:12 +02:00
var dropdownHTML = '<ul class="tabs">'
+ '<li class="dropdown">'
+ '<a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown</a>'
+ '<ul class="dropdown-menu">'
+ '<li><a href="#">Secondary link</a></li>'
+ '<li><a href="#">Something else here</a></li>'
+ '<li class="divider"/>'
+ '<li><a href="#">Another link</a></li>'
+ '</ul>'
+ '</li>'
+ '</ul>'
2014-06-18 21:34:12 +02:00
var $dropdown = $(dropdownHTML)
.appendTo('#qunit-fixture')
.find('[data-toggle="dropdown"]')
.bootstrapDropdown()
2014-02-13 08:55:12 +01:00
stop()
2014-06-18 21:34:12 +02:00
$dropdown
2014-02-13 08:55:12 +01:00
.parent('.dropdown')
.on('shown.bs.dropdown', function () {
2014-06-18 21:34:12 +02:00
ok(true, 'shown was fired')
})
.on('hidden.bs.dropdown', function () {
2014-06-18 21:34:12 +02:00
ok(true, 'hidden was fired')
2014-02-13 08:55:12 +01:00
start()
})
2014-06-18 21:34:12 +02:00
$dropdown.click()
2014-02-13 08:55:12 +01:00
$(document.body).click()
})
})