popover.js 9.13 KB
Newer Older
1
$(function () {
XhmikosR's avatar
XhmikosR committed
2
  'use strict';
3

4
  module('popover plugin')
XhmikosR's avatar
XhmikosR committed
5
6

  test('should be defined on jquery object', function () {
Heinrich Fenkart's avatar
Heinrich Fenkart committed
7
    ok($(document.body).popover, 'popover method is defined')
XhmikosR's avatar
XhmikosR committed
8
9
  })

10
  module('popover', {
XhmikosR's avatar
XhmikosR committed
11
    setup: function () {
12
13
14
      // 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()
    },
XhmikosR's avatar
XhmikosR committed
15
    teardown: function () {
16
17
18
19
20
21
      $.fn.popover = $.fn.bootstrapPopover
      delete $.fn.bootstrapPopover
    }
  })

  test('should provide no conflict', function () {
Heinrich Fenkart's avatar
Heinrich Fenkart committed
22
    strictEqual($.fn.popover, undefined, 'popover was set back to undefined (org value)')
23
24
  })

Heinrich Fenkart's avatar
Heinrich Fenkart committed
25
26
27
28
29
  test('should return jquery collection containing the element', function () {
    var $el = $('<div/>')
    var $popover = $el.bootstrapPopover()
    ok($popover instanceof $, 'returns jquery collection')
    strictEqual($popover[0], $el[0], 'collection contains element')
XhmikosR's avatar
XhmikosR committed
30
31
32
  })

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

Heinrich Fenkart's avatar
Heinrich Fenkart committed
37
38
39
    notEqual($('.popover').length, 0, 'popover was inserted')
    $popover.bootstrapPopover('hide')
    equal($('.popover').length, 0, 'popover removed')
XhmikosR's avatar
XhmikosR committed
40
41
42
  })

  test('should store popover instance in popover data object', function () {
43
    var $popover = $('<a href="#" title="mdo" data-content="https://twitter.com/mdo">@mdo</a>').bootstrapPopover()
XhmikosR's avatar
XhmikosR committed
44

Heinrich Fenkart's avatar
Heinrich Fenkart committed
45
    ok($popover.data('bs.popover'), 'popover instance exists')
XhmikosR's avatar
XhmikosR committed
46
47
  })

48
  test('should store popover trigger in popover instance data object', function () {
Heinrich Fenkart's avatar
Heinrich Fenkart committed
49
    var $popover = $('<a href="#" title="ResentedHook">@ResentedHook</a>')
50
51
      .appendTo('#qunit-fixture')
      .bootstrapPopover()
Heinrich Fenkart's avatar
Heinrich Fenkart committed
52
53
54
55

    $popover.bootstrapPopover('show')

    ok($('.popover').data('bs.popover'), 'popover trigger stored in instance data')
56
57
  })

