tab.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. }
  17. })
  18. QUnit.test('should provide no conflict', function (assert) {
  19. assert.expect(1)
  20. assert.strictEqual($.fn.tab, undefined, 'tab 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 $tab = $el.bootstrapTab()
  26. assert.ok($tab instanceof $, 'returns jquery collection')
  27. assert.strictEqual($tab[0], $el[0], 'collection contains element')
  28. })
  29. QUnit.test('should activate element by tab id', function (assert) {
  30. assert.expect(2)
  31. var tabsHTML = '<ul class="tabs">'
  32. + '<li><a href="#home">Home</a></li>'
  33. + '<li><a href="#profile">Profile</a></li>'
  34. + '</ul>'
  35. $('<ul><li id="home"/><li id="profile"/></ul>').appendTo('#qunit-fixture')
  36. $(tabsHTML).find('li:last a').bootstrapTab('show')
  37. assert.strictEqual($('#qunit-fixture').find('.active').attr('id'), 'profile')
  38. $(tabsHTML).find('li:first a').bootstrapTab('show')
  39. assert.strictEqual($('#qunit-fixture').find('.active').attr('id'), 'home')
  40. })
  41. QUnit.test('should activate element by tab id', function (assert) {
  42. assert.expect(2)
  43. var pillsHTML = '<ul class="pills">'
  44. + '<li><a href="#home">Home</a></li>'
  45. + '<li><a href="#profile">Profile</a></li>'
  46. + '</ul>'
  47. $('<ul><li id="home"/><li id="profile"/></ul>').appendTo('#qunit-fixture')
  48. $(pillsHTML).find('li:last a').bootstrapTab('show')
  49. assert.strictEqual($('#qunit-fixture').find('.active').attr('id'), 'profile')
  50. $(pillsHTML).find('li:first a').bootstrapTab('show')
  51. assert.strictEqual($('#qunit-fixture').find('.active').attr('id'), 'home')
  52. })
  53. QUnit.test('should not fire shown when show is prevented', function (assert) {
  54. assert.expect(1)
  55. var done = assert.async()
  56. $('<div class="tab"/>')
  57. .on('show.bs.tab', function (e) {
  58. e.preventDefault()
  59. assert.ok(true, 'show event fired')
  60. done()
  61. })
  62. .on('shown.bs.tab', function () {
  63. assert.ok(false, 'shown event fired')
  64. })
  65. .bootstrapTab('show')
  66. })
  67. QUnit.test('show and shown events should reference correct relatedTarget', function (assert) {
  68. assert.expect(2)
  69. var done = assert.async()
  70. var dropHTML = '<ul class="drop">'
  71. + '<li class="dropdown"><a data-toggle="dropdown" href="#">1</a>'
  72. + '<ul class="dropdown-menu">'
  73. + '<li><a href="#1-1" data-toggle="tab">1-1</a></li>'
  74. + '<li><a href="#1-2" data-toggle="tab">1-2</a></li>'
  75. + '</ul>'
  76. + '</li>'
  77. + '</ul>'
  78. $(dropHTML)
  79. .find('ul > li:first a')
  80. .bootstrapTab('show')
  81. .end()
  82. .find('ul > li:last a')
  83. .on('show.bs.tab', function (e) {
  84. assert.strictEqual(e.relatedTarget.hash, '#1-1', 'references correct element as relatedTarget')
  85. })
  86. .on('shown.bs.tab', function (e) {
  87. assert.strictEqual(e.relatedTarget.hash, '#1-1', 'references correct element as relatedTarget')
  88. done()
  89. })
  90. .bootstrapTab('show')
  91. })
  92. QUnit.test('should fire hide and hidden events', function (assert) {
  93. assert.expect(2)
  94. var done = assert.async()
  95. var tabsHTML = '<ul class="tabs">'
  96. + '<li><a href="#home">Home</a></li>'
  97. + '<li><a href="#profile">Profile</a></li>'
  98. + '</ul>'
  99. $(tabsHTML)
  100. .find('li:first a')
  101. .on('hide.bs.tab', function () {
  102. assert.ok(true, 'hide event fired')
  103. })
  104. .bootstrapTab('show')
  105. .end()
  106. .find('li:last a')
  107. .bootstrapTab('show')
  108. $(tabsHTML)
  109. .find('li:first a')
  110. .on('hidden.bs.tab', function () {
  111. assert.ok(true, 'hidden event fired')
  112. done()
  113. })
  114. .bootstrapTab('show')
  115. .end()
  116. .find('li:last a')
  117. .bootstrapTab('show')
  118. })
  119. QUnit.test('should not fire hidden when hide is prevented', function (assert) {
  120. assert.expect(1)
  121. var done = assert.async()
  122. var tabsHTML = '<ul class="tabs">'
  123. + '<li><a href="#home">Home</a></li>'
  124. + '<li><a href="#profile">Profile</a></li>'
  125. + '</ul>'
  126. $(tabsHTML)
  127. .find('li:first a')
  128. .on('hide.bs.tab', function (e) {
  129. e.preventDefault()
  130. assert.ok(true, 'hide event fired')
  131. done()
  132. })
  133. .on('hidden.bs.tab', function () {
  134. assert.ok(false, 'hidden event fired')
  135. })
  136. .bootstrapTab('show')
  137. .end()
  138. .find('li:last a')
  139. .bootstrapTab('show')
  140. })
  141. QUnit.test('hide and hidden events contain correct relatedTarget', function (assert) {
  142. assert.expect(2)
  143. var done = assert.async()
  144. var tabsHTML = '<ul class="tabs">'
  145. + '<li><a href="#home">Home</a></li>'
  146. + '<li><a href="#profile">Profile</a></li>'
  147. + '</ul>'
  148. $(tabsHTML)
  149. .find('li:first a')
  150. .on('hide.bs.tab', function (e) {
  151. assert.strictEqual(e.relatedTarget.hash, '#profile', 'references correct element as relatedTarget')
  152. })
  153. .on('hidden.bs.tab', function (e) {
  154. assert.strictEqual(e.relatedTarget.hash, '#profile', 'references correct element as relatedTarget')
  155. done()
  156. })
  157. .bootstrapTab('show')
  158. .end()
  159. .find('li:last a')
  160. .bootstrapTab('show')
  161. })
  162. QUnit.test('selected tab should have aria-expanded', function (assert) {
  163. assert.expect(8)
  164. var tabsHTML = '<ul class="nav nav-tabs">'
  165. + '<li class="active"><a href="#home" toggle="tab" aria-expanded="true">Home</a></li>'
  166. + '<li><a href="#profile" toggle="tab" aria-expanded="false">Profile</a></li>'
  167. + '</ul>'
  168. var $tabs = $(tabsHTML).appendTo('#qunit-fixture')
  169. $tabs.find('li:first a').bootstrapTab('show')
  170. assert.strictEqual($tabs.find('.active a').attr('aria-expanded'), 'true', 'shown tab has aria-expanded = true')
  171. assert.strictEqual($tabs.find('li:not(.active) a').attr('aria-expanded'), 'false', 'hidden tab has aria-expanded = false')
  172. $tabs.find('li:last a').trigger('click')
  173. assert.strictEqual($tabs.find('.active a').attr('aria-expanded'), 'true', 'after click, shown tab has aria-expanded = true')
  174. assert.strictEqual($tabs.find('li:not(.active) a').attr('aria-expanded'), 'false', 'after click, hidden tab has aria-expanded = false')
  175. $tabs.find('li:first a').bootstrapTab('show')
  176. assert.strictEqual($tabs.find('.active a').attr('aria-expanded'), 'true', 'shown tab has aria-expanded = true')
  177. assert.strictEqual($tabs.find('li:not(.active) a').attr('aria-expanded'), 'false', 'hidden tab has aria-expanded = false')
  178. $tabs.find('li:first a').trigger('click')
  179. assert.strictEqual($tabs.find('.active a').attr('aria-expanded'), 'true', 'after second show event, shown tab still has aria-expanded = true')
  180. assert.strictEqual($tabs.find('li:not(.active) a').attr('aria-expanded'), 'false', 'after second show event, hidden tab has aria-expanded = false')
  181. })
  182. })