popover.js 10.1 KB
Newer Older
1
$(function () {
XhmikosR's avatar
XhmikosR committed
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
  QUnit.test('should return jquery collection containing the element', function (assert) {
29
    assert.expect(2)
Heinrich Fenkart's avatar
Heinrich Fenkart committed
30
31
    var $el = $('<div/>')
    var $popover = $el.bootstrapPopover()
32
33
    assert.ok($popover instanceof $, 'returns jquery collection')
    assert.strictEqual($popover[0], $el[0], 'collection contains element')
XhmikosR's avatar
XhmikosR committed
34
35
  })

36
  QUnit.test('should render popover element', function (assert) {
37
    assert.expect(2)
38
    var $popover = $('<a href="#" title="mdo" data-content="https://twitter.com/mdo">@mdo</a>')
XhmikosR's avatar
XhmikosR committed
39
      .appendTo('#qunit-fixture')
40
      .bootstrapPopover('show')
XhmikosR's avatar
XhmikosR committed
41

42
    assert.notEqual($('.popover').length, 0, 'popover was inserted')
Heinrich Fenkart's avatar
Heinrich Fenkart committed
43
    $popover.bootstrapPopover('hide')
44
    assert.strictEqual($('.popover').length, 0, 'popover removed')
XhmikosR's avatar
XhmikosR committed
45
46
  })

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

51
    assert.ok($popover.data('bs.popover'), 'popover instance exists')
XhmikosR's avatar
XhmikosR committed
52
53
  })

54
  QUnit.test('should store popover trigger in popover instance data object', function (assert) {
55
    assert.expect(1)
Heinrich Fenkart's avatar
Heinrich Fenkart committed
56
    var $popover = $('<a href="#" title="ResentedHook">@ResentedHook</a>')
57
58
      .appendTo('#qunit-fixture')
      .bootstrapPopover()
Heinrich Fenkart's avatar
Heinrich Fenkart committed
59
60
61

    $popover.bootstrapPopover('show')

62
    assert.ok($('.popover').data('bs.popover'), 'popover trigger stored in instance data')
63
64
  })

65
  QUnit.test('should get title and content from options', function (assert) {
66
    assert.expect(4)
Heinrich Fenkart's avatar
Heinrich Fenkart committed
67
    var $popover = $('<a href="#">@fat</a>')
XhmikosR's avatar
XhmikosR committed
68
      .appendTo('#qunit-fixture')
69
      .bootstrapPopover({
XhmikosR's avatar
XhmikosR committed
70
71
72
73
74
75
        title: function () {
          return '@fat'
        },
        content: function () {
          return 'loves writing tests (╯°□°)╯︵ ┻━┻'
        }
76
77
      })

Heinrich Fenkart's avatar
Heinrich Fenkart committed
78
    $popover.bootstrapPopover('show')
XhmikosR's avatar
XhmikosR committed
79

80
    assert.notEqual($('.popover').length, 0, 'popover was inserted')
81
82
    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
83

Heinrich Fenkart's avatar
Heinrich Fenkart committed
84
    $popover.bootstrapPopover('hide')
fat's avatar
fat committed
85

86
    assert.strictEqual($('.popover').length, 0, 'popover was removed')
Stefan Neculai's avatar
Stefan Neculai committed
87
88
  })

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

Heinrich Fenkart's avatar
Heinrich Fenkart committed
93
    var $popover = $('<a href="#">@fat</a>')
Stefan Neculai's avatar
Stefan Neculai committed
94
      .appendTo('#qunit-fixture')
95
      .bootstrapPopover({
fat's avatar
fat committed
96
        html: true,
Stefan Neculai's avatar
Stefan Neculai committed
97
98
99
100
101
        content: function () {
          return $div
        }
      })

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

Heinrich Fenkart's avatar
Heinrich Fenkart committed
106
    $popover.bootstrapPopover('hide')
107
    assert.strictEqual($('.popover').length, 0, 'popover was removed')
Stefan Neculai's avatar
Stefan Neculai committed
108

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

Heinrich Fenkart's avatar
Heinrich Fenkart committed
113
    $popover.bootstrapPopover('hide')
114
    assert.strictEqual($('.popover').length, 0, 'popover was removed')
XhmikosR's avatar
XhmikosR committed
115
116
  })

117
  QUnit.test('should get title and content from attributes', function (assert) {
118
    assert.expect(4)
Heinrich Fenkart's avatar
Heinrich Fenkart committed
119
    var $popover = $('<a href="#" title="@mdo" data-content="loves data attributes (づ。◕‿‿◕。)づ ︵ ┻━┻" >@mdo</a>')
XhmikosR's avatar
XhmikosR committed
120
      .appendTo('#qunit-fixture')
121
122
      .bootstrapPopover()
      .bootstrapPopover('show')
XhmikosR's avatar
XhmikosR committed
123

124
    assert.notEqual($('.popover').length, 0, 'popover was inserted')
125
126
    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
127

Heinrich Fenkart's avatar
Heinrich Fenkart committed
128
    $popover.bootstrapPopover('hide')
129
    assert.strictEqual($('.popover').length, 0, 'popover was removed')
XhmikosR's avatar
XhmikosR committed
130
131
  })

