popover.js 6.22 KB
Newer Older
1
2
$(function () {

3
  module('popover plugin')
XhmikosR's avatar
XhmikosR committed
4
5
6
7
8
9

  test('should be defined on jquery object', function () {
    var div = $('<div></div>')
    ok(div.popover, 'popover method is defined')
  })

10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  module('popover', {
    setup: function() {
      // Run all tests in noConflict mode -- it's the only way to ensure that the plugin works in noConflict mode
      $.fn.bootstrapPopover = $.fn.popover.noConflict()
    },
    teardown: function() {
      $.fn.popover = $.fn.bootstrapPopover
      delete $.fn.bootstrapPopover
    }
  })

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

XhmikosR's avatar
XhmikosR committed
25
26
  test('should return element', function () {
    var div = $('<div></div>')
27
    ok(div.bootstrapPopover() == div, 'document.body returned')
XhmikosR's avatar
XhmikosR committed
28
29
30
31
32
33
  })

  test('should render popover element', function () {
    $.support.transition = false
    var popover = $('<a href="#" title="mdo" data-content="http://twitter.com/mdo">@mdo</a>')
      .appendTo('#qunit-fixture')
34
      .bootstrapPopover('show')
XhmikosR's avatar
XhmikosR committed
35
36

    ok($('.popover').length, 'popover was inserted')
37
    popover.bootstrapPopover('hide')
XhmikosR's avatar
XhmikosR committed
38
39
40
41
42
43
    ok(!$('.popover').length, 'popover removed')
  })

  test('should store popover instance in popover data object', function () {
    $.support.transition = false
    var popover = $('<a href="#" title="mdo" data-content="http://twitter.com/mdo">@mdo</a>')
44
      .bootstrapPopover()
XhmikosR's avatar
XhmikosR committed
45
46
47
48
49
50
51
52

    ok(!!popover.data('bs.popover'), 'popover instance exists')
  })

  test('should get title and content from options', function () {
    $.support.transition = false
    var popover = $('<a href="#">@fat</a>')
      .appendTo('#qunit-fixture')
53
      .bootstrapPopover({
XhmikosR's avatar
XhmikosR committed
54
55
56
57
58
59
        title: function () {
          return '@fat'
        },
        content: function () {
          return 'loves writing tests (╯°□°)╯︵ ┻━┻'
        }
60
61
      })

62
    popover.bootstrapPopover('show')
XhmikosR's avatar
XhmikosR committed
63
64
65
66
67

    ok($('.popover').length, 'popover was inserted')
    equal($('.popover .popover-title').text(), '@fat', 'title correctly inserted')
    equal($('.popover .popover-content').text(), 'loves writing tests (╯°□°)╯︵ ┻━┻', 'content correctly inserted')

68
    popover.bootstrapPopover('hide')
XhmikosR's avatar
XhmikosR committed
69
    ok(!$('.popover').length, 'popover was removed')
Stefan Neculai's avatar
Stefan Neculai committed
70
71
72
73
74
75
76
77
78
79
    $('#qunit-fixture').empty()
  })

  test('should not duplicate HTML object', function () {
    $.support.transition = false

    $div = $('<div>').html('loves writing tests (╯°□°)╯︵ ┻━┻')

    var popover = $('<a href="#">@fat</a>')
      .appendTo('#qunit-fixture')
80
      .bootstrapPopover({
Stefan Neculai's avatar
Stefan Neculai committed
81
82
83
84
85
        content: function () {
          return $div
        }
      })

86
    popover.bootstrapPopover('show')
Stefan Neculai's avatar
Stefan Neculai committed
87
88
89
    ok($('.popover').length, 'popover was inserted')
    equal($('.popover .popover-content').html(), $div, 'content correctly inserted')

90
    popover.bootstrapPopover('hide')
Stefan Neculai's avatar
Stefan Neculai committed
91
92
    ok(!$('.popover').length, 'popover was removed')

93
    popover.bootstrapPopover('show')
Stefan Neculai's avatar
Stefan Neculai committed
94
95
96
    ok($('.popover').length, 'popover was inserted')
    equal($('.popover .popover-content').html(), $div, 'content correctly inserted')

97
    popover.bootstrapPopover('hide')
Stefan Neculai's avatar
Stefan Neculai committed
98
    ok(!$('.popover').length, 'popover was removed')
XhmikosR's avatar
XhmikosR committed
99
100
101
102
103
104
105
    $('#qunit-fixture').empty()
  })

  test('should get title and content from attributes', function () {
    $.support.transition = false
    var popover = $('<a href="#" title="@mdo" data-content="loves data attributes (づ。◕‿‿◕。)づ ︵ ┻━┻" >@mdo</a>')
      .appendTo('#qunit-fixture')
106
107
      .bootstrapPopover()
      .bootstrapPopover('show')
XhmikosR's avatar
XhmikosR committed
108
109
110
111
112

    ok($('.popover').length, 'popover was inserted')
    equal($('.popover .popover-title').text(), '@mdo', 'title correctly inserted')
    equal($('.popover .popover-content').text(), 'loves data attributes (づ。◕‿‿◕。)づ ︵ ┻━┻', 'content correctly inserted')

113
    popover.bootstrapPopover('hide')
XhmikosR's avatar
XhmikosR committed
114
115
116
117
118
119
120
121
122
    ok(!$('.popover').length, 'popover was removed')
    $('#qunit-fixture').empty()
  })


  test('should get title and content from attributes #2', function () {
    $.support.transition = false
    var popover = $('<a href="#" title="@mdo" data-content="loves data attributes (づ。◕‿‿◕。)づ ︵ ┻━┻" >@mdo</a>')
      .appendTo('#qunit-fixture')
123
      .bootstrapPopover({
XhmikosR's avatar
XhmikosR committed
124
125
        title: 'ignored title option',
        content: 'ignored content option'
126
      })
127
      .bootstrapPopover('show')
XhmikosR's avatar
XhmikosR committed
128
129
130
131
132

    ok($('.popover').length, 'popover was inserted')
    equal($('.popover .popover-title').text(), '@mdo', 'title correctly inserted')
    equal($('.popover .popover-content').text(), 'loves data attributes (づ。◕‿‿◕。)づ ︵ ┻━┻', 'content correctly inserted')

133
    popover.bootstrapPopover('hide')
XhmikosR's avatar
XhmikosR committed
134
135
136
137
138
139
140
141
    ok(!$('.popover').length, 'popover was removed')
    $('#qunit-fixture').empty()
  })

  test('should respect custom classes', function () {
    $.support.transition = false
    var popover = $('<a href="#">@fat</a>')
      .appendTo('#qunit-fixture')
142
      .bootstrapPopover({
XhmikosR's avatar
XhmikosR committed
143
144
145
        title: 'Test',
        content: 'Test',
        template: '<div class="popover foobar"><div class="arrow"></div><div class="inner"><h3 class="title"></h3><div class="content"><p></p></div></div></div>'
146
147
      })

148
    popover.bootstrapPopover('show')
XhmikosR's avatar
XhmikosR committed
149
150
151
152

    ok($('.popover').length, 'popover was inserted')
    ok($('.popover').hasClass('foobar'), 'custom class is present')

153
    popover.bootstrapPopover('hide')
XhmikosR's avatar
XhmikosR committed
154
155
156
157
158
    ok(!$('.popover').length, 'popover was removed')
    $('#qunit-fixture').empty()
  })

  test('should destroy popover', function () {
159
    var popover = $('<div/>').bootstrapPopover({trigger: 'hover'}).on('click.foo', function () {})
XhmikosR's avatar
XhmikosR committed
160
161
162
    ok(popover.data('bs.popover'), 'popover has data')
    ok($._data(popover[0], 'events').mouseover && $._data(popover[0], 'events').mouseout, 'popover has hover event')
    ok($._data(popover[0], 'events').click[0].namespace == 'foo', 'popover has extra click.foo event')
163
164
    popover.bootstrapPopover('show')
    popover.bootstrapPopover('destroy')
XhmikosR's avatar
XhmikosR committed
165
166
167
168
169
    ok(!popover.hasClass('in'), 'popover is hidden')
    ok(!popover.data('popover'), 'popover does not have data')
    ok($._data(popover[0],'events').click[0].namespace == 'foo', 'popover still has click.foo')
    ok(!$._data(popover[0], 'events').mouseover && !$._data(popover[0], 'events').mouseout, 'popover does not have any events')
  })
170

171
})