XhmikosR's avatar
XhmikosR committed
58
  test('should get title and content from options', function () {
Heinrich Fenkart's avatar
Heinrich Fenkart committed
59
    var $popover = $('<a href="#">@fat</a>')
XhmikosR's avatar
XhmikosR committed
60
      .appendTo('#qunit-fixture')
61
      .bootstrapPopover({
XhmikosR's avatar
XhmikosR committed
62
63
64
65
66
67
        title: function () {
          return '@fat'
        },
        content: function () {
          return 'loves writing tests (╯°□°)╯︵ ┻━┻'
        }
68
69
      })

Heinrich Fenkart's avatar
Heinrich Fenkart committed
70
    $popover.bootstrapPopover('show')
XhmikosR's avatar
XhmikosR committed
71

Heinrich Fenkart's avatar
Heinrich Fenkart committed
72
    notEqual($('.popover').length, 0, 'popover was inserted')
XhmikosR's avatar
XhmikosR committed
73
74
75
    equal($('.popover .popover-title').text(), '@fat', 'title correctly inserted')
    equal($('.popover .popover-content').text(), 'loves writing tests (╯°□°)╯︵ ┻━┻', 'content correctly inserted')

Heinrich Fenkart's avatar
Heinrich Fenkart committed
76
77
    $popover.bootstrapPopover('hide')
    equal($('.popover').length, 0, 'popover was removed')
Stefan Neculai's avatar
Stefan Neculai committed
78
79
80
  })

  test('should not duplicate HTML object', function () {
Heinrich Fenkart's avatar
Heinrich Fenkart committed
81
    var $div = $('<div/>').html('loves writing tests (╯°□°)╯︵ ┻━┻')
Stefan Neculai's avatar
Stefan Neculai committed
82

Heinrich Fenkart's avatar
Heinrich Fenkart committed
83
    var $popover = $('<a href="#">@fat</a>')
Stefan Neculai's avatar
Stefan Neculai committed
84
      .appendTo('#qunit-fixture')
85
      .bootstrapPopover({
Stefan Neculai's avatar
Stefan Neculai committed
86
87
88
89
90
        content: function () {
          return $div
        }
      })

Heinrich Fenkart's avatar
Heinrich Fenkart committed
91
92
    $popover.bootstrapPopover('show')
    notEqual($('.popover').length, 0, 'popover was inserted')
Stefan Neculai's avatar
Stefan Neculai committed
93
94
    equal($('.popover .popover-content').html(), $div, 'content correctly inserted')

Heinrich Fenkart's avatar
Heinrich Fenkart committed
95
96
    $popover.bootstrapPopover('hide')
    equal($('.popover').length, 0, 'popover was removed')
Stefan Neculai's avatar
Stefan Neculai committed
97

Heinrich Fenkart's avatar
Heinrich Fenkart committed
98
99
    $popover.bootstrapPopover('show')
    notEqual($('.popover').length, 0, 'popover was inserted')
Stefan Neculai's avatar
Stefan Neculai committed
100
101
    equal($('.popover .popover-content').html(), $div, 'content correctly inserted')

Heinrich Fenkart's avatar
Heinrich Fenkart committed
102
103
    $popover.bootstrapPopover('hide')
    equal($('.popover').length, 0, 'popover was removed')
XhmikosR's avatar
XhmikosR committed
104
105
106
  })

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

Heinrich Fenkart's avatar
Heinrich Fenkart committed
112
    notEqual($('.popover').length, 0, 'popover was inserted')
XhmikosR's avatar
XhmikosR committed
113
114
115
    equal($('.popover .popover-title').text(), '@mdo', 'title correctly inserted')
    equal($('.popover .popover-content').text(), 'loves data attributes (づ。◕‿‿◕。)づ ︵ ┻━┻', 'content correctly inserted')

Heinrich Fenkart's avatar
Heinrich Fenkart committed
116
117
    $popover.bootstrapPopover('hide')
    equal($('.popover').length, 0, 'popover was removed')
XhmikosR's avatar
XhmikosR committed
118
119
120
  })


Heinrich Fenkart's avatar
Heinrich Fenkart committed
121
122
  test('should get title and content from attributes ignoring options passed via js', function () {
    var $popover = $('<a href="#" title="@mdo" data-content="loves data attributes (づ。◕‿‿◕。)づ ︵ ┻━┻" >@mdo</a>')
XhmikosR's avatar
XhmikosR committed
123
      .appendTo('#qunit-fixture')
124
      .bootstrapPopover({
XhmikosR's avatar
XhmikosR committed
125
126
        title: 'ignored title option',
        content: 'ignored content option'
127
      })
128
      .bootstrapPopover('show')
XhmikosR's avatar
XhmikosR committed
129

Heinrich Fenkart's avatar
Heinrich Fenkart committed
130
    notEqual($('.popover').length, 0, 'popover was inserted')
XhmikosR's avatar
XhmikosR committed
131
132
133
    equal($('.popover .popover-title').text(), '@mdo', 'title correctly inserted')
    equal($('.popover .popover-content').text(), 'loves data attributes (づ。◕‿‿◕。)づ ︵ ┻━┻', 'content correctly inserted')

Heinrich Fenkart's avatar
Heinrich Fenkart committed
134
135
    $popover.bootstrapPopover('hide')
    equal($('.popover').length, 0, 'popover was removed')
XhmikosR's avatar
XhmikosR committed
136
137
  })

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

