popover.js 13.9 KB
Newer Older
1
$(function () {
2
  'use strict'
3

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

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

11
  QUnit.module('popover', {
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.bootstrapPopover = $.fn.popover.noConflict()
    },
16
    afterEach: function () {
17
18
      $.fn.popover = $.fn.bootstrapPopover
      delete $.fn.bootstrapPopover
fat's avatar
fat committed
19
      $('.popover').remove()
20
21
22
    }
  })

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

28
29
30
31
32
33
34
35
36
37
38
39
  QUnit.test('should throw explicit error on undefined method', function (assert) {
    assert.expect(1)
    var $el = $('<div/>')
    $el.bootstrapPopover()
    try {
      $el.bootstrapPopover('noMethod')
    }
    catch (err) {
      assert.strictEqual(err.message, 'No method named "noMethod"')
    }
  })

40
  QUnit.test('should return jquery collection containing the element', function (assert) {
41
    assert.expect(2)
Heinrich Fenkart's avatar
Heinrich Fenkart committed
42
43
    var $el = $('<div/>')
    var $popover = $el.bootstrapPopover()
44
45
    assert.ok($popover instanceof $, 'returns jquery collection')
    assert.strictEqual($popover[0], $el[0], 'collection contains element')
XhmikosR's avatar
XhmikosR committed
46
47
  })

48
  QUnit.test('should render popover element', function (assert) {
49
    assert.expect(2)
50
    var $popover = $('<a href="#" title="mdo" data-content="https://twitter.com/mdo">@mdo</a>')
XhmikosR's avatar
XhmikosR committed
51
      .appendTo('#qunit-fixture')
52
      .bootstrapPopover('show')
XhmikosR's avatar
XhmikosR committed
53

54
    assert.notEqual($('.popover').length, 0, 'popover was inserted')
Heinrich Fenkart's avatar
Heinrich Fenkart committed
55
    $popover.bootstrapPopover('hide')
56
    assert.strictEqual($('.popover').length, 0, 'popover removed')
XhmikosR's avatar
XhmikosR committed
57
58
  })

59
  QUnit.test('should store popover instance in popover data object', function (assert) {
60
    assert.expect(1)
61
    var $popover = $('<a href="#" title="mdo" data-content="https://twitter.com/mdo">@mdo</a>').bootstrapPopover()
XhmikosR's avatar
XhmikosR committed
62

63
    assert.ok($popover.data('bs.popover'), 'popover instance exists')
XhmikosR's avatar
XhmikosR committed
64
65
  })

66
  QUnit.test('should store popover trigger in popover instance data object', function (assert) {
67
    assert.expect(1)
Heinrich Fenkart's avatar
Heinrich Fenkart committed
68
    var $popover = $('<a href="#" title="ResentedHook">@ResentedHook</a>')
69
70
      .appendTo('#qunit-fixture')
      .bootstrapPopover()
Heinrich Fenkart's avatar
Heinrich Fenkart committed
71
72
73

    $popover.bootstrapPopover('show')

74
    assert.ok($('.popover').data('bs.popover'), 'popover trigger stored in instance data')
75
76
  })

77
  QUnit.test('should get title and content from options', function (assert) {
78
    assert.expect(4)
Heinrich Fenkart's avatar
Heinrich Fenkart committed
79
    var $popover = $('<a href="#">@fat</a>')
XhmikosR's avatar
XhmikosR committed
80
      .appendTo('#qunit-fixture')
81
      .bootstrapPopover({
XhmikosR's avatar
XhmikosR committed
82
83
84
85
86
87
        title: function () {
          return '@fat'
        },
        content: function () {
          return 'loves writing tests (╯°□°)╯︵ ┻━┻'
        }
88
89
      })

Heinrich Fenkart's avatar
Heinrich Fenkart committed
90
    $popover.bootstrapPopover('show')
XhmikosR's avatar
XhmikosR committed
91

92
    assert.notEqual($('.popover').length, 0, 'popover was inserted')
93
94
    assert.strictEqual($('.popover .popover-title').text(), '@fat', 'title correctly inserted')
    assert.strictEqual($('.popover .popover-content').text(), 'loves writing tests (╯°□°)╯︵ ┻━┻', 'content correctly inserted')
XhmikosR's avatar
XhmikosR committed
95

Heinrich Fenkart's avatar
Heinrich Fenkart committed
96
    $popover.bootstrapPopover('hide')
fat's avatar
fat committed
97

98
    assert.strictEqual($('.popover').length, 0, 'popover was removed')
Stefan Neculai's avatar
Stefan Neculai committed
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
124
125
126
127
128
129
130
131
132
133
134
135
136
  QUnit.test('should allow DOMElement title and content (html: true)', function (assert) {
    assert.expect(5)
    var title = document.createTextNode('@glebm <3 writing tests')
    var content = $('<i>¯\\_(ツ)_/¯</i>').get(0)
    var $popover = $('<a href="#" rel="tooltip"/>')
      .appendTo('#qunit-fixture')
      .bootstrapPopover({ html: true, title: title, content: content })

    $popover.bootstrapPopover('show')

    assert.notEqual($('.popover').length, 0, 'popover inserted')
    assert.strictEqual($('.popover .popover-title').text(), '@glebm <3 writing tests', 'title inserted')
    assert.ok($.contains($('.popover').get(0), title), 'title node moved, not copied')
    // toLowerCase because IE8 will return <I>...</I>
    assert.strictEqual($('.popover .popover-content').html().toLowerCase(), '<i>¯\\_(ツ)_/¯</i>', 'content inserted')
    assert.ok($.contains($('.popover').get(0), content), 'content node moved, not copied')
  })

  QUnit.test('should allow DOMElement title and content (html: false)', function (assert) {
    assert.expect(5)
    var title = document.createTextNode('@glebm <3 writing tests')
    var content = $('<i>¯\\_(ツ)_/¯</i>').get(0)
    var $popover = $('<a href="#" rel="tooltip"/>')
      .appendTo('#qunit-fixture')
      .bootstrapPopover({ title: title, content: content })

    $popover.bootstrapPopover('show')

    assert.notEqual($('.popover').length, 0, 'popover inserted')
    assert.strictEqual($('.popover .popover-title').text(), '@glebm <3 writing tests', 'title inserted')
    assert.ok(!$.contains($('.popover').get(0), title), 'title node copied, not moved')
    assert.strictEqual($('.popover .popover-content').html(), '¯\\_(ツ)_/¯', 'content inserted')
    assert.ok(!$.contains($('.popover').get(0), content), 'content node copied, not moved')
  })


137
  QUnit.test('should not duplicate HTML object', function (assert) {
138
    assert.expect(6)
Heinrich Fenkart's avatar
Heinrich Fenkart committed
139
    var $div = $('<div/>').html('loves writing tests (╯°□°)╯︵ ┻━┻')
Stefan Neculai's avatar
Stefan Neculai committed
140

Heinrich Fenkart's avatar
Heinrich Fenkart committed
141
    var $popover = $('<a href="#">@fat</a>')
Stefan Neculai's avatar
Stefan Neculai committed
142
      .appendTo('#qunit-fixture')
143
      .bootstrapPopover({
fat's avatar
fat committed
144
        html: true,
Stefan Neculai's avatar
Stefan Neculai committed
145
146
147
148
149
        content: function () {
          return $div
        }
      })

Heinrich Fenkart's avatar
Heinrich Fenkart committed
150
    $popover.bootstrapPopover('show')
151
    assert.notEqual($('.popover').length, 0, 'popover was inserted')
fat's avatar
fat committed
152
    assert.equal($('.popover .popover-content').html(), $div[0].outerHTML, 'content correctly inserted')
Stefan Neculai's avatar
Stefan Neculai committed
153

Heinrich Fenkart's avatar
Heinrich Fenkart committed
154
    $popover.bootstrapPopover('hide')
155
    assert.strictEqual($('.popover').length, 0, 'popover was removed')
Stefan Neculai's avatar
Stefan Neculai committed
156

Heinrich Fenkart's avatar
Heinrich Fenkart committed
157
    $popover.bootstrapPopover('show')
158
    assert.notEqual($('.popover').length, 0, 'popover was inserted')
fat's avatar
fat committed
159
    assert.equal($('.popover .popover-content').html(), $div[0].outerHTML, 'content correctly inserted')
Stefan Neculai's avatar
Stefan Neculai committed
160

Heinrich Fenkart's avatar
Heinrich Fenkart committed
161
    $popover.bootstrapPopover('hide')
162
    assert.strictEqual($('.popover').length, 0, 'popover was removed')
XhmikosR's avatar
XhmikosR committed
163
164
  })

165
  QUnit.test('should get title and content from attributes', function (assert) {
166
    assert.expect(4)
Heinrich Fenkart's avatar
Heinrich Fenkart committed
167
    var $popover = $('<a href="#" title="@mdo" data-content="loves data attributes (づ。◕‿‿◕。)づ ︵ ┻━┻" >@mdo</a>')
XhmikosR's avatar
XhmikosR committed
168
      .appendTo('#qunit-fixture')
169
170
      .bootstrapPopover()
      .bootstrapPopover('show')
XhmikosR's avatar
XhmikosR committed
171

172
    assert.notEqual($('.popover').length, 0, 'popover was inserted')
173
174
    assert.strictEqual($('.popover .popover-title').text(), '@mdo', 'title correctly inserted')
    assert.strictEqual($('.popover .popover-content').text(), 'loves data attributes (づ。◕‿‿◕。)づ ︵ ┻━┻', 'content correctly inserted')
XhmikosR's avatar
XhmikosR committed
175

Heinrich Fenkart's avatar
Heinrich Fenkart committed
176
    $popover.bootstrapPopover('hide')
177
    assert.strictEqual($('.popover').length, 0, 'popover was removed')
XhmikosR's avatar
XhmikosR committed
178
179
  })

180
  QUnit.test('should get title and content from attributes ignoring options passed via js', function (assert) {
181
    assert.expect(4)
Heinrich Fenkart's avatar
Heinrich Fenkart committed
182
    var $popover = $('<a href="#" title="@mdo" data-content="loves data attributes (づ。◕‿‿◕。)づ ︵ ┻━┻" >@mdo</a>')
XhmikosR's avatar
XhmikosR committed
183
      .appendTo('#qunit-fixture')
184
      .bootstrapPopover({
XhmikosR's avatar
XhmikosR committed
185
186
        title: 'ignored title option',
        content: 'ignored content option'
187
      })
188
      .bootstrapPopover('show')
XhmikosR's avatar
XhmikosR committed
189

190
    assert.notEqual($('.popover').length, 0, 'popover was inserted')
191
192
    assert.strictEqual($('.popover .popover-title').text(), '@mdo', 'title correctly inserted')
    assert.strictEqual($('.popover .popover-content').text(), 'loves data attributes (づ。◕‿‿◕。)づ ︵ ┻━┻', 'content correctly inserted')
XhmikosR's avatar
XhmikosR committed
193

Heinrich Fenkart's avatar
Heinrich Fenkart committed
194
    $popover.bootstrapPopover('hide')
195
    assert.strictEqual($('.popover').length, 0, 'popover was removed')
XhmikosR's avatar
XhmikosR committed
196
197
  })

198
  QUnit.test('should respect custom template', function (assert) {
199
    assert.expect(3)
Heinrich Fenkart's avatar
Heinrich Fenkart committed
200
    var $popover = $('<a href="#">@fat</a>')
XhmikosR's avatar
XhmikosR committed
201
      .appendTo('#qunit-fixture')
202
      .bootstrapPopover({
XhmikosR's avatar
XhmikosR committed
203
204
        title: 'Test',
        content: 'Test',
fat's avatar
fat committed
205
        template: '<div class="popover foobar"><div class="arrow"></div><div class="inner"><h3 class="title"/><div class="content"><p/></div></div></div>'
206
207
      })

Heinrich Fenkart's avatar
Heinrich Fenkart committed
208
    $popover.bootstrapPopover('show')
XhmikosR's avatar
XhmikosR committed
209

210
211
    assert.notEqual($('.popover').length, 0, 'popover was inserted')
    assert.ok($('.popover').hasClass('foobar'), 'custom class is present')
XhmikosR's avatar
XhmikosR committed
212

Heinrich Fenkart's avatar
Heinrich Fenkart committed
213
    $popover.bootstrapPopover('hide')
214
    assert.strictEqual($('.popover').length, 0, 'popover was removed')
XhmikosR's avatar
XhmikosR committed
215
216
  })

217
  QUnit.test('should destroy popover', function (assert) {
218
    assert.expect(7)
fat's avatar
fat committed
219
    var $popover = $('<div/>')
Heinrich Fenkart's avatar
Heinrich Fenkart committed
220
221
222
223
224
      .bootstrapPopover({
        trigger: 'hover'
      })
      .on('click.foo', $.noop)

225
226
    assert.ok($popover.data('bs.popover'), 'popover has data')
    assert.ok($._data($popover[0], 'events').mouseover && $._data($popover[0], 'events').mouseout, 'popover has hover event')
227
    assert.strictEqual($._data($popover[0], 'events').click[0].namespace, 'foo', 'popover has extra click.foo event')
Heinrich Fenkart's avatar
Heinrich Fenkart committed
228
229

    $popover.bootstrapPopover('show')
fat's avatar
fat committed
230
    $popover.bootstrapPopover('dispose')
Heinrich Fenkart's avatar
Heinrich Fenkart committed
231

Starsam80's avatar
Starsam80 committed
232
    assert.ok(!$popover.hasClass('show'), 'popover is hidden')
233
    assert.ok(!$popover.data('popover'), 'popover does not have data')
234
    assert.strictEqual($._data($popover[0], 'events').click[0].namespace, 'foo', 'popover still has click.foo')
235
    assert.ok(!$._data($popover[0], 'events').mouseover && !$._data($popover[0], 'events').mouseout, 'popover does not have any events')
XhmikosR's avatar
XhmikosR committed
236
  })
237

238
  QUnit.test('should render popover element using delegated selector', function (assert) {
239
    assert.expect(2)
240
241
242
243
244
245
246
    var $div = $('<div><a href="#" title="mdo" data-content="http://twitter.com/mdo">@mdo</a></div>')
      .appendTo('#qunit-fixture')
      .bootstrapPopover({
        selector: 'a',
        trigger: 'click'
      })

247
    $div.find('a').trigger('click')
248
    assert.notEqual($('.popover').length, 0, 'popover was inserted')
249

250
    $div.find('a').trigger('click')
251
    assert.strictEqual($('.popover').length, 0, 'popover was removed')
252
253
  })

254
  QUnit.test('should detach popover content rather than removing it so that event handlers are left intact', function (assert) {
255
    assert.expect(1)
Chris Rebert's avatar
Chris Rebert committed
256
    var $content = $('<div class="content-with-handler"><a class="btn btn-warning">Button with event handler</a></div>').appendTo('#qunit-fixture')
257

258
    var handlerCalled = false
259
    $('.content-with-handler .btn').on('click', function () {
Chris Rebert's avatar
Chris Rebert committed
260
      handlerCalled = true
261
    })
Chris Rebert's avatar
Chris Rebert committed
262
263

    var $div = $('<div><a href="#">Show popover</a></div>')
264
265
      .appendTo('#qunit-fixture')
      .bootstrapPopover({
Chris Rebert's avatar
Chris Rebert committed
266
267
268
269
        html: true,
        trigger: 'manual',
        container: 'body',
        content: function () {
270
          return $content
Chris Rebert's avatar
Chris Rebert committed
271
        }
272
273
      })

274
    var done = assert.async()
Chris Rebert's avatar
Chris Rebert committed
275
276
277
278
279
280
    $div
      .one('shown.bs.popover', function () {
        $div
          .one('hidden.bs.popover', function () {
            $div
              .one('shown.bs.popover', function () {
281
                $('.content-with-handler .btn').trigger('click')
fat's avatar
fat committed
282
                $div.bootstrapPopover('dispose')
283
                assert.ok(handlerCalled, 'content\'s event handler still present')
284
                done()
Chris Rebert's avatar
Chris Rebert committed
285
286
287
288
289
290
              })
              .bootstrapPopover('show')
          })
          .bootstrapPopover('hide')
      })
      .bootstrapPopover('show')
291
  })
292

293
294
295
296
297
298
299
300
301
302
303
304
  QUnit.test('should do nothing when an attempt is made to hide an uninitialized popover', function (assert) {
    assert.expect(1)

    var $popover = $('<span data-toggle="popover" data-title="some title" data-content="some content">some text</span>')
      .appendTo('#qunit-fixture')
      .on('hidden.bs.popover shown.bs.popover', function () {
        assert.ok(false, 'should not fire any popover events')
      })
      .bootstrapPopover('hide')
    assert.strictEqual($popover.data('bs.popover'), undefined, 'should not initialize the popover')
  })

fat's avatar
fat committed
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
  QUnit.test('should fire inserted event', function (assert) {
    assert.expect(2)
    var done = assert.async()

    $('<a href="#">@Johann-S</a>')
      .appendTo('#qunit-fixture')
      .on('inserted.bs.popover', function () {
        assert.notEqual($('.popover').length, 0, 'popover was inserted')
        assert.ok(true, 'inserted event fired')
        done()
      })
      .bootstrapPopover({
        title: 'Test',
        content: 'Test'
      })
      .bootstrapPopover('show')
  })

323
324
325
326
327
328
329
330
331
332
333
334
  QUnit.test('should throw an error when show is called on hidden elements', function (assert) {
    assert.expect(1)
    var done = assert.async()

    try {
      $('<div data-toggle="popover" data-title="some title" data-content="@Johann-S" style="display: none"/>').bootstrapPopover('show')
    }
    catch (err) {
      assert.strictEqual(err.message, 'Please use show on visible elements')
      done()
    }
  })
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366

  QUnit.test('should hide popovers when their containing modal is closed', function (assert) {
    assert.expect(1)
    var done = assert.async()
    var templateHTML = '<div id="modal-test" class="modal">' +
                          '<div class="modal-dialog" role="document">' +
                            '<div class="modal-content">' +
                              '<div class="modal-body">' +
                                '<button id="popover-test" type="button" class="btn btn-secondary" data-toggle="popover" data-placement="top" data-content="Popover">' +
                                  'Popover on top' +
                                '</button>' +
                              '</div>' +
                            '</div>' +
                          '</div>' +
                        '</div>'

    $(templateHTML).appendTo('#qunit-fixture')
    $('#popover-test')
      .on('shown.bs.popover', function () {
        $('#modal-test').modal('hide')
      })
      .on('hide.bs.popover', function () {
        assert.ok(true, 'popover hide')
        done()
      })

    $('#modal-test')
      .on('shown.bs.modal', function () {
        $('#popover-test').bootstrapPopover('show')
      })
      .modal('show')
  })
367
})