132
  QUnit.test('should get title and content from attributes ignoring options passed via js', function (assert) {
133
    assert.expect(4)
Heinrich Fenkart's avatar
Heinrich Fenkart committed
134
    var $popover = $('<a href="#" title="@mdo" data-content="loves data attributes (づ。◕‿‿◕。)づ ︵ ┻━┻" >@mdo</a>')
XhmikosR's avatar
XhmikosR committed
135
      .appendTo('#qunit-fixture')
136
      .bootstrapPopover({
XhmikosR's avatar
XhmikosR committed
137
138
        title: 'ignored title option',
        content: 'ignored content option'
139
      })
140
      .bootstrapPopover('show')
XhmikosR's avatar
XhmikosR committed
141

142
    assert.notEqual($('.popover').length, 0, 'popover was inserted')
143
144
    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
145

Heinrich Fenkart's avatar
Heinrich Fenkart committed
146
    $popover.bootstrapPopover('hide')
147
    assert.strictEqual($('.popover').length, 0, 'popover was removed')
XhmikosR's avatar
XhmikosR committed
148
149
  })

150
  QUnit.test('should respect custom template', function (assert) {
151
    assert.expect(3)
Heinrich Fenkart's avatar
Heinrich Fenkart committed
152
    var $popover = $('<a href="#">@fat</a>')
XhmikosR's avatar
XhmikosR committed
153
      .appendTo('#qunit-fixture')
154
      .bootstrapPopover({
XhmikosR's avatar
XhmikosR committed
155
156
        title: 'Test',
        content: 'Test',
fat's avatar
fat committed
157
        template: '<div class="popover foobar"><div class="arrow"></div><div class="inner"><h3 class="title"/><div class="content"><p/></div></div></div>'
158
159
      })

Heinrich Fenkart's avatar
Heinrich Fenkart committed
160
    $popover.bootstrapPopover('show')
XhmikosR's avatar
XhmikosR committed
161

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

Heinrich Fenkart's avatar
Heinrich Fenkart committed
165
    $popover.bootstrapPopover('hide')
166
    assert.strictEqual($('.popover').length, 0, 'popover was removed')
XhmikosR's avatar
XhmikosR committed
167
168
  })

169
  QUnit.test('should destroy popover', function (assert) {
170
    assert.expect(7)
fat's avatar
fat committed
171
    var $popover = $('<div/>')
Heinrich Fenkart's avatar
Heinrich Fenkart committed
172
173
174
175
176
      .bootstrapPopover({
        trigger: 'hover'
      })
      .on('click.foo', $.noop)

177
178
    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')
179
    assert.strictEqual($._data($popover[0], 'events').click[0].namespace, 'foo', 'popover has extra click.foo event')
Heinrich Fenkart's avatar
Heinrich Fenkart committed
180
181
182
183

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

184
185
    assert.ok(!$popover.hasClass('in'), 'popover is hidden')
    assert.ok(!$popover.data('popover'), 'popover does not have data')
186
    assert.strictEqual($._data($popover[0], 'events').click[0].namespace, 'foo', 'popover still has click.foo')
187
    assert.ok(!$._data($popover[0], 'events').mouseover && !$._data($popover[0], 'events').mouseout, 'popover does not have any events')
XhmikosR's avatar
XhmikosR committed
188
  })
189

190
  QUnit.test('should render popover element using delegated selector', function (assert) {
191
    assert.expect(2)
192
193
194
195
196
197
198
    var $div = $('<div><a href="#" title="mdo" data-content="http://twitter.com/mdo">@mdo</a></div>')
      .appendTo('#qunit-fixture')
      .bootstrapPopover({
        selector: 'a',
        trigger: 'click'
      })

199
    $div.find('a').trigger('click')
200
    assert.notEqual($('.popover').length, 0, 'popover was inserted')
201

202
    $div.find('a').trigger('click')
203
    assert.strictEqual($('.popover').length, 0, 'popover was removed')
204
205
  })

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

210
    var handlerCalled = false
211
    $('.content-with-handler .btn').on('click', function () {
Chris Rebert's avatar
Chris Rebert committed
212
      handlerCalled = true
213
    })
Chris Rebert's avatar
Chris Rebert committed
214
215

    var $div = $('<div><a href="#">Show popover</a></div>')
216
217
      .appendTo('#qunit-fixture')
      .bootstrapPopover({
Chris Rebert's avatar
Chris Rebert committed
218
219
220
221
        html: true,
        trigger: 'manual',
        container: 'body',
        content: function () {
222
          return $content
Chris Rebert's avatar
Chris Rebert committed
223
        }
224
225
      })

226
    var done = assert.async()
Chris Rebert's avatar
Chris Rebert committed
227
228
229
230
231
232
    $div
      .one('shown.bs.popover', function () {
        $div
          .one('hidden.bs.popover', function () {
            $div
              .one('shown.bs.popover', function () {
233
                $('.content-with-handler .btn').trigger('click')
Chris Rebert's avatar
Chris Rebert committed
234
                $div.bootstrapPopover('destroy')
235
                assert.ok(handlerCalled, 'content\'s event handler still present')
236
                done()
Chris Rebert's avatar
Chris Rebert committed
237
238
239
240
241
242
              })
              .bootstrapPopover('show')
          })
          .bootstrapPopover('hide')
      })
      .bootstrapPopover('show')
243
  })
244

245
246
247
248
249
250
251
252
253
254
255
256
  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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
  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')
  })

275
})