tab.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. $(function () {
  2. 'use strict'
  3. QUnit.module('tabs plugin')
  4. QUnit.test('should be defined on jquery object', function (assert) {
  5. assert.expect(1)
  6. assert.ok($(document.body).tab, 'tabs method is defined')
  7. })
  8. QUnit.module('tabs', {
  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.bootstrapTab = $.fn.tab.noConflict()
  12. },
  13. afterEach: function () {
  14. $.fn.tab = $.fn.bootstrapTab
  15. delete $.fn.bootstrapTab
  16. $('#qunit-fixture').html('')
  17. }
  18. })
  19. QUnit.test('should provide no conflict', function (assert) {
  20. assert.expect(1)
  21. assert.strictEqual(typeof $.fn.tab, 'undefined', 'tab 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.bootstrapTab()
  27. try {
  28. $el.bootstrapTab('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 $tab = $el.bootstrapTab()
  37. assert.ok($tab instanceof $, 'returns jquery collection')
  38. assert.strictEqual($tab[0], $el[0], 'collection contains element')
  39. })
  40. QUnit.test('should activate element by tab id', function (assert) {
  41. assert.expect(2)
  42. var tabsHTML = '<ul class="nav">' +
  43. '<li><a href="#home">Home</a></li>' +
  44. '<li><a href="#profile">Profile</a></li>' +
  45. '</ul>'
  46. $('<ul><li id="home"/><li id="profile"/></ul>').appendTo('#qunit-fixture')
  47. $(tabsHTML).find('li:last-child a').bootstrapTab('show')
  48. assert.strictEqual($('#qunit-fixture').find('.active').attr('id'), 'profile')
  49. $(tabsHTML).find('li:first-child a').bootstrapTab('show')
  50. assert.strictEqual($('#qunit-fixture').find('.active').attr('id'), 'home')
  51. })
  52. QUnit.test('should activate element by tab id', function (assert) {
  53. assert.expect(2)
  54. var pillsHTML = '<ul class="nav nav-pills">' +
  55. '<li><a href="#home">Home</a></li>' +
  56. '<li><a href="#profile">Profile</a></li>' +
  57. '</ul>'
  58. $('<ul><li id="home"/><li id="profile"/></ul>').appendTo('#qunit-fixture')
  59. $(pillsHTML).find('li:last-child a').bootstrapTab('show')
  60. assert.strictEqual($('#qunit-fixture').find('.active').attr('id'), 'profile')
  61. $(pillsHTML).find('li:first-child a').bootstrapTab('show')
  62. assert.strictEqual($('#qunit-fixture').find('.active').attr('id'), 'home')
  63. })
  64. QUnit.test('should activate element by tab id in ordered list', function (assert) {
  65. assert.expect(2)
  66. var pillsHTML = '<ol class="nav nav-pills">' +
  67. '<li><a href="#home">Home</a></li>' +
  68. '<li><a href="#profile">Profile</a></li>' +
  69. '</ol>'
  70. $('<ol><li id="home"/><li id="profile"/></ol>').appendTo('#qunit-fixture')
  71. $(pillsHTML).find('li:last-child a').bootstrapTab('show')
  72. assert.strictEqual($('#qunit-fixture').find('.active').attr('id'), 'profile')
  73. $(pillsHTML).find('li:first-child a').bootstrapTab('show')
  74. assert.strictEqual($('#qunit-fixture').find('.active').attr('id'), 'home')
  75. })
  76. QUnit.test('should activate element by tab id in nav list', function (assert) {
  77. assert.expect(2)
  78. var tabsHTML = '<nav class="nav">' +
  79. '<a href="#home">Home</a>' +
  80. '<a href="#profile">Profile</a>' +
  81. '</nav>'
  82. $('<nav><div id="home"></div><div id="profile"></div></nav>').appendTo('#qunit-fixture')
  83. $(tabsHTML).find('a:last-child').bootstrapTab('show')
  84. assert.strictEqual($('#qunit-fixture').find('.active').attr('id'), 'profile')
  85. $(tabsHTML).find('a:first-child').bootstrapTab('show')
  86. assert.strictEqual($('#qunit-fixture').find('.active').attr('id'), 'home')
  87. })
  88. QUnit.test('should activate element by tab id in list group', function (assert) {
  89. assert.expect(2)
  90. var tabsHTML = '<div class="list-group">' +
  91. '<a href="#home">Home</a>' +
  92. '<a href="#profile">Profile</a>' +
  93. '</div>'
  94. $('<nav><div id="home"></div><div id="profile"></div></nav>').appendTo('#qunit-fixture')
  95. $(tabsHTML).find('a:last-child').bootstrapTab('show')
  96. assert.strictEqual($('#qunit-fixture').find('.active').attr('id'), 'profile')
  97. $(tabsHTML).find('a:first-child').bootstrapTab('show')
  98. assert.strictEqual($('#qunit-fixture').find('.active').attr('id'), 'home')
  99. })
  100. QUnit.test('should not fire shown when show is prevented', function (assert) {
  101. assert.expect(1)
  102. var done = assert.async()
  103. $('<div class="nav"/>')
  104. .on('show.bs.tab', function (e) {
  105. e.preventDefault()
  106. assert.ok(true, 'show event fired')
  107. done()
  108. })
  109. .on('shown.bs.tab', function () {
  110. assert.ok(false, 'shown event fired')
  111. })
  112. .bootstrapTab('show')
  113. })
  114. QUnit.test('should not fire shown when tab is already active', function (assert) {
  115. assert.expect(0)
  116. var tabsHTML = '<ul class="nav nav-tabs" role="tablist">' +
  117. '<li class="nav-item"><a href="#home" class="nav-link active" role="tab">Home</a></li>' +
  118. '<li class="nav-item"><a href="#profile" class="nav-link" role="tab">Profile</a></li>' +
  119. '</ul>' +
  120. '<div class="tab-content">' +
  121. '<div class="tab-pane active" id="home" role="tabpanel"></div>' +
  122. '<div class="tab-pane" id="profile" role="tabpanel"></div>' +
  123. '</div>'
  124. $(tabsHTML)
  125. .find('a.active')
  126. .on('shown.bs.tab', function () {
  127. assert.ok(true, 'shown event fired')
  128. })
  129. .bootstrapTab('show')
  130. })
  131. QUnit.test('should not fire shown when tab is disabled', function (assert) {
  132. assert.expect(0)
  133. var tabsHTML = '<ul class="nav nav-tabs" role="tablist">' +
  134. '<li class="nav-item"><a href="#home" class="nav-link active" role="tab">Home</a></li>' +
  135. '<li class="nav-item"><a href="#profile" class="nav-link disabled" role="tab">Profile</a></li>' +
  136. '</ul>' +
  137. '<div class="tab-content">' +
  138. '<div class="tab-pane active" id="home" role="tabpanel"></div>' +
  139. '<div class="tab-pane" id="profile" role="tabpanel"></div>' +
  140. '</div>'
  141. $(tabsHTML)
  142. .find('a.disabled')
  143. .on('shown.bs.tab', function () {
  144. assert.ok(true, 'shown event fired')
  145. })
  146. .bootstrapTab('show')
  147. })
  148. QUnit.test('show and shown events should reference correct relatedTarget', function (assert) {
  149. assert.expect(2)
  150. var done = assert.async()
  151. var dropHTML =
  152. '<ul class="drop nav">' +
  153. ' <li class="dropdown"><a data-toggle="dropdown" href="#">1</a>' +
  154. ' <ul class="dropdown-menu nav">' +
  155. ' <li><a href="#a1-1" data-toggle="tab">1-1</a></li>' +
  156. ' <li><a href="#a1-2" data-toggle="tab">1-2</a></li>' +
  157. ' </ul>' +
  158. ' </li>' +
  159. '</ul>'
  160. $(dropHTML)
  161. .find('ul > li:first-child a')
  162. .bootstrapTab('show')
  163. .end()
  164. .find('ul > li:last-child a')
  165. .on('show.bs.tab', function (e) {
  166. assert.strictEqual(e.relatedTarget.hash, '#a1-1', 'references correct element as relatedTarget')
  167. })
  168. .on('shown.bs.tab', function (e) {
  169. assert.strictEqual(e.relatedTarget.hash, '#a1-1', 'references correct element as relatedTarget')
  170. done()
  171. })
  172. .bootstrapTab('show')
  173. })
  174. QUnit.test('should fire hide and hidden events', function (assert) {
  175. assert.expect(2)
  176. var done = assert.async()
  177. var tabsHTML = '<ul class="nav">' +
  178. '<li><a href="#home">Home</a></li>' +
  179. '<li><a href="#profile">Profile</a></li>' +
  180. '</ul>'
  181. $(tabsHTML)
  182. .find('li:first-child a')
  183. .on('hide.bs.tab', function () {
  184. assert.ok(true, 'hide event fired')
  185. })
  186. .bootstrapTab('show')
  187. .end()
  188. .find('li:last-child a')
  189. .bootstrapTab('show')
  190. $(tabsHTML)
  191. .find('li:first-child a')
  192. .on('hidden.bs.tab', function () {
  193. assert.ok(true, 'hidden event fired')
  194. done()
  195. })
  196. .bootstrapTab('show')
  197. .end()
  198. .find('li:last-child a')
  199. .bootstrapTab('show')
  200. })
  201. QUnit.test('should not fire hidden when hide is prevented', function (assert) {
  202. assert.expect(1)
  203. var done = assert.async()
  204. var tabsHTML = '<ul class="nav">' +
  205. '<li><a href="#home">Home</a></li>' +
  206. '<li><a href="#profile">Profile</a></li>' +
  207. '</ul>'
  208. $(tabsHTML)
  209. .find('li:first-child a')
  210. .on('hide.bs.tab', function (e) {
  211. e.preventDefault()
  212. assert.ok(true, 'hide event fired')
  213. done()
  214. })
  215. .on('hidden.bs.tab', function () {
  216. assert.ok(false, 'hidden event fired')
  217. })
  218. .bootstrapTab('show')
  219. .end()
  220. .find('li:last-child a')
  221. .bootstrapTab('show')
  222. })
  223. QUnit.test('hide and hidden events contain correct relatedTarget', function (assert) {
  224. assert.expect(2)
  225. var done = assert.async()
  226. var tabsHTML = '<ul class="nav">' +
  227. '<li><a href="#home">Home</a></li>' +
  228. '<li><a href="#profile">Profile</a></li>' +
  229. '</ul>'
  230. $(tabsHTML)
  231. .find('li:first-child a')
  232. .on('hide.bs.tab', function (e) {
  233. assert.strictEqual(e.relatedTarget.hash, '#profile', 'references correct element as relatedTarget')
  234. })
  235. .on('hidden.bs.tab', function (e) {
  236. assert.strictEqual(e.relatedTarget.hash, '#profile', 'references correct element as relatedTarget')
  237. done()
  238. })
  239. .bootstrapTab('show')
  240. .end()
  241. .find('li:last-child a')
  242. .bootstrapTab('show')
  243. })
  244. QUnit.test('selected tab should have aria-selected', function (assert) {
  245. assert.expect(8)
  246. var tabsHTML = '<ul class="nav nav-tabs">' +
  247. '<li><a class="nav-item active" href="#home" toggle="tab" aria-selected="true">Home</a></li>' +
  248. '<li><a class="nav-item" href="#profile" toggle="tab" aria-selected="false">Profile</a></li>' +
  249. '</ul>'
  250. var $tabs = $(tabsHTML).appendTo('#qunit-fixture')
  251. $tabs.find('li:first-child a').bootstrapTab('show')
  252. assert.strictEqual($tabs.find('.active').attr('aria-selected'), 'true', 'shown tab has aria-selected = true')
  253. assert.strictEqual($tabs.find('a:not(.active)').attr('aria-selected'), 'false', 'hidden tab has aria-selected = false')
  254. $tabs.find('li:last-child a').trigger('click')
  255. assert.strictEqual($tabs.find('.active').attr('aria-selected'), 'true', 'after click, shown tab has aria-selected = true')
  256. assert.strictEqual($tabs.find('a:not(.active)').attr('aria-selected'), 'false', 'after click, hidden tab has aria-selected = false')
  257. $tabs.find('li:first-child a').bootstrapTab('show')
  258. assert.strictEqual($tabs.find('.active').attr('aria-selected'), 'true', 'shown tab has aria-selected = true')
  259. assert.strictEqual($tabs.find('a:not(.active)').attr('aria-selected'), 'false', 'hidden tab has aria-selected = false')
  260. $tabs.find('li:first-child a').trigger('click')
  261. assert.strictEqual($tabs.find('.active').attr('aria-selected'), 'true', 'after second show event, shown tab still has aria-selected = true')
  262. assert.strictEqual($tabs.find('a:not(.active)').attr('aria-selected'), 'false', 'after second show event, hidden tab has aria-selected = false')
  263. })
  264. QUnit.test('selected tab should deactivate previous selected tab', function (assert) {
  265. assert.expect(2)
  266. var tabsHTML = '<ul class="nav nav-tabs">' +
  267. '<li class="nav-item"><a class="nav-link active" href="#home" data-toggle="tab">Home</a></li>' +
  268. '<li class="nav-item"><a class="nav-link" href="#profile" data-toggle="tab">Profile</a></li>' +
  269. '</ul>'
  270. var $tabs = $(tabsHTML).appendTo('#qunit-fixture')
  271. $tabs.find('li:last-child a').trigger('click')
  272. assert.notOk($tabs.find('li:first-child a').hasClass('active'))
  273. assert.ok($tabs.find('li:last-child a').hasClass('active'))
  274. })
  275. QUnit.test('selected tab should deactivate previous selected link in dropdown', function (assert) {
  276. assert.expect(3)
  277. var tabsHTML = '<ul class="nav nav-tabs">' +
  278. '<li class="nav-item"><a class="nav-link" href="#home" data-toggle="tab">Home</a></li>' +
  279. '<li class="nav-item"><a class="nav-link" href="#profile" data-toggle="tab">Profile</a></li>' +
  280. '<li class="nav-item dropdown"><a class="nav-link dropdown-toggle active" data-toggle="dropdown" href="#">Dropdown</a>' +
  281. '<div class="dropdown-menu">' +
  282. '<a class="dropdown-item active" href="#dropdown1" id="dropdown1-tab" data-toggle="tab">@fat</a>' +
  283. '<a class="dropdown-item" href="#dropdown2" id="dropdown2-tab" data-toggle="tab">@mdo</a>' +
  284. '</div>' +
  285. '</li>' +
  286. '</ul>'
  287. var $tabs = $(tabsHTML).appendTo('#qunit-fixture')
  288. $tabs.find('li:first-child a').trigger('click')
  289. assert.ok($tabs.find('li:first-child a').hasClass('active'))
  290. assert.notOk($tabs.find('li:last-child a').hasClass('active'))
  291. assert.notOk($tabs.find('li:last-child .dropdown-menu a:first-child').hasClass('active'))
  292. })
  293. QUnit.test('Nested tabs', function (assert) {
  294. assert.expect(2)
  295. var done = assert.async()
  296. var tabsHTML =
  297. '<nav class="nav nav-tabs" role="tablist">' +
  298. ' <a id="tab1" href="#x-tab1" class="nav-item nav-link" data-toggle="tab" role="tab" aria-controls="x-tab1">Tab 1</a>' +
  299. ' <a href="#x-tab2" class="nav-item nav-link active" data-toggle="tab" role="tab" aria-controls="x-tab2" aria-selected="true">Tab 2</a>' +
  300. ' <a href="#x-tab3" class="nav-item nav-link" data-toggle="tab" role="tab" aria-controls="x-tab3">Tab 3</a>' +
  301. '</nav>' +
  302. '<div class="tab-content">' +
  303. ' <div class="tab-pane" id="x-tab1" role="tabpanel">' +
  304. ' <nav class="nav nav-tabs" role="tablist">' +
  305. ' <a href="#nested-tab1" class="nav-item nav-link active" data-toggle="tab" role="tab" aria-controls="x-tab1" aria-selected="true">Nested Tab 1</a>' +
  306. ' <a id="tabNested2" href="#nested-tab2" class="nav-item nav-link" data-toggle="tab" role="tab" aria-controls="x-profile">Nested Tab2</a>' +
  307. ' </nav>' +
  308. ' <div class="tab-content">' +
  309. ' <div class="tab-pane active" id="nested-tab1" role="tabpanel">Nested Tab1 Content</div>' +
  310. ' <div class="tab-pane" id="nested-tab2" role="tabpanel">Nested Tab2 Content</div>' +
  311. ' </div>' +
  312. ' </div>' +
  313. ' <div class="tab-pane active" id="x-tab2" role="tabpanel">Tab2 Content</div>' +
  314. ' <div class="tab-pane" id="x-tab3" role="tabpanel">Tab3 Content</div>' +
  315. '</div>'
  316. $(tabsHTML).appendTo('#qunit-fixture')
  317. $('#tabNested2').on('shown.bs.tab', function () {
  318. assert.ok($('#x-tab1').hasClass('active'))
  319. done()
  320. })
  321. $('#tab1').on('shown.bs.tab', function () {
  322. assert.ok($('#x-tab1').hasClass('active'))
  323. $('#tabNested2').trigger($.Event('click'))
  324. })
  325. .trigger($.Event('click'))
  326. })
  327. QUnit.test('should not remove fade class if no active pane is present', function (assert) {
  328. assert.expect(6)
  329. var done = assert.async()
  330. var tabsHTML = '<ul class="nav nav-tabs" role="tablist">' +
  331. '<li class="nav-item"><a id="tab-home" href="#home" class="nav-link" data-toggle="tab" role="tab">Home</a></li>' +
  332. '<li class="nav-item"><a id="tab-profile" href="#profile" class="nav-link" data-toggle="tab" role="tab">Profile</a></li>' +
  333. '</ul>' +
  334. '<div class="tab-content">' +
  335. '<div class="tab-pane fade" id="home" role="tabpanel"></div>' +
  336. '<div class="tab-pane fade" id="profile" role="tabpanel"></div>' +
  337. '</div>'
  338. $(tabsHTML).appendTo('#qunit-fixture')
  339. $('#tab-profile')
  340. .on('shown.bs.tab', function () {
  341. assert.ok($('#profile').hasClass('fade'))
  342. assert.ok($('#profile').hasClass('show'))
  343. $('#tab-home')
  344. .on('shown.bs.tab', function () {
  345. assert.ok($('#profile').hasClass('fade'))
  346. assert.notOk($('#profile').hasClass('show'))
  347. assert.ok($('#home').hasClass('fade'))
  348. assert.ok($('#home').hasClass('show'))
  349. done()
  350. })
  351. .trigger($.Event('click'))
  352. })
  353. .trigger($.Event('click'))
  354. })
  355. QUnit.test('should handle removed tabs', function (assert) {
  356. assert.expect(1)
  357. var done = assert.async()
  358. var html = [
  359. '<ul class="nav nav-tabs" role="tablist">',
  360. ' <li class="nav-item">',
  361. ' <a class="nav-link nav-tab" href="#profile" role="tab" data-toggle="tab">',
  362. ' <button class="close"><span aria-hidden="true">&times;</span></button>',
  363. ' </a>',
  364. ' </li>',
  365. ' <li class="nav-item">',
  366. ' <a id="secondNav" class="nav-link nav-tab" href="#buzz" role="tab" data-toggle="tab">',
  367. ' <button class="close"><span aria-hidden="true">&times;</span></button>',
  368. ' </a>',
  369. ' </li>',
  370. ' <li class="nav-item">',
  371. ' <a class="nav-link nav-tab" href="#references" role="tab" data-toggle="tab">',
  372. ' <button id="btnClose" class="close"><span aria-hidden="true">&times;</span></button>',
  373. ' </a>',
  374. ' </li>',
  375. '</ul>',
  376. '<div class="tab-content">',
  377. ' <div role="tabpanel" class="tab-pane fade show active" id="profile">test 1</div>',
  378. ' <div role="tabpanel" class="tab-pane fade" id="buzz">test 2</div>',
  379. ' <div role="tabpanel" class="tab-pane fade" id="references">test 3</div>',
  380. '</div>'
  381. ].join('')
  382. $(html).appendTo('#qunit-fixture')
  383. $('#secondNav').on('shown.bs.tab', function () {
  384. assert.strictEqual($('.nav-tab').length, 2)
  385. done()
  386. })
  387. $('#btnClose').one('click', function () {
  388. var tabId = $(this).parents('a').attr('href')
  389. $(this).parents('li').remove()
  390. $(tabId).remove()
  391. $('.nav-tabs a:last').bootstrapTab('show')
  392. })
  393. .trigger($.Event('click'))
  394. })
  395. QUnit.test('should not add show class to tab panes if there is no `.fade` class', function (assert) {
  396. assert.expect(1)
  397. var done = assert.async()
  398. var html = [
  399. '<ul class="nav nav-tabs" role="tablist">',
  400. ' <li class="nav-item">',
  401. ' <a class="nav-link nav-tab" href="#home" role="tab" data-toggle="tab">Home</a>',
  402. ' </li>',
  403. ' <li class="nav-item">',
  404. ' <a id="secondNav" class="nav-link nav-tab" href="#profile" role="tab" data-toggle="tab">Profile</a>',
  405. ' </li>',
  406. '</ul>',
  407. '<div class="tab-content">',
  408. ' <div role="tabpanel" class="tab-pane" id="home">test 1</div>',
  409. ' <div role="tabpanel" class="tab-pane" id="profile">test 2</div>',
  410. '</div>'
  411. ].join('')
  412. $(html).appendTo('#qunit-fixture')
  413. $('#secondNav').on('shown.bs.tab', function () {
  414. assert.strictEqual($('.show').length, 0)
  415. done()
  416. })
  417. .trigger($.Event('click'))
  418. })
  419. QUnit.test('should add show class to tab panes if there is a `.fade` class', function (assert) {
  420. assert.expect(1)
  421. var done = assert.async()
  422. var html = [
  423. '<ul class="nav nav-tabs" role="tablist">',
  424. ' <li class="nav-item">',
  425. ' <a class="nav-link nav-tab" href="#home" role="tab" data-toggle="tab">Home</a>',
  426. ' </li>',
  427. ' <li class="nav-item">',
  428. ' <a id="secondNav" class="nav-link nav-tab" href="#profile" role="tab" data-toggle="tab">Profile</a>',
  429. ' </li>',
  430. '</ul>',
  431. '<div class="tab-content">',
  432. ' <div role="tabpanel" class="tab-pane fade" id="home">test 1</div>',
  433. ' <div role="tabpanel" class="tab-pane fade" id="profile">test 2</div>',
  434. '</div>'
  435. ].join('')
  436. $(html).appendTo('#qunit-fixture')
  437. $('#secondNav').on('shown.bs.tab', function () {
  438. assert.strictEqual($('.show').length, 1)
  439. done()
  440. })
  441. .trigger($.Event('click'))
  442. })
  443. })