scrollspy.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. $(function () {
  2. 'use strict';
  3. QUnit.module('scrollspy plugin')
  4. QUnit.test('should be defined on jquery object', function (assert) {
  5. assert.expect(1)
  6. assert.ok($(document.body).scrollspy, 'scrollspy method is defined')
  7. })
  8. QUnit.module('scrollspy', {
  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.bootstrapScrollspy = $.fn.scrollspy.noConflict()
  12. },
  13. afterEach: function () {
  14. $.fn.scrollspy = $.fn.bootstrapScrollspy
  15. delete $.fn.bootstrapScrollspy
  16. }
  17. })
  18. QUnit.test('should provide no conflict', function (assert) {
  19. assert.expect(1)
  20. assert.strictEqual($.fn.scrollspy, undefined, 'scrollspy 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 $scrollspy = $el.bootstrapScrollspy()
  26. assert.ok($scrollspy instanceof $, 'returns jquery collection')
  27. assert.strictEqual($scrollspy[0], $el[0], 'collection contains element')
  28. })
  29. QUnit.test('should only switch "active" class on current target', function (assert) {
  30. assert.expect(1)
  31. var done = assert.async()
  32. var sectionHTML = '<div id="root" class="active">'
  33. + '<div class="topbar">'
  34. + '<div class="topbar-inner">'
  35. + '<div class="container" id="ss-target">'
  36. + '<ul class="nav">'
  37. + '<li><a href="#masthead">Overview</a></li>'
  38. + '<li><a href="#detail">Detail</a></li>'
  39. + '</ul>'
  40. + '</div>'
  41. + '</div>'
  42. + '</div>'
  43. + '<div id="scrollspy-example" style="height: 100px; overflow: auto;">'
  44. + '<div style="height: 200px;">'
  45. + '<h4 id="masthead">Overview</h4>'
  46. + '<p style="height: 200px">'
  47. + 'Ad leggings keytar, brunch id art party dolor labore.'
  48. + '</p>'
  49. + '</div>'
  50. + '<div style="height: 200px;">'
  51. + '<h4 id="detail">Detail</h4>'
  52. + '<p style="height: 200px">'
  53. + 'Veniam marfa mustache skateboard, adipisicing fugiat velit pitchfork beard.'
  54. + '</p>'
  55. + '</div>'
  56. + '</div>'
  57. + '</div>'
  58. var $section = $(sectionHTML).appendTo('#qunit-fixture')
  59. var $scrollspy = $section
  60. .show()
  61. .find('#scrollspy-example')
  62. .bootstrapScrollspy({ target: '#ss-target' })
  63. $scrollspy.on('scroll.bs.scrollspy', function () {
  64. assert.ok($section.hasClass('active'), '"active" class still on root node')
  65. done()
  66. })
  67. $scrollspy.scrollTop(350)
  68. })
  69. QUnit.test('should correctly select middle navigation option when large offset is used', function (assert) {
  70. assert.expect(3)
  71. var done = assert.async()
  72. var sectionHTML = '<div id="header" style="height: 500px;"></div>'
  73. + '<nav id="navigation" class="navbar">'
  74. + '<ul class="nav navbar-nav">'
  75. + '<li class="active"><a id="one-link" href="#one">One</a></li>'
  76. + '<li><a id="two-link" href="#two">Two</a></li>'
  77. + '<li><a id="three-link" href="#three">Three</a></li>'
  78. + '</ul>'
  79. + '</nav>'
  80. + '<div id="content" style="height: 200px; overflow-y: auto;">'
  81. + '<div id="one" style="height: 500px;"></div>'
  82. + '<div id="two" style="height: 300px;"></div>'
  83. + '<div id="three" style="height: 10px;"></div>'
  84. + '</div>'
  85. var $section = $(sectionHTML).appendTo('#qunit-fixture')
  86. var $scrollspy = $section
  87. .show()
  88. .filter('#content')
  89. $scrollspy.bootstrapScrollspy({ target: '#navigation', offset: $scrollspy.position().top })
  90. $scrollspy.on('scroll.bs.scrollspy', function () {
  91. assert.ok(!$section.find('#one-link').parent().hasClass('active'), '"active" class removed from first section')
  92. assert.ok($section.find('#two-link').parent().hasClass('active'), '"active" class on middle section')
  93. assert.ok(!$section.find('#three-link').parent().hasClass('active'), '"active" class not on last section')
  94. done()
  95. })
  96. $scrollspy.scrollTop(550)
  97. })
  98. QUnit.test('should add the active class to the correct element', function (assert) {
  99. assert.expect(2)
  100. var navbarHtml =
  101. '<nav class="navbar">'
  102. + '<ul class="nav">'
  103. + '<li id="li-1"><a href="#div-1">div 1</a></li>'
  104. + '<li id="li-2"><a href="#div-2">div 2</a></li>'
  105. + '</ul>'
  106. + '</nav>'
  107. var contentHtml =
  108. '<div class="content" style="overflow: auto; height: 50px">'
  109. + '<div id="div-1" style="height: 100px; padding: 0; margin: 0">div 1</div>'
  110. + '<div id="div-2" style="height: 200px; padding: 0; margin: 0">div 2</div>'
  111. + '</div>'
  112. $(navbarHtml).appendTo('#qunit-fixture')
  113. var $content = $(contentHtml)
  114. .appendTo('#qunit-fixture')
  115. .bootstrapScrollspy({ offset: 0, target: '.navbar' })
  116. var done = assert.async()
  117. var testElementIsActiveAfterScroll = function (element, target) {
  118. var deferred = $.Deferred()
  119. var scrollHeight = Math.ceil($content.scrollTop() + $(target).position().top)
  120. $content.one('scroll', function () {
  121. assert.ok($(element).hasClass('active'), 'target:' + target + ', element' + element)
  122. deferred.resolve()
  123. })
  124. $content.scrollTop(scrollHeight)
  125. return deferred.promise()
  126. }
  127. $.when(testElementIsActiveAfterScroll('#li-1', '#div-1'))
  128. .then(function () { return testElementIsActiveAfterScroll('#li-2', '#div-2') })
  129. .then(function () { done() })
  130. })
  131. QUnit.test('should add the active class correctly when there are nested elements at 0 scroll offset', function (assert) {
  132. assert.expect(6)
  133. var times = 0
  134. var done = assert.async()
  135. var navbarHtml = '<nav id="navigation" class="navbar">'
  136. + '<ul class="nav">'
  137. + '<li id="li-1"><a href="#div-1">div 1</a>'
  138. + '<ul>'
  139. + '<li id="li-2"><a href="#div-2">div 2</a></li>'
  140. + '</ul>'
  141. + '</li>'
  142. + '</ul>'
  143. + '</nav>'
  144. var contentHtml = '<div class="content" style="position: absolute; top: 0px; overflow: auto; height: 50px">'
  145. + '<div id="div-1" style="padding: 0; margin: 0">'
  146. + '<div id="div-2" style="height: 200px; padding: 0; margin: 0">div 2</div>'
  147. + '</div>'
  148. + '</div>'
  149. $(navbarHtml).appendTo('#qunit-fixture')
  150. var $content = $(contentHtml)
  151. .appendTo('#qunit-fixture')
  152. .bootstrapScrollspy({ offset: 0, target: '#navigation' })
  153. !function testActiveElements() {
  154. if (++times > 3) return done()
  155. $content.one('scroll', function () {
  156. assert.ok($('#li-1').hasClass('active'), 'nav item for outer element has "active" class')
  157. assert.ok($('#li-2').hasClass('active'), 'nav item for inner element has "active" class')
  158. testActiveElements()
  159. })
  160. $content.scrollTop($content.scrollTop() + 10)
  161. }()
  162. })
  163. QUnit.test('should clear selection if above the first section', function (assert) {
  164. assert.expect(3)
  165. var done = assert.async()
  166. var sectionHTML = '<div id="header" style="height: 500px;"></div>'
  167. + '<nav id="navigation" class="navbar">'
  168. + '<ul class="nav navbar-nav">'
  169. + '<li class="active"><a id="one-link" href="#one">One</a></li>'
  170. + '<li><a id="two-link" href="#two">Two</a></li>'
  171. + '<li><a id="three-link" href="#three">Three</a></li>'
  172. + '</ul>'
  173. + '</nav>'
  174. $(sectionHTML).appendTo('#qunit-fixture')
  175. var scrollspyHTML = '<div id="content" style="height: 200px; overflow-y: auto;">'
  176. + '<div id="spacer" style="height: 100px;"/>'
  177. + '<div id="one" style="height: 100px;"/>'
  178. + '<div id="two" style="height: 100px;"/>'
  179. + '<div id="three" style="height: 100px;"/>'
  180. + '<div id="spacer" style="height: 100px;"/>'
  181. + '</div>'
  182. var $scrollspy = $(scrollspyHTML).appendTo('#qunit-fixture')
  183. $scrollspy
  184. .bootstrapScrollspy({
  185. target: '#navigation',
  186. offset: $scrollspy.position().top
  187. })
  188. .one('scroll.bs.scrollspy', function () {
  189. assert.strictEqual($('.active').length, 1, '"active" class on only one element present')
  190. assert.strictEqual($('.active').has('#two-link').length, 1, '"active" class on second section')
  191. $scrollspy
  192. .one('scroll.bs.scrollspy', function () {
  193. assert.strictEqual($('.active').length, 0, 'selection cleared')
  194. done()
  195. })
  196. .scrollTop(0)
  197. })
  198. .scrollTop(201)
  199. })
  200. QUnit.test('should correctly select navigation element on backward scrolling when each target section height is 100%', function (assert) {
  201. assert.expect(5)
  202. var navbarHtml =
  203. '<nav class="navbar">'
  204. + '<ul class="nav">'
  205. + '<li id="li-100-1"><a href="#div-100-1">div 1</a></li>'
  206. + '<li id="li-100-2"><a href="#div-100-2">div 2</a></li>'
  207. + '<li id="li-100-3"><a href="#div-100-3">div 3</a></li>'
  208. + '<li id="li-100-4"><a href="#div-100-4">div 4</a></li>'
  209. + '<li id="li-100-5"><a href="#div-100-5">div 5</a></li>'
  210. + '</ul>'
  211. + '</nav>'
  212. var contentHtml =
  213. '<div class="content" style="position: relative; overflow: auto; height: 100px">'
  214. + '<div id="div-100-1" style="position: relative; height: 100%; padding: 0; margin: 0">div 1</div>'
  215. + '<div id="div-100-2" style="position: relative; height: 100%; padding: 0; margin: 0">div 2</div>'
  216. + '<div id="div-100-3" style="position: relative; height: 100%; padding: 0; margin: 0">div 3</div>'
  217. + '<div id="div-100-4" style="position: relative; height: 100%; padding: 0; margin: 0">div 4</div>'
  218. + '<div id="div-100-5" style="position: relative; height: 100%; padding: 0; margin: 0">div 5</div>'
  219. + '</div>'
  220. $(navbarHtml).appendTo('#qunit-fixture')
  221. var $content = $(contentHtml)
  222. .appendTo('#qunit-fixture')
  223. .bootstrapScrollspy({ offset: 0, target: '.navbar' })
  224. var testElementIsActiveAfterScroll = function (element, target) {
  225. var deferred = $.Deferred()
  226. var scrollHeight = Math.ceil($content.scrollTop() + $(target).position().top)
  227. $content.one('scroll', function () {
  228. assert.ok($(element).hasClass('active'), 'target:' + target + ', element: ' + element)
  229. deferred.resolve()
  230. })
  231. $content.scrollTop(scrollHeight)
  232. return deferred.promise()
  233. }
  234. var done = assert.async()
  235. $.when(testElementIsActiveAfterScroll('#li-100-5', '#div-100-5'))
  236. .then(function () { return testElementIsActiveAfterScroll('#li-100-4', '#div-100-4') })
  237. .then(function () { return testElementIsActiveAfterScroll('#li-100-3', '#div-100-3') })
  238. .then(function () { return testElementIsActiveAfterScroll('#li-100-2', '#div-100-2') })
  239. .then(function () { return testElementIsActiveAfterScroll('#li-100-1', '#div-100-1') })
  240. .then(function () { done() })
  241. })
  242. })