tooltip.js 46.7 KB
Newer Older
Jacob Thornton's avatar
Jacob Thornton committed
1
$(function () {
XhmikosR's avatar
XhmikosR committed
2
  'use strict';
Jacob Thornton's avatar
Jacob Thornton committed
3

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

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

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

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

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

fat's avatar
fat committed
35
36
37
  QUnit.test('should expose default settings', function (assert) {
    assert.expect(1)
    assert.ok($.fn.bootstrapTooltip.Constructor.DEFAULTS, 'defaults is defined')
XhmikosR's avatar
XhmikosR committed
38
39
  })

fat's avatar
fat committed
40
41
  QUnit.test('should empty title attribute', function (assert) {
    assert.expect(1)
Heinrich Fenkart's avatar
Heinrich Fenkart committed
42
    var $trigger = $('<a href="#" rel="tooltip" title="Another tooltip"/>').bootstrapTooltip()
fat's avatar
fat committed
43
    assert.strictEqual($trigger.attr('title'), '', 'title attribute was emptied')
XhmikosR's avatar
XhmikosR committed
44
45
  })

fat's avatar
fat committed
46
47
  QUnit.test('should add data attribute for referencing original title', function (assert) {
    assert.expect(1)
Heinrich Fenkart's avatar
Heinrich Fenkart committed
48
    var $trigger = $('<a href="#" rel="tooltip" title="Another tooltip"/>').bootstrapTooltip()
fat's avatar
fat committed
49
    assert.strictEqual($trigger.attr('data-original-title'), 'Another tooltip', 'original title preserved in data attribute')
XhmikosR's avatar
XhmikosR committed
50
51
  })

fat's avatar
fat committed
52
53
54
  QUnit.test('should add aria-describedby to the trigger on show', function (assert) {
    assert.expect(3)
    var $trigger = $('<a href="#" rel="tooltip" title="Another tooltip"/>')
Heinrich Fenkart's avatar
Heinrich Fenkart committed
55
      .bootstrapTooltip()
56
57
      .appendTo('#qunit-fixture')
      .bootstrapTooltip('show')
Heinrich Fenkart's avatar
Heinrich Fenkart committed
58

59
60
    var id = $('.tooltip').attr('id')

fat's avatar
fat committed
61
62
63
    assert.strictEqual($('#' + id).length, 1, 'has a unique id')
    assert.strictEqual($('.tooltip').attr('aria-describedby'), $trigger.attr('id'), 'tooltip id and aria-describedby on trigger match')
    assert.ok($trigger[0].hasAttribute('aria-describedby'), 'trigger has aria-describedby')
64
65
  })

fat's avatar
fat committed
66
67
68
  QUnit.test('should remove aria-describedby from trigger on hide', function (assert) {
    assert.expect(2)
    var $trigger = $('<a href="#" rel="tooltip" title="Another tooltip"/>')
Heinrich Fenkart's avatar
Heinrich Fenkart committed
69
      .bootstrapTooltip()
70
      .appendTo('#qunit-fixture')
Heinrich Fenkart's avatar
Heinrich Fenkart committed
71
72

    $trigger.bootstrapTooltip('show')
fat's avatar
fat committed
73
    assert.ok($trigger[0].hasAttribute('aria-describedby'), 'trigger has aria-describedby')
Heinrich Fenkart's avatar
Heinrich Fenkart committed
74
75

    $trigger.bootstrapTooltip('hide')
fat's avatar
fat committed
76
    assert.ok(!$trigger[0].hasAttribute('aria-describedby'), 'trigger does not have aria-describedby')
77
78
  })

fat's avatar
fat committed
79
80
81
  QUnit.test('should assign a unique id tooltip element', function (assert) {
    assert.expect(2)
    $('<a href="#" rel="tooltip" title="Another tooltip"/>')
82
      .appendTo('#qunit-fixture')
XhmikosR's avatar
XhmikosR committed
83
      .bootstrapTooltip('show')
Heinrich Fenkart's avatar
Heinrich Fenkart committed
84

XhmikosR's avatar
XhmikosR committed
85
    var id = $('.tooltip').attr('id')
86

fat's avatar
fat committed
87
88
    assert.strictEqual($('#' + id).length, 1, 'tooltip has unique id')
    assert.strictEqual(id.indexOf('tooltip'), 0, 'tooltip id has prefix')
89
90
  })

fat's avatar
fat committed
91
92
93
  QUnit.test('should place tooltips relative to placement option', function (assert) {
    assert.expect(2)
    var $tooltip = $('<a href="#" rel="tooltip" title="Another tooltip"/>')
XhmikosR's avatar
XhmikosR committed
94
      .appendTo('#qunit-fixture')
XhmikosR's avatar
XhmikosR committed
95
      .bootstrapTooltip({ placement: 'bottom' })
XhmikosR's avatar
XhmikosR committed
96

Heinrich Fenkart's avatar
Heinrich Fenkart committed
97
    $tooltip.bootstrapTooltip('show')
fat's avatar
fat committed
98
    assert.ok($('.tooltip').is('.fade.bottom.in'), 'has correct classes applied')
Heinrich Fenkart's avatar
Heinrich Fenkart committed
99
100

    $tooltip.bootstrapTooltip('hide')
fat's avatar
fat committed
101
    assert.strictEqual($('.tooltip').length, 0, 'tooltip removed')
XhmikosR's avatar
XhmikosR committed
102
103
  })

fat's avatar
fat committed
104
105
106
  QUnit.test('should allow html entities', function (assert) {
    assert.expect(2)
    var $tooltip = $('<a href="#" rel="tooltip" title="&lt;b&gt;@fat&lt;/b&gt;"/>')
XhmikosR's avatar
XhmikosR committed
107
      .appendTo('#qunit-fixture')
XhmikosR's avatar
XhmikosR committed
108
      .bootstrapTooltip({ html: true })
XhmikosR's avatar
XhmikosR committed
109

Heinrich Fenkart's avatar
Heinrich Fenkart committed
110
    $tooltip.bootstrapTooltip('show')
fat's avatar
fat committed
111
    assert.notEqual($('.tooltip b').length, 0, 'b tag was inserted')
Heinrich Fenkart's avatar
Heinrich Fenkart committed
112
113

    $tooltip.bootstrapTooltip('hide')
fat's avatar
fat committed
114
    assert.strictEqual($('.tooltip').length, 0, 'tooltip removed')
XhmikosR's avatar
XhmikosR committed
115
116
  })

fat's avatar
fat committed
117
118
119
  QUnit.test('should respect custom classes', function (assert) {
    assert.expect(2)
    var $tooltip = $('<a href="#" rel="tooltip" title="Another tooltip"/>')
XhmikosR's avatar
XhmikosR committed
120
      .appendTo('#qunit-fixture')
XhmikosR's avatar
XhmikosR committed
121
      .bootstrapTooltip({ template: '<div class="tooltip some-class"><div class="tooltip-arrow"/><div class="tooltip-inner"/></div>' })
XhmikosR's avatar
XhmikosR committed
122

Heinrich Fenkart's avatar
Heinrich Fenkart committed
123
    $tooltip.bootstrapTooltip('show')
fat's avatar
fat committed
124
    assert.ok($('.tooltip').hasClass('some-class'), 'custom class is present')
Heinrich Fenkart's avatar
Heinrich Fenkart committed
125
126

    $tooltip.bootstrapTooltip('hide')
fat's avatar
fat committed
127
    assert.strictEqual($('.tooltip').length, 0, 'tooltip removed')
XhmikosR's avatar
XhmikosR committed
128
129
  })

fat's avatar
fat committed
130
131
  QUnit.test('should fire show event', function (assert) {
    assert.expect(1)
132
    var done = assert.async()
Heinrich Fenkart's avatar
Heinrich Fenkart committed
133

fat's avatar
fat committed
134
    $('<div title="tooltip title"/>')
XhmikosR's avatar
XhmikosR committed
135
      .on('show.bs.tooltip', function () {
fat's avatar
fat committed
136
        assert.ok(true, 'show event fired')
137
        done()
XhmikosR's avatar
XhmikosR committed
138
      })
139
      .bootstrapTooltip('show')
XhmikosR's avatar
XhmikosR committed
140
141
  })

fat's avatar
fat committed
142
143
  QUnit.test('should fire inserted event', function (assert) {
    assert.expect(2)
144
    var done = assert.async()
Heinrich Fenkart's avatar
Heinrich Fenkart committed
145

fat's avatar
fat committed
146
    $('<div title="tooltip title"/>')
Heinrich Fenkart's avatar
Heinrich Fenkart committed
147
      .appendTo('#qunit-fixture')
fat's avatar
fat committed
148
149
150
      .on('inserted.bs.tooltip', function () {
        assert.notEqual($('.tooltip').length, 0, 'tooltip was inserted')
        assert.ok(true, 'inserted event fired')
151
        done()
XhmikosR's avatar
XhmikosR committed
152
      })
153
      .bootstrapTooltip('show')
XhmikosR's avatar
XhmikosR committed
154
155
  })

fat's avatar
fat committed
156
157
  QUnit.test('should fire shown event', function (assert) {
    assert.expect(1)
158
    var done = assert.async()
Heinrich Fenkart's avatar
Heinrich Fenkart committed
159

fat's avatar
fat committed
160
    $('<div title="tooltip title"></div>')
161
      .appendTo('#qunit-fixture')
fat's avatar
fat committed
162
163
164
165
166
167
168
169
170
171
172
173
      .on('shown.bs.tooltip', function () {
        assert.ok(true, 'shown was called')
        done()
      })
      .bootstrapTooltip('show')
  })

  QUnit.test('should not fire shown event when show was prevented', function (assert) {
    assert.expect(1)
    var done = assert.async()

    $('<div title="tooltip title"/>')
XhmikosR's avatar
XhmikosR committed
174
175
      .on('show.bs.tooltip', function (e) {
        e.preventDefault()
fat's avatar
fat committed
176
        assert.ok(true, 'show event fired')
177
        done()
XhmikosR's avatar
XhmikosR committed
178
179
      })
      .on('shown.bs.tooltip', function () {
fat's avatar
fat committed
180
        assert.ok(false, 'shown event fired')
XhmikosR's avatar
XhmikosR committed
181
      })
182
      .bootstrapTooltip('show')
XhmikosR's avatar
XhmikosR committed
183
184
  })

fat's avatar
fat committed
185
186
  QUnit.test('should fire hide event', function (assert) {
    assert.expect(1)
187
    var done = assert.async()
Heinrich Fenkart's avatar
Heinrich Fenkart committed
188

fat's avatar
fat committed
189
    $('<div title="tooltip title"/>')
Heinrich Fenkart's avatar
Heinrich Fenkart committed
190
      .appendTo('#qunit-fixture')
XhmikosR's avatar
XhmikosR committed
191
      .on('shown.bs.tooltip', function () {
192
        $(this).bootstrapTooltip('hide')
XhmikosR's avatar
XhmikosR committed
193
194
      })
      .on('hide.bs.tooltip', function () {
fat's avatar
fat committed
195
        assert.ok(true, 'hide event fired')
196
        done()
XhmikosR's avatar
XhmikosR committed
197
      })
198
      .bootstrapTooltip('show')
XhmikosR's avatar
XhmikosR committed
199
200
  })

fat's avatar
fat committed
201
202
  QUnit.test('should fire hidden event', function (assert) {
    assert.expect(1)
203
    var done = assert.async()
Heinrich Fenkart's avatar
Heinrich Fenkart committed
204

fat's avatar
fat committed
205
    $('<div title="tooltip title"/>')
Heinrich Fenkart's avatar
Heinrich Fenkart committed
206
      .appendTo('#qunit-fixture')
XhmikosR's avatar
XhmikosR committed
207
      .on('shown.bs.tooltip', function () {
208
        $(this).bootstrapTooltip('hide')
XhmikosR's avatar
XhmikosR committed
209
210
      })
      .on('hidden.bs.tooltip', function () {
fat's avatar
fat committed
211
        assert.ok(true, 'hidden event fired')
212
        done()
XhmikosR's avatar
XhmikosR committed
213
      })
214
      .bootstrapTooltip('show')
XhmikosR's avatar
XhmikosR committed
215
216
  })

fat's avatar
fat committed
217
218
  QUnit.test('should not fire hidden event when hide was prevented', function (assert) {
    assert.expect(1)
219
    var done = assert.async()
Heinrich Fenkart's avatar
Heinrich Fenkart committed
220

fat's avatar
fat committed
221
    $('<div title="tooltip title"/>')
Heinrich Fenkart's avatar
Heinrich Fenkart committed
222
      .appendTo('#qunit-fixture')
XhmikosR's avatar
XhmikosR committed
223
      .on('shown.bs.tooltip', function () {
224
        $(this).bootstrapTooltip('hide')
XhmikosR's avatar
XhmikosR committed
225
226
227
      })
      .on('hide.bs.tooltip', function (e) {
        e.preventDefault()
fat's avatar
fat committed
228
        assert.ok(true, 'hide event fired')
229
        done()
XhmikosR's avatar
XhmikosR committed
230
231
      })
      .on('hidden.bs.tooltip', function () {
fat's avatar
fat committed
232
        assert.ok(false, 'hidden event fired')
XhmikosR's avatar
XhmikosR committed
233
      })
234
      .bootstrapTooltip('show')
XhmikosR's avatar
XhmikosR committed
235
236
  })

fat's avatar
fat committed
237
238
239
  QUnit.test('should destroy tooltip', function (assert) {
    assert.expect(7)
    var $tooltip = $('<div/>')
Heinrich Fenkart's avatar
Heinrich Fenkart committed
240
241
242
      .bootstrapTooltip()
      .on('click.foo', function () {})

fat's avatar
fat committed
243
244
245
    assert.ok($tooltip.data('bs.tooltip'), 'tooltip has data')
    assert.ok($._data($tooltip[0], 'events').mouseover && $._data($tooltip[0], 'events').mouseout, 'tooltip has hover events')
    assert.strictEqual($._data($tooltip[0], 'events').click[0].namespace, 'foo', 'tooltip has extra click.foo event')
Heinrich Fenkart's avatar
Heinrich Fenkart committed
246
247
248
249

    $tooltip.bootstrapTooltip('show')
    $tooltip.bootstrapTooltip('destroy')

fat's avatar
fat committed
250
251
252
253
    assert.ok(!$tooltip.hasClass('in'), 'tooltip is hidden')
    assert.ok(!$._data($tooltip[0], 'bs.tooltip'), 'tooltip does not have data')
    assert.strictEqual($._data($tooltip[0], 'events').click[0].namespace, 'foo', 'tooltip still has click.foo')
    assert.ok(!$._data($tooltip[0], 'events').mouseover && !$._data($tooltip[0], 'events').mouseout, 'tooltip does not have hover events')
XhmikosR's avatar
XhmikosR committed
254
255
  })

fat's avatar
fat committed
256
257
258
  QUnit.test('should show tooltip with delegate selector on click', function (assert) {
    assert.expect(2)
    var $div = $('<div><a href="#" rel="tooltip" title="Another tooltip"/></div>')
Heinrich Fenkart's avatar
Heinrich Fenkart committed
259
260
261
262
263
264
      .appendTo('#qunit-fixture')
      .bootstrapTooltip({
        selector: 'a[rel="tooltip"]',
        trigger: 'click'
      })

fat's avatar
fat committed
265
266
    $div.find('a').trigger('click')
    assert.ok($('.tooltip').is('.fade.in'), 'tooltip is faded in')
Heinrich Fenkart's avatar
Heinrich Fenkart committed
267

fat's avatar
fat committed
268
269
    $div.find('a').trigger('click')
    assert.strictEqual($('.tooltip').length, 0, 'tooltip was removed from dom')
XhmikosR's avatar
XhmikosR committed
270
271
  })

fat's avatar
fat committed
272
273
274
  QUnit.test('should show tooltip when toggle is called', function (assert) {
    assert.expect(1)
    $('<a href="#" rel="tooltip" title="tooltip on toggle"/>')
XhmikosR's avatar
XhmikosR committed
275
      .appendTo('#qunit-fixture')
XhmikosR's avatar
XhmikosR committed
276
      .bootstrapTooltip({ trigger: 'manual' })
277
      .bootstrapTooltip('toggle')
Heinrich Fenkart's avatar
Heinrich Fenkart committed
278

fat's avatar
fat committed
279
    assert.ok($('.tooltip').is('.fade.in'), 'tooltip is faded in')
XhmikosR's avatar
XhmikosR committed
280
281
  })

fat's avatar
fat committed
282
283
  QUnit.test('should hide previously shown tooltip when toggle is called on tooltip', function (assert) {
    assert.expect(1)
Heinrich Fenkart's avatar
Heinrich Fenkart committed
284
    $('<a href="#" rel="tooltip" title="tooltip on toggle">@ResentedHook</a>')
285
      .appendTo('#qunit-fixture')
XhmikosR's avatar
XhmikosR committed
286
      .bootstrapTooltip({ trigger: 'manual' })
Heinrich Fenkart's avatar
Heinrich Fenkart committed
287
288
289
      .bootstrapTooltip('show')

    $('.tooltip').bootstrapTooltip('toggle')
fat's avatar
fat committed
290
    assert.ok($('.tooltip').not('.fade.in'), 'tooltip was faded out')
291
292
  })

fat's avatar
fat committed
293
294
295
  QUnit.test('should place tooltips inside body when container is body', function (assert) {
    assert.expect(3)
    var $tooltip = $('<a href="#" rel="tooltip" title="Another tooltip"/>')
XhmikosR's avatar
XhmikosR committed
296
      .appendTo('#qunit-fixture')
XhmikosR's avatar
XhmikosR committed
297
      .bootstrapTooltip({ container: 'body' })
298
      .bootstrapTooltip('show')
Heinrich Fenkart's avatar
Heinrich Fenkart committed
299

fat's avatar
fat committed
300
301
    assert.notEqual($('body > .tooltip').length, 0, 'tooltip is direct descendant of body')
    assert.strictEqual($('#qunit-fixture > .tooltip').length, 0, 'tooltip is not in parent')
Heinrich Fenkart's avatar
Heinrich Fenkart committed
302
303

    $tooltip.bootstrapTooltip('hide')
fat's avatar
fat committed
304
    assert.strictEqual($('body > .tooltip').length, 0, 'tooltip was removed from dom')
XhmikosR's avatar
XhmikosR committed
305
306
  })

fat's avatar
fat committed
307
308
  QUnit.test('should add position class before positioning so that position-specific styles are taken into account', function (assert) {
    assert.expect(1)
Heinrich Fenkart's avatar
Heinrich Fenkart committed
309
    var styles = '<style>'
fat's avatar
fat committed
310
311
        + '.tooltip.right { white-space: nowrap; }'
        + '.tooltip.right .tooltip-inner { max-width: none; }'
Heinrich Fenkart's avatar
Heinrich Fenkart committed
312
        + '</style>'
313
    var $styles = $(styles).appendTo('head')
Heinrich Fenkart's avatar
Heinrich Fenkart committed
314

fat's avatar
fat committed
315
    var $container = $('<div/>').appendTo('#qunit-fixture')
fat's avatar
fat committed
316
    var $target = $('<a href="#" rel="tooltip" title="very very very very very very very very long tooltip in one line"/>')
Heinrich Fenkart's avatar
Heinrich Fenkart committed
317
318
319
320
321
322
323
      .appendTo($container)
      .bootstrapTooltip({
        placement: 'right',
        viewport: null
      })
      .bootstrapTooltip('show')
    var $tooltip = $container.find('.tooltip')
XhmikosR's avatar
XhmikosR committed
324
325

    // this is some dumb hack shit because sub pixels in firefox
Heinrich Fenkart's avatar
Heinrich Fenkart committed
326
327
    var top = Math.round($target.offset().top + ($target[0].offsetHeight / 2) - ($tooltip[0].offsetHeight / 2))
    var top2 = Math.round($tooltip.offset().top)
XhmikosR's avatar
XhmikosR committed
328
    var topDiff = top - top2
fat's avatar
fat committed
329
    assert.ok(topDiff <= 1 && topDiff >= -1)
Heinrich Fenkart's avatar
Heinrich Fenkart committed
330
331
332
333
    $target.bootstrapTooltip('hide')

    $container.remove()
    $styles.remove()
XhmikosR's avatar
XhmikosR committed
334
335
  })

fat's avatar
fat committed
336
337
338
  QUnit.test('should use title attribute for tooltip text', function (assert) {
    assert.expect(2)
    var $tooltip = $('<a href="#" rel="tooltip" title="Simple tooltip"/>')
XhmikosR's avatar
XhmikosR committed
339
      .appendTo('#qunit-fixture')
Heinrich Fenkart's avatar
Heinrich Fenkart committed
340
341
342
      .bootstrapTooltip()

    $tooltip.bootstrapTooltip('show')
fat's avatar
fat committed
343
    assert.strictEqual($('.tooltip').children('.tooltip-inner').text(), 'Simple tooltip', 'title from title attribute is set')
Heinrich Fenkart's avatar
Heinrich Fenkart committed
344
345

    $tooltip.bootstrapTooltip('hide')
fat's avatar
fat committed
346
    assert.strictEqual($('.tooltip').length, 0, 'tooltip removed from dom')
XhmikosR's avatar
XhmikosR committed
347
348
  })

fat's avatar
fat committed
349
350
351
  QUnit.test('should prefer title attribute over title option', function (assert) {
    assert.expect(2)
    var $tooltip = $('<a href="#" rel="tooltip" title="Simple tooltip"/>')
XhmikosR's avatar
XhmikosR committed
352
      .appendTo('#qunit-fixture')
353
      .bootstrapTooltip({
XhmikosR's avatar
XhmikosR committed
354
355
        title: 'This is a tooltip with some content'
      })
Heinrich Fenkart's avatar
Heinrich Fenkart committed
356
357

    $tooltip.bootstrapTooltip('show')
fat's avatar
fat committed
358
    assert.strictEqual($('.tooltip').children('.tooltip-inner').text(), 'Simple tooltip', 'title is set from title attribute while preferred over title option')
Heinrich Fenkart's avatar
Heinrich Fenkart committed
359
360

    $tooltip.bootstrapTooltip('hide')
fat's avatar
fat committed
361
    assert.strictEqual($('.tooltip').length, 0, 'tooltip removed from dom')
XhmikosR's avatar
XhmikosR committed
362
363
  })

fat's avatar
fat committed
364
365
366
  QUnit.test('should use title option', function (assert) {
    assert.expect(2)
    var $tooltip = $('<a href="#" rel="tooltip"/>')
XhmikosR's avatar
XhmikosR committed
367
      .appendTo('#qunit-fixture')
368
      .bootstrapTooltip({
XhmikosR's avatar
XhmikosR committed
369
370
        title: 'This is a tooltip with some content'
      })
Heinrich Fenkart's avatar
Heinrich Fenkart committed
371
372

    $tooltip.bootstrapTooltip('show')
fat's avatar
fat committed
373
    assert.strictEqual($('.tooltip').children('.tooltip-inner').text(), 'This is a tooltip with some content', 'title from title option is set')
Heinrich Fenkart's avatar
Heinrich Fenkart committed
374
375

    $tooltip.bootstrapTooltip('hide')
fat's avatar
fat committed
376
    assert.strictEqual($('.tooltip').length, 0, 'tooltip removed from dom')
Heinrich Fenkart's avatar
Heinrich Fenkart committed
377
378
  })

fat's avatar
fat committed
379
380
381
  QUnit.test('should be placed dynamically to viewport with the dynamic placement option', function (assert) {
    assert.expect(6)
    var $style = $('<style> div[rel="tooltip"] { position: absolute; } #qunit-fixture { top: inherit; left: inherit } </style>').appendTo('head')
Heinrich Fenkart's avatar
Heinrich Fenkart committed
382
383
    var $container = $('<div/>')
      .css({
fat's avatar
fat committed
384
385
        position: 'relative',
        height: '100%'
Heinrich Fenkart's avatar
Heinrich Fenkart committed
386
      })
fat's avatar
fat committed
387
      .appendTo('#qunit-fixture')
Heinrich Fenkart's avatar
Heinrich Fenkart committed
388
389
390

    var $topTooltip = $('<div style="left: 0; top: 0;" rel="tooltip" title="Top tooltip">Top Dynamic Tooltip</div>')
      .appendTo($container)
fat's avatar
fat committed
391
      .bootstrapTooltip({ placement: 'auto', viewport: '#qunit-fixture' })
XhmikosR's avatar
XhmikosR committed
392

Heinrich Fenkart's avatar
Heinrich Fenkart committed
393
    $topTooltip.bootstrapTooltip('show')
fat's avatar
fat committed
394
    assert.ok($('.tooltip').is('.bottom'), 'top positioned tooltip is dynamically positioned to bottom')
XhmikosR's avatar
XhmikosR committed
395

Heinrich Fenkart's avatar
Heinrich Fenkart committed
396
    $topTooltip.bootstrapTooltip('hide')
fat's avatar
fat committed
397
    assert.strictEqual($('.tooltip').length, 0, 'top positioned tooltip removed from dom')
XhmikosR's avatar
XhmikosR committed
398

Heinrich Fenkart's avatar
Heinrich Fenkart committed
399
400
    var $rightTooltip = $('<div style="right: 0;" rel="tooltip" title="Right tooltip">Right Dynamic Tooltip</div>')
      .appendTo($container)
fat's avatar
fat committed
401
      .bootstrapTooltip({ placement: 'right auto', viewport: '#qunit-fixture' })
XhmikosR's avatar
XhmikosR committed
402

Heinrich Fenkart's avatar
Heinrich Fenkart committed
403
    $rightTooltip.bootstrapTooltip('show')
fat's avatar
fat committed
404
    assert.ok($('.tooltip').is('.left'), 'right positioned tooltip is dynamically positioned left')
XhmikosR's avatar
XhmikosR committed
405

Heinrich Fenkart's avatar
Heinrich Fenkart committed
406
    $rightTooltip.bootstrapTooltip('hide')
fat's avatar
fat committed
407
    assert.strictEqual($('.tooltip').length, 0, 'right positioned tooltip removed from dom')
Heinrich Fenkart's avatar
Heinrich Fenkart committed
408
409
410

    var $leftTooltip = $('<div style="left: 0;" rel="tooltip" title="Left tooltip">Left Dynamic Tooltip</div>')
      .appendTo($container)
fat's avatar
fat committed
411
      .bootstrapTooltip({ placement: 'auto left', viewport: '#qunit-fixture' })
XhmikosR's avatar
XhmikosR committed
412

Heinrich Fenkart's avatar
Heinrich Fenkart committed
413
    $leftTooltip.bootstrapTooltip('show')
fat's avatar
fat committed
414
    assert.ok($('.tooltip').is('.right'), 'left positioned tooltip is dynamically positioned right')
XhmikosR's avatar
XhmikosR committed
415

Heinrich Fenkart's avatar
Heinrich Fenkart committed
416
    $leftTooltip.bootstrapTooltip('hide')
fat's avatar
fat committed
417
    assert.strictEqual($('.tooltip').length, 0, 'left positioned tooltip removed from dom')
Heinrich Fenkart's avatar
Heinrich Fenkart committed
418
419
420

    $container.remove()
    $style.remove()
XhmikosR's avatar
XhmikosR committed
421
  })
422

fat's avatar
fat committed
423
424
  QUnit.test('should position tip on top if viewport has enough space and placement is "auto top"', function (assert) {
    assert.expect(2)
425
426
    var styles = '<style>'
        + 'body { padding-top: 100px; }'
427
        + '#section { height: 300px; border: 1px solid red; padding-top: 50px }'
428
429
430
431
        + 'div[rel="tooltip"] { width: 150px; border: 1px solid blue; }'
        + '</style>'
    var $styles = $(styles).appendTo('head')

432
    var $container = $('<div id="section"/>').appendTo('#qunit-fixture')
433
434
435
436
    var $target = $('<div rel="tooltip" title="tip"/>')
      .appendTo($container)
      .bootstrapTooltip({
        placement: 'auto top',
437
        viewport: '#section'
438
439
440
      })

    $target.bootstrapTooltip('show')
fat's avatar
fat committed
441
    assert.ok($('.tooltip').is('.top'), 'top positioned tooltip is dynamically positioned to top')
442
443

    $target.bootstrapTooltip('hide')
fat's avatar
fat committed
444
    assert.strictEqual($('.tooltip').length, 0, 'tooltip removed from dom')
445
446
447
448

    $styles.remove()
  })

fat's avatar
fat committed
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
  QUnit.test('should position tip on top if viewport has enough space and is not parent', function (assert) {
    assert.expect(2)
    var styles = '<style>'
        + '#section { height: 300px; border: 1px solid red; margin-top: 100px; }'
        + 'div[rel="tooltip"] { width: 150px; border: 1px solid blue; }'
        + '</style>'
    var $styles = $(styles).appendTo('head')

    var $container = $('<div id="section"/>').appendTo('#qunit-fixture')
    var $target = $('<div rel="tooltip" title="tip"/>')
      .appendTo($container)
      .bootstrapTooltip({
        placement: 'auto top',
        viewport: '#qunit-fixture'
      })

    $target.bootstrapTooltip('show')
    assert.ok($('.tooltip').is('.top'), 'top positioned tooltip is dynamically positioned to top')

    $target.bootstrapTooltip('hide')
    assert.strictEqual($('.tooltip').length, 0, 'tooltip removed from dom')

    $styles.remove()
  })

  QUnit.test('should position tip on bottom if the tip\'s dimension exceeds the viewport area and placement is "auto top"', function (assert) {
    assert.expect(2)
476
477
    var styles = '<style>'
        + 'body { padding-top: 100px; }'
478
        + '#section { height: 300px; border: 1px solid red; }'
479
480
481
482
        + 'div[rel="tooltip"] { width: 150px; border: 1px solid blue; }'
        + '</style>'
    var $styles = $(styles).appendTo('head')

483
    var $container = $('<div id="section"/>').appendTo('#qunit-fixture')
484
485
486
487
    var $target = $('<div rel="tooltip" title="tip"/>')
      .appendTo($container)
      .bootstrapTooltip({
        placement: 'auto top',
488
        viewport: '#section'
489
490
491
      })

    $target.bootstrapTooltip('show')
fat's avatar
fat committed
492
    assert.ok($('.tooltip').is('.bottom'), 'top positioned tooltip is dynamically positioned to bottom')
493
494

    $target.bootstrapTooltip('hide')
fat's avatar
fat committed
495
    assert.strictEqual($('.tooltip').length, 0, 'tooltip removed from dom')
496
497
498
499

    $styles.remove()
  })

fat's avatar
fat committed
500
501
  QUnit.test('should display the tip on top whenever scrollable viewport has enough room if the given placement is "auto top"', function (assert) {
    assert.expect(2)
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
    var styles = '<style>'
        + '#scrollable-div { height: 200px; overflow: auto; }'
        + '.tooltip-item { margin: 200px 0 400px; width: 150px; }'
        + '</style>'
    var $styles = $(styles).appendTo('head')

    var $container = $('<div id="scrollable-div"/>').appendTo('#qunit-fixture')
    var $target = $('<div rel="tooltip" title="tip" class="tooltip-item">Tooltip Item</div>')
      .appendTo($container)
      .bootstrapTooltip({
        placement: 'top auto',
        viewport: '#scrollable-div'
      })

    $('#scrollable-div').scrollTop(100)

    $target.bootstrapTooltip('show')
fat's avatar
fat committed
519
    assert.ok($('.tooltip').is('.fade.top.in'), 'has correct classes applied')
520
521

    $target.bootstrapTooltip('hide')
fat's avatar
fat committed
522
    assert.strictEqual($('.tooltip').length, 0, 'tooltip removed from dom')
523
524
525
526

    $styles.remove()
  })

fat's avatar
fat committed
527
528
  QUnit.test('should display the tip on bottom whenever scrollable viewport doesn\'t have enough room if the given placement is "auto top"', function (assert) {
    assert.expect(2)
529
530
    var styles = '<style>'
        + '#scrollable-div { height: 200px; overflow: auto; }'
531
        + '.tooltip-item { padding: 200px 0 400px; width: 150px; }'
532
533
534
535
536
537
538
539
540
541
542
543
544
545
        + '</style>'
    var $styles = $(styles).appendTo('head')

    var $container = $('<div id="scrollable-div"/>').appendTo('#qunit-fixture')
    var $target = $('<div rel="tooltip" title="tip" class="tooltip-item">Tooltip Item</div>')
      .appendTo($container)
      .bootstrapTooltip({
        placement: 'top auto',
        viewport: '#scrollable-div'
      })

    $('#scrollable-div').scrollTop(200)

    $target.bootstrapTooltip('show')
fat's avatar
fat committed
546
    assert.ok($('.tooltip').is('.fade.bottom.in'), 'has correct classes applied')
547
548

    $target.bootstrapTooltip('hide')
fat's avatar
fat committed
549
    assert.strictEqual($('.tooltip').length, 0, 'tooltip removed from dom')
550
551
552
553

    $styles.remove()
  })

fat's avatar
fat committed
554
555
  QUnit.test('should display the tip on bottom whenever scrollable viewport has enough room if the given placement is "auto bottom"', function (assert) {
    assert.expect(2)
556
557
    var styles = '<style>'
        + '#scrollable-div { height: 200px; overflow: auto; }'
558
559
560
        + '.spacer { height: 400px; }'
        + '.spacer:first-child { height: 200px; }'
        + '.tooltip-item { width: 150px; }'
561
562
563
564
565
566
        + '</style>'
    var $styles = $(styles).appendTo('head')

    var $container = $('<div id="scrollable-div"/>').appendTo('#qunit-fixture')
    var $target = $('<div rel="tooltip" title="tip" class="tooltip-item">Tooltip Item</div>')
      .appendTo($container)
567
568
      .before('<div class="spacer"/>')
      .after('<div class="spacer"/>')
569
570
571
572
573
574
575
576
      .bootstrapTooltip({
        placement: 'bottom auto',
        viewport: '#scrollable-div'
      })

    $('#scrollable-div').scrollTop(200)

    $target.bootstrapTooltip('show')
fat's avatar
fat committed
577
    assert.ok($('.tooltip').is('.fade.bottom.in'), 'has correct classes applied')
578
579

    $target.bootstrapTooltip('hide')
fat's avatar
fat committed
580
    assert.strictEqual($('.tooltip').length, 0, 'tooltip removed from dom')
581
582
583
584

    $styles.remove()
  })

fat's avatar
fat committed
585
586
  QUnit.test('should display the tip on top whenever scrollable viewport doesn\'t have enough room if the given placement is "auto bottom"', function (assert) {
    assert.expect(2)
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
    var styles = '<style>'
        + '#scrollable-div { height: 200px; overflow: auto; }'
        + '.tooltip-item { margin-top: 400px; width: 150px; }'
        + '</style>'
    var $styles = $(styles).appendTo('head')

    var $container = $('<div id="scrollable-div"/>').appendTo('#qunit-fixture')
    var $target = $('<div rel="tooltip" title="tip" class="tooltip-item">Tooltip Item</div>')
      .appendTo($container)
      .bootstrapTooltip({
        placement: 'bottom auto',
        viewport: '#scrollable-div'
      })

    $('#scrollable-div').scrollTop(400)

    $target.bootstrapTooltip('show')
fat's avatar
fat committed
604
    assert.ok($('.tooltip').is('.fade.top.in'), 'has correct classes applied')
605
606

    $target.bootstrapTooltip('hide')
fat's avatar
fat committed
607
    assert.strictEqual($('.tooltip').length, 0, 'tooltip removed from dom')
608
609
610
611

    $styles.remove()
  })

fat's avatar
fat committed
612
613
  QUnit.test('should adjust the tip\'s top position when up against the top of the viewport', function (assert) {
    assert.expect(2)
Heinrich Fenkart's avatar
Heinrich Fenkart committed
614
615
616
617
    var styles = '<style>'
        + '.tooltip .tooltip-inner { width: 200px; height: 200px; max-width: none; }'
        + 'a[rel="tooltip"] { position: fixed; }'
        + '</style>'
618
    var $styles = $(styles).appendTo('head')
Heinrich Fenkart's avatar
Heinrich Fenkart committed
619

fat's avatar
fat committed
620
    var $container = $('<div/>').appendTo('#qunit-fixture')
fat's avatar
fat committed
621
    var $target = $('<a href="#" rel="tooltip" title="tip" style="top: 0px; left: 0px;"/>')
Heinrich Fenkart's avatar
Heinrich Fenkart committed
622
623
624
625
626
627
628
629
630
631
      .appendTo($container)
      .bootstrapTooltip({
        placement: 'right',
        viewport: {
          selector: 'body',
          padding: 12
        }
      })

    $target.bootstrapTooltip('show')
fat's avatar
fat committed
632
    assert.strictEqual(Math.round($container.find('.tooltip').offset().top), 12)
633

Heinrich Fenkart's avatar
Heinrich Fenkart committed
634
    $target.bootstrapTooltip('hide')
fat's avatar
fat committed
635
    assert.strictEqual($('.tooltip').length, 0, 'tooltip removed from dom')
636

Heinrich Fenkart's avatar
Heinrich Fenkart committed
637
    $styles.remove()
638
639
  })

fat's avatar
fat committed
640
641
  QUnit.test('should adjust the tip\'s top position when up against the bottom of the viewport', function (assert) {
    assert.expect(2)
Heinrich Fenkart's avatar
Heinrich Fenkart committed
642
643
644
645
    var styles = '<style>'
        + '.tooltip .tooltip-inner { width: 200px; height: 200px; max-width: none; }'
        + 'a[rel="tooltip"] { position: fixed; }'
        + '</style>'
646
    var $styles = $(styles).appendTo('head')
Heinrich Fenkart's avatar
Heinrich Fenkart committed
647

fat's avatar
fat committed
648
    var $container = $('<div/>').appendTo('#qunit-fixture')
fat's avatar
fat committed
649
    var $target = $('<a href="#" rel="tooltip" title="tip" style="bottom: 0px; left: 0px;"/>')
Heinrich Fenkart's avatar
Heinrich Fenkart committed
650
651
652
653
654
655
656
657
      .appendTo($container)
      .bootstrapTooltip({
        placement: 'right',
        viewport: {
          selector: 'body',
          padding: 12
        }
      })
658

Heinrich Fenkart's avatar
Heinrich Fenkart committed
659
660
    $target.bootstrapTooltip('show')
    var $tooltip = $container.find('.tooltip')
fat's avatar
fat committed
661
    assert.strictEqual(Math.round($tooltip.offset().top), Math.round($(window).height() - 12 - $tooltip[0].offsetHeight))
662

Heinrich Fenkart's avatar
Heinrich Fenkart committed
663
    $target.bootstrapTooltip('hide')
fat's avatar
fat committed
664
    assert.strictEqual($('.tooltip').length, 0, 'tooltip removed from dom')
Heinrich Fenkart's avatar
Heinrich Fenkart committed
665
666
667

    $container.remove()
    $styles.remove()
668
669
  })

fat's avatar
fat committed
670
671
  QUnit.test('should adjust the tip\'s left position when up against the left of the viewport', function (assert) {
    assert.expect(2)
Heinrich Fenkart's avatar
Heinrich Fenkart committed
672
673
674
675
    var styles = '<style>'
        + '.tooltip .tooltip-inner { width: 200px; height: 200px; max-width: none; }'
        + 'a[rel="tooltip"] { position: fixed; }'
        + '</style>'
676
    var $styles = $(styles).appendTo('head')
Heinrich Fenkart's avatar
Heinrich Fenkart committed
677

fat's avatar
fat committed
678
    var $container = $('<div/>').appendTo('#qunit-fixture')
fat's avatar
fat committed
679
    var $target = $('<a href="#" rel="tooltip" title="tip" style="top: 0px; left: 0px;"/>')
Heinrich Fenkart's avatar
Heinrich Fenkart committed
680
681
682
683
684
685
686
687
688
689
      .appendTo($container)
      .bootstrapTooltip({
        placement: 'bottom',
        viewport: {
          selector: 'body',
          padding: 12
        }
      })

    $target.bootstrapTooltip('show')
fat's avatar
fat committed
690
    assert.strictEqual(Math.round($container.find('.tooltip').offset().left), 12)
691

Heinrich Fenkart's avatar
Heinrich Fenkart committed
692
    $target.bootstrapTooltip('hide')
fat's avatar
fat committed
693
    assert.strictEqual($('.tooltip').length, 0, 'tooltip removed from dom')
694

Heinrich Fenkart's avatar
Heinrich Fenkart committed
695
696
    $container.remove()
    $styles.remove()
697
698
  })

fat's avatar
fat committed
699
700
  QUnit.test('should adjust the tip\'s left position when up against the right of the viewport', function (assert) {
    assert.expect(2)
Heinrich Fenkart's avatar
Heinrich Fenkart committed
701
702
703
704
    var styles = '<style>'
        + '.tooltip .tooltip-inner { width: 200px; height: 200px; max-width: none; }'
        + 'a[rel="tooltip"] { position: fixed; }'
        + '</style>'
705
    var $styles = $(styles).appendTo('head')
706

Heinrich Fenkart's avatar
Heinrich Fenkart committed
707
    var $container = $('<div/>').appendTo('body')
fat's avatar
fat committed
708
    var $target = $('<a href="#" rel="tooltip" title="tip" style="top: 0px; right: 0px;"/>')
Heinrich Fenkart's avatar
Heinrich Fenkart committed
709
710
711
712
713
714
715
716
717
718
719
      .appendTo($container)
      .bootstrapTooltip({
        placement: 'bottom',
        viewport: {
          selector: 'body',
          padding: 12
        }
      })

    $target.bootstrapTooltip('show')
    var $tooltip = $container.find('.tooltip')
fat's avatar
fat committed
720
    assert.strictEqual(Math.round($tooltip.offset().left), Math.round($(window).width() - 12 - $tooltip[0].offsetWidth))
721

Heinrich Fenkart's avatar
Heinrich Fenkart committed
722
    $target.bootstrapTooltip('hide')
fat's avatar
fat committed
723
    assert.strictEqual($('.tooltip').length, 0, 'tooltip removed from dom')
Heinrich Fenkart's avatar
Heinrich Fenkart committed
724
725
726

    $container.remove()
    $styles.remove()
727
728
  })

fat's avatar
fat committed
729
730
  QUnit.test('should adjust the tip when up against the right of an arbitrary viewport', function (assert) {
    assert.expect(2)
Heinrich Fenkart's avatar
Heinrich Fenkart committed
731
732
733
734
735
    var styles = '<style>'
        + '.tooltip, .tooltip .tooltip-inner { width: 200px; height: 200px; max-width: none; }'
        + '.container-viewport { position: absolute; top: 50px; left: 60px; width: 300px; height: 300px; }'
        + 'a[rel="tooltip"] { position: fixed; }'
        + '</style>'
736
    var $styles = $(styles).appendTo('head')
Heinrich Fenkart's avatar
Heinrich Fenkart committed
737
738

    var $container = $('<div class="container-viewport"/>').appendTo(document.body)
fat's avatar
fat committed
739
    var $target = $('<a href="#" rel="tooltip" title="tip" style="top: 50px; left: 350px;"/>')
Heinrich Fenkart's avatar
Heinrich Fenkart committed
740
741
742
743
744
745
746
747
      .appendTo($container)
      .bootstrapTooltip({
        placement: 'bottom',
        viewport: '.container-viewport'
      })

    $target.bootstrapTooltip('show')
    var $tooltip = $container.find('.tooltip')
fat's avatar
fat committed
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
    assert.strictEqual(Math.round($tooltip.offset().left), Math.round(60 + $container.width() - $tooltip[0].offsetWidth))

    $target.bootstrapTooltip('hide')
    assert.strictEqual($('.tooltip').length, 0, 'tooltip removed from dom')

    $container.remove()
    $styles.remove()
  })

  QUnit.test('should get viewport element from function', function (assert) {
    assert.expect(3)
    var styles = '<style>'
        + '.tooltip, .tooltip .tooltip-inner { width: 200px; height: 200px; max-width: none; }'
        + '.container-viewport { position: absolute; top: 50px; left: 60px; width: 300px; height: 300px; }'
        + 'a[rel="tooltip"] { position: fixed; }'
        + '</style>'
    var $styles = $(styles).appendTo('head')

    var $container = $('<div class="container-viewport"/>').appendTo(document.body)
    var $target = $('<a href="#" rel="tooltip" title="tip" style="top: 50px; left: 350px;"/>').appendTo($container)
    $target
      .bootstrapTooltip({
        placement: 'bottom',
        viewport: function ($element) {
          assert.strictEqual($element[0], $target[0], 'viewport function was passed target as argument')
          return ($element.closest('.container-viewport'))
        }
      })

    $target.bootstrapTooltip('show')
    var $tooltip = $container.find('.tooltip')
    assert.strictEqual(Math.round($tooltip.offset().left), Math.round(60 + $container.width() - $tooltip[0].offsetWidth))

    $target.bootstrapTooltip('hide')
    assert.strictEqual($('.tooltip').length, 0, 'tooltip removed from dom')

    $container.remove()
    $styles.remove()
  })

  QUnit.test('should not misplace the tip when the right edge offset is greater or equal than the viewport width', function (assert) {
    assert.expect(2)
    var styles = '<style>'
        + '.tooltip, .tooltip *, .tooltip *:before, .tooltip *:after { box-sizing: border-box; }'
        + '.container-viewport, .container-viewport *, .container-viewport *:before, .container-viewport *:after { box-sizing: border-box; }'
        + '.tooltip, .tooltip .tooltip-inner { width: 50px; height: 50px; max-width: none; background: red; }'
        + '.container-viewport { padding: 100px; margin-left: 100px; width: 100px; }'
        + '</style>'
    var $styles = $(styles).appendTo('head')

    var $container = $('<div class="container-viewport"/>').appendTo(document.body)
    var $target = $('<a href="#" rel="tooltip" title="tip">foobar</a>')
      .appendTo($container)
      .bootstrapTooltip({
        viewport: '.container-viewport'
      })

    $target.bootstrapTooltip('show')
    var $tooltip = $container.find('.tooltip')
    assert.strictEqual(Math.round($tooltip.offset().left), Math.round($target.position().left + $target.width() / 2 - $tooltip[0].offsetWidth / 2))
808

Heinrich Fenkart's avatar
Heinrich Fenkart committed
809
    $target.bootstrapTooltip('hide')
fat's avatar
fat committed
810
    assert.strictEqual($('.tooltip').length, 0, 'tooltip removed from dom')
811

Heinrich Fenkart's avatar
Heinrich Fenkart committed
812
813
    $container.remove()
    $styles.remove()
814
  })
Chris Rebert's avatar
Chris Rebert committed
815

fat's avatar
fat committed
816
817
  QUnit.test('should not error when trying to show an auto-placed tooltip that has been removed from the dom', function (assert) {
    assert.expect(1)
Heinrich Fenkart's avatar
Heinrich Fenkart committed
818
    var passed = true
fat's avatar
fat committed
819
    var $tooltip = $('<a href="#" rel="tooltip" title="Another tooltip"/>')
Heinrich Fenkart's avatar
Heinrich Fenkart committed
820
      .appendTo('#qunit-fixture')
Chris Rebert's avatar
Chris Rebert committed
821
      .one('show.bs.tooltip', function () {
Heinrich Fenkart's avatar
Heinrich Fenkart committed
822
        $(this).remove()
Chris Rebert's avatar
Chris Rebert committed
823
824
825
826
      })
      .bootstrapTooltip({ placement: 'auto' })

    try {
Heinrich Fenkart's avatar
Heinrich Fenkart committed
827
828
      $tooltip.bootstrapTooltip('show')
    } catch (err) {
Chris Rebert's avatar
Chris Rebert committed
829
830
831
832
      passed = false
      console.log(err)
    }

fat's avatar
fat committed
833
    assert.ok(passed, '.tooltip(\'show\') should not throw an error if element no longer is in dom')
Chris Rebert's avatar
Chris Rebert committed
834
  })
fat's avatar
fat committed
835

fat's avatar
fat committed
836
837
  QUnit.test('should place tooltip on top of element', function (assert) {
    assert.expect(1)
838
    var done = assert.async()
fat's avatar
fat committed
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868

    var containerHTML = '<div>'
        + '<p style="margin-top: 200px">'
        + '<a href="#" title="very very very very very very very long tooltip">Hover me</a>'
        + '</p>'
        + '</div>'

    var $container = $(containerHTML)
      .css({
        position: 'absolute',
        bottom: 0,
        left: 0,
        textAlign: 'right',
        width: 300,
        height: 300
      })
      .appendTo('#qunit-fixture')

    var $trigger = $container
      .find('a')
      .css('margin-top', 200)
      .bootstrapTooltip({
        placement: 'top',
        animate: false
      })
      .bootstrapTooltip('show')

    var $tooltip = $container.find('.tooltip')

    setTimeout(function () {
fat's avatar
fat committed
869
      assert.ok(Math.round($tooltip.offset().top + $tooltip.outerHeight()) <= Math.round($trigger.offset().top))
870
      done()
fat's avatar
fat committed
871
872
873
    }, 0)
  })

fat's avatar
fat committed
874
875
  QUnit.test('should place tooltip inside viewport', function (assert) {
    assert.expect(1)
876
    var done = assert.async()
fat's avatar
fat committed
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900

    var $container = $('<div/>')
      .css({
        position: 'absolute',
        width: 200,
        height: 200,
        bottom: 0,
        left: 0
      })
      .appendTo('#qunit-fixture')

    $('<a href="#" title="Very very very very very very very very long tooltip">Hover me</a>')
      .css({
        position: 'absolute',
        top: 0,
        left: 0
      })
      .appendTo($container)
      .bootstrapTooltip({
        placement: 'top'
      })
      .bootstrapTooltip('show')

    setTimeout(function () {
fat's avatar
fat committed
901
      assert.ok($('.tooltip').offset().left >= 0)
902
      done()
fat's avatar
fat committed
903
904
905
    }, 0)
  })

fat's avatar
fat committed
906
907
  QUnit.test('should show tooltip if leave event hasn\'t occurred before delay expires', function (assert) {
    assert.expect(2)
908
    var done = assert.async()
fat's avatar
fat committed
909

fat's avatar
fat committed
910
    var $tooltip = $('<a href="#" rel="tooltip" title="Another tooltip"/>')
fat's avatar
fat committed
911
      .appendTo('#qunit-fixture')
912
      .bootstrapTooltip({ delay: 150 })
fat's avatar
fat committed
913
914

    setTimeout(function () {
fat's avatar
fat committed
915
      assert.ok(!$('.tooltip').is('.fade.in'), '100ms: tooltip is not faded in')
916
    }, 100)
fat's avatar
fat committed
917
918

    setTimeout(function () {
fat's avatar
fat committed
919
      assert.ok($('.tooltip').is('.fade.in'), '200ms: tooltip is faded in')
920
      done()
921
    }, 200)
fat's avatar
fat committed
922
923
924
925

    $tooltip.trigger('mouseenter')
  })

fat's avatar
fat committed
926
927
  QUnit.test('should not show tooltip if leave event occurs before delay expires', function (assert) {
    assert.expect(2)
928
    var done = assert.async()
fat's avatar
fat committed
929

fat's avatar
fat committed
930
    var $tooltip = $('<a href="#" rel="tooltip" title="Another tooltip"/>')
fat's avatar
fat committed
931
      .appendTo('#qunit-fixture')
932
      .bootstrapTooltip({ delay: 150 })
fat's avatar
fat committed
933
934

    setTimeout(function () {
fat's avatar
fat committed
935
      assert.ok(!$('.tooltip').is('.fade.in'), '100ms: tooltip not faded in')
fat's avatar
fat committed
936
      $tooltip.trigger('mouseout')
937
    }, 100)
fat's avatar
fat committed
938
939

    setTimeout(function () {
fat's avatar
fat committed
940
      assert.ok(!$('.tooltip').is('.fade.in'), '200ms: tooltip not faded in')
941
      done()
942
    }, 200)
fat's avatar
fat committed
943
944
945
946

    $tooltip.trigger('mouseenter')
  })

fat's avatar
fat committed
947
948
  QUnit.test('should not hide tooltip if leave event occurs and enter event occurs within the hide delay', function (assert) {
    assert.expect(3)
949
    var done = assert.async()
fat's avatar
fat committed
950

fat's avatar
fat committed
951
    var $tooltip = $('<a href="#" rel="tooltip" title="Another tooltip"/>')
fat's avatar
fat committed
952
      .appendTo('#qunit-fixture')
953
      .bootstrapTooltip({ delay: { show: 0, hide: 150 }})
fat's avatar
fat committed
954
955

    setTimeout(function () {
fat's avatar
fat committed
956
      assert.ok($('.tooltip').is('.fade.in'), '1ms: tooltip faded in')
fat's avatar
fat committed
957
958
959
      $tooltip.trigger('mouseout')

      setTimeout(function () {
fat's avatar
fat committed
960
        assert.ok($('.tooltip').is('.fade.in'), '100ms: tooltip still faded in')
fat's avatar
fat committed
961
        $tooltip.trigger('mouseenter')
962
      }, 100)
fat's avatar
fat committed
963
964

      setTimeout(function () {
fat's avatar
fat committed
965
        assert.ok($('.tooltip').is('.fade.in'), '200ms: tooltip still faded in')
966
        done()
967
      }, 200)
fat's avatar
fat committed
968
969
970
971
972
    }, 0)

    $tooltip.trigger('mouseenter')
  })

fat's avatar
fat committed
973
974
  QUnit.test('should not show tooltip if leave event occurs before delay expires', function (assert) {
    assert.expect(2)
975
    var done = assert.async()
fat's avatar
fat committed
976

fat's avatar
fat committed
977
    var $tooltip = $('<a href="#" rel="tooltip" title="Another tooltip"/>')
fat's avatar
fat committed
978
      .appendTo('#qunit-fixture')
979
      .bootstrapTooltip({ delay: 150 })
fat's avatar
fat committed
980
981

    setTimeout(function () {
fat's avatar
fat committed
982
      assert.ok(!$('.tooltip').is('.fade.in'), '100ms: tooltip not faded in')
fat's avatar
fat committed
983
      $tooltip.trigger('mouseout')
984
    }, 100)
fat's avatar
fat committed
985
986

    setTimeout(function () {
fat's avatar
fat committed
987
      assert.ok(!$('.tooltip').is('.fade.in'), '200ms: tooltip not faded in')
988
      done()
989
    }, 200)
fat's avatar
fat committed
990
991
992
993

    $tooltip.trigger('mouseenter')
  })

fat's avatar
fat committed
994
995
  QUnit.test('should not show tooltip if leave event occurs before delay expires, even if hide delay is 0', function (assert) {
    assert.expect(2)
996
    var done = assert.async()
fat's avatar
fat committed
997

fat's avatar
fat committed
998
    var $tooltip = $('<a href="#" rel="tooltip" title="Another tooltip"/>')
fat's avatar
fat committed
999
      .appendTo('#qunit-fixture')
1000
      .bootstrapTooltip({ delay: { show: 150, hide: 0 }})
For faster browsing, not all history is shown. View entire blame