scrollspy.js 31.5 KB
Newer Older
Jacob Thornton's avatar
Jacob Thornton committed
1
$(function () {
2
  'use strict'
Jacob Thornton's avatar
Jacob Thornton committed
3

4
  QUnit.module('scrollspy plugin')
5

6
  QUnit.test('should be defined on jquery object', function (assert) {
7
    assert.expect(1)
8
    assert.ok($(document.body).scrollspy, 'scrollspy method is defined')
XhmikosR's avatar
XhmikosR committed
9
  })
Jacob Thornton's avatar
Jacob Thornton committed
10

11
  QUnit.module('scrollspy', {
12
    beforeEach: function () {
13
14
15
      // Run all tests in noConflict mode -- it's the only way to ensure that the plugin works in noConflict mode
      $.fn.bootstrapScrollspy = $.fn.scrollspy.noConflict()
    },
16
    afterEach: function () {
17
18
      $.fn.scrollspy = $.fn.bootstrapScrollspy
      delete $.fn.bootstrapScrollspy
19
      $('#qunit-fixture').html('')
20
21
22
    }
  })

23
  QUnit.test('should provide no conflict', function (assert) {
24
    assert.expect(1)
XhmikosR's avatar
XhmikosR committed
25
    assert.strictEqual(typeof $.fn.scrollspy, 'undefined', 'scrollspy was set back to undefined (org value)')
26
27
  })

28
29
  QUnit.test('should throw explicit error on undefined method', function (assert) {
    assert.expect(1)
30
    var $el = $('<div/>').appendTo('#qunit-fixture')
31
32
33
    $el.bootstrapScrollspy()
    try {
      $el.bootstrapScrollspy('noMethod')
XhmikosR's avatar
XhmikosR committed
34
35
    } catch (error) {
      assert.strictEqual(error.message, 'No method named "noMethod"')
36
37
38
    }
  })

39
  QUnit.test('should return jquery collection containing the element', function (assert) {
40
    assert.expect(2)
41
    var $el = $('<div/>').appendTo('#qunit-fixture')
42
    var $scrollspy = $el.bootstrapScrollspy()
XhmikosR's avatar
XhmikosR committed
43
    assert.true($scrollspy instanceof $, 'returns jquery collection')
44
    assert.strictEqual($scrollspy[0], $el[0], 'collection contains element')
XhmikosR's avatar
XhmikosR committed
45
  })
Jacob Thornton's avatar
Jacob Thornton committed
46

47
  QUnit.test('should only switch "active" class on current target', function (assert) {
48
    assert.expect(1)
49
    var done = assert.async()
50

XhmikosR's avatar
XhmikosR committed
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
    var sectionHTML = '<div id="root" class="active">' +
        '<div class="topbar">' +
        '<div class="topbar-inner">' +
        '<div class="container" id="ss-target">' +
        '<ul class="nav">' +
        '<li class="nav-item"><a href="#masthead">Overview</a></li>' +
        '<li class="nav-item"><a href="#detail">Detail</a></li>' +
        '</ul>' +
        '</div>' +
        '</div>' +
        '</div>' +
        '<div id="scrollspy-example" style="height: 100px; overflow: auto;">' +
        '<div style="height: 200px;">' +
        '<h4 id="masthead">Overview</h4>' +
        '<p style="height: 200px">' +
        'Ad leggings keytar, brunch id art party dolor labore.' +
        '</p>' +
        '</div>' +
        '<div style="height: 200px;">' +
        '<h4 id="detail">Detail</h4>' +
        '<p style="height: 200px">' +
        'Veniam marfa mustache skateboard, adipisicing fugiat velit pitchfork beard.' +
        '</p>' +
        '</div>' +
        '</div>' +
        '</div>'
XhmikosR's avatar
XhmikosR committed
77
    var $section = $(sectionHTML).appendTo('#qunit-fixture')
78
79

    var $scrollspy = $section
80
81
      .show()
      .find('#scrollspy-example')
XhmikosR's avatar
XhmikosR committed
82
83
84
      .bootstrapScrollspy({
        target: '#ss-target'
      })
85

86
    $scrollspy.one('scroll', function () {
XhmikosR's avatar
XhmikosR committed
87
      assert.true($section.hasClass('active'), '"active" class still on root node')
88
      done()
89
    })
90
91

    $scrollspy.scrollTop(350)
92
93
  })

fat's avatar
fat committed
94
95
96
97
  QUnit.test('should only switch "active" class on current target specified w element', function (assert) {
    assert.expect(1)
    var done = assert.async()

XhmikosR's avatar
XhmikosR committed
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
    var sectionHTML = '<div id="root" class="active">' +
        '<div class="topbar">' +
        '<div class="topbar-inner">' +
        '<div class="container" id="ss-target">' +
        '<ul class="nav">' +
        '<li class="nav-item"><a href="#masthead">Overview</a></li>' +
        '<li class="nav-item"><a href="#detail">Detail</a></li>' +
        '</ul>' +
        '</div>' +
        '</div>' +
        '</div>' +
        '<div id="scrollspy-example" style="height: 100px; overflow: auto;">' +
        '<div style="height: 200px;">' +
        '<h4 id="masthead">Overview</h4>' +
        '<p style="height: 200px">' +
        'Ad leggings keytar, brunch id art party dolor labore.' +
        '</p>' +
        '</div>' +
        '<div style="height: 200px;">' +
        '<h4 id="detail">Detail</h4>' +
        '<p style="height: 200px">' +
        'Veniam marfa mustache skateboard, adipisicing fugiat velit pitchfork beard.' +
        '</p>' +
        '</div>' +
        '</div>' +
        '</div>'
fat's avatar
fat committed
124
125
126
127
128
    var $section = $(sectionHTML).appendTo('#qunit-fixture')

    var $scrollspy = $section
      .show()
      .find('#scrollspy-example')
XhmikosR's avatar
XhmikosR committed
129
      .bootstrapScrollspy({
130
        target: document.getElementById('ss-target')
XhmikosR's avatar
XhmikosR committed
131
      })
fat's avatar
fat committed
132

133
    $scrollspy.one('scroll', function () {
XhmikosR's avatar
XhmikosR committed
134
      assert.true($section.hasClass('active'), '"active" class still on root node')
fat's avatar
fat committed
135
136
137
138
139
140
      done()
    })

    $scrollspy.scrollTop(350)
  })

XhmikosR's avatar
XhmikosR committed
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
  // This could be simplified/improved later
  QUnit.test('should only switch "active" class on current target specified w jQuery element', function (assert) {
    assert.expect(1)
    var done = assert.async()

    var sectionHTML = '<div id="root" class="active">' +
        '<div class="topbar">' +
        '<div class="topbar-inner">' +
        '<div class="container" id="ss-target">' +
        '<ul class="nav">' +
        '<li class="nav-item"><a href="#masthead">Overview</a></li>' +
        '<li class="nav-item"><a href="#detail">Detail</a></li>' +
        '</ul>' +
        '</div>' +
        '</div>' +
        '</div>' +
        '<div id="scrollspy-example" style="height: 100px; overflow: auto;">' +
        '<div style="height: 200px;">' +
        '<h4 id="masthead">Overview</h4>' +
        '<p style="height: 200px">' +
        'Ad leggings keytar, brunch id art party dolor labore.' +
        '</p>' +
        '</div>' +
        '<div style="height: 200px;">' +
        '<h4 id="detail">Detail</h4>' +
        '<p style="height: 200px">' +
        'Veniam marfa mustache skateboard, adipisicing fugiat velit pitchfork beard.' +
        '</p>' +
        '</div>' +
        '</div>' +
        '</div>'
    var $section = $(sectionHTML).appendTo('#qunit-fixture')

    var $scrollspy = $section
      .show()
      .find('#scrollspy-example')
      .bootstrapScrollspy({
        target: $('#ss-target')
      })

    $scrollspy.one('scroll', function () {
XhmikosR's avatar
XhmikosR committed
182
      assert.true($section.hasClass('active'), '"active" class still on root node')
XhmikosR's avatar
XhmikosR committed
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
      done()
    })

    $scrollspy.scrollTop(350)
  })

  // This could be simplified/improved later
  QUnit.test('should only switch "active" class on current target specified without ID', function (assert) {
    assert.expect(2)
    var done = assert.async()

    var sectionHTML = '<div id="root" class="active">' +
        '<div class="topbar">' +
        '<div class="topbar-inner">' +
        '<div class="container">' +
        '<ul class="nav">' +
        '<li class="nav-item"><a href="#masthead">Overview</a></li>' +
        '<li class="nav-item"><a href="#detail">Detail</a></li>' +
        '</ul>' +
        '</div>' +
        '</div>' +
        '</div>' +
        '<div id="scrollspy-example" style="height: 100px; overflow: auto;">' +
        '<div style="height: 200px;">' +
        '<h4 id="masthead">Overview</h4>' +
        '<p style="height: 200px">' +
        'Ad leggings keytar, brunch id art party dolor labore.' +
        '</p>' +
        '</div>' +
        '<div style="height: 200px;">' +
        '<h4 id="detail">Detail</h4>' +
        '<p style="height: 200px">' +
        'Veniam marfa mustache skateboard, adipisicing fugiat velit pitchfork beard.' +
        '</p>' +
        '</div>' +
        '</div>' +
        '</div>'
    var $section = $(sectionHTML).appendTo('#qunit-fixture')

    var $scrollspy = $section
      .show()
      .find('#scrollspy-example')
      .bootstrapScrollspy({
        target: $('.container')
      })

XhmikosR's avatar
XhmikosR committed
229
    assert.notStrictEqual($('.container').attr('id').length, 0, '`target` has an ID attribute')
XhmikosR's avatar
XhmikosR committed
230
231

    $scrollspy.one('scroll', function () {
XhmikosR's avatar
XhmikosR committed
232
      assert.true($section.hasClass('active'), '"active" class still on root node')
XhmikosR's avatar
XhmikosR committed
233
234
235
236
237
238
      done()
    })

    $scrollspy.scrollTop(350)
  })

239
  QUnit.test('should correctly select middle navigation option when large offset is used', function (assert) {
240
    assert.expect(3)
241
    var done = assert.async()
242

XhmikosR's avatar
XhmikosR committed
243
244
245
246
247
248
249
250
251
252
253
254
255
    var sectionHTML = '<div id="header" style="height: 500px;"></div>' +
        '<nav id="navigation" class="navbar">' +
        '<ul class="navbar-nav">' +
        '<li class="nav-item active"><a class="nav-link" id="one-link" href="#one">One</a></li>' +
        '<li class="nav-item"><a class="nav-link" id="two-link" href="#two">Two</a></li>' +
        '<li class="nav-item"><a class="nav-link" id="three-link" href="#three">Three</a></li>' +
        '</ul>' +
        '</nav>' +
        '<div id="content" style="height: 200px; overflow-y: auto;">' +
        '<div id="one" style="height: 500px;"></div>' +
        '<div id="two" style="height: 300px;"></div>' +
        '<div id="three" style="height: 10px;"></div>' +
        '</div>'
XhmikosR's avatar
XhmikosR committed
256
    var $section = $(sectionHTML).appendTo('#qunit-fixture')
257
    var $scrollspy = $section
258
259
      .show()
      .filter('#content')
260

XhmikosR's avatar
XhmikosR committed
261
262
263
264
    $scrollspy.bootstrapScrollspy({
      target: '#navigation',
      offset: $scrollspy.position().top
    })
265

266
    $scrollspy.one('scroll', function () {
XhmikosR's avatar
XhmikosR committed
267
268
269
      assert.false($section.find('#one-link').hasClass('active'), '"active" class removed from first section')
      assert.true($section.find('#two-link').hasClass('active'), '"active" class on middle section')
      assert.false($section.find('#three-link').hasClass('active'), '"active" class not on last section')
270
      done()
271
    })
272
273

    $scrollspy.scrollTop(550)
Julian Thilo's avatar
Julian Thilo committed
274
  })
275

276
  QUnit.test('should add the active class to the correct element', function (assert) {
277
    assert.expect(2)
278
    var navbarHtml =
XhmikosR's avatar
XhmikosR committed
279
280
281
282
283
284
      '<nav class="navbar">' +
      '<ul class="nav">' +
      '<li class="nav-item"><a class="nav-link" id="a-1" href="#div-1">div 1</a></li>' +
      '<li class="nav-item"><a class="nav-link" id="a-2" href="#div-2">div 2</a></li>' +
      '</ul>' +
      '</nav>'
285
    var contentHtml =
XhmikosR's avatar
XhmikosR committed
286
287
288
289
        '<div class="content" style="overflow: auto; height: 50px">' +
      '<div id="div-1" style="height: 100px; padding: 0; margin: 0">div 1</div>' +
      '<div id="div-2" style="height: 200px; padding: 0; margin: 0">div 2</div>' +
      '</div>'
290
291
292
293

    $(navbarHtml).appendTo('#qunit-fixture')
    var $content = $(contentHtml)
      .appendTo('#qunit-fixture')
XhmikosR's avatar
XhmikosR committed
294
295
296
297
      .bootstrapScrollspy({
        offset: 0,
        target: '.navbar'
      })
298

299
    var done = assert.async()
300
301
    var testElementIsActiveAfterScroll = function (element, target) {
      var deferred = $.Deferred()
Johann-S's avatar
Johann-S committed
302
303
304
      // add top padding to fix Chrome on Android failures
      var paddingTop = 5
      var scrollHeight = Math.ceil($content.scrollTop() + $(target).position().top) + paddingTop
305
      $content.one('scroll', function () {
XhmikosR's avatar
XhmikosR committed
306
        assert.true($(element).hasClass('active'), 'target:' + target + ', element' + element)
307
308
309
310
311
312
        deferred.resolve()
      })
      $content.scrollTop(scrollHeight)
      return deferred.promise()
    }

313
    $.when(testElementIsActiveAfterScroll('#a-1', '#div-1'))
XhmikosR's avatar
XhmikosR committed
314
315
316
317
318
319
      .then(function () {
        return testElementIsActiveAfterScroll('#a-2', '#div-2')
      })
      .then(function () {
        done()
      })
320
321
  })

322
323
324
  QUnit.test('should add the active class to the correct element (nav markup)', function (assert) {
    assert.expect(2)
    var navbarHtml =
XhmikosR's avatar
XhmikosR committed
325
326
327
328
329
330
      '<nav class="navbar">' +
      '<nav class="nav">' +
      '<a class="nav-link" id="a-1" href="#div-1">div 1</a>' +
      '<a class="nav-link" id="a-2" href="#div-2">div 2</a>' +
      '</nav>' +
      '</nav>'
331
    var contentHtml =
XhmikosR's avatar
XhmikosR committed
332
333
334
335
        '<div class="content" style="overflow: auto; height: 50px">' +
      '<div id="div-1" style="height: 100px; padding: 0; margin: 0">div 1</div>' +
      '<div id="div-2" style="height: 200px; padding: 0; margin: 0">div 2</div>' +
      '</div>'
336
337
338
339

    $(navbarHtml).appendTo('#qunit-fixture')
    var $content = $(contentHtml)
      .appendTo('#qunit-fixture')
XhmikosR's avatar
XhmikosR committed
340
341
342
343
      .bootstrapScrollspy({
        offset: 0,
        target: '.navbar'
      })
344
345
346
347

    var done = assert.async()
    var testElementIsActiveAfterScroll = function (element, target) {
      var deferred = $.Deferred()
Johann-S's avatar
Johann-S committed
348
349
350
      // add top padding to fix Chrome on Android failures
      var paddingTop = 5
      var scrollHeight = Math.ceil($content.scrollTop() + $(target).position().top) + paddingTop
351
      $content.one('scroll', function () {
XhmikosR's avatar
XhmikosR committed
352
        assert.true($(element).hasClass('active'), 'target:' + target + ', element' + element)
353
354
355
356
357
358
359
        deferred.resolve()
      })
      $content.scrollTop(scrollHeight)
      return deferred.promise()
    }

    $.when(testElementIsActiveAfterScroll('#a-1', '#div-1'))
XhmikosR's avatar
XhmikosR committed
360
361
362
363
364
365
      .then(function () {
        return testElementIsActiveAfterScroll('#a-2', '#div-2')
      })
      .then(function () {
        done()
      })
366
367
368
369
370
  })

  QUnit.test('should add the active class to the correct element (list-group markup)', function (assert) {
    assert.expect(2)
    var navbarHtml =
XhmikosR's avatar
XhmikosR committed
371
372
373
374
375
376
      '<nav class="navbar">' +
      '<div class="list-group">' +
      '<a class="list-group-item" id="a-1" href="#div-1">div 1</a>' +
      '<a class="list-group-item" id="a-2" href="#div-2">div 2</a>' +
      '</div>' +
      '</nav>'
377
    var contentHtml =
XhmikosR's avatar
XhmikosR committed
378
379
380
381
        '<div class="content" style="overflow: auto; height: 50px">' +
      '<div id="div-1" style="height: 100px; padding: 0; margin: 0">div 1</div>' +
      '<div id="div-2" style="height: 200px; padding: 0; margin: 0">div 2</div>' +
      '</div>'
382
383
384
385

    $(navbarHtml).appendTo('#qunit-fixture')
    var $content = $(contentHtml)
      .appendTo('#qunit-fixture')
XhmikosR's avatar
XhmikosR committed
386
387
388
389
      .bootstrapScrollspy({
        offset: 0,
        target: '.navbar'
      })
390
391
392
393

    var done = assert.async()
    var testElementIsActiveAfterScroll = function (element, target) {
      var deferred = $.Deferred()
Johann-S's avatar
Johann-S committed
394
395
396
      // add top padding to fix Chrome on Android failures
      var paddingTop = 5
      var scrollHeight = Math.ceil($content.scrollTop() + $(target).position().top) + paddingTop
397
      $content.one('scroll', function () {
XhmikosR's avatar
XhmikosR committed
398
        assert.true($(element).hasClass('active'), 'target:' + target + ', element' + element)
399
400
401
402
403
404
405
        deferred.resolve()
      })
      $content.scrollTop(scrollHeight)
      return deferred.promise()
    }

    $.when(testElementIsActiveAfterScroll('#a-1', '#div-1'))
XhmikosR's avatar
XhmikosR committed
406
407
408
409
410
411
      .then(function () {
        return testElementIsActiveAfterScroll('#a-2', '#div-2')
      })
      .then(function () {
        done()
      })
412
413
  })

414
  QUnit.test('should add the active class correctly when there are nested elements at 0 scroll offset', function (assert) {
415
    assert.expect(6)
416
417
    var times = 0
    var done = assert.async()
XhmikosR's avatar
XhmikosR committed
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
    var navbarHtml = '<nav id="navigation" class="navbar">' +
      '<ul class="nav">' +
      '<li class="nav-item"><a id="a-1" class="nav-link" href="#div-1">div 1</a>' +
      '<ul class="nav">' +
      '<li class="nav-item"><a id="a-2" class="nav-link" href="#div-2">div 2</a></li>' +
      '</ul>' +
      '</li>' +
      '</ul>' +
      '</nav>'

    var contentHtml = '<div class="content" style="position: absolute; top: 0px; overflow: auto; height: 50px">' +
      '<div id="div-1" style="padding: 0; margin: 0">' +
      '<div id="div-2" style="height: 200px; padding: 0; margin: 0">div 2</div>' +
      '</div>' +
      '</div>'
433
434
435
436
437

    $(navbarHtml).appendTo('#qunit-fixture')

    var $content = $(contentHtml)
      .appendTo('#qunit-fixture')
XhmikosR's avatar
XhmikosR committed
438
439
440
441
      .bootstrapScrollspy({
        offset: 0,
        target: '#navigation'
      })
442

443
    function testActiveElements() {
XhmikosR's avatar
XhmikosR committed
444
445
446
      if (++times > 3) {
        return done()
      }
447
448

      $content.one('scroll', function () {
XhmikosR's avatar
XhmikosR committed
449
450
        assert.true($('#a-1').hasClass('active'), 'nav item for outer element has "active" class')
        assert.true($('#a-2').hasClass('active'), 'nav item for inner element has "active" class')
451
452
453
454
455
456
457
458
459
460
461
462
463
        testActiveElements()
      })

      $content.scrollTop($content.scrollTop() + 10)
    }

    testActiveElements()
  })

  QUnit.test('should add the active class correctly when there are nested elements (nav markup)', function (assert) {
    assert.expect(6)
    var times = 0
    var done = assert.async()
XhmikosR's avatar
XhmikosR committed
464
465
466
467
468
469
470
471
472
473
474
475
476
477
    var navbarHtml = '<nav id="navigation" class="navbar">' +
      '<nav class="nav">' +
      '<a id="a-1" class="nav-link" href="#div-1">div 1</a>' +
      '<nav class="nav">' +
      '<a id="a-2" class="nav-link" href="#div-2">div 2</a>' +
      '</nav>' +
      '</nav>' +
      '</nav>'

    var contentHtml = '<div class="content" style="position: absolute; top: 0px; overflow: auto; height: 50px">' +
      '<div id="div-1" style="padding: 0; margin: 0">' +
      '<div id="div-2" style="height: 200px; padding: 0; margin: 0">div 2</div>' +
      '</div>' +
      '</div>'
478
479
480
481
482

    $(navbarHtml).appendTo('#qunit-fixture')

    var $content = $(contentHtml)
      .appendTo('#qunit-fixture')
XhmikosR's avatar
XhmikosR committed
483
484
485
486
      .bootstrapScrollspy({
        offset: 0,
        target: '#navigation'
      })
487
488

    function testActiveElements() {
XhmikosR's avatar
XhmikosR committed
489
490
491
      if (++times > 3) {
        return done()
      }
492
493

      $content.one('scroll', function () {
XhmikosR's avatar
XhmikosR committed
494
495
        assert.true($('#a-1').hasClass('active'), 'nav item for outer element has "active" class')
        assert.true($('#a-2').hasClass('active'), 'nav item for inner element has "active" class')
496
497
498
499
500
501
502
503
504
505
506
507
508
        testActiveElements()
      })

      $content.scrollTop($content.scrollTop() + 10)
    }

    testActiveElements()
  })

  QUnit.test('should add the active class correctly when there are nested elements (nav nav-item markup)', function (assert) {
    assert.expect(6)
    var times = 0
    var done = assert.async()
XhmikosR's avatar
XhmikosR committed
509
510
511
512
513
514
515
516
517
518
519
520
521
522
    var navbarHtml = '<nav id="navigation" class="navbar">' +
      '<ul class="nav">' +
      '<li class="nav-item"><a id="a-1" class="nav-link" href="#div-1">div 1</a></li>' +
      '<ul class="nav">' +
      '<li class="nav-item"><a id="a-2" class="nav-link" href="#div-2">div 2</a></li>' +
      '</ul>' +
      '</ul>' +
      '</nav>'

    var contentHtml = '<div class="content" style="position: absolute; top: 0px; overflow: auto; height: 50px">' +
      '<div id="div-1" style="padding: 0; margin: 0">' +
      '<div id="div-2" style="height: 200px; padding: 0; margin: 0">div 2</div>' +
      '</div>' +
      '</div>'
523
524
525
526
527

    $(navbarHtml).appendTo('#qunit-fixture')

    var $content = $(contentHtml)
      .appendTo('#qunit-fixture')
XhmikosR's avatar
XhmikosR committed
528
529
530
531
      .bootstrapScrollspy({
        offset: 0,
        target: '#navigation'
      })
532
533

    function testActiveElements() {
XhmikosR's avatar
XhmikosR committed
534
535
536
      if (++times > 3) {
        return done()
      }
537
538

      $content.one('scroll', function () {
XhmikosR's avatar
XhmikosR committed
539
540
        assert.true($('#a-1').hasClass('active'), 'nav item for outer element has "active" class')
        assert.true($('#a-2').hasClass('active'), 'nav item for inner element has "active" class')
541
542
543
544
545
546
547
548
549
550
551
552
553
        testActiveElements()
      })

      $content.scrollTop($content.scrollTop() + 10)
    }

    testActiveElements()
  })

  QUnit.test('should add the active class correctly when there are nested elements (list-group markup)', function (assert) {
    assert.expect(6)
    var times = 0
    var done = assert.async()
XhmikosR's avatar
XhmikosR committed
554
555
556
557
558
559
560
561
562
563
564
565
566
567
    var navbarHtml = '<nav id="navigation" class="navbar">' +
      '<div class="list-group">' +
      '<a id="a-1" class="list-group-item" href="#div-1">div 1</a>' +
      '<div class="list-group">' +
      '<a id="a-2" class="list-group-item" href="#div-2">div 2</a>' +
      '</div>' +
      '</div>' +
      '</nav>'

    var contentHtml = '<div class="content" style="position: absolute; top: 0px; overflow: auto; height: 50px">' +
      '<div id="div-1" style="padding: 0; margin: 0">' +
      '<div id="div-2" style="height: 200px; padding: 0; margin: 0">div 2</div>' +
      '</div>' +
      '</div>'
568
569
570
571
572

    $(navbarHtml).appendTo('#qunit-fixture')

    var $content = $(contentHtml)
      .appendTo('#qunit-fixture')
XhmikosR's avatar
XhmikosR committed
573
574
575
576
      .bootstrapScrollspy({
        offset: 0,
        target: '#navigation'
      })
577
578

    function testActiveElements() {
XhmikosR's avatar
XhmikosR committed
579
580
581
      if (++times > 3) {
        return done()
      }
582
583

      $content.one('scroll', function () {
XhmikosR's avatar
XhmikosR committed
584
585
        assert.true($('#a-1').hasClass('active'), 'nav item for outer element has "active" class')
        assert.true($('#a-2').hasClass('active'), 'nav item for inner element has "active" class')
586
587
588
589
        testActiveElements()
      })

      $content.scrollTop($content.scrollTop() + 10)
590
591
592
    }

    testActiveElements()
593
594
  })

595
  QUnit.test('should clear selection if above the first section', function (assert) {
596
    assert.expect(3)
597
    var done = assert.async()
598

XhmikosR's avatar
XhmikosR committed
599
600
601
602
603
604
605
606
    var sectionHTML = '<div id="header" style="height: 500px;"></div>' +
        '<nav id="navigation" class="navbar">' +
        '<ul class="navbar-nav">' +
        '<li class="nav-item"><a id="one-link"   class="nav-link active" href="#one">One</a></li>' +
        '<li class="nav-item"><a id="two-link"   class="nav-link" href="#two">Two</a></li>' +
        '<li class="nav-item"><a id="three-link" class="nav-link" href="#three">Three</a></li>' +
        '</ul>' +
        '</nav>'
607
    $(sectionHTML).appendTo('#qunit-fixture')
608

XhmikosR's avatar
XhmikosR committed
609
    var scrollspyHTML = '<div id="content" style="height: 200px; overflow-y: auto;">' +
XhmikosR's avatar
XhmikosR committed
610
611
612
613
614
        '<div id="spacer" style="height: 100px;"></div>' +
        '<div id="one" style="height: 100px;"></div>' +
        '<div id="two" style="height: 100px;"></div>' +
        '<div id="three" style="height: 100px;"></div>' +
        '<div id="spacer" style="height: 100px;"></div>' +
XhmikosR's avatar
XhmikosR committed
615
        '</div>'
616
617
618
619
620
621
622
    var $scrollspy = $(scrollspyHTML).appendTo('#qunit-fixture')

    $scrollspy
      .bootstrapScrollspy({
        target: '#navigation',
        offset: $scrollspy.position().top
      })
623
      .one('scroll', function () {
624
        assert.strictEqual($('.active').length, 1, '"active" class on only one element present')
XhmikosR's avatar
XhmikosR committed
625
        assert.true($('.active').is('#two-link'), '"active" class on second section')
626
        $scrollspy
627
          .one('scroll', function () {
628
            assert.strictEqual($('.active').length, 0, 'selection cleared')
629
            done()
630
631
632
633
634
635
          })
          .scrollTop(0)
      })
      .scrollTop(201)
  })

636
637
638
639
  QUnit.test('should NOT clear selection if above the first section and first section is at the top', function (assert) {
    assert.expect(4)
    var done = assert.async()

XhmikosR's avatar
XhmikosR committed
640
641
642
643
644
645
646
647
    var sectionHTML = '<div id="header" style="height: 500px;"></div>' +
        '<nav id="navigation" class="navbar">' +
        '<ul class="navbar-nav">' +
        '<li class="nav-item"><a id="one-link"   class="nav-link active" href="#one">One</a></li>' +
        '<li class="nav-item"><a id="two-link"   class="nav-link" href="#two">Two</a></li>' +
        '<li class="nav-item"><a id="three-link" class="nav-link" href="#three">Three</a></li>' +
        '</ul>' +
        '</nav>'
648
649
650
651
652
    $(sectionHTML).appendTo('#qunit-fixture')

    var negativeHeight = -10
    var startOfSectionTwo = 101

XhmikosR's avatar
XhmikosR committed
653
    var scrollspyHTML = '<div id="content" style="height: 200px; overflow-y: auto;">' +
XhmikosR's avatar
XhmikosR committed
654
655
656
657
        '<div id="one" style="height: 100px;"></div>' +
        '<div id="two" style="height: 100px;"></div>' +
        '<div id="three" style="height: 100px;"></div>' +
        '<div id="spacer" style="height: 100px;"></div>' +
XhmikosR's avatar
XhmikosR committed
658
        '</div>'
659
660
661
662
663
664
665
666
667
    var $scrollspy = $(scrollspyHTML).appendTo('#qunit-fixture')

    $scrollspy
      .bootstrapScrollspy({
        target: '#navigation',
        offset: $scrollspy.position().top
      })
      .one('scroll', function () {
        assert.strictEqual($('.active').length, 1, '"active" class on only one element present')
XhmikosR's avatar
XhmikosR committed
668
        assert.true($('.active').is('#two-link'), '"active" class on second section')
669
670
671
        $scrollspy
          .one('scroll', function () {
            assert.strictEqual($('.active').length, 1, '"active" class on only one element present')
XhmikosR's avatar
XhmikosR committed
672
            assert.true($('.active').is('#one-link'), '"active" class on first section')
673
674
675
676
677
678
679
            done()
          })
          .scrollTop(negativeHeight)
      })
      .scrollTop(startOfSectionTwo)
  })

680
681
682
  QUnit.test('should correctly select navigation element on backward scrolling when each target section height is 100%', function (assert) {
    assert.expect(5)
    var navbarHtml =
XhmikosR's avatar
XhmikosR committed
683
684
685
686
687
688
689
690
691
      '<nav class="navbar">' +
      '<ul class="nav">' +
      '<li class="nav-item"><a id="li-100-1" class="nav-link" href="#div-100-1">div 1</a></li>' +
      '<li class="nav-item"><a id="li-100-2" class="nav-link" href="#div-100-2">div 2</a></li>' +
      '<li class="nav-item"><a id="li-100-3" class="nav-link" href="#div-100-3">div 3</a></li>' +
      '<li class="nav-item"><a id="li-100-4" class="nav-link" href="#div-100-4">div 4</a></li>' +
      '<li class="nav-item"><a id="li-100-5" class="nav-link" href="#div-100-5">div 5</a></li>' +
      '</ul>' +
      '</nav>'
692
    var contentHtml =
XhmikosR's avatar
XhmikosR committed
693
694
695
696
697
698
699
        '<div class="content" style="position: relative; overflow: auto; height: 100px">' +
      '<div id="div-100-1" style="position: relative; height: 100%; padding: 0; margin: 0">div 1</div>' +
      '<div id="div-100-2" style="position: relative; height: 100%; padding: 0; margin: 0">div 2</div>' +
      '<div id="div-100-3" style="position: relative; height: 100%; padding: 0; margin: 0">div 3</div>' +
      '<div id="div-100-4" style="position: relative; height: 100%; padding: 0; margin: 0">div 4</div>' +
      '<div id="div-100-5" style="position: relative; height: 100%; padding: 0; margin: 0">div 5</div>' +
      '</div>'
700
701
702
703

    $(navbarHtml).appendTo('#qunit-fixture')
    var $content = $(contentHtml)
      .appendTo('#qunit-fixture')
XhmikosR's avatar
XhmikosR committed
704
705
706
707
      .bootstrapScrollspy({
        offset: 0,
        target: '.navbar'
      })
708
709
710

    var testElementIsActiveAfterScroll = function (element, target) {
      var deferred = $.Deferred()
Johann-S's avatar
Johann-S committed
711
712
713
      // add top padding to fix Chrome on Android failures
      var paddingTop = 5
      var scrollHeight = Math.ceil($content.scrollTop() + $(target).position().top) + paddingTop
714
      $content.one('scroll', function () {
XhmikosR's avatar
XhmikosR committed
715
        assert.true($(element).hasClass('active'), 'target:' + target + ', element: ' + element)
716
717
718
719
720
721
        deferred.resolve()
      })
      $content.scrollTop(scrollHeight)
      return deferred.promise()
    }

722
    var done = assert.async()
723
    $.when(testElementIsActiveAfterScroll('#li-100-5', '#div-100-5'))
XhmikosR's avatar
XhmikosR committed
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
      .then(function () {
        return testElementIsActiveAfterScroll('#li-100-4', '#div-100-4')
      })
      .then(function () {
        return testElementIsActiveAfterScroll('#li-100-3', '#div-100-3')
      })
      .then(function () {
        return testElementIsActiveAfterScroll('#li-100-2', '#div-100-2')
      })
      .then(function () {
        return testElementIsActiveAfterScroll('#li-100-1', '#div-100-1')
      })
      .then(function () {
        done()
      })
739
740
  })

741
  QUnit.test('should allow passed in option offset method: offset', function (assert) {
fat's avatar
fat committed
742
    assert.expect(4)
743

fat's avatar
fat committed
744
    var testOffsetMethod = function (type) {
745
      var $navbar = $(
XhmikosR's avatar
XhmikosR committed
746
747
748
749
750
751
752
        '<nav class="navbar"' + (type === 'data' ? ' id="navbar-offset-method-menu"' : '') + '>' +
        '<ul class="nav">' +
        '<li class="nav-item"><a id="li-' + type + 'm-1" class="nav-link" href="#div-' + type + 'm-1">div 1</a></li>' +
        '<li class="nav-item"><a id="li-' + type + 'm-2" class="nav-link" href="#div-' + type + 'm-2">div 2</a></li>' +
        '<li class="nav-item"><a id="li-' + type + 'm-3" class="nav-link" href="#div-' + type + 'm-3">div 3</a></li>' +
        '</ul>' +
        '</nav>'
753
754
      )
      var $content = $(
XhmikosR's avatar
XhmikosR committed
755
756
757
758
759
        '<div class="content"' + (type === 'data' ? ' data-spy="scroll" data-target="#navbar-offset-method-menu" data-offset="0" data-method="offset"' : '') + ' style="position: relative; overflow: auto; height: 100px">' +
        '<div id="div-' + type + 'm-1" style="position: relative; height: 200px; padding: 0; margin: 0">div 1</div>' +
        '<div id="div-' + type + 'm-2" style="position: relative; height: 150px; padding: 0; margin: 0">div 2</div>' +
        '<div id="div-' + type + 'm-3" style="position: relative; height: 250px; padding: 0; margin: 0">div 3</div>' +
        '</div>'
760
      )
761

762
763
      $navbar.appendTo('#qunit-fixture')
      $content.appendTo('#qunit-fixture')
764

765
      if (type === 'js') {
XhmikosR's avatar
XhmikosR committed
766
767
768
769
770
771
        $content.bootstrapScrollspy({
          target: '.navbar',
          offset: 0,
          method: 'offset'
        })
      } else if (type === 'data') {
772
773
        $(window).trigger('load')
      }
774

fat's avatar
fat committed
775
776
      var $target = $('#div-' + type + 'm-2')
      var scrollspy = $content.data('bs.scrollspy')
777

XhmikosR's avatar
XhmikosR committed
778
779
      assert.strictEqual(scrollspy._offsets[1], $target.offset().top, 'offset method with ' + type + ' option')
      assert.notStrictEqual(scrollspy._offsets[1], $target.position().top, 'position method with ' + type + ' option')
780
781
      $navbar.remove()
      $content.remove()
fat's avatar
fat committed
782
    }
783

784
785
    testOffsetMethod('js')
    testOffsetMethod('data')
fat's avatar
fat committed
786
  })
787

fat's avatar
fat committed
788
789
  QUnit.test('should allow passed in option offset method: position', function (assert) {
    assert.expect(4)
790

fat's avatar
fat committed
791
    var testOffsetMethod = function (type) {
792
      var $navbar = $(
XhmikosR's avatar
XhmikosR committed
793
794
795
796
797
798
799
        '<nav class="navbar"' + (type === 'data' ? ' id="navbar-offset-method-menu"' : '') + '>' +
        '<ul class="nav">' +
        '<li class="nav-item"><a class="nav-link" id="li-' + type + 'm-1" href="#div-' + type + 'm-1">div 1</a></li>' +
        '<li class="nav-item"><a class="nav-link" id="li-' + type + 'm-2" href="#div-' + type + 'm-2">div 2</a></li>' +
        '<li class="nav-item"><a class="nav-link" id="li-' + type + 'm-3" href="#div-' + type + 'm-3">div 3</a></li>' +
        '</ul>' +
        '</nav>'
800
801
      )
      var $content = $(
XhmikosR's avatar
XhmikosR committed
802
803
804
805
806
        '<div class="content"' + (type === 'data' ? ' data-spy="scroll" data-target="#navbar-offset-method-menu" data-offset="0" data-method="position"' : '') + ' style="position: relative; overflow: auto; height: 100px">' +
        '<div id="div-' + type + 'm-1" style="position: relative; height: 200px; padding: 0; margin: 0">div 1</div>' +
        '<div id="div-' + type + 'm-2" style="position: relative; height: 150px; padding: 0; margin: 0">div 2</div>' +
        '<div id="div-' + type + 'm-3" style="position: relative; height: 250px; padding: 0; margin: 0">div 3</div>' +
        '</div>'
807
      )
808

809
810
      $navbar.appendTo('#qunit-fixture')
      $content.appendTo('#qunit-fixture')
fat's avatar
fat committed
811

XhmikosR's avatar
XhmikosR committed
812
813
814
815
816
817
818
819
820
      if (type === 'js') {
        $content.bootstrapScrollspy({
          target: '.navbar',
          offset: 0,
          method: 'position'
        })
      } else if (type === 'data') {
        $(window).trigger('load')
      }
fat's avatar
fat committed
821
822
823
824

      var $target = $('#div-' + type + 'm-2')
      var scrollspy = $content.data('bs.scrollspy')

XhmikosR's avatar
XhmikosR committed
825
826
      assert.notStrictEqual(scrollspy._offsets[1], $target.offset().top, 'offset method with ' + type + ' option')
      assert.strictEqual(scrollspy._offsets[1], $target.position().top, 'position method with ' + type + ' option')
827
828
      $navbar.remove()
      $content.remove()
fat's avatar
fat committed
829
830
    }

831
832
    testOffsetMethod('js')
    testOffsetMethod('data')
fat's avatar
fat committed
833
  })
XhmikosR's avatar
XhmikosR committed
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872

  // This could be simplified/improved later
  QUnit.test('should raise exception to avoid xss on target', function (assert) {
    assert.expect(1)
    assert.throws(function () {
      var templateHTML = '<div id="root" class="active">' +
        '<div class="topbar">' +
        '<div class="topbar-inner">' +
        '<div class="container" id="ss-target">' +
        '<ul class="nav">' +
        '<li class="nav-item"><a href="#masthead">Overview</a></li>' +
        '<li class="nav-item"><a href="#detail">Detail</a></li>' +
        '</ul>' +
        '</div>' +
        '</div>' +
        '</div>' +
        '<div id="scrollspy-example" style="height: 100px; overflow: auto;">' +
        '<div style="height: 200px;">' +
        '<h4 id="masthead">Overview</h4>' +
        '<p style="height: 200px">' +
        'Ad leggings keytar, brunch id art party dolor labore.' +
        '</p>' +
        '</div>' +
        '<div style="height: 200px;">' +
        '<h4 id="detail">Detail</h4>' +
        '<p style="height: 200px">' +
        'Veniam marfa mustache skateboard, adipisicing fugiat velit pitchfork beard.' +
        '</p>' +
        '</div>' +
        '</div>' +
        '</div>'

      $(templateHTML).appendTo(document.body)

      $('#ss-target').bootstrapScrollspy({
        target: '<img src=1 onerror=\'alert(0)\'>'
      })
    }, /SyntaxError/)
  })
873
})