collapse.js 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892
  1. $(function () {
  2. 'use strict'
  3. QUnit.module('collapse plugin')
  4. QUnit.test('should be defined on jquery object', function (assert) {
  5. assert.expect(1)
  6. assert.ok($(document.body).collapse, 'collapse method is defined')
  7. })
  8. QUnit.module('collapse', {
  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.bootstrapCollapse = $.fn.collapse.noConflict()
  12. },
  13. afterEach: function () {
  14. $.fn.collapse = $.fn.bootstrapCollapse
  15. delete $.fn.bootstrapCollapse
  16. $('#qunit-fixture').html('')
  17. }
  18. })
  19. QUnit.test('should provide no conflict', function (assert) {
  20. assert.expect(1)
  21. assert.strictEqual(typeof $.fn.collapse, 'undefined', 'collapse was set back to undefined (org value)')
  22. })
  23. QUnit.test('should throw explicit error on undefined method', function (assert) {
  24. assert.expect(1)
  25. var $el = $('<div/>')
  26. $el.bootstrapCollapse()
  27. try {
  28. $el.bootstrapCollapse('noMethod')
  29. } catch (err) {
  30. assert.strictEqual(err.message, 'No method named "noMethod"')
  31. }
  32. })
  33. QUnit.test('should return jquery collection containing the element', function (assert) {
  34. assert.expect(2)
  35. var $el = $('<div/>')
  36. var $collapse = $el.bootstrapCollapse()
  37. assert.ok($collapse instanceof $, 'returns jquery collection')
  38. assert.strictEqual($collapse[0], $el[0], 'collection contains element')
  39. })
  40. QUnit.test('should show a collapsed element', function (assert) {
  41. assert.expect(2)
  42. var done = assert.async()
  43. var $el = $('<div class="collapse"/>')
  44. $el.one('shown.bs.collapse', function () {
  45. assert.ok($el.hasClass('show'), 'has class "show"')
  46. assert.ok(!/height/i.test($el.attr('style')), 'has height reset')
  47. done()
  48. }).bootstrapCollapse('show')
  49. })
  50. QUnit.test('should show multiple collapsed elements', function (assert) {
  51. assert.expect(4)
  52. var done = assert.async()
  53. var $target = $('<a role="button" data-toggle="collapse" class="collapsed" href=".multi"/>').appendTo('#qunit-fixture')
  54. var $el = $('<div class="collapse multi"/>').appendTo('#qunit-fixture')
  55. var $el2 = $('<div class="collapse multi"/>').appendTo('#qunit-fixture')
  56. $el.one('shown.bs.collapse', function () {
  57. assert.ok($el.hasClass('show'), 'has class "show"')
  58. assert.ok(!/height/i.test($el.attr('style')), 'has height reset')
  59. })
  60. $el2.one('shown.bs.collapse', function () {
  61. assert.ok($el2.hasClass('show'), 'has class "show"')
  62. assert.ok(!/height/i.test($el2.attr('style')), 'has height reset')
  63. done()
  64. })
  65. $target.trigger('click')
  66. })
  67. QUnit.test('should collapse only the first collapse', function (assert) {
  68. assert.expect(2)
  69. var done = assert.async()
  70. var html = [
  71. '<div class="panel-group" id="accordion1">',
  72. '<div class="panel">',
  73. '<div id="collapse1" class="collapse"/>',
  74. '</div>',
  75. '</div>',
  76. '<div class="panel-group" id="accordion2">',
  77. '<div class="panel">',
  78. '<div id="collapse2" class="collapse show"/>',
  79. '</div>',
  80. '</div>'
  81. ].join('')
  82. $(html).appendTo('#qunit-fixture')
  83. var $el1 = $('#collapse1')
  84. var $el2 = $('#collapse2')
  85. $el1.one('shown.bs.collapse', function () {
  86. assert.ok($el1.hasClass('show'))
  87. assert.ok($el2.hasClass('show'))
  88. done()
  89. }).bootstrapCollapse('show')
  90. })
  91. QUnit.test('should hide a collapsed element', function (assert) {
  92. assert.expect(1)
  93. var $el = $('<div class="collapse"/>').bootstrapCollapse('hide')
  94. assert.ok(!$el.hasClass('show'), 'does not have class "show"')
  95. })
  96. QUnit.test('should not fire shown when show is prevented', function (assert) {
  97. assert.expect(1)
  98. var done = assert.async()
  99. $('<div class="collapse"/>')
  100. .on('show.bs.collapse', function (e) {
  101. e.preventDefault()
  102. assert.ok(true, 'show event fired')
  103. done()
  104. })
  105. .on('shown.bs.collapse', function () {
  106. assert.ok(false, 'shown event fired')
  107. })
  108. .bootstrapCollapse('show')
  109. })
  110. QUnit.test('should reset style to auto after finishing opening collapse', function (assert) {
  111. assert.expect(2)
  112. var done = assert.async()
  113. $('<div class="collapse" style="height: 0px"/>')
  114. .on('show.bs.collapse', function () {
  115. assert.strictEqual(this.style.height, '0px', 'height is 0px')
  116. })
  117. .on('shown.bs.collapse', function () {
  118. assert.strictEqual(this.style.height, '', 'height is auto')
  119. done()
  120. })
  121. .bootstrapCollapse('show')
  122. })
  123. QUnit.test('should reset style to auto after finishing closing collapse', function (assert) {
  124. assert.expect(1)
  125. var done = assert.async()
  126. $('<div class="collapse"/>')
  127. .on('shown.bs.collapse', function () {
  128. $(this).bootstrapCollapse('hide')
  129. })
  130. .on('hidden.bs.collapse', function () {
  131. assert.strictEqual(this.style.height, '', 'height is auto')
  132. done()
  133. })
  134. .bootstrapCollapse('show')
  135. })
  136. QUnit.test('should remove "collapsed" class from target when collapse is shown', function (assert) {
  137. assert.expect(1)
  138. var done = assert.async()
  139. var $target = $('<a role="button" data-toggle="collapse" class="collapsed" href="#test1"/>').appendTo('#qunit-fixture')
  140. $('<div id="test1"/>')
  141. .appendTo('#qunit-fixture')
  142. .on('shown.bs.collapse', function () {
  143. assert.ok(!$target.hasClass('collapsed'), 'target does not have collapsed class')
  144. done()
  145. })
  146. $target.trigger('click')
  147. })
  148. QUnit.test('should add "collapsed" class to target when collapse is hidden', function (assert) {
  149. assert.expect(1)
  150. var done = assert.async()
  151. var $target = $('<a role="button" data-toggle="collapse" href="#test1"/>').appendTo('#qunit-fixture')
  152. $('<div id="test1" class="show"/>')
  153. .appendTo('#qunit-fixture')
  154. .on('hidden.bs.collapse', function () {
  155. assert.ok($target.hasClass('collapsed'), 'target has collapsed class')
  156. done()
  157. })
  158. $target.trigger('click')
  159. })
  160. QUnit.test('should remove "collapsed" class from all triggers targeting the collapse when the collapse is shown', function (assert) {
  161. assert.expect(2)
  162. var done = assert.async()
  163. var $target = $('<a role="button" data-toggle="collapse" class="collapsed" href="#test1"/>').appendTo('#qunit-fixture')
  164. var $alt = $('<a role="button" data-toggle="collapse" class="collapsed" href="#test1"/>').appendTo('#qunit-fixture')
  165. $('<div id="test1"/>')
  166. .appendTo('#qunit-fixture')
  167. .on('shown.bs.collapse', function () {
  168. assert.ok(!$target.hasClass('collapsed'), 'target trigger does not have collapsed class')
  169. assert.ok(!$alt.hasClass('collapsed'), 'alt trigger does not have collapsed class')
  170. done()
  171. })
  172. $target.trigger('click')
  173. })
  174. QUnit.test('should add "collapsed" class to all triggers targeting the collapse when the collapse is hidden', function (assert) {
  175. assert.expect(2)
  176. var done = assert.async()
  177. var $target = $('<a role="button" data-toggle="collapse" href="#test1"/>').appendTo('#qunit-fixture')
  178. var $alt = $('<a role="button" data-toggle="collapse" href="#test1"/>').appendTo('#qunit-fixture')
  179. $('<div id="test1" class="show"/>')
  180. .appendTo('#qunit-fixture')
  181. .on('hidden.bs.collapse', function () {
  182. assert.ok($target.hasClass('collapsed'), 'target has collapsed class')
  183. assert.ok($alt.hasClass('collapsed'), 'alt trigger has collapsed class')
  184. done()
  185. })
  186. $target.trigger('click')
  187. })
  188. QUnit.test('should not close a collapse when initialized with "show" option if already shown', function (assert) {
  189. assert.expect(0)
  190. var done = assert.async()
  191. var $test = $('<div id="test1" class="show"/>')
  192. .appendTo('#qunit-fixture')
  193. .on('hide.bs.collapse', function () {
  194. assert.ok(false)
  195. })
  196. $test.bootstrapCollapse('show')
  197. setTimeout(done, 0)
  198. })
  199. QUnit.test('should open a collapse when initialized with "show" option if not already shown', function (assert) {
  200. assert.expect(1)
  201. var done = assert.async()
  202. var $test = $('<div id="test1" />')
  203. .appendTo('#qunit-fixture')
  204. .on('show.bs.collapse', function () {
  205. assert.ok(true)
  206. })
  207. $test.bootstrapCollapse('show')
  208. setTimeout(done, 0)
  209. })
  210. QUnit.test('should not show a collapse when initialized with "hide" option if already hidden', function (assert) {
  211. assert.expect(0)
  212. var done = assert.async()
  213. $('<div class="collapse"></div>')
  214. .appendTo('#qunit-fixture')
  215. .on('show.bs.collapse', function () {
  216. assert.ok(false, 'showing a previously-uninitialized hidden collapse when the "hide" method is called')
  217. })
  218. .bootstrapCollapse('hide')
  219. setTimeout(done, 0)
  220. })
  221. QUnit.test('should hide a collapse when initialized with "hide" option if not already hidden', function (assert) {
  222. assert.expect(1)
  223. var done = assert.async()
  224. $('<div class="collapse show"></div>')
  225. .appendTo('#qunit-fixture')
  226. .on('hide.bs.collapse', function () {
  227. assert.ok(true, 'hiding a previously-uninitialized shown collapse when the "hide" method is called')
  228. })
  229. .bootstrapCollapse('hide')
  230. setTimeout(done, 0)
  231. })
  232. QUnit.test('should remove "collapsed" class from active accordion target', function (assert) {
  233. assert.expect(3)
  234. var done = assert.async()
  235. var accordionHTML = '<div id="accordion">' +
  236. '<div class="card"/>' +
  237. '<div class="card"/>' +
  238. '<div class="card"/>' +
  239. '</div>'
  240. var $groups = $(accordionHTML).appendTo('#qunit-fixture').find('.card')
  241. var $target1 = $('<a role="button" data-toggle="collapse" href="#body1" />').appendTo($groups.eq(0))
  242. $('<div id="body1" class="show" data-parent="#accordion"/>').appendTo($groups.eq(0))
  243. var $target2 = $('<a class="collapsed" data-toggle="collapse" role="button" href="#body2" />').appendTo($groups.eq(1))
  244. $('<div id="body2" data-parent="#accordion"/>').appendTo($groups.eq(1))
  245. var $target3 = $('<a class="collapsed" data-toggle="collapse" role="button" href="#body3" />').appendTo($groups.eq(2))
  246. $('<div id="body3" data-parent="#accordion"/>')
  247. .appendTo($groups.eq(2))
  248. .on('shown.bs.collapse', function () {
  249. assert.ok($target1.hasClass('collapsed'), 'inactive target 1 does have class "collapsed"')
  250. assert.ok($target2.hasClass('collapsed'), 'inactive target 2 does have class "collapsed"')
  251. assert.ok(!$target3.hasClass('collapsed'), 'active target 3 does not have class "collapsed"')
  252. done()
  253. })
  254. $target3.trigger('click')
  255. })
  256. QUnit.test('should allow dots in data-parent', function (assert) {
  257. assert.expect(3)
  258. var done = assert.async()
  259. var accordionHTML = '<div class="accordion">' +
  260. '<div class="card"/>' +
  261. '<div class="card"/>' +
  262. '<div class="card"/>' +
  263. '</div>'
  264. var $groups = $(accordionHTML).appendTo('#qunit-fixture').find('.card')
  265. var $target1 = $('<a role="button" data-toggle="collapse" href="#body1"/>').appendTo($groups.eq(0))
  266. $('<div id="body1" class="show" data-parent=".accordion"/>').appendTo($groups.eq(0))
  267. var $target2 = $('<a class="collapsed" data-toggle="collapse" role="button" href="#body2"/>').appendTo($groups.eq(1))
  268. $('<div id="body2" data-parent=".accordion"/>').appendTo($groups.eq(1))
  269. var $target3 = $('<a class="collapsed" data-toggle="collapse" role="button" href="#body3"/>').appendTo($groups.eq(2))
  270. $('<div id="body3" data-parent=".accordion"/>')
  271. .appendTo($groups.eq(2))
  272. .on('shown.bs.collapse', function () {
  273. assert.ok($target1.hasClass('collapsed'), 'inactive target 1 does have class "collapsed"')
  274. assert.ok($target2.hasClass('collapsed'), 'inactive target 2 does have class "collapsed"')
  275. assert.ok(!$target3.hasClass('collapsed'), 'active target 3 does not have class "collapsed"')
  276. done()
  277. })
  278. $target3.trigger('click')
  279. })
  280. QUnit.test('should set aria-expanded="true" on trigger/control when collapse is shown', function (assert) {
  281. assert.expect(1)
  282. var done = assert.async()
  283. var $target = $('<a role="button" data-toggle="collapse" class="collapsed" href="#test1" aria-expanded="false"/>').appendTo('#qunit-fixture')
  284. $('<div id="test1"/>')
  285. .appendTo('#qunit-fixture')
  286. .on('shown.bs.collapse', function () {
  287. assert.strictEqual($target.attr('aria-expanded'), 'true', 'aria-expanded on target is "true"')
  288. done()
  289. })
  290. $target.trigger('click')
  291. })
  292. QUnit.test('should set aria-expanded="false" on trigger/control when collapse is hidden', function (assert) {
  293. assert.expect(1)
  294. var done = assert.async()
  295. var $target = $('<a role="button" data-toggle="collapse" href="#test1" aria-expanded="true"/>').appendTo('#qunit-fixture')
  296. $('<div id="test1" class="show"/>')
  297. .appendTo('#qunit-fixture')
  298. .on('hidden.bs.collapse', function () {
  299. assert.strictEqual($target.attr('aria-expanded'), 'false', 'aria-expanded on target is "false"')
  300. done()
  301. })
  302. $target.trigger('click')
  303. })
  304. QUnit.test('should set aria-expanded="true" on all triggers targeting the collapse when the collapse is shown', function (assert) {
  305. assert.expect(2)
  306. var done = assert.async()
  307. var $target = $('<a role="button" data-toggle="collapse" class="collapsed" href="#test1" aria-expanded="false"/>').appendTo('#qunit-fixture')
  308. var $alt = $('<a role="button" data-toggle="collapse" class="collapsed" href="#test1" aria-expanded="false"/>').appendTo('#qunit-fixture')
  309. $('<div id="test1"/>')
  310. .appendTo('#qunit-fixture')
  311. .on('shown.bs.collapse', function () {
  312. assert.strictEqual($target.attr('aria-expanded'), 'true', 'aria-expanded on trigger/control is "true"')
  313. assert.strictEqual($alt.attr('aria-expanded'), 'true', 'aria-expanded on alternative trigger/control is "true"')
  314. done()
  315. })
  316. $target.trigger('click')
  317. })
  318. QUnit.test('should set aria-expanded="false" on all triggers targeting the collapse when the collapse is hidden', function (assert) {
  319. assert.expect(2)
  320. var done = assert.async()
  321. var $target = $('<a role="button" data-toggle="collapse" href="#test1" aria-expanded="true"/>').appendTo('#qunit-fixture')
  322. var $alt = $('<a role="button" data-toggle="collapse" href="#test1" aria-expanded="true"/>').appendTo('#qunit-fixture')
  323. $('<div id="test1" class="show"/>')
  324. .appendTo('#qunit-fixture')
  325. .on('hidden.bs.collapse', function () {
  326. assert.strictEqual($target.attr('aria-expanded'), 'false', 'aria-expanded on trigger/control is "false"')
  327. assert.strictEqual($alt.attr('aria-expanded'), 'false', 'aria-expanded on alternative trigger/control is "false"')
  328. done()
  329. })
  330. $target.trigger('click')
  331. })
  332. QUnit.test('should change aria-expanded from active accordion trigger/control to "false" and set the trigger/control for the newly active one to "true"', function (assert) {
  333. assert.expect(3)
  334. var done = assert.async()
  335. var accordionHTML = '<div id="accordion">' +
  336. '<div class="card"/>' +
  337. '<div class="card"/>' +
  338. '<div class="card"/>' +
  339. '</div>'
  340. var $groups = $(accordionHTML).appendTo('#qunit-fixture').find('.card')
  341. var $target1 = $('<a role="button" data-toggle="collapse" aria-expanded="true" href="#body1"/>').appendTo($groups.eq(0))
  342. $('<div id="body1" class="show" data-parent="#accordion"/>').appendTo($groups.eq(0))
  343. var $target2 = $('<a role="button" data-toggle="collapse" aria-expanded="false" href="#body2" class="collapsed" aria-expanded="false" />').appendTo($groups.eq(1))
  344. $('<div id="body2" data-parent="#accordion"/>').appendTo($groups.eq(1))
  345. var $target3 = $('<a class="collapsed" data-toggle="collapse" aria-expanded="false" role="button" href="#body3"/>').appendTo($groups.eq(2))
  346. $('<div id="body3" data-parent="#accordion"/>')
  347. .appendTo($groups.eq(2))
  348. .on('shown.bs.collapse', function () {
  349. assert.strictEqual($target1.attr('aria-expanded'), 'false', 'inactive trigger/control 1 has aria-expanded="false"')
  350. assert.strictEqual($target2.attr('aria-expanded'), 'false', 'inactive trigger/control 2 has aria-expanded="false"')
  351. assert.strictEqual($target3.attr('aria-expanded'), 'true', 'active trigger/control 3 has aria-expanded="true"')
  352. done()
  353. })
  354. $target3.trigger('click')
  355. })
  356. QUnit.test('should not fire show event if show is prevented because other element is still transitioning', function (assert) {
  357. assert.expect(1)
  358. var done = assert.async()
  359. var accordionHTML = '<div id="accordion">' +
  360. '<div class="card"/>' +
  361. '<div class="card"/>' +
  362. '</div>'
  363. var showFired = false
  364. var $groups = $(accordionHTML).appendTo('#qunit-fixture').find('.card')
  365. var $target1 = $('<a role="button" data-toggle="collapse" href="#body1"/>').appendTo($groups.eq(0))
  366. $('<div id="body1" class="collapse" data-parent="#accordion"/>')
  367. .appendTo($groups.eq(0))
  368. .on('show.bs.collapse', function () {
  369. showFired = true
  370. })
  371. var $target2 = $('<a role="button" data-toggle="collapse" href="#body2"/>').appendTo($groups.eq(1))
  372. var $body2 = $('<div id="body2" class="collapse" data-parent="#accordion"/>').appendTo($groups.eq(1))
  373. $target2.trigger('click')
  374. $body2
  375. .toggleClass('show collapsing')
  376. .data('bs.collapse')._isTransitioning = 1
  377. $target1.trigger('click')
  378. setTimeout(function () {
  379. assert.ok(!showFired, 'show event did not fire')
  380. done()
  381. }, 1)
  382. })
  383. QUnit.test('should add "collapsed" class to target when collapse is hidden via manual invocation', function (assert) {
  384. assert.expect(1)
  385. var done = assert.async()
  386. var $target = $('<a role="button" data-toggle="collapse" href="#test1"/>').appendTo('#qunit-fixture')
  387. $('<div id="test1" class="show"/>')
  388. .appendTo('#qunit-fixture')
  389. .on('hidden.bs.collapse', function () {
  390. assert.ok($target.hasClass('collapsed'))
  391. done()
  392. })
  393. .bootstrapCollapse('hide')
  394. })
  395. QUnit.test('should remove "collapsed" class from target when collapse is shown via manual invocation', function (assert) {
  396. assert.expect(1)
  397. var done = assert.async()
  398. var $target = $('<a role="button" data-toggle="collapse" class="collapsed" href="#test1"/>').appendTo('#qunit-fixture')
  399. $('<div id="test1"/>')
  400. .appendTo('#qunit-fixture')
  401. .on('shown.bs.collapse', function () {
  402. assert.ok(!$target.hasClass('collapsed'))
  403. done()
  404. })
  405. .bootstrapCollapse('show')
  406. })
  407. QUnit.test('should allow accordion to use children other than card', function (assert) {
  408. assert.expect(4)
  409. var done = assert.async()
  410. var accordionHTML = '<div id="accordion">' +
  411. '<div class="item">' +
  412. '<a id="linkTrigger" data-toggle="collapse" href="#collapseOne" aria-expanded="false" aria-controls="collapseOne"></a>' +
  413. '<div id="collapseOne" class="collapse" role="tabpanel" aria-labelledby="headingThree" data-parent="#accordion"></div>' +
  414. '</div>' +
  415. '<div class="item">' +
  416. '<a id="linkTriggerTwo" data-toggle="collapse" href="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo"></a>' +
  417. '<div id="collapseTwo" class="collapse show" role="tabpanel" aria-labelledby="headingTwo" data-parent="#accordion"></div>' +
  418. '</div>' +
  419. '</div>'
  420. $(accordionHTML).appendTo('#qunit-fixture')
  421. var $trigger = $('#linkTrigger')
  422. var $triggerTwo = $('#linkTriggerTwo')
  423. var $collapseOne = $('#collapseOne')
  424. var $collapseTwo = $('#collapseTwo')
  425. $collapseOne.on('shown.bs.collapse', function () {
  426. assert.ok($collapseOne.hasClass('show'), '#collapseOne is shown')
  427. assert.ok(!$collapseTwo.hasClass('show'), '#collapseTwo is not shown')
  428. $collapseTwo.on('shown.bs.collapse', function () {
  429. assert.ok(!$collapseOne.hasClass('show'), '#collapseOne is not shown')
  430. assert.ok($collapseTwo.hasClass('show'), '#collapseTwo is shown')
  431. done()
  432. })
  433. $triggerTwo.trigger($.Event('click'))
  434. })
  435. $trigger.trigger($.Event('click'))
  436. })
  437. QUnit.test('should allow accordion to contain nested elements', function (assert) {
  438. assert.expect(4)
  439. var done = assert.async()
  440. var accordionHTML = '<div id="accordion">' +
  441. '<div class="row">' +
  442. '<div class="col-lg-6">' +
  443. '<div class="item">' +
  444. '<a id="linkTrigger" data-toggle="collapse" href="#collapseOne" aria-expanded="false" aria-controls="collapseOne"></a>' +
  445. '<div id="collapseOne" class="collapse" role="tabpanel" aria-labelledby="headingThree" data-parent="#accordion"></div>' +
  446. '</div>' +
  447. '</div>' +
  448. '<div class="col-lg-6">' +
  449. '<div class="item">' +
  450. '<a id="linkTriggerTwo" data-toggle="collapse" href="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo"></a>' +
  451. '<div id="collapseTwo" class="collapse show" role="tabpanel" aria-labelledby="headingTwo" data-parent="#accordion"></div>' +
  452. '</div>' +
  453. '</div>' +
  454. '</div>' +
  455. '</div>'
  456. $(accordionHTML).appendTo('#qunit-fixture')
  457. var $trigger = $('#linkTrigger')
  458. var $triggerTwo = $('#linkTriggerTwo')
  459. var $collapseOne = $('#collapseOne')
  460. var $collapseTwo = $('#collapseTwo')
  461. $collapseOne.on('shown.bs.collapse', function () {
  462. assert.ok($collapseOne.hasClass('show'), '#collapseOne is shown')
  463. assert.ok(!$collapseTwo.hasClass('show'), '#collapseTwo is not shown')
  464. $collapseTwo.on('shown.bs.collapse', function () {
  465. assert.ok(!$collapseOne.hasClass('show'), '#collapseOne is not shown')
  466. assert.ok($collapseTwo.hasClass('show'), '#collapseTwo is shown')
  467. done()
  468. })
  469. $triggerTwo.trigger($.Event('click'))
  470. })
  471. $trigger.trigger($.Event('click'))
  472. })
  473. QUnit.test('should allow accordion to target multiple elements', function (assert) {
  474. assert.expect(8)
  475. var done = assert.async()
  476. var accordionHTML = '<div id="accordion">' +
  477. '<a id="linkTriggerOne" data-toggle="collapse" data-target=".collapseOne" href="#" aria-expanded="false" aria-controls="collapseOne"></a>' +
  478. '<a id="linkTriggerTwo" data-toggle="collapse" data-target=".collapseTwo" href="#" aria-expanded="false" aria-controls="collapseTwo"></a>' +
  479. '<div id="collapseOneOne" class="collapse collapseOne" role="tabpanel" data-parent="#accordion"></div>' +
  480. '<div id="collapseOneTwo" class="collapse collapseOne" role="tabpanel" data-parent="#accordion"></div>' +
  481. '<div id="collapseTwoOne" class="collapse collapseTwo" role="tabpanel" data-parent="#accordion"></div>' +
  482. '<div id="collapseTwoTwo" class="collapse collapseTwo" role="tabpanel" data-parent="#accordion"></div>' +
  483. '</div>'
  484. $(accordionHTML).appendTo('#qunit-fixture')
  485. var $trigger = $('#linkTriggerOne')
  486. var $triggerTwo = $('#linkTriggerTwo')
  487. var $collapseOneOne = $('#collapseOneOne')
  488. var $collapseOneTwo = $('#collapseOneTwo')
  489. var $collapseTwoOne = $('#collapseTwoOne')
  490. var $collapseTwoTwo = $('#collapseTwoTwo')
  491. var collapsedElements = {
  492. one : false,
  493. two : false
  494. }
  495. function firstTest() {
  496. assert.ok($collapseOneOne.hasClass('show'), '#collapseOneOne is shown')
  497. assert.ok($collapseOneTwo.hasClass('show'), '#collapseOneTwo is shown')
  498. assert.ok(!$collapseTwoOne.hasClass('show'), '#collapseTwoOne is not shown')
  499. assert.ok(!$collapseTwoTwo.hasClass('show'), '#collapseTwoTwo is not shown')
  500. $triggerTwo.trigger($.Event('click'))
  501. }
  502. function secondTest() {
  503. assert.ok(!$collapseOneOne.hasClass('show'), '#collapseOneOne is not shown')
  504. assert.ok(!$collapseOneTwo.hasClass('show'), '#collapseOneTwo is not shown')
  505. assert.ok($collapseTwoOne.hasClass('show'), '#collapseTwoOne is shown')
  506. assert.ok($collapseTwoTwo.hasClass('show'), '#collapseTwoTwo is shown')
  507. done()
  508. }
  509. $collapseOneOne.on('shown.bs.collapse', function () {
  510. if (collapsedElements.one) {
  511. firstTest()
  512. } else {
  513. collapsedElements.one = true
  514. }
  515. })
  516. $collapseOneTwo.on('shown.bs.collapse', function () {
  517. if (collapsedElements.one) {
  518. firstTest()
  519. } else {
  520. collapsedElements.one = true
  521. }
  522. })
  523. $collapseTwoOne.on('shown.bs.collapse', function () {
  524. if (collapsedElements.two) {
  525. secondTest()
  526. } else {
  527. collapsedElements.two = true
  528. }
  529. })
  530. $collapseTwoTwo.on('shown.bs.collapse', function () {
  531. if (collapsedElements.two) {
  532. secondTest()
  533. } else {
  534. collapsedElements.two = true
  535. }
  536. })
  537. $trigger.trigger($.Event('click'))
  538. })
  539. QUnit.test('should collapse accordion children but not nested accordion children', function (assert) {
  540. assert.expect(9)
  541. var done = assert.async()
  542. $('<div id="accordion">' +
  543. '<div class="item">' +
  544. '<a id="linkTrigger" data-toggle="collapse" href="#collapseOne" aria-expanded="false" aria-controls="collapseOne"></a>' +
  545. '<div id="collapseOne" data-parent="#accordion" class="collapse" role="tabpanel" aria-labelledby="headingThree">' +
  546. '<div id="nestedAccordion">' +
  547. '<div class="item">' +
  548. '<a id="nestedLinkTrigger" data-toggle="collapse" href="#nestedCollapseOne" aria-expanded="false" aria-controls="nestedCollapseOne"></a>' +
  549. '<div id="nestedCollapseOne" data-parent="#nestedAccordion" class="collapse" role="tabpanel" aria-labelledby="headingThree">' +
  550. '</div>' +
  551. '</div>' +
  552. '</div>' +
  553. '</div>' +
  554. '</div>' +
  555. '<div class="item">' +
  556. '<a id="linkTriggerTwo" data-toggle="collapse" href="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo"></a>' +
  557. '<div id="collapseTwo" data-parent="#accordion" class="collapse show" role="tabpanel" aria-labelledby="headingTwo"></div>' +
  558. '</div>' +
  559. '</div>').appendTo('#qunit-fixture')
  560. var $trigger = $('#linkTrigger')
  561. var $triggerTwo = $('#linkTriggerTwo')
  562. var $nestedTrigger = $('#nestedLinkTrigger')
  563. var $collapseOne = $('#collapseOne')
  564. var $collapseTwo = $('#collapseTwo')
  565. var $nestedCollapseOne = $('#nestedCollapseOne')
  566. $collapseOne.one('shown.bs.collapse', function () {
  567. assert.ok($collapseOne.hasClass('show'), '#collapseOne is shown')
  568. assert.ok(!$collapseTwo.hasClass('show'), '#collapseTwo is not shown')
  569. assert.ok(!$('#nestedCollapseOne').hasClass('show'), '#nestedCollapseOne is not shown')
  570. $nestedCollapseOne.one('shown.bs.collapse', function () {
  571. assert.ok($collapseOne.hasClass('show'), '#collapseOne is shown')
  572. assert.ok(!$collapseTwo.hasClass('show'), '#collapseTwo is not shown')
  573. assert.ok($nestedCollapseOne.hasClass('show'), '#nestedCollapseOne is shown')
  574. $collapseTwo.one('shown.bs.collapse', function () {
  575. assert.ok(!$collapseOne.hasClass('show'), '#collapseOne is not shown')
  576. assert.ok($collapseTwo.hasClass('show'), '#collapseTwo is shown')
  577. assert.ok($nestedCollapseOne.hasClass('show'), '#nestedCollapseOne is shown')
  578. done()
  579. })
  580. $triggerTwo.trigger($.Event('click'))
  581. })
  582. $nestedTrigger.trigger($.Event('click'))
  583. })
  584. $trigger.trigger($.Event('click'))
  585. })
  586. QUnit.test('should not prevent event for input', function (assert) {
  587. assert.expect(3)
  588. var done = assert.async()
  589. var $target = $('<input type="checkbox" data-toggle="collapse" data-target="#collapsediv1" />').appendTo('#qunit-fixture')
  590. $('<div id="collapsediv1"/>')
  591. .appendTo('#qunit-fixture')
  592. .on('shown.bs.collapse', function () {
  593. assert.ok($(this).hasClass('show'))
  594. assert.ok($target.attr('aria-expanded') === 'true')
  595. assert.ok($target.prop('checked'))
  596. done()
  597. })
  598. $target.trigger($.Event('click'))
  599. })
  600. QUnit.test('should add "collapsed" class to triggers only when all the targeted collapse are hidden', function (assert) {
  601. assert.expect(9)
  602. var done = assert.async()
  603. var $trigger1 = $('<a role="button" data-toggle="collapse" href="#test1"/>').appendTo('#qunit-fixture')
  604. var $trigger2 = $('<a role="button" data-toggle="collapse" href="#test2"/>').appendTo('#qunit-fixture')
  605. var $trigger3 = $('<a role="button" data-toggle="collapse" href=".multi"/>').appendTo('#qunit-fixture')
  606. var $target1 = $('<div id="test1" class="multi"/>').appendTo('#qunit-fixture')
  607. var $target2 = $('<div id="test2" class="multi"/>').appendTo('#qunit-fixture')
  608. $target2.one('shown.bs.collapse', function () {
  609. assert.ok(!$trigger1.hasClass('collapsed'), 'trigger1 does not have collapsed class')
  610. assert.ok(!$trigger2.hasClass('collapsed'), 'trigger2 does not have collapsed class')
  611. assert.ok(!$trigger3.hasClass('collapsed'), 'trigger3 does not have collapsed class')
  612. $target2.one('hidden.bs.collapse', function () {
  613. assert.ok(!$trigger1.hasClass('collapsed'), 'trigger1 does not have collapsed class')
  614. assert.ok($trigger2.hasClass('collapsed'), 'trigger2 has collapsed class')
  615. assert.ok(!$trigger3.hasClass('collapsed'), 'trigger3 does not have collapsed class')
  616. $target1.one('hidden.bs.collapse', function () {
  617. assert.ok($trigger1.hasClass('collapsed'), 'trigger1 has collapsed class')
  618. assert.ok($trigger2.hasClass('collapsed'), 'trigger2 has collapsed class')
  619. assert.ok($trigger3.hasClass('collapsed'), 'trigger3 has collapsed class')
  620. done()
  621. })
  622. $trigger1.trigger('click')
  623. })
  624. $trigger2.trigger('click')
  625. })
  626. $trigger3.trigger('click')
  627. })
  628. QUnit.test('should set aria-expanded="true" to triggers targeting shown collaspe and aria-expanded="false" only when all the targeted collapses are shown', function (assert) {
  629. assert.expect(9)
  630. var done = assert.async()
  631. var $trigger1 = $('<a role="button" data-toggle="collapse" href="#test1"/>').appendTo('#qunit-fixture')
  632. var $trigger2 = $('<a role="button" data-toggle="collapse" href="#test2"/>').appendTo('#qunit-fixture')
  633. var $trigger3 = $('<a role="button" data-toggle="collapse" href=".multi"/>').appendTo('#qunit-fixture')
  634. var $target1 = $('<div id="test1" class="multi collapse"/>').appendTo('#qunit-fixture')
  635. var $target2 = $('<div id="test2" class="multi collapse"/>').appendTo('#qunit-fixture')
  636. $target2.one('shown.bs.collapse', function () {
  637. assert.strictEqual($trigger1.attr('aria-expanded'), 'true', 'aria-expanded on trigger1 is "true"')
  638. assert.strictEqual($trigger2.attr('aria-expanded'), 'true', 'aria-expanded on trigger2 is "true"')
  639. assert.strictEqual($trigger3.attr('aria-expanded'), 'true', 'aria-expanded on trigger3 is "true"')
  640. $target2.one('hidden.bs.collapse', function () {
  641. assert.strictEqual($trigger1.attr('aria-expanded'), 'true', 'aria-expanded on trigger1 is "true"')
  642. assert.strictEqual($trigger2.attr('aria-expanded'), 'false', 'aria-expanded on trigger2 is "false"')
  643. assert.strictEqual($trigger3.attr('aria-expanded'), 'true', 'aria-expanded on trigger3 is "true"')
  644. $target1.one('hidden.bs.collapse', function () {
  645. assert.strictEqual($trigger1.attr('aria-expanded'), 'false', 'aria-expanded on trigger1 is "fasle"')
  646. assert.strictEqual($trigger2.attr('aria-expanded'), 'false', 'aria-expanded on trigger2 is "false"')
  647. assert.strictEqual($trigger3.attr('aria-expanded'), 'false', 'aria-expanded on trigger3 is "false"')
  648. done()
  649. })
  650. $trigger1.trigger('click')
  651. })
  652. $trigger2.trigger('click')
  653. })
  654. $trigger3.trigger('click')
  655. })
  656. QUnit.test('should not prevent interactions inside the collapse element', function (assert) {
  657. assert.expect(2)
  658. var done = assert.async()
  659. var $target = $('<input type="checkbox" data-toggle="collapse" data-target="#collapsediv1" />').appendTo('#qunit-fixture')
  660. var htmlCollapse =
  661. '<div id="collapsediv1" class="collapse">' +
  662. ' <input type="checkbox" id="testCheckbox" />' +
  663. '</div>'
  664. $(htmlCollapse)
  665. .appendTo('#qunit-fixture')
  666. .on('shown.bs.collapse', function () {
  667. assert.ok($target.prop('checked'), '$trigger is checked')
  668. var $testCheckbox = $('#testCheckbox')
  669. $testCheckbox.trigger($.Event('click'))
  670. setTimeout(function () {
  671. assert.ok($testCheckbox.prop('checked'), '$testCheckbox is checked too')
  672. done()
  673. }, 5)
  674. })
  675. $target.trigger($.Event('click'))
  676. })
  677. QUnit.test('should allow jquery object in parent config', function (assert) {
  678. assert.expect(1)
  679. var html =
  680. '<div class="my-collapse">' +
  681. ' <div class="item">' +
  682. ' <a data-toggle="collapse" href="#">Toggle item</a>' +
  683. ' <div class="collapse">Lorem ipsum</div>' +
  684. ' </div>' +
  685. '</div>'
  686. $(html).appendTo('#qunit-fixture')
  687. try {
  688. $('[data-toggle="collapse"]').bootstrapCollapse({
  689. parent: $('.my-collapse')
  690. })
  691. assert.ok(true, 'collapse correctly created')
  692. } catch (err) {
  693. assert.ok(false, 'collapse not created')
  694. }
  695. })
  696. QUnit.test('should allow DOM object in parent config', function (assert) {
  697. assert.expect(1)
  698. var html =
  699. '<div class="my-collapse">' +
  700. ' <div class="item">' +
  701. ' <a data-toggle="collapse" href="#">Toggle item</a>' +
  702. ' <div class="collapse">Lorem ipsum</div>' +
  703. ' </div>' +
  704. '</div>'
  705. $(html).appendTo('#qunit-fixture')
  706. try {
  707. $('[data-toggle="collapse"]').bootstrapCollapse({
  708. parent: $('.my-collapse')[0]
  709. })
  710. assert.ok(true, 'collapse correctly created')
  711. } catch (err) {
  712. assert.ok(false, 'collapse not created')
  713. }
  714. })
  715. QUnit.test('should find collapse children if they have collapse class too not only data-parent', function (assert) {
  716. assert.expect(2)
  717. var done = assert.async()
  718. var html =
  719. '<div class="my-collapse">' +
  720. ' <div class="item">' +
  721. ' <a data-toggle="collapse" href="#">Toggle item 1</a>' +
  722. ' <div id="collapse1" class="collapse show">Lorem ipsum 1</div>' +
  723. ' </div>' +
  724. ' <div class="item">' +
  725. ' <a id="triggerCollapse2" data-toggle="collapse" href="#">Toggle item 2</a>' +
  726. ' <div id="collapse2" class="collapse">Lorem ipsum 2</div>' +
  727. ' </div>' +
  728. '</div>'
  729. $(html).appendTo('#qunit-fixture')
  730. var $parent = $('.my-collapse')
  731. var $collapse2 = $('#collapse2')
  732. $parent.find('.collapse').bootstrapCollapse({
  733. parent: $parent,
  734. toggle: false
  735. })
  736. $collapse2.on('shown.bs.collapse', function () {
  737. assert.ok($collapse2.hasClass('show'))
  738. assert.ok(!$('#collapse1').hasClass('show'))
  739. done()
  740. })
  741. $collapse2.bootstrapCollapse('toggle')
  742. })
  743. })