popover.js 10.5 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
19
20
21
      $.fn.popover = $.fn.bootstrapPopover
      delete $.fn.bootstrapPopover
    }
  })

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

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

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

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

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

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

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

    $popover.bootstrapPopover('show')

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

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

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

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

Heinrich Fenkart's avatar
Heinrich Fenkart committed
83
    $popover.bootstrapPopover('hide')
84
    assert.strictEqual($('.popover').length, 0, 'popover was removed')
Stefan Neculai's avatar
Stefan Neculai committed
85
86
  })

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

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

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

Heinrich Fenkart's avatar
Heinrich Fenkart committed
103
    $popover.bootstrapPopover('hide')
104
    assert.strictEqual($('.popover').length, 0, 'popover was removed')
Stefan Neculai's avatar
Stefan Neculai committed
105

Heinrich Fenkart's avatar
Heinrich Fenkart committed
106
    $popover.bootstrapPopover('show')
107
108
    assert.notEqual($('.popover').length, 0, 'popover was inserted')
    assert.equal($('.popover .popover-content').html(), $div, 'content correctly inserted')
Stefan Neculai's avatar
Stefan Neculai committed
109

Heinrich Fenkart's avatar
Heinrich Fenkart committed
110
    $popover.bootstrapPopover('hide')
111
    assert.strictEqual($('.popover').length, 0, 'popover was removed')
XhmikosR's avatar
XhmikosR committed
112
113
  })

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

121
    assert.notEqual($('.popover').length, 0, 'popover was inserted')
122
123
    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
124

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


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

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

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

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

Heinrich Fenkart's avatar
Heinrich Fenkart committed
158
    $popover.bootstrapPopover('show')
XhmikosR's avatar
XhmikosR committed
159

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

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

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

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

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

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

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

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

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

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

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

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

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

244
245
246
247
248
249
250
251
252
253
254
  QUnit.test('should throw an error when trying to show a popover on a hidden element', function (assert) {
    assert.expect(1)
    var $target = $('<a href="#" title="Another popover" data-content="Body" style="display: none;">I am hidden</a>').appendTo('#qunit-fixture')

    assert.throws(function () {
      $target.bootstrapPopover('show')
    }, new Error('Can\'t show a tooltip/popover on a hidden element'))

    $target.remove()
  })

255
  QUnit.test('should throw an error when initializing popover on the document object without specifying a delegation selector', function (assert) {
256
    assert.expect(1)
257
    assert.throws(function () {
258
259
260
261
      $(document).bootstrapPopover({ title: 'What am I on?', content: 'My selector is missing' })
    }, new Error('`selector` option must be specified when initializing popover on the window.document object!'))
  })

262
263
264
265
266
267
268
269
270
271
272
273
  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')
  })

274
})