1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/bootstrap-source/bootstrap-3.0.3/js/tests/unit/dropdown.js Fri Dec 20 22:49:16 2013 +0100
1.3 @@ -0,0 +1,219 @@
1.4 +$(function () {
1.5 +
1.6 + module("dropdowns")
1.7 +
1.8 + test("should provide no conflict", function () {
1.9 + var dropdown = $.fn.dropdown.noConflict()
1.10 + ok(!$.fn.dropdown, 'dropdown was set back to undefined (org value)')
1.11 + $.fn.dropdown = dropdown
1.12 + })
1.13 +
1.14 + test("should be defined on jquery object", function () {
1.15 + ok($(document.body).dropdown, 'dropdown method is defined')
1.16 + })
1.17 +
1.18 + test("should return element", function () {
1.19 + var el = $("<div />")
1.20 + ok(el.dropdown()[0] === el[0], 'same element returned')
1.21 + })
1.22 +
1.23 + test("should not open dropdown if target is disabled", function () {
1.24 + var dropdownHTML = '<ul class="tabs">'
1.25 + + '<li class="dropdown">'
1.26 + + '<button disabled href="#" class="btn dropdown-toggle" data-toggle="dropdown">Dropdown</button>'
1.27 + + '<ul class="dropdown-menu">'
1.28 + + '<li><a href="#">Secondary link</a></li>'
1.29 + + '<li><a href="#">Something else here</a></li>'
1.30 + + '<li class="divider"></li>'
1.31 + + '<li><a href="#">Another link</a></li>'
1.32 + + '</ul>'
1.33 + + '</li>'
1.34 + + '</ul>'
1.35 + , dropdown = $(dropdownHTML).find('[data-toggle="dropdown"]').dropdown().click()
1.36 +
1.37 + ok(!dropdown.parent('.dropdown').hasClass('open'), 'open class added on click')
1.38 + })
1.39 +
1.40 + test("should not open dropdown if target is disabled", function () {
1.41 + var dropdownHTML = '<ul class="tabs">'
1.42 + + '<li class="dropdown">'
1.43 + + '<button href="#" class="btn dropdown-toggle disabled" data-toggle="dropdown">Dropdown</button>'
1.44 + + '<ul class="dropdown-menu">'
1.45 + + '<li><a href="#">Secondary link</a></li>'
1.46 + + '<li><a href="#">Something else here</a></li>'
1.47 + + '<li class="divider"></li>'
1.48 + + '<li><a href="#">Another link</a></li>'
1.49 + + '</ul>'
1.50 + + '</li>'
1.51 + + '</ul>'
1.52 + , dropdown = $(dropdownHTML).find('[data-toggle="dropdown"]').dropdown().click()
1.53 +
1.54 + ok(!dropdown.parent('.dropdown').hasClass('open'), 'open class added on click')
1.55 + })
1.56 +
1.57 + test("should add class open to menu if clicked", function () {
1.58 + var dropdownHTML = '<ul class="tabs">'
1.59 + + '<li class="dropdown">'
1.60 + + '<a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown</a>'
1.61 + + '<ul class="dropdown-menu">'
1.62 + + '<li><a href="#">Secondary link</a></li>'
1.63 + + '<li><a href="#">Something else here</a></li>'
1.64 + + '<li class="divider"></li>'
1.65 + + '<li><a href="#">Another link</a></li>'
1.66 + + '</ul>'
1.67 + + '</li>'
1.68 + + '</ul>'
1.69 + , dropdown = $(dropdownHTML).find('[data-toggle="dropdown"]').dropdown().click()
1.70 +
1.71 + ok(dropdown.parent('.dropdown').hasClass('open'), 'open class added on click')
1.72 + })
1.73 +
1.74 + test("should test if element has a # before assuming it's a selector", function () {
1.75 + var dropdownHTML = '<ul class="tabs">'
1.76 + + '<li class="dropdown">'
1.77 + + '<a href="/foo/" class="dropdown-toggle" data-toggle="dropdown">Dropdown</a>'
1.78 + + '<ul class="dropdown-menu">'
1.79 + + '<li><a href="#">Secondary link</a></li>'
1.80 + + '<li><a href="#">Something else here</a></li>'
1.81 + + '<li class="divider"></li>'
1.82 + + '<li><a href="#">Another link</a></li>'
1.83 + + '</ul>'
1.84 + + '</li>'
1.85 + + '</ul>'
1.86 + , dropdown = $(dropdownHTML).find('[data-toggle="dropdown"]').dropdown().click()
1.87 +
1.88 + ok(dropdown.parent('.dropdown').hasClass('open'), 'open class added on click')
1.89 + })
1.90 +
1.91 +
1.92 + test("should remove open class if body clicked", function () {
1.93 + var dropdownHTML = '<ul class="tabs">'
1.94 + + '<li class="dropdown">'
1.95 + + '<a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown</a>'
1.96 + + '<ul class="dropdown-menu">'
1.97 + + '<li><a href="#">Secondary link</a></li>'
1.98 + + '<li><a href="#">Something else here</a></li>'
1.99 + + '<li class="divider"></li>'
1.100 + + '<li><a href="#">Another link</a></li>'
1.101 + + '</ul>'
1.102 + + '</li>'
1.103 + + '</ul>'
1.104 + , dropdown = $(dropdownHTML)
1.105 + .appendTo('#qunit-fixture')
1.106 + .find('[data-toggle="dropdown"]')
1.107 + .dropdown()
1.108 + .click()
1.109 +
1.110 + ok(dropdown.parent('.dropdown').hasClass('open'), 'open class added on click')
1.111 + $('body').click()
1.112 + ok(!dropdown.parent('.dropdown').hasClass('open'), 'open class removed')
1.113 + dropdown.remove()
1.114 + })
1.115 +
1.116 + test("should remove open class if body clicked, with multiple drop downs", function () {
1.117 + var dropdownHTML =
1.118 + '<ul class="nav">'
1.119 + + ' <li><a href="#menu1">Menu 1</a></li>'
1.120 + + ' <li class="dropdown" id="testmenu">'
1.121 + + ' <a class="dropdown-toggle" data-toggle="dropdown" href="#testmenu">Test menu <b class="caret"></b></a>'
1.122 + + ' <ul class="dropdown-menu" role="menu">'
1.123 + + ' <li><a href="#sub1">Submenu 1</a></li>'
1.124 + + ' </ul>'
1.125 + + ' </li>'
1.126 + + '</ul>'
1.127 + + '<div class="btn-group">'
1.128 + + ' <button class="btn">Actions</button>'
1.129 + + ' <button class="btn dropdown-toggle" data-toggle="dropdown"><span class="caret"></span></button>'
1.130 + + ' <ul class="dropdown-menu">'
1.131 + + ' <li><a href="#">Action 1</a></li>'
1.132 + + ' </ul>'
1.133 + + '</div>'
1.134 + , dropdowns = $(dropdownHTML).appendTo('#qunit-fixture').find('[data-toggle="dropdown"]')
1.135 + , first = dropdowns.first()
1.136 + , last = dropdowns.last()
1.137 +
1.138 + ok(dropdowns.length == 2, "Should be two dropdowns")
1.139 +
1.140 + first.click()
1.141 + ok(first.parents('.open').length == 1, 'open class added on click')
1.142 + ok($('#qunit-fixture .open').length == 1, 'only one object is open')
1.143 + $('body').click()
1.144 + ok($("#qunit-fixture .open").length === 0, 'open class removed')
1.145 +
1.146 + last.click()
1.147 + ok(last.parent('.open').length == 1, 'open class added on click')
1.148 + ok($('#qunit-fixture .open').length == 1, 'only one object is open')
1.149 + $('body').click()
1.150 + ok($("#qunit-fixture .open").length === 0, 'open class removed')
1.151 +
1.152 + $("#qunit-fixture").html("")
1.153 + })
1.154 +
1.155 + test("should fire show and hide event", function () {
1.156 + var dropdownHTML = '<ul class="tabs">'
1.157 + + '<li class="dropdown">'
1.158 + + '<a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown</a>'
1.159 + + '<ul class="dropdown-menu">'
1.160 + + '<li><a href="#">Secondary link</a></li>'
1.161 + + '<li><a href="#">Something else here</a></li>'
1.162 + + '<li class="divider"></li>'
1.163 + + '<li><a href="#">Another link</a></li>'
1.164 + + '</ul>'
1.165 + + '</li>'
1.166 + + '</ul>'
1.167 + , dropdown = $(dropdownHTML)
1.168 + .appendTo('#qunit-fixture')
1.169 + .find('[data-toggle="dropdown"]')
1.170 + .dropdown()
1.171 +
1.172 + stop()
1.173 +
1.174 + dropdown
1.175 + .parent('.dropdown')
1.176 + .bind('show.bs.dropdown', function () {
1.177 + ok(true, 'show was called')
1.178 + })
1.179 + .bind('hide.bs.dropdown', function () {
1.180 + ok(true, 'hide was called')
1.181 + start()
1.182 + })
1.183 +
1.184 + dropdown.click()
1.185 + $(document.body).click()
1.186 + })
1.187 +
1.188 +
1.189 + test("should fire shown and hiden event", function () {
1.190 + var dropdownHTML = '<ul class="tabs">'
1.191 + + '<li class="dropdown">'
1.192 + + '<a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown</a>'
1.193 + + '<ul class="dropdown-menu">'
1.194 + + '<li><a href="#">Secondary link</a></li>'
1.195 + + '<li><a href="#">Something else here</a></li>'
1.196 + + '<li class="divider"></li>'
1.197 + + '<li><a href="#">Another link</a></li>'
1.198 + + '</ul>'
1.199 + + '</li>'
1.200 + + '</ul>'
1.201 + , dropdown = $(dropdownHTML)
1.202 + .appendTo('#qunit-fixture')
1.203 + .find('[data-toggle="dropdown"]')
1.204 + .dropdown()
1.205 +
1.206 + stop()
1.207 +
1.208 + dropdown
1.209 + .parent('.dropdown')
1.210 + .bind('shown.bs.dropdown', function () {
1.211 + ok(true, 'show was called')
1.212 + })
1.213 + .bind('hidden.bs.dropdown', function () {
1.214 + ok(true, 'hide was called')
1.215 + start()
1.216 + })
1.217 +
1.218 + dropdown.click()
1.219 + $(document.body).click()
1.220 + })
1.221 +
1.222 +})