Heinrich Fenkart's avatar
Heinrich Fenkart committed
147
    $popover.bootstrapPopover('show')
XhmikosR's avatar
XhmikosR committed
148

Heinrich Fenkart's avatar
Heinrich Fenkart committed
149
    notEqual($('.popover').length, 0, 'popover was inserted')
XhmikosR's avatar
XhmikosR committed
150
151
    ok($('.popover').hasClass('foobar'), 'custom class is present')

Heinrich Fenkart's avatar
Heinrich Fenkart committed
152
153
    $popover.bootstrapPopover('hide')
    equal($('.popover').length, 0, 'popover was removed')
XhmikosR's avatar
XhmikosR committed
154
155
156
  })

  test('should destroy popover', function () {
Heinrich Fenkart's avatar
Heinrich Fenkart committed
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
    var $popover = $('<div/>')
      .bootstrapPopover({
        trigger: 'hover'
      })
      .on('click.foo', $.noop)

    ok($popover.data('bs.popover'), 'popover has data')
    ok($._data($popover[0], 'events').mouseover && $._data($popover[0], 'events').mouseout, 'popover has hover event')
    equal($._data($popover[0], 'events').click[0].namespace, 'foo', 'popover has extra click.foo event')

    $popover.bootstrapPopover('show')
    $popover.bootstrapPopover('destroy')

    ok(!$popover.hasClass('in'), 'popover is hidden')
    ok(!$popover.data('popover'), 'popover does not have data')
    equal($._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')
XhmikosR's avatar
XhmikosR committed
174
  })
175

176
177
178
179
180
181
182
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
  test('should render popover element using delegated selector', function () {
    var $div = $('<div><a href="#" title="mdo" data-content="http://twitter.com/mdo">@mdo</a></div>')
      .appendTo('#qunit-fixture')
      .bootstrapPopover({
        selector: 'a',
        trigger: 'click'
      })

    $div.find('a').click()
    notEqual($('.popover').length, 0, 'popover was inserted')

    $div.find('a').click()
    equal($('.popover').length, 0, 'popover was removed')
  })

  test('should render popover elements using different delegated selectors on the same node', function () {
    var popoverHTML = '<div>'
        + '<a href="#" class="first" title="mdo" data-content="http://twitter.com/mdo">@mdo</a>'
        + '<a href="#" class="second" title="mdo" data-content="http://twitter.com/mdo">@mdo</a>'
        + '</div>'

    var $div = $(popoverHTML)
      .appendTo('#qunit-fixture')
      .bootstrapPopover({
        selector: 'a.first',
        trigger: 'click'
      })
      .bootstrapPopover({
        selector: 'a.second',
        trigger: 'click'
      })

    $div.find('a.first').click()
    notEqual($('.popover').length, 0, 'first popover was inserted')

    $div.find('a.first').click()
    equal($('.popover').length, 0, 'first popover removed')

    $div.find('a.second').click()
    notEqual($('.popover').length, 0, 'second popover was inserted')

    $div.find('a.second').click()
    equal($('.popover').length, 0, 'second popover removed')
  })
Chris Rebert's avatar
Chris Rebert committed
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257

  test('should detach popover content rather than removing it so that event handlers are left intact', function () {
    var $content = $('<div class="content-with-handler"><a class="btn btn-warning">Button with event handler</a></div>').appendTo('#qunit-fixture')

    var handlerCalled = false;
    $('.content-with-handler .btn').click(function () {
      handlerCalled = true
    });

    var $div = $('<div><a href="#">Show popover</a></div>')
      .appendTo('#qunit-fixture')
      .bootstrapPopover({
        html: true,
        trigger: 'manual',
        container: 'body',
        content: function () {
          return $content;
        }
      })

    stop()
    $div
      .one('shown.bs.popover', function () {
        $div
          .one('hidden.bs.popover', function () {
            $div
              .one('shown.bs.popover', function () {
                $('.content-with-handler .btn').click()
                $div.bootstrapPopover('destroy')
                ok(handlerCalled, 'content\'s event handler still present')
                start()
              })
              .bootstrapPopover('show')
          })
          .bootstrapPopover('hide')
      })
      .bootstrapPopover('show')
  })
258
})