Welcome to mirror list, hosted at ThFree Co, Russian Federation.

scrollspy.js « unit « tests « js - github.com/twbs/bootstrap.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9a00221fd8e34893c80da4f92c8cbb8f2ae594d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
$(function () {

  module('scrollspy plugin')

  test('should be defined on jquery object', function () {
    ok($(document.body).scrollspy, 'scrollspy method is defined')
  })

  module('scrollspy', {
    setup: function() {
      // 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()
    },
    teardown: function() {
      $.fn.scrollspy = $.fn.bootstrapScrollspy
      delete $.fn.bootstrapScrollspy
    }
  })

  test('should provide no conflict', function () {
    ok(!$.fn.scrollspy, 'scrollspy was set back to undefined (org value)')
  })

  test('should return element', function () {
    ok($(document.body).bootstrapScrollspy()[0] == document.body, 'document.body returned')
  })

  test('should switch active class on scroll', function () {
    var sectionHTML = '<div id="masthead"></div>',
        topbarHTML = '<div class="topbar">' +
        '<div class="topbar-inner">' +
        '<div class="container">' +
        '<h3><a href="#">Bootstrap</a></h3>' +
        '<li><a href="#masthead">Overview</a></li>' +
        '</ul>' +
        '</div>' +
        '</div>' +
        '</div>',
        $topbar = $(topbarHTML).bootstrapScrollspy()

    $(sectionHTML).append('#qunit-fixture')
    ok($topbar.find('.active', true))
  })

  asyncTest('should only switch active class on current target', function () {
    expect(1);
    var sectionHTML = '<div id="root" class="active">' +
        '<div class="topbar">' +
        '<div class="topbar-inner">' +
        '<div class="container" id="ss-target">' +
        '<ul class="nav">' +
        '<li><a href="#masthead">Overview</a></li>' +
        '<li><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>',
        $section = $(sectionHTML).appendTo('#qunit-fixture'),
        $scrollSpy = $section
        .show()
        .find('#scrollspy-example')
        .bootstrapScrollspy({target: '#ss-target'})

    $scrollSpy.on('scroll.bs.scrollspy', function () {
      ok($section.hasClass('active'), 'Active class still on root node')
      start()
    })
    $scrollSpy.scrollTop(350);
  })

  asyncTest('middle navigation option correctly selected when large offset is used', function () {
    expect(3);
    var sectionHTML = '<div id="header" style="height: 500px;"></div>' +
        '<nav id="navigation" class="navbar">' +
        '<ul class="nav navbar-nav">' +
        '<li class="active"><a id="one-link" href="#one">One</a></li>' +
        '<li><a id="two-link" href="#two">Two</a></li>' +
        '<li><a 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>',
        $section = $(sectionHTML).appendTo('#qunit-fixture'),
        $scrollSpy = $section
        .show()
        .filter('#content')
    $scrollSpy.bootstrapScrollspy({target: '#navigation', offset: $scrollSpy.position().top})

    $scrollSpy.on('scroll.bs.scrollspy', function () {
      ok(!$section.find('#one-link').parent().hasClass('active'), 'Active class removed from first section')
      ok($section.find('#two-link').parent().hasClass('active'), 'Active class on middle section')
      ok(!$section.find('#three-link').parent().hasClass('active'), 'Active class not on last section')
      start()
    })
    $scrollSpy.scrollTop(550);
  })
})