dropdown.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. $(function () {
  2. 'use strict';
  3. QUnit.module('dropdowns plugin')
  4. QUnit.test('should be defined on jquery object', function (assert) {
  5. assert.expect(1)
  6. assert.ok($(document.body).dropdown, 'dropdown method is defined')
  7. })
  8. QUnit.module('dropdowns', {
  9. beforeEach: function () {
  10. // Run all tests in noConflict mode -- it's the only way to ensure that the plugin works in noConflict mode
  11. $.fn.bootstrapDropdown = $.fn.dropdown.noConflict()
  12. },
  13. afterEach: function () {
  14. $.fn.dropdown = $.fn.bootstrapDropdown
  15. delete $.fn.bootstrapDropdown
  16. }
  17. })
  18. QUnit.test('should provide no conflict', function (assert) {
  19. assert.expect(1)
  20. assert.strictEqual($.fn.dropdown, undefined, 'dropdown was set back to undefined (org value)')
  21. })
  22. QUnit.test('should return jquery collection containing the element', function (assert) {
  23. assert.expect(2)
  24. var $el = $('<div/>')
  25. var $dropdown = $el.bootstrapDropdown()
  26. assert.ok($dropdown instanceof $, 'returns jquery collection')
  27. assert.strictEqual($dropdown[0], $el[0], 'collection contains element')
  28. })
  29. QUnit.test('should not open dropdown if target is disabled via attribute', function (assert) {
  30. assert.expect(1)
  31. var dropdownHTML = '<ul class="tabs">'
  32. + '<li class="dropdown">'
  33. + '<button disabled href="#" class="btn dropdown-toggle" data-toggle="dropdown">Dropdown</button>'
  34. + '<ul class="dropdown-menu">'
  35. + '<li><a href="#">Secondary link</a></li>'
  36. + '<li><a href="#">Something else here</a></li>'
  37. + '<li class="divider"/>'
  38. + '<li><a href="#">Another link</a></li>'
  39. + '</ul>'
  40. + '</li>'
  41. + '</ul>'
  42. var $dropdown = $(dropdownHTML).find('[data-toggle="dropdown"]').bootstrapDropdown().trigger('click')
  43. assert.ok(!$dropdown.parent('.dropdown').hasClass('open'), '"open" class added on click')
  44. })
  45. QUnit.test('should set aria-expanded="true" on target when dropdown menu is shown', function (assert) {
  46. assert.expect(1)
  47. var dropdownHTML = '<ul class="tabs">'
  48. + '<li class="dropdown">'
  49. + '<a href="#" class="dropdown-toggle" data-toggle="dropdown" aria-expanded="false">Dropdown</a>'
  50. + '<ul class="dropdown-menu">'
  51. + '<li><a href="#">Secondary link</a></li>'
  52. + '<li><a href="#">Something else here</a></li>'
  53. + '<li class="divider"/>'
  54. + '<li><a href="#">Another link</a></li>'
  55. + '</ul>'
  56. + '</li>'
  57. + '</ul>'
  58. var $dropdown = $(dropdownHTML)
  59. .find('[data-toggle="dropdown"]')
  60. .bootstrapDropdown()
  61. .trigger('click')
  62. assert.strictEqual($dropdown.attr('aria-expanded'), 'true', 'aria-expanded is set to string "true" on click')
  63. })
  64. QUnit.test('should set aria-expanded="false" on target when dropdown menu is hidden', function (assert) {
  65. assert.expect(1)
  66. var done = assert.async()
  67. var dropdownHTML = '<ul class="tabs">'
  68. + '<li class="dropdown">'
  69. + '<a href="#" class="dropdown-toggle" aria-expanded="false" data-toggle="dropdown">Dropdown</a>'
  70. + '<ul class="dropdown-menu">'
  71. + '<li><a href="#">Secondary link</a></li>'
  72. + '<li><a href="#">Something else here</a></li>'
  73. + '<li class="divider"/>'
  74. + '<li><a href="#">Another link</a></li>'
  75. + '</ul>'
  76. + '</li>'
  77. + '</ul>'
  78. var $dropdown = $(dropdownHTML)
  79. .appendTo('#qunit-fixture')
  80. .find('[data-toggle="dropdown"]')
  81. .bootstrapDropdown()
  82. $dropdown
  83. .parent('.dropdown')
  84. .on('hidden.bs.dropdown', function () {
  85. assert.strictEqual($dropdown.attr('aria-expanded'), 'false', 'aria-expanded is set to string "false" on hide')
  86. done()
  87. })
  88. $dropdown.trigger('click')
  89. $(document.body).trigger('click')
  90. })
  91. QUnit.test('should not open dropdown if target is disabled via class', function (assert) {
  92. assert.expect(1)
  93. var dropdownHTML = '<ul class="tabs">'
  94. + '<li class="dropdown">'
  95. + '<button href="#" class="btn dropdown-toggle disabled" data-toggle="dropdown">Dropdown</button>'
  96. + '<ul class="dropdown-menu">'
  97. + '<li><a href="#">Secondary link</a></li>'
  98. + '<li><a href="#">Something else here</a></li>'
  99. + '<li class="divider"/>'
  100. + '<li><a href="#">Another link</a></li>'
  101. + '</ul>'
  102. + '</li>'
  103. + '</ul>'
  104. var $dropdown = $(dropdownHTML).find('[data-toggle="dropdown"]').bootstrapDropdown().trigger('click')
  105. assert.ok(!$dropdown.parent('.dropdown').hasClass('open'), '"open" class added on click')
  106. })
  107. QUnit.test('should add class open to menu if clicked', function (assert) {
  108. assert.expect(1)
  109. var dropdownHTML = '<ul class="tabs">'
  110. + '<li class="dropdown">'
  111. + '<a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown</a>'
  112. + '<ul class="dropdown-menu">'
  113. + '<li><a href="#">Secondary link</a></li>'
  114. + '<li><a href="#">Something else here</a></li>'
  115. + '<li class="divider"/>'
  116. + '<li><a href="#">Another link</a></li>'
  117. + '</ul>'
  118. + '</li>'
  119. + '</ul>'
  120. var $dropdown = $(dropdownHTML).find('[data-toggle="dropdown"]').bootstrapDropdown().trigger('click')
  121. assert.ok($dropdown.parent('.dropdown').hasClass('open'), '"open" class added on click')
  122. })
  123. QUnit.test('should test if element has a # before assuming it\'s a selector', function (assert) {
  124. assert.expect(1)
  125. var dropdownHTML = '<ul class="tabs">'
  126. + '<li class="dropdown">'
  127. + '<a href="/foo/" class="dropdown-toggle" data-toggle="dropdown">Dropdown</a>'
  128. + '<ul class="dropdown-menu">'
  129. + '<li><a href="#">Secondary link</a></li>'
  130. + '<li><a href="#">Something else here</a></li>'
  131. + '<li class="divider"/>'
  132. + '<li><a href="#">Another link</a></li>'
  133. + '</ul>'
  134. + '</li>'
  135. + '</ul>'
  136. var $dropdown = $(dropdownHTML).find('[data-toggle="dropdown"]').bootstrapDropdown().trigger('click')
  137. assert.ok($dropdown.parent('.dropdown').hasClass('open'), '"open" class added on click')
  138. })
  139. QUnit.test('should remove "open" class if body is clicked', function (assert) {
  140. assert.expect(2)
  141. var dropdownHTML = '<ul class="tabs">'
  142. + '<li class="dropdown">'
  143. + '<a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown</a>'
  144. + '<ul class="dropdown-menu">'
  145. + '<li><a href="#">Secondary link</a></li>'
  146. + '<li><a href="#">Something else here</a></li>'
  147. + '<li class="divider"/>'
  148. + '<li><a href="#">Another link</a></li>'
  149. + '</ul>'
  150. + '</li>'
  151. + '</ul>'
  152. var $dropdown = $(dropdownHTML)
  153. .appendTo('#qunit-fixture')
  154. .find('[data-toggle="dropdown"]')
  155. .bootstrapDropdown()
  156. .trigger('click')
  157. assert.ok($dropdown.parent('.dropdown').hasClass('open'), '"open" class added on click')
  158. $(document.body).trigger('click')
  159. assert.ok(!$dropdown.parent('.dropdown').hasClass('open'), '"open" class removed')
  160. })
  161. QUnit.test('should remove "open" class if body is clicked, with multiple dropdowns', function (assert) {
  162. assert.expect(7)
  163. var dropdownHTML = '<ul class="nav">'
  164. + '<li><a href="#menu1">Menu 1</a></li>'
  165. + '<li class="dropdown" id="testmenu">'
  166. + '<a class="dropdown-toggle" data-toggle="dropdown" href="#testmenu">Test menu <span class="caret"/></a>'
  167. + '<ul class="dropdown-menu">'
  168. + '<li><a href="#sub1">Submenu 1</a></li>'
  169. + '</ul>'
  170. + '</li>'
  171. + '</ul>'
  172. + '<div class="btn-group">'
  173. + '<button class="btn">Actions</button>'
  174. + '<button class="btn dropdown-toggle" data-toggle="dropdown"><span class="caret"/></button>'
  175. + '<ul class="dropdown-menu">'
  176. + '<li><a href="#">Action 1</a></li>'
  177. + '</ul>'
  178. + '</div>'
  179. var $dropdowns = $(dropdownHTML).appendTo('#qunit-fixture').find('[data-toggle="dropdown"]')
  180. var $first = $dropdowns.first()
  181. var $last = $dropdowns.last()
  182. assert.strictEqual($dropdowns.length, 2, 'two dropdowns')
  183. $first.trigger('click')
  184. assert.strictEqual($first.parents('.open').length, 1, '"open" class added on click')
  185. assert.strictEqual($('#qunit-fixture .open').length, 1, 'only one dropdown is open')
  186. $(document.body).trigger('click')
  187. assert.strictEqual($('#qunit-fixture .open').length, 0, '"open" class removed')
  188. $last.trigger('click')
  189. assert.strictEqual($last.parent('.open').length, 1, '"open" class added on click')
  190. assert.strictEqual($('#qunit-fixture .open').length, 1, 'only one dropdown is open')
  191. $(document.body).trigger('click')
  192. assert.strictEqual($('#qunit-fixture .open').length, 0, '"open" class removed')
  193. })
  194. QUnit.test('should fire show and hide event', function (assert) {
  195. assert.expect(2)
  196. var dropdownHTML = '<ul class="tabs">'
  197. + '<li class="dropdown">'
  198. + '<a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown</a>'
  199. + '<ul class="dropdown-menu">'
  200. + '<li><a href="#">Secondary link</a></li>'
  201. + '<li><a href="#">Something else here</a></li>'
  202. + '<li class="divider"/>'
  203. + '<li><a href="#">Another link</a></li>'
  204. + '</ul>'
  205. + '</li>'
  206. + '</ul>'
  207. var $dropdown = $(dropdownHTML)
  208. .appendTo('#qunit-fixture')
  209. .find('[data-toggle="dropdown"]')
  210. .bootstrapDropdown()
  211. var done = assert.async()
  212. $dropdown
  213. .parent('.dropdown')
  214. .on('show.bs.dropdown', function () {
  215. assert.ok(true, 'show was fired')
  216. })
  217. .on('hide.bs.dropdown', function () {
  218. assert.ok(true, 'hide was fired')
  219. done()
  220. })
  221. $dropdown.trigger('click')
  222. $(document.body).trigger('click')
  223. })
  224. QUnit.test('should fire shown and hidden event', function (assert) {
  225. assert.expect(2)
  226. var dropdownHTML = '<ul class="tabs">'
  227. + '<li class="dropdown">'
  228. + '<a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown</a>'
  229. + '<ul class="dropdown-menu">'
  230. + '<li><a href="#">Secondary link</a></li>'
  231. + '<li><a href="#">Something else here</a></li>'
  232. + '<li class="divider"/>'
  233. + '<li><a href="#">Another link</a></li>'
  234. + '</ul>'
  235. + '</li>'
  236. + '</ul>'
  237. var $dropdown = $(dropdownHTML)
  238. .appendTo('#qunit-fixture')
  239. .find('[data-toggle="dropdown"]')
  240. .bootstrapDropdown()
  241. var done = assert.async()
  242. $dropdown
  243. .parent('.dropdown')
  244. .on('shown.bs.dropdown', function () {
  245. assert.ok(true, 'shown was fired')
  246. })
  247. .on('hidden.bs.dropdown', function () {
  248. assert.ok(true, 'hidden was fired')
  249. done()
  250. })
  251. $dropdown.trigger('click')
  252. $(document.body).trigger('click')
  253. })
  254. QUnit.test('should fire shown and hidden event with a relatedTarget', function (assert) {
  255. assert.expect(2)
  256. var dropdownHTML = '<ul class="tabs">'
  257. + '<li class="dropdown">'
  258. + '<a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown</a>'
  259. + '<ul class="dropdown-menu">'
  260. + '<li><a href="#">Secondary link</a></li>'
  261. + '<li><a href="#">Something else here</a></li>'
  262. + '<li class="divider"/>'
  263. + '<li><a href="#">Another link</a></li>'
  264. + '</ul>'
  265. + '</li>'
  266. + '</ul>'
  267. var $dropdown = $(dropdownHTML)
  268. .appendTo('#qunit-fixture')
  269. .find('[data-toggle="dropdown"]')
  270. .bootstrapDropdown()
  271. var done = assert.async()
  272. $dropdown.parent('.dropdown')
  273. .on('hidden.bs.dropdown', function (e) {
  274. assert.strictEqual(e.relatedTarget, $dropdown[0])
  275. done()
  276. })
  277. .on('shown.bs.dropdown', function (e) {
  278. assert.strictEqual(e.relatedTarget, $dropdown[0])
  279. $(document.body).trigger('click')
  280. })
  281. $dropdown.trigger('click')
  282. })
  283. QUnit.test('should ignore keyboard events within <input>s and <textarea>s', function (assert) {
  284. assert.expect(3)
  285. var done = assert.async()
  286. var dropdownHTML = '<ul class="tabs">'
  287. + '<li class="dropdown">'
  288. + '<a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown</a>'
  289. + '<ul class="dropdown-menu">'
  290. + '<li><a href="#">Secondary link</a></li>'
  291. + '<li><a href="#">Something else here</a></li>'
  292. + '<li class="divider"/>'
  293. + '<li><a href="#">Another link</a></li>'
  294. + '<li><input type="text" id="input"></li>'
  295. + '<li><textarea id="textarea"/></li>'
  296. + '</ul>'
  297. + '</li>'
  298. + '</ul>'
  299. var $dropdown = $(dropdownHTML)
  300. .appendTo('#qunit-fixture')
  301. .find('[data-toggle="dropdown"]')
  302. .bootstrapDropdown()
  303. var $input = $('#input')
  304. var $textarea = $('#textarea')
  305. $dropdown
  306. .parent('.dropdown')
  307. .on('shown.bs.dropdown', function () {
  308. assert.ok(true, 'shown was fired')
  309. $input.trigger('focus').trigger($.Event('keydown', { which: 38 }))
  310. assert.ok($(document.activeElement).is($input), 'input still focused')
  311. $textarea.trigger('focus').trigger($.Event('keydown', { which: 38 }))
  312. assert.ok($(document.activeElement).is($textarea), 'textarea still focused')
  313. done()
  314. })
  315. $dropdown.trigger('click')
  316. })
  317. QUnit.test('should skip disabled element when using keyboard navigation', function (assert) {
  318. assert.expect(1)
  319. var dropdownHTML = '<ul class="tabs">'
  320. + '<li class="dropdown">'
  321. + '<a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown</a>'
  322. + '<ul class="dropdown-menu">'
  323. + '<li class="disabled"><a href="#">Disabled link</a></li>'
  324. + '<li><a href="#">Another link</a></li>'
  325. + '</ul>'
  326. + '</li>'
  327. + '</ul>'
  328. var $dropdown = $(dropdownHTML)
  329. .appendTo('#qunit-fixture')
  330. .find('[data-toggle="dropdown"]')
  331. .bootstrapDropdown()
  332. .trigger('click')
  333. $dropdown.trigger($.Event('keydown', { which: 40 }))
  334. $dropdown.trigger($.Event('keydown', { which: 40 }))
  335. assert.ok(!$(document.activeElement).parent().is('.disabled'), '.disabled is not focused')
  336. })
  337. QUnit.test('should not close the dropdown if the user clicks on a text field', function (assert) {
  338. assert.expect(1)
  339. var dropdownHTML = '<div class="btn-group">'
  340. + '<button type="button" data-toggle="dropdown">Dropdown</button>'
  341. + '<ul class="dropdown-menu">'
  342. + '<li><input id="textField" type="text" /></li>'
  343. + '</ul>'
  344. + '</div>'
  345. var $dropdown = $(dropdownHTML)
  346. .appendTo('#qunit-fixture')
  347. .find('[data-toggle="dropdown"]')
  348. .bootstrapDropdown()
  349. .trigger('click')
  350. $('#textField').trigger('click')
  351. assert.ok($dropdown.parent('.btn-group').hasClass('open'), 'dropdown menu is open')
  352. })
  353. QUnit.test('should not close the dropdown if the user clicks on a textarea', function (assert) {
  354. assert.expect(1)
  355. var dropdownHTML = '<div class="btn-group">'
  356. + '<button type="button" data-toggle="dropdown">Dropdown</button>'
  357. + '<ul class="dropdown-menu">'
  358. + '<li><textarea id="textArea"></textarea></li>'
  359. + '</ul>'
  360. + '</div>'
  361. var $dropdown = $(dropdownHTML)
  362. .appendTo('#qunit-fixture')
  363. .find('[data-toggle="dropdown"]')
  364. .bootstrapDropdown()
  365. .trigger('click')
  366. $('#textArea').trigger('click')
  367. assert.ok($dropdown.parent('.btn-group').hasClass('open'), 'dropdown menu is open')
  368. })
  369. QUnit.test('should handle # in data-target', function (assert) {
  370. assert.expect(1)
  371. var done = assert.async()
  372. var html = [
  373. '<div class="dropdown">',
  374. ' <a id="dLabel" data-target="#" href="http://example.com/" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">',
  375. ' Dropdown trigger',
  376. ' </a>',
  377. ' <ul class="dropdown-menu" aria-labelledby="dLabel">',
  378. ' <li><a href="/test">One</a></li>',
  379. ' <li><a href="/test2">Two</a></li>',
  380. ' </ul>',
  381. '</div>'
  382. ].join('')
  383. var $dropdown = $(html)
  384. .appendTo('#qunit-fixture')
  385. .find('[data-toggle="dropdown"]')
  386. .bootstrapDropdown()
  387. $dropdown
  388. .parent('.dropdown')
  389. .on('shown.bs.dropdown', function () {
  390. assert.ok($dropdown.parent('.dropdown').hasClass('open'), '"open" class added on click')
  391. done()
  392. })
  393. $dropdown.trigger('click')
  394. })
  395. })