diff options
Diffstat (limited to 'js/tests/unit')
| -rw-r--r-- | js/tests/unit/affix.js | 107 | ||||
| -rw-r--r-- | js/tests/unit/alert.js | 3 | ||||
| -rw-r--r-- | js/tests/unit/button.js | 58 | ||||
| -rw-r--r-- | js/tests/unit/carousel.js | 192 | ||||
| -rw-r--r-- | js/tests/unit/collapse.js | 4 | ||||
| -rw-r--r-- | js/tests/unit/modal.js | 2 | ||||
| -rw-r--r-- | js/tests/unit/popover.js | 29 | ||||
| -rw-r--r-- | js/tests/unit/scrollspy.js | 131 | ||||
| -rw-r--r-- | js/tests/unit/tooltip.js | 648 |
9 files changed, 300 insertions, 874 deletions
diff --git a/js/tests/unit/affix.js b/js/tests/unit/affix.js deleted file mode 100644 index 3a6918f86..000000000 --- a/js/tests/unit/affix.js +++ /dev/null @@ -1,107 +0,0 @@ -$(function () { - 'use strict'; - - QUnit.module('affix plugin') - - QUnit.test('should be defined on jquery object', function (assert) { - assert.expect(1) - assert.ok($(document.body).affix, 'affix method is defined') - }) - - QUnit.module('affix', { - beforeEach: function () { - // Run all tests in noConflict mode -- it's the only way to ensure that the plugin works in noConflict mode - $.fn.bootstrapAffix = $.fn.affix.noConflict() - }, - afterEach: function () { - $.fn.affix = $.fn.bootstrapAffix - delete $.fn.bootstrapAffix - } - }) - - QUnit.test('should provide no conflict', function (assert) { - assert.expect(1) - assert.strictEqual($.fn.affix, undefined, 'affix was set back to undefined (org value)') - }) - - QUnit.test('should return jquery collection containing the element', function (assert) { - assert.expect(2) - var $el = $('<div/>') - var $affix = $el.bootstrapAffix() - assert.ok($affix instanceof $, 'returns jquery collection') - assert.strictEqual($affix[0], $el[0], 'collection contains element') - }) - - QUnit.test('should exit early if element is not visible', function (assert) { - assert.expect(1) - var $affix = $('<div style="display: none"/>').bootstrapAffix() - $affix.data('bs.affix').checkPosition() - assert.ok(!$affix.hasClass('affix'), 'affix class was not added') - }) - - QUnit.test('should trigger affixed event after affix', function (assert) { - assert.expect(2) - var done = assert.async() - - var templateHTML = '<div id="affixTarget">' - + '<ul>' - + '<li>Please affix</li>' - + '<li>And unaffix</li>' - + '</ul>' - + '</div>' - + '<div id="affixAfter" style="height: 20000px; display: block;"/>' - $(templateHTML).appendTo(document.body) - - $('#affixTarget').bootstrapAffix({ - offset: $('#affixTarget ul').position() - }) - - $('#affixTarget') - .on('affix.bs.affix', function () { - assert.ok(true, 'affix event fired') - }).on('affixed.bs.affix', function () { - assert.ok(true, 'affixed event fired') - $('#affixTarget, #affixAfter').remove() - done() - }) - - setTimeout(function () { - window.scrollTo(0, document.body.scrollHeight) - - setTimeout(function () { - window.scroll(0, 0) - }, 16) // for testing in a browser - }, 0) - }) - - QUnit.test('should affix-top when scrolling up to offset when parent has padding', function (assert) { - assert.expect(1) - var done = assert.async() - - var templateHTML = '<div id="padding-offset" style="padding-top: 20px;">' - + '<div id="affixTopTarget">' - + '<p>Testing affix-top class is added</p>' - + '</div>' - + '<div style="height: 1000px; display: block;"/>' - + '</div>' - $(templateHTML).appendTo(document.body) - - $('#affixTopTarget') - .bootstrapAffix({ - offset: { top: 120, bottom: 0 } - }) - .on('affixed-top.bs.affix', function () { - assert.ok($('#affixTopTarget').hasClass('affix-top'), 'affix-top class applied') - $('#padding-offset').remove() - done() - }) - - setTimeout(function () { - window.scrollTo(0, document.body.scrollHeight) - - setTimeout(function () { - window.scroll(0, 119) - }, 250) - }, 250) - }) -}) diff --git a/js/tests/unit/alert.js b/js/tests/unit/alert.js index 6be990a51..97818960a 100644 --- a/js/tests/unit/alert.js +++ b/js/tests/unit/alert.js @@ -38,7 +38,8 @@ $(function () { + '<a class="close" href="#" data-dismiss="alert">×</a>' + '<p><strong>Holy guacamole!</strong> Best check yo self, you\'re not looking too good.</p>' + '</div>' - var $alert = $(alertHTML).bootstrapAlert() + + var $alert = $(alertHTML).bootstrapAlert().appendTo($('#qunit-fixture')) $alert.find('.close').trigger('click') diff --git a/js/tests/unit/button.js b/js/tests/unit/button.js index 691796c42..5648506cf 100644 --- a/js/tests/unit/button.js +++ b/js/tests/unit/button.js @@ -32,64 +32,6 @@ $(function () { assert.strictEqual($button[0], $el[0], 'collection contains element') }) - QUnit.test('should return set state to loading', function (assert) { - assert.expect(4) - var $btn = $('<button class="btn" data-loading-text="fat">mdo</button>') - assert.strictEqual($btn.html(), 'mdo', 'btn text equals mdo') - $btn.bootstrapButton('loading') - var done = assert.async() - setTimeout(function () { - assert.strictEqual($btn.html(), 'fat', 'btn text equals fat') - assert.ok($btn[0].hasAttribute('disabled'), 'btn is disabled') - assert.ok($btn.hasClass('disabled'), 'btn has disabled class') - done() - }, 0) - }) - - QUnit.test('should return reset state', function (assert) { - assert.expect(7) - var $btn = $('<button class="btn" data-loading-text="fat">mdo</button>') - assert.strictEqual($btn.html(), 'mdo', 'btn text equals mdo') - $btn.bootstrapButton('loading') - var doneOne = assert.async() - setTimeout(function () { - assert.strictEqual($btn.html(), 'fat', 'btn text equals fat') - assert.ok($btn[0].hasAttribute('disabled'), 'btn is disabled') - assert.ok($btn.hasClass('disabled'), 'btn has disabled class') - doneOne() - var doneTwo = assert.async() - $btn.bootstrapButton('reset') - setTimeout(function () { - assert.strictEqual($btn.html(), 'mdo', 'btn text equals mdo') - assert.ok(!$btn[0].hasAttribute('disabled'), 'btn is not disabled') - assert.ok(!$btn.hasClass('disabled'), 'btn does not have disabled class') - doneTwo() - }, 0) - }, 0) - }) - - QUnit.test('should work with an empty string as reset state', function (assert) { - assert.expect(7) - var $btn = $('<button class="btn" data-loading-text="fat"/>') - assert.strictEqual($btn.html(), '', 'btn text equals ""') - $btn.bootstrapButton('loading') - var doneOne = assert.async() - setTimeout(function () { - assert.strictEqual($btn.html(), 'fat', 'btn text equals fat') - assert.ok($btn[0].hasAttribute('disabled'), 'btn is disabled') - assert.ok($btn.hasClass('disabled'), 'btn has disabled class') - doneOne() - var doneTwo = assert.async() - $btn.bootstrapButton('reset') - setTimeout(function () { - assert.strictEqual($btn.html(), '', 'btn text equals ""') - assert.ok(!$btn[0].hasAttribute('disabled'), 'btn is not disabled') - assert.ok(!$btn.hasClass('disabled'), 'btn does not have disabled class') - doneTwo() - }, 0) - }, 0) - }) - QUnit.test('should toggle active', function (assert) { assert.expect(2) var $btn = $('<button class="btn" data-toggle="button">mdo</button>') diff --git a/js/tests/unit/carousel.js b/js/tests/unit/carousel.js index 39d250598..017bd9bee 100644 --- a/js/tests/unit/carousel.js +++ b/js/tests/unit/carousel.js @@ -32,6 +32,38 @@ $(function () { assert.strictEqual($carousel[0], $el[0], 'collection contains element') }) + QUnit.test('should type check config options', function (assert) { + assert.expect(2) + + var message + var expectedMessage = 'CAROUSEL: Option "interval" provided type "string" but expected type "(number|boolean)".' + var config = { + interval: 'fat sux' + } + + try { + $('<div/>').bootstrapCarousel(config) + } catch (e) { + message = e.message + } + + assert.ok(message === expectedMessage, 'correct error message') + + config = { + keyboard: document.createElement('div') + } + expectedMessage = 'CAROUSEL: Option "keyboard" provided type "element" but expected type "boolean".' + + try { + $('<div/>').bootstrapCarousel(config) + } catch (e) { + message = e.message + } + + assert.ok(message === expectedMessage, 'correct error message') + }) + + QUnit.test('should not fire slid when slide is prevented', function (assert) { assert.expect(1) var done = assert.async() @@ -56,13 +88,13 @@ $(function () { + '<li data-target="#carousel-example-generic" data-slide-to="2"/>' + '</ol>' + '<div class="carousel-inner">' - + '<div class="item active">' + + '<div class="carousel-item active">' + '<div class="carousel-caption"/>' + '</div>' - + '<div class="item">' + + '<div class="carousel-item">' + '<div class="carousel-caption"/>' + '</div>' - + '<div class="item">' + + '<div class="carousel-item">' + '<div class="carousel-caption"/>' + '</div>' + '</div>' @@ -76,16 +108,16 @@ $(function () { .one('slide.bs.carousel', function (e) { e.preventDefault() setTimeout(function () { - assert.ok($carousel.find('.item:eq(0)').is('.active'), 'first item still active') + assert.ok($carousel.find('.carousel-item:eq(0)').is('.active'), 'first item still active') assert.ok($carousel.find('.carousel-indicators li:eq(0)').is('.active'), 'first indicator still active') $carousel.bootstrapCarousel('next') }, 0) }) .one('slid.bs.carousel', function () { setTimeout(function () { - assert.ok(!$carousel.find('.item:eq(0)').is('.active'), 'first item still active') + assert.ok(!$carousel.find('.carousel-item:eq(0)').is('.active'), 'first item still active') assert.ok(!$carousel.find('.carousel-indicators li:eq(0)').is('.active'), 'first indicator still active') - assert.ok($carousel.find('.item:eq(1)').is('.active'), 'second item active') + assert.ok($carousel.find('.carousel-item:eq(1)').is('.active'), 'second item active') assert.ok($carousel.find('.carousel-indicators li:eq(1)').is('.active'), 'second indicator active') done() }, 0) @@ -97,7 +129,7 @@ $(function () { assert.expect(4) var carouselHTML = '<div id="myCarousel" class="carousel slide">' + '<div class="carousel-inner">' - + '<div class="item active">' + + '<div class="carousel-item active">' + '<img alt="">' + '<div class="carousel-caption">' + '<h4>First Thumbnail label</h4>' @@ -106,7 +138,7 @@ $(function () { + 'ultricies vehicula ut id elit.</p>' + '</div>' + '</div>' - + '<div class="item">' + + '<div class="carousel-item">' + '<img alt="">' + '<div class="carousel-caption">' + '<h4>Second Thumbnail label</h4>' @@ -115,7 +147,7 @@ $(function () { + 'ultricies vehicula ut id elit.</p>' + '</div>' + '</div>' - + '<div class="item">' + + '<div class="carousel-item">' + '<img alt="">' + '<div class="carousel-caption">' + '<h4>Third Thumbnail label</h4>' @@ -152,7 +184,7 @@ $(function () { assert.expect(4) var carouselHTML = '<div id="myCarousel" class="carousel slide">' + '<div class="carousel-inner">' - + '<div class="item active">' + + '<div class="carousel-item active">' + '<img alt="">' + '<div class="carousel-caption">' + '<h4>First Thumbnail label</h4>' @@ -161,7 +193,7 @@ $(function () { + 'ultricies vehicula ut id elit.</p>' + '</div>' + '</div>' - + '<div class="item">' + + '<div class="carousel-item">' + '<img alt="">' + '<div class="carousel-caption">' + '<h4>Second Thumbnail label</h4>' @@ -170,7 +202,7 @@ $(function () { + 'ultricies vehicula ut id elit.</p>' + '</div>' + '</div>' - + '<div class="item">' + + '<div class="carousel-item">' + '<img alt="">' + '<div class="carousel-caption">' + '<h4>Third Thumbnail label</h4>' @@ -207,7 +239,7 @@ $(function () { assert.expect(2) var template = '<div id="myCarousel" class="carousel slide">' + '<div class="carousel-inner">' - + '<div class="item active">' + + '<div class="carousel-item active">' + '<img alt="">' + '<div class="carousel-caption">' + '<h4>First Thumbnail label</h4>' @@ -216,7 +248,7 @@ $(function () { + 'ultricies vehicula ut id elit.</p>' + '</div>' + '</div>' - + '<div class="item">' + + '<div class="carousel-item">' + '<img alt="">' + '<div class="carousel-caption">' + '<h4>Second Thumbnail label</h4>' @@ -225,7 +257,7 @@ $(function () { + 'ultricies vehicula ut id elit.</p>' + '</div>' + '</div>' - + '<div class="item">' + + '<div class="carousel-item">' + '<img alt="">' + '<div class="carousel-caption">' + '<h4>Third Thumbnail label</h4>' @@ -244,7 +276,7 @@ $(function () { $(template) .on('slide.bs.carousel', function (e) { assert.ok(e.relatedTarget, 'relatedTarget present') - assert.ok($(e.relatedTarget).hasClass('item'), 'relatedTarget has class "item"') + assert.ok($(e.relatedTarget).hasClass('carousel-item'), 'relatedTarget has class "item"') done() }) .bootstrapCarousel('next') @@ -254,7 +286,7 @@ $(function () { assert.expect(2) var template = '<div id="myCarousel" class="carousel slide">' + '<div class="carousel-inner">' - + '<div class="item active">' + + '<div class="carousel-item active">' + '<img alt="">' + '<div class="carousel-caption">' + '<h4>First Thumbnail label</h4>' @@ -263,7 +295,7 @@ $(function () { + 'ultricies vehicula ut id elit.</p>' + '</div>' + '</div>' - + '<div class="item">' + + '<div class="carousel-item">' + '<img alt="">' + '<div class="carousel-caption">' + '<h4>Second Thumbnail label</h4>' @@ -272,7 +304,7 @@ $(function () { + 'ultricies vehicula ut id elit.</p>' + '</div>' + '</div>' - + '<div class="item">' + + '<div class="carousel-item">' + '<img alt="">' + '<div class="carousel-caption">' + '<h4>Third Thumbnail label</h4>' @@ -291,7 +323,7 @@ $(function () { $(template) .on('slid.bs.carousel', function (e) { assert.ok(e.relatedTarget, 'relatedTarget present') - assert.ok($(e.relatedTarget).hasClass('item'), 'relatedTarget has class "item"') + assert.ok($(e.relatedTarget).hasClass('carousel-item'), 'relatedTarget has class "item"') done() }) .bootstrapCarousel('next') @@ -301,7 +333,7 @@ $(function () { assert.expect(4) var templateHTML = '<div id="myCarousel" class="carousel slide">' + '<div class="carousel-inner">' - + '<div class="item active">' + + '<div class="carousel-item active">' + '<img alt="">' + '<div class="carousel-caption">' + '<h4>First Thumbnail label</h4>' @@ -310,7 +342,7 @@ $(function () { + 'ultricies vehicula ut id elit.</p>' + '</div>' + '</div>' - + '<div class="item">' + + '<div class="carousel-item">' + '<img alt="">' + '<div class="carousel-caption">' + '<h4>Second Thumbnail label</h4>' @@ -319,7 +351,7 @@ $(function () { + 'ultricies vehicula ut id elit.</p>' + '</div>' + '</div>' - + '<div class="item">' + + '<div class="carousel-item">' + '<img alt="">' + '<div class="carousel-caption">' + '<h4>Third Thumbnail label</h4>' @@ -337,25 +369,25 @@ $(function () { $carousel.appendTo('body') $('[data-slide]').first().trigger('click') - assert.strictEqual($carousel.data('bs.carousel').options.interval, 1814) + assert.strictEqual($carousel.data('bs.carousel')._config.interval, 1814) $carousel.remove() $carousel.appendTo('body').attr('data-modal', 'foobar') $('[data-slide]').first().trigger('click') - assert.strictEqual($carousel.data('bs.carousel').options.interval, 1814, 'even if there is an data-modal attribute set') + assert.strictEqual($carousel.data('bs.carousel')._config.interval, 1814, 'even if there is an data-modal attribute set') $carousel.remove() $carousel.appendTo('body') $('[data-slide]').first().trigger('click') $carousel.attr('data-interval', 1860) $('[data-slide]').first().trigger('click') - assert.strictEqual($carousel.data('bs.carousel').options.interval, 1814, 'attributes should be read only on initialization') + assert.strictEqual($carousel.data('bs.carousel')._config.interval, 1814, 'attributes should be read only on initialization') $carousel.remove() $carousel.attr('data-interval', false) $carousel.appendTo('body') $carousel.bootstrapCarousel(1) - assert.strictEqual($carousel.data('bs.carousel').options.interval, false, 'data attribute has higher priority than default options') + assert.strictEqual($carousel.data('bs.carousel')._config.interval, false, 'data attribute has higher priority than default options') $carousel.remove() }) @@ -363,14 +395,14 @@ $(function () { assert.expect(2) var templateHTML = '<div id="myCarousel" class="carousel" data-interval="1814">' + '<div class="carousel-inner">' - + '<div class="item active">' + + '<div class="carousel-item active">' + '<img alt="">' + '</div>' + '<script type="text/x-metamorph" id="thingy"/>' - + '<div class="item">' + + '<div class="carousel-item">' + '<img alt="">' + '</div>' - + '<div class="item">' + + '<div class="carousel-item">' + '</div>' + '</div>' + '</div>' @@ -378,25 +410,25 @@ $(function () { $template.bootstrapCarousel() - assert.strictEqual($template.find('.item')[0], $template.find('.active')[0], 'first item active') + assert.strictEqual($template.find('.carousel-item')[0], $template.find('.active')[0], 'first item active') $template.bootstrapCarousel(1) - assert.strictEqual($template.find('.item')[1], $template.find('.active')[0], 'second item active') + assert.strictEqual($template.find('.carousel-item')[1], $template.find('.active')[0], 'second item active') }) QUnit.test('should skip over non-items when using next/prev methods', function (assert) { assert.expect(2) var templateHTML = '<div id="myCarousel" class="carousel" data-interval="1814">' + '<div class="carousel-inner">' - + '<div class="item active">' + + '<div class="carousel-item active">' + '<img alt="">' + '</div>' + '<script type="text/x-metamorph" id="thingy"/>' - + '<div class="item">' + + '<div class="carousel-item">' + '<img alt="">' + '</div>' - + '<div class="item">' + + '<div class="carousel-item">' + '</div>' + '</div>' + '</div>' @@ -404,24 +436,24 @@ $(function () { $template.bootstrapCarousel() - assert.strictEqual($template.find('.item')[0], $template.find('.active')[0], 'first item active') + assert.strictEqual($template.find('.carousel-item')[0], $template.find('.active')[0], 'first item active') $template.bootstrapCarousel('next') - assert.strictEqual($template.find('.item')[1], $template.find('.active')[0], 'second item active') + assert.strictEqual($template.find('.carousel-item')[1], $template.find('.active')[0], 'second item active') }) QUnit.test('should go to previous item if left arrow key is pressed', function (assert) { assert.expect(2) var templateHTML = '<div id="myCarousel" class="carousel" data-interval="false">' + '<div class="carousel-inner">' - + '<div id="first" class="item">' + + '<div id="first" class="carousel-item">' + '<img alt="">' + '</div>' - + '<div id="second" class="item active">' + + '<div id="second" class="carousel-item active">' + '<img alt="">' + '</div>' - + '<div id="third" class="item">' + + '<div id="third" class="carousel-item">' + '<img alt="">' + '</div>' + '</div>' @@ -430,24 +462,24 @@ $(function () { $template.bootstrapCarousel() - assert.strictEqual($template.find('.item')[1], $template.find('.active')[0], 'second item active') + assert.strictEqual($template.find('.carousel-item')[1], $template.find('.active')[0], 'second item active') $template.trigger($.Event('keydown', { which: 37 })) - assert.strictEqual($template.find('.item')[0], $template.find('.active')[0], 'first item active') + assert.strictEqual($template.find('.carousel-item')[0], $template.find('.active')[0], 'first item active') }) QUnit.test('should go to next item if right arrow key is pressed', function (assert) { assert.expect(2) var templateHTML = '<div id="myCarousel" class="carousel" data-interval="false">' + '<div class="carousel-inner">' - + '<div id="first" class="item active">' + + '<div id="first" class="carousel-item active">' + '<img alt="">' + '</div>' - + '<div id="second" class="item">' + + '<div id="second" class="carousel-item">' + '<img alt="">' + '</div>' - + '<div id="third" class="item">' + + '<div id="third" class="carousel-item">' + '<img alt="">' + '</div>' + '</div>' @@ -456,24 +488,24 @@ $(function () { $template.bootstrapCarousel() - assert.strictEqual($template.find('.item')[0], $template.find('.active')[0], 'first item active') + assert.strictEqual($template.find('.carousel-item')[0], $template.find('.active')[0], 'first item active') $template.trigger($.Event('keydown', { which: 39 })) - assert.strictEqual($template.find('.item')[1], $template.find('.active')[0], 'second item active') + assert.strictEqual($template.find('.carousel-item')[1], $template.find('.active')[0], 'second item active') }) QUnit.test('should support disabling the keyboard navigation', function (assert) { assert.expect(3) var templateHTML = '<div id="myCarousel" class="carousel" data-interval="false" data-keyboard="false">' + '<div class="carousel-inner">' - + '<div id="first" class="item active">' + + '<div id="first" class="carousel-item active">' + '<img alt="">' + '</div>' - + '<div id="second" class="item">' + + '<div id="second" class="carousel-item">' + '<img alt="">' + '</div>' - + '<div id="third" class="item">' + + '<div id="third" class="carousel-item">' + '<img alt="">' + '</div>' + '</div>' @@ -482,30 +514,30 @@ $(function () { $template.bootstrapCarousel() - assert.strictEqual($template.find('.item')[0], $template.find('.active')[0], 'first item active') + assert.strictEqual($template.find('.carousel-item')[0], $template.find('.active')[0], 'first item active') $template.trigger($.Event('keydown', { which: 39 })) - assert.strictEqual($template.find('.item')[0], $template.find('.active')[0], 'first item still active after right arrow press') + assert.strictEqual($template.find('.carousel-item')[0], $template.find('.active')[0], 'first item still active after right arrow press') $template.trigger($.Event('keydown', { which: 37 })) - assert.strictEqual($template.find('.item')[0], $template.find('.active')[0], 'first item still active after left arrow press') + assert.strictEqual($template.find('.carousel-item')[0], $template.find('.active')[0], 'first item still active after left arrow press') }) QUnit.test('should ignore keyboard events within <input>s and <textarea>s', function (assert) { assert.expect(7) var templateHTML = '<div id="myCarousel" class="carousel" data-interval="false">' + '<div class="carousel-inner">' - + '<div id="first" class="item active">' + + '<div id="first" class="carousel-item active">' + '<img alt="">' + '<input type="text" id="in-put">' + '<textarea id="text-area"></textarea>' + '</div>' - + '<div id="second" class="item">' + + '<div id="second" class="carousel-item">' + '<img alt="">' + '</div>' - + '<div id="third" class="item">' + + '<div id="third" class="carousel-item">' + '<img alt="">' + '</div>' + '</div>' @@ -519,21 +551,21 @@ $(function () { $template.bootstrapCarousel() - assert.strictEqual($template.find('.item')[0], $template.find('.active')[0], 'first item active') + assert.strictEqual($template.find('.carousel-item')[0], $template.find('.active')[0], 'first item active') $input.trigger($.Event('keydown', { which: 39 })) - assert.strictEqual($template.find('.item')[0], $template.find('.active')[0], 'first item still active after right arrow press in <input>') + assert.strictEqual($template.find('.carousel-item')[0], $template.find('.active')[0], 'first item still active after right arrow press in <input>') $input.trigger($.Event('keydown', { which: 37 })) - assert.strictEqual($template.find('.item')[0], $template.find('.active')[0], 'first item still active after left arrow press in <input>') + assert.strictEqual($template.find('.carousel-item')[0], $template.find('.active')[0], 'first item still active after left arrow press in <input>') $textarea.trigger($.Event('keydown', { which: 39 })) - assert.strictEqual($template.find('.item')[0], $template.find('.active')[0], 'first item still active after right arrow press in <textarea>') + assert.strictEqual($template.find('.carousel-item')[0], $template.find('.active')[0], 'first item still active after right arrow press in <textarea>') $textarea.trigger($.Event('keydown', { which: 37 })) - assert.strictEqual($template.find('.item')[0], $template.find('.active')[0], 'first item still active after left arrow press in <textarea>') + assert.strictEqual($template.find('.carousel-item')[0], $template.find('.active')[0], 'first item still active after left arrow press in <textarea>') }) QUnit.test('should only add mouseenter and mouseleave listeners when not on mobile', function (assert) { @@ -541,13 +573,13 @@ $(function () { var isMobile = 'ontouchstart' in document.documentElement var templateHTML = '<div id="myCarousel" class="carousel" data-interval="false" data-pause="hover">' + '<div class="carousel-inner">' - + '<div id="first" class="item active">' + + '<div id="first" class="carousel-item active">' + '<img alt="">' + '</div>' - + '<div id="second" class="item">' + + '<div id="second" class="carousel-item">' + '<img alt="">' + '</div>' - + '<div id="third" class="item">' + + '<div id="third" class="carousel-item">' + '<img alt="">' + '</div>' + '</div>' @@ -568,13 +600,13 @@ $(function () { + '<li data-target="#carousel-example-generic" data-slide-to="2"/>' + '</ol>' + '<div class="carousel-inner">' - + '<div class="item active" id="one">' + + '<div class="carousel-item active" id="one">' + '<div class="carousel-caption"/>' + '</div>' - + '<div class="item" id="two">' + + '<div class="carousel-item" id="two">' + '<div class="carousel-caption"/>' + '</div>' - + '<div class="item" id="three">' + + '<div class="carousel-item" id="three">' + '<div class="carousel-caption"/>' + '</div>' + '</div>' @@ -582,7 +614,7 @@ $(function () { + '<a class="right carousel-control" href="#carousel-example-generic" data-slide="next"/>' + '</div>' var $carousel = $(carouselHTML) - var getActiveId = function () { return $carousel.find('.item.active').attr('id') } + var getActiveId = function () { return $carousel.find('.carousel-item.active').attr('id') } var done = assert.async() @@ -613,13 +645,13 @@ $(function () { + '<li data-target="#carousel-example-generic" data-slide-to="2"/>' + '</ol>' + '<div class="carousel-inner">' - + '<div class="item active" id="one">' + + '<div class="carousel-item active" id="one">' + '<div class="carousel-caption"/>' + '</div>' - + '<div class="item" id="two">' + + '<div class="carousel-item" id="two">' + '<div class="carousel-caption"/>' + '</div>' - + '<div class="item" id="three">' + + '<div class="carousel-item" id="three">' + '<div class="carousel-caption"/>' + '</div>' + '</div>' @@ -632,7 +664,7 @@ $(function () { $carousel .on('slid.bs.carousel', function () { - assert.strictEqual($carousel.find('.item.active').attr('id'), 'three', 'carousel wrapped around and slid from 1st to 3rd slide') + assert.strictEqual($carousel.find('.carousel-item.active').attr('id'), 'three', 'carousel wrapped around and slid from 1st to 3rd slide') done() }) .bootstrapCarousel('prev') @@ -647,13 +679,13 @@ $(function () { + '<li data-target="#carousel-example-generic" data-slide-to="2"/>' + '</ol>' + '<div class="carousel-inner">' - + '<div class="item active" id="one">' + + '<div class="carousel-item active" id="one">' + '<div class="carousel-caption"/>' + '</div>' - + '<div class="item" id="two">' + + '<div class="carousel-item" id="two">' + '<div class="carousel-caption"/>' + '</div>' - + '<div class="item" id="three">' + + '<div class="carousel-item" id="three">' + '<div class="carousel-caption"/>' + '</div>' + '</div>' @@ -661,7 +693,7 @@ $(function () { + '<a class="right carousel-control" href="#carousel-example-generic" data-slide="next"/>' + '</div>' var $carousel = $(carouselHTML) - var getActiveId = function () { return $carousel.find('.item.active').attr('id') } + var getActiveId = function () { return $carousel.find('.carousel-item.active').attr('id') } var done = assert.async() @@ -693,13 +725,13 @@ $(function () { + '<li data-target="#carousel-example-generic" data-slide-to="2"/>' + '</ol>' + '<div class="carousel-inner">' - + '<div class="item active" id="one">' + + '<div class="carousel-item active" id="one">' + '<div class="carousel-caption"/>' + '</div>' - + '<div class="item" id="two">' + + '<div class="carousel-item" id="two">' + '<div class="carousel-caption"/>' + '</div>' - + '<div class="item" id="three">' + + '<div class="carousel-item" id="three">' + '<div class="carousel-caption"/>' + '</div>' + '</div>' @@ -713,6 +745,6 @@ $(function () { assert.ok(false, 'carousel slid when it should not have slid') }) .bootstrapCarousel('prev') - assert.strictEqual($carousel.find('.item.active').attr('id'), 'one', 'carousel did not wrap around and stayed on 1st slide') + assert.strictEqual($carousel.find('.carousel-item.active').attr('id'), 'one', 'carousel did not wrap around and stayed on 1st slide') }) }) diff --git a/js/tests/unit/collapse.js b/js/tests/unit/collapse.js index 0efa65400..78fafc6c2 100644 --- a/js/tests/unit/collapse.js +++ b/js/tests/unit/collapse.js @@ -355,7 +355,7 @@ $(function () { $('<div id="body1" aria-expanded="true" class="in"/>').appendTo($groups.eq(0)) - var $target2 = $('<a class="collapsed" data-toggle="collapse" role="button" href="#body2" data-parent="#accordion"/>').appendTo($groups.eq(1)) + var $target2 = $('<a role="button" data-toggle="collapse" href="#body2" data-parent="#accordion" class="collapsed" />').appendTo($groups.eq(1)) $('<div id="body2" aria-expanded="false"/>').appendTo($groups.eq(1)) @@ -400,7 +400,7 @@ $(function () { $body2 .toggleClass('in collapsing') - .data('bs.collapse').transitioning = 1 + .data('bs.collapse')._isTransitioning = 1 $target1.trigger('click') diff --git a/js/tests/unit/modal.js b/js/tests/unit/modal.js index 217e4d7f7..6da09e3c6 100644 --- a/js/tests/unit/modal.js +++ b/js/tests/unit/modal.js @@ -34,7 +34,7 @@ $(function () { QUnit.test('should expose defaults var for settings', function (assert) { assert.expect(1) - assert.ok($.fn.bootstrapModal.Constructor.DEFAULTS, 'default object exposed') + assert.ok($.fn.bootstrapModal.Constructor.Default, 'default object exposed') }) QUnit.test('should insert into dom when show method is called', function (assert) { diff --git a/js/tests/unit/popover.js b/js/tests/unit/popover.js index a25df3a58..8347e9f03 100644 --- a/js/tests/unit/popover.js +++ b/js/tests/unit/popover.js @@ -16,6 +16,7 @@ $(function () { afterEach: function () { $.fn.popover = $.fn.bootstrapPopover delete $.fn.bootstrapPopover + $('.popover').remove() } }) @@ -81,6 +82,7 @@ $(function () { assert.strictEqual($('.popover .popover-content').text(), 'loves writing tests (╯°□°)╯︵ ┻━┻', 'content correctly inserted') $popover.bootstrapPopover('hide') + assert.strictEqual($('.popover').length, 0, 'popover was removed') }) @@ -91,6 +93,7 @@ $(function () { var $popover = $('<a href="#">@fat</a>') .appendTo('#qunit-fixture') .bootstrapPopover({ + html: true, content: function () { return $div } @@ -98,14 +101,14 @@ $(function () { $popover.bootstrapPopover('show') assert.notEqual($('.popover').length, 0, 'popover was inserted') - assert.equal($('.popover .popover-content').html(), $div, 'content correctly inserted') + assert.equal($('.popover .popover-content').html(), $div[0].outerHTML, 'content correctly inserted') $popover.bootstrapPopover('hide') assert.strictEqual($('.popover').length, 0, 'popover was removed') $popover.bootstrapPopover('show') assert.notEqual($('.popover').length, 0, 'popover was inserted') - assert.equal($('.popover .popover-content').html(), $div, 'content correctly inserted') + assert.equal($('.popover .popover-content').html(), $div[0].outerHTML, 'content correctly inserted') $popover.bootstrapPopover('hide') assert.strictEqual($('.popover').length, 0, 'popover was removed') @@ -126,7 +129,6 @@ $(function () { assert.strictEqual($('.popover').length, 0, 'popover was removed') }) - QUnit.test('should get title and content from attributes ignoring options passed via js', function (assert) { assert.expect(4) var $popover = $('<a href="#" title="@mdo" data-content="loves data attributes (づ。◕‿‿◕。)づ ︵ ┻━┻" >@mdo</a>') @@ -177,7 +179,7 @@ $(function () { assert.strictEqual($._data($popover[0], 'events').click[0].namespace, 'foo', 'popover has extra click.foo event') $popover.bootstrapPopover('show') - $popover.bootstrapPopover('destroy') + $popover.bootstrapPopover('dispose') assert.ok(!$popover.hasClass('in'), 'popover is hidden') assert.ok(!$popover.data('popover'), 'popover does not have data') @@ -229,7 +231,7 @@ $(function () { $div .one('shown.bs.popover', function () { $('.content-with-handler .btn').trigger('click') - $div.bootstrapPopover('destroy') + $div.bootstrapPopover('dispose') assert.ok(handlerCalled, 'content\'s event handler still present') done() }) @@ -240,13 +242,6 @@ $(function () { .bootstrapPopover('show') }) - QUnit.test('should throw an error when initializing popover on the document object without specifying a delegation selector', function (assert) { - assert.expect(1) - assert.throws(function () { - $(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!')) - }) - QUnit.test('should do nothing when an attempt is made to hide an uninitialized popover', function (assert) { assert.expect(1) @@ -259,16 +254,6 @@ $(function () { assert.strictEqual($popover.data('bs.popover'), undefined, 'should not initialize the popover') }) - QUnit.test('should throw an error when template contains multiple top-level elements', function (assert) { - assert.expect(1) - assert.throws(function () { - $('<span data-toggle="popover" data-title="some title" data-content="some content">some text</span>') - .appendTo('#qunit-fixture') - .bootstrapPopover({ template: '<div>Foo</div><div>Bar</div>' }) - .bootstrapPopover('show') - }, new Error('popover `template` option must consist of exactly 1 top-level element!')) - }) - QUnit.test('should fire inserted event', function (assert) { assert.expect(2) var done = assert.async() diff --git a/js/tests/unit/scrollspy.js b/js/tests/unit/scrollspy.js index bf5fa0bff..063dfabcf 100644 --- a/js/tests/unit/scrollspy.js +++ b/js/tests/unit/scrollspy.js @@ -77,6 +77,51 @@ $(function () { $scrollspy.scrollTop(350) }) + QUnit.test('should only switch "active" class on current target specified w element', function (assert) { + assert.expect(1) + var done = assert.async() + + var sectionHTML = '<div id="root" class="active">' + + '<div class="topbar">' + + '<div class="topbar-inner">' + + '<div class="container" id="ss-target">' + + '<ul class="nav">' + + '<li><a href="#masthead">Overview</a></li>' + + '<li><a href="#detail">Detail</a></li>' + + '</ul>' + + '</div>' + + '</div>' + + '</div>' + + '<div id="scrollspy-example" style="height: 100px; overflow: auto;">' + + '<div style="height: 200px;">' + + '<h4 id="masthead">Overview</h4>' + + '<p style="height: 200px">' + + 'Ad leggings keytar, brunch id art party dolor labore.' + + '</p>' + + '</div>' + + '<div style="height: 200px;">' + + '<h4 id="detail">Detail</h4>' + + '<p style="height: 200px">' + + 'Veniam marfa mustache skateboard, adipisicing fugiat velit pitchfork beard.' + + '</p>' + + '</div>' + + '</div>' + + '</div>' + var $section = $(sectionHTML).appendTo('#qunit-fixture') + + var $scrollspy = $section + .show() + .find('#scrollspy-example') + .bootstrapScrollspy({ target: document.getElementById('#ss-target') }) + + $scrollspy.on('scroll.bs.scrollspy', function () { + assert.ok($section.hasClass('active'), '"active" class still on root node') + done() + }) + + $scrollspy.scrollTop(350) + }) + QUnit.test('should correctly select middle navigation option when large offset is used', function (assert) { assert.expect(3) var done = assert.async() @@ -275,4 +320,90 @@ $(function () { .then(function () { return testElementIsActiveAfterScroll('#li-100-1', '#div-100-1') }) }) + QUnit.test('should allow passed in option offset method: offset', function (assert) { + assert.expect(4) + + var testOffsetMethod = function (type) { + var deferred = $.Deferred() + var navbarHtml = + '<nav class="navbar"' + (type === 'data' ? ' id="navbar-offset-method-menu"' : '') + '>' + + '<ul class="nav">' + + '<li id="li-' + type + 'm-1"><a href="#div-' + type + 'm-1">div 1</a></li>' + + '<li id="li-' + type + 'm-2"><a href="#div-' + type + 'm-2">div 2</a></li>' + + '<li id="li-' + type + 'm-3"><a href="#div-' + type + 'm-3">div 3</a></li>' + + '</ul>' + + '</nav>' + var contentHtml = + '<div class="content"' + (type === 'data' ? ' data-spy="scroll" data-target="#navbar-offset-method-menu" data-offset="0" data-method="offset"' : '') + ' style="position: relative; overflow: auto; height: 100px">' + + '<div id="div-' + type + 'm-1" style="position: relative; height: 200px; padding: 0; margin: 0">div 1</div>' + + '<div id="div-' + type + 'm-2" style="position: relative; height: 150px; padding: 0; margin: 0">div 2</div>' + + '<div id="div-' + type + 'm-3" style="position: relative; height: 250px; padding: 0; margin: 0">div 3</div>' + + '</div>' + + + $(navbarHtml).appendTo('#qunit-fixture') + var $content = $(contentHtml) + .appendTo('#qunit-fixture') + + if (type === 'js') $content.bootstrapScrollspy({ target: '.navbar', offset: 0, method: 'offset' }) + else if (type === 'data') $(window).trigger('load.bs.scrollspy.data-api') + + var $target = $('#div-' + type + 'm-2') + var scrollspy = $content.data('bs.scrollspy') + + assert.ok(scrollspy._offsets[1] === $target.offset().top, 'offsed method with ' + type + ' option') + assert.ok(scrollspy._offsets[1] !== $target.position().top, 'position method with ' + type + ' option') + + deferred.resolve() + + return deferred.promise() + } + + $.when(testOffsetMethod('js')) + .then(function () { testOffsetMethod('data') }) + }) + + QUnit.test('should allow passed in option offset method: position', function (assert) { + assert.expect(4) + + var testOffsetMethod = function (type) { + var deferred = $.Deferred() + var navbarHtml = + '<nav class="navbar"' + (type === 'data' ? ' id="navbar-offset-method-menu"' : '') + '>' + + '<ul class="nav">' + + '<li id="li-' + type + 'm-1"><a href="#div-' + type + 'm-1">div 1</a></li>' + + '<li id="li-' + type + 'm-2"><a href="#div-' + type + 'm-2">div 2</a></li>' + + '<li id="li-' + type + 'm-3"><a href="#div-' + type + 'm-3">div 3</a></li>' + + '</ul>' + + '</nav>' + var contentHtml = + '<div class="content"' + (type === 'data' ? ' data-spy="scroll" data-target="#navbar-offset-method-menu" data-offset="0" data-method="position"' : '') + ' style="position: relative; overflow: auto; height: 100px">' + + '<div id="div-' + type + 'm-1" style="position: relative; height: 200px; padding: 0; margin: 0">div 1</div>' + + '<div id="div-' + type + 'm-2" style="position: relative; height: 150px; padding: 0; margin: 0">div 2</div>' + + '<div id="div-' + type + 'm-3" style="position: relative; height: 250px; padding: 0; margin: 0">div 3</div>' + + '</div>' + + + $(navbarHtml).appendTo('#qunit-fixture') + var $content = $(contentHtml) + .appendTo('#qunit-fixture') + + if (type === 'js') $content.bootstrapScrollspy({ target: '.navbar', offset: 0, method: 'position' }) + else if (type === 'data') $(window).trigger('load.bs.scrollspy.data-api') + + var $target = $('#div-' + type + 'm-2') + var scrollspy = $content.data('bs.scrollspy') + + assert.ok(scrollspy._offsets[1] !== $target.offset().top, 'offsed method with ' + type + ' option') + assert.ok(scrollspy._offsets[1] === $target.position().top, 'position method with ' + type + ' option') + + deferred.resolve() + + return deferred.promise() + } + + $.when(testOffsetMethod('js')) + .then(function () { testOffsetMethod('data') }) + }) + }) diff --git a/js/tests/unit/tooltip.js b/js/tests/unit/tooltip.js index 27ce6208e..f4deb29f8 100644 --- a/js/tests/unit/tooltip.js +++ b/js/tests/unit/tooltip.js @@ -16,6 +16,7 @@ $(function () { afterEach: function () { $.fn.tooltip = $.fn.bootstrapTooltip delete $.fn.bootstrapTooltip + $('.tooltip').remove() } }) @@ -34,7 +35,7 @@ $(function () { QUnit.test('should expose default settings', function (assert) { assert.expect(1) - assert.ok($.fn.bootstrapTooltip.Constructor.DEFAULTS, 'defaults is defined') + assert.ok($.fn.bootstrapTooltip.Constructor.Default, 'defaults is defined') }) QUnit.test('should empty title attribute', function (assert) { @@ -95,10 +96,14 @@ $(function () { .bootstrapTooltip({ placement: 'bottom' }) $tooltip.bootstrapTooltip('show') - assert.ok($('.tooltip').is('.fade.bottom.in'), 'has correct classes applied') + + assert + .ok($('.tooltip') + .is('.fade.bs-tether-element-attached-top.bs-tether-element-attached-center.in'), 'has correct classes applied') $tooltip.bootstrapTooltip('hide') - assert.strictEqual($('.tooltip').length, 0, 'tooltip removed') + + assert.strictEqual($tooltip.data('bs.tooltip').tip.parentNode, null, 'tooltip removed') }) QUnit.test('should allow html entities', function (assert) { @@ -111,7 +116,7 @@ $(function () { assert.notEqual($('.tooltip b').length, 0, 'b tag was inserted') $tooltip.bootstrapTooltip('hide') - assert.strictEqual($('.tooltip').length, 0, 'tooltip removed') + assert.strictEqual($tooltip.data('bs.tooltip').tip.parentNode, null, 'tooltip removed') }) QUnit.test('should respect custom classes', function (assert) { @@ -124,7 +129,7 @@ $(function () { assert.ok($('.tooltip').hasClass('some-class'), 'custom class is present') $tooltip.bootstrapTooltip('hide') - assert.strictEqual($('.tooltip').length, 0, 'tooltip removed') + assert.strictEqual($tooltip.data('bs.tooltip').tip.parentNode, null, 'tooltip removed') }) QUnit.test('should fire show event', function (assert) { @@ -245,7 +250,7 @@ $(function () { assert.strictEqual($._data($tooltip[0], 'events').click[0].namespace, 'foo', 'tooltip has extra click.foo event') $tooltip.bootstrapTooltip('show') - $tooltip.bootstrapTooltip('destroy') + $tooltip.bootstrapTooltip('dispose') assert.ok(!$tooltip.hasClass('in'), 'tooltip is hidden') assert.ok(!$._data($tooltip[0], 'bs.tooltip'), 'tooltip does not have data') @@ -253,21 +258,21 @@ $(function () { assert.ok(!$._data($tooltip[0], 'events').mouseover && !$._data($tooltip[0], 'events').mouseout, 'tooltip does not have hover events') }) - 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>') - .appendTo('#qunit-fixture') - .bootstrapTooltip({ - selector: 'a[rel="tooltip"]', - trigger: 'click' - }) + // 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>') + // .appendTo('#qunit-fixture') + // .bootstrapTooltip({ + // selector: 'a[rel="tooltip"]', + // trigger: 'click' + // }) - $div.find('a').trigger('click') - assert.ok($('.tooltip').is('.fade.in'), 'tooltip is faded in') + // $div.find('a').trigger('click') + // assert.ok($('.tooltip').is('.fade.in'), 'tooltip is faded in') - $div.find('a').trigger('click') - assert.strictEqual($('.tooltip').length, 0, 'tooltip was removed from dom') - }) + // $div.find('a').trigger('click') + // assert.strictEqual($div.data('bs.tooltip').tip.parentNode, null, 'tooltip removed') + // }) QUnit.test('should show tooltip when toggle is called', function (assert) { assert.expect(1) @@ -307,20 +312,20 @@ $(function () { QUnit.test('should add position class before positioning so that position-specific styles are taken into account', function (assert) { assert.expect(1) var styles = '<style>' - + '.tooltip.right { white-space: nowrap; }' - + '.tooltip.right .tooltip-inner { max-width: none; }' - + '</style>' + + '.tooltip.right { white-space: nowrap; }' + + '.tooltip.right .tooltip-inner { max-width: none; }' + + '</style>' var $styles = $(styles).appendTo('head') var $container = $('<div/>').appendTo('#qunit-fixture') var $target = $('<a href="#" rel="tooltip" title="very very very very very very very very long tooltip in one line"/>') .appendTo($container) .bootstrapTooltip({ - placement: 'right', - viewport: null + placement: 'right' }) .bootstrapTooltip('show') - var $tooltip = $container.find('.tooltip') + + var $tooltip = $($target.data('bs.tooltip').tip) // this is some dumb hack shit because sub pixels in firefox var top = Math.round($target.offset().top + ($target[0].offsetHeight / 2) - ($tooltip[0].offsetHeight / 2)) @@ -376,444 +381,7 @@ $(function () { assert.strictEqual($('.tooltip').length, 0, 'tooltip removed from dom') }) - 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') - var $container = $('<div/>') - .css({ - position: 'relative', - height: '100%' - }) - .appendTo('#qunit-fixture') - - var $topTooltip = $('<div style="left: 0; top: 0;" rel="tooltip" title="Top tooltip">Top Dynamic Tooltip</div>') - .appendTo($container) - .bootstrapTooltip({ placement: 'auto', viewport: '#qunit-fixture' }) - - $topTooltip.bootstrapTooltip('show') - assert.ok($('.tooltip').is('.bottom'), 'top positioned tooltip is dynamically positioned to bottom') - - $topTooltip.bootstrapTooltip('hide') - assert.strictEqual($('.tooltip').length, 0, 'top positioned tooltip removed from dom') - - var $rightTooltip = $('<div style="right: 0;" rel="tooltip" title="Right tooltip">Right Dynamic Tooltip</div>') - .appendTo($container) - .bootstrapTooltip({ placement: 'right auto', viewport: '#qunit-fixture' }) - - $rightTooltip.bootstrapTooltip('show') - assert.ok($('.tooltip').is('.left'), 'right positioned tooltip is dynamically positioned left') - - $rightTooltip.bootstrapTooltip('hide') - assert.strictEqual($('.tooltip').length, 0, 'right positioned tooltip removed from dom') - - var $leftTooltip = $('<div style="left: 0;" rel="tooltip" title="Left tooltip">Left Dynamic Tooltip</div>') - .appendTo($container) - .bootstrapTooltip({ placement: 'auto left', viewport: '#qunit-fixture' }) - - $leftTooltip.bootstrapTooltip('show') - assert.ok($('.tooltip').is('.right'), 'left positioned tooltip is dynamically positioned right') - - $leftTooltip.bootstrapTooltip('hide') - assert.strictEqual($('.tooltip').length, 0, 'left positioned tooltip removed from dom') - - $container.remove() - $style.remove() - }) - - QUnit.test('should position tip on top if viewport has enough space and placement is "auto top"', function (assert) { - assert.expect(2) - var styles = '<style>' - + 'body { padding-top: 100px; }' - + '#section { height: 300px; border: 1px solid red; padding-top: 50px }' - + '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: '#section' - }) - - $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 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) - var styles = '<style>' - + 'body { padding-top: 100px; }' - + '#section { height: 300px; border: 1px solid red; }' - + '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: '#section' - }) - - $target.bootstrapTooltip('show') - assert.ok($('.tooltip').is('.bottom'), 'top positioned tooltip is dynamically positioned to bottom') - - $target.bootstrapTooltip('hide') - assert.strictEqual($('.tooltip').length, 0, 'tooltip removed from dom') - - $styles.remove() - }) - - 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) - 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') - assert.ok($('.tooltip').is('.fade.top.in'), 'has correct classes applied') - - $target.bootstrapTooltip('hide') - assert.strictEqual($('.tooltip').length, 0, 'tooltip removed from dom') - - $styles.remove() - }) - - 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) - var styles = '<style>' - + '#scrollable-div { height: 200px; overflow: auto; }' - + '.tooltip-item { padding: 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(200) - - $target.bootstrapTooltip('show') - assert.ok($('.tooltip').is('.fade.bottom.in'), 'has correct classes applied') - - $target.bootstrapTooltip('hide') - assert.strictEqual($('.tooltip').length, 0, 'tooltip removed from dom') - - $styles.remove() - }) - - 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) - var styles = '<style>' - + '#scrollable-div { height: 200px; overflow: auto; }' - + '.spacer { height: 400px; }' - + '.spacer:first-child { height: 200px; }' - + '.tooltip-item { 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) - .before('<div class="spacer"/>') - .after('<div class="spacer"/>') - .bootstrapTooltip({ - placement: 'bottom auto', - viewport: '#scrollable-div' - }) - - $('#scrollable-div').scrollTop(200) - - $target.bootstrapTooltip('show') - assert.ok($('.tooltip').is('.fade.bottom.in'), 'has correct classes applied') - - $target.bootstrapTooltip('hide') - assert.strictEqual($('.tooltip').length, 0, 'tooltip removed from dom') - - $styles.remove() - }) - - 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) - 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') - assert.ok($('.tooltip').is('.fade.top.in'), 'has correct classes applied') - - $target.bootstrapTooltip('hide') - assert.strictEqual($('.tooltip').length, 0, 'tooltip removed from dom') - - $styles.remove() - }) - - QUnit.test('should adjust the tip\'s top position when up against the top of the viewport', function (assert) { - assert.expect(2) - var styles = '<style>' - + '.tooltip .tooltip-inner { width: 200px; height: 200px; max-width: none; }' - + 'a[rel="tooltip"] { position: fixed; }' - + '</style>' - var $styles = $(styles).appendTo('head') - - var $container = $('<div/>').appendTo('#qunit-fixture') - var $target = $('<a href="#" rel="tooltip" title="tip" style="top: 0px; left: 0px;"/>') - .appendTo($container) - .bootstrapTooltip({ - placement: 'right', - viewport: { - selector: 'body', - padding: 12 - } - }) - - $target.bootstrapTooltip('show') - assert.strictEqual(Math.round($container.find('.tooltip').offset().top), 12) - - $target.bootstrapTooltip('hide') - assert.strictEqual($('.tooltip').length, 0, 'tooltip removed from dom') - - $styles.remove() - }) - - QUnit.test('should adjust the tip\'s top position when up against the bottom of the viewport', function (assert) { - assert.expect(2) - var styles = '<style>' - + '.tooltip .tooltip-inner { width: 200px; height: 200px; max-width: none; }' - + 'a[rel="tooltip"] { position: fixed; }' - + '</style>' - var $styles = $(styles).appendTo('head') - - var $container = $('<div/>').appendTo('#qunit-fixture') - var $target = $('<a href="#" rel="tooltip" title="tip" style="bottom: 0px; left: 0px;"/>') - .appendTo($container) - .bootstrapTooltip({ - placement: 'right', - viewport: { - selector: 'body', - padding: 12 - } - }) - - $target.bootstrapTooltip('show') - var $tooltip = $container.find('.tooltip') - assert.strictEqual(Math.round($tooltip.offset().top), Math.round($(window).height() - 12 - $tooltip[0].offsetHeight)) - - $target.bootstrapTooltip('hide') - assert.strictEqual($('.tooltip').length, 0, 'tooltip removed from dom') - - $container.remove() - $styles.remove() - }) - - QUnit.test('should adjust the tip\'s left position when up against the left of the viewport', function (assert) { - assert.expect(2) - var styles = '<style>' - + '.tooltip .tooltip-inner { width: 200px; height: 200px; max-width: none; }' - + 'a[rel="tooltip"] { position: fixed; }' - + '</style>' - var $styles = $(styles).appendTo('head') - - var $container = $('<div/>').appendTo('#qunit-fixture') - var $target = $('<a href="#" rel="tooltip" title="tip" style="top: 0px; left: 0px;"/>') - .appendTo($container) - .bootstrapTooltip({ - placement: 'bottom', - viewport: { - selector: 'body', - padding: 12 - } - }) - - $target.bootstrapTooltip('show') - assert.strictEqual(Math.round($container.find('.tooltip').offset().left), 12) - - $target.bootstrapTooltip('hide') - assert.strictEqual($('.tooltip').length, 0, 'tooltip removed from dom') - - $container.remove() - $styles.remove() - }) - - QUnit.test('should adjust the tip\'s left position when up against the right of the viewport', function (assert) { - assert.expect(2) - var styles = '<style>' - + '.tooltip .tooltip-inner { width: 200px; height: 200px; max-width: none; }' - + 'a[rel="tooltip"] { position: fixed; }' - + '</style>' - var $styles = $(styles).appendTo('head') - - var $container = $('<div/>').appendTo('body') - var $target = $('<a href="#" rel="tooltip" title="tip" style="top: 0px; right: 0px;"/>') - .appendTo($container) - .bootstrapTooltip({ - placement: 'bottom', - viewport: { - selector: 'body', - padding: 12 - } - }) - - $target.bootstrapTooltip('show') - var $tooltip = $container.find('.tooltip') - assert.strictEqual(Math.round($tooltip.offset().left), Math.round($(window).width() - 12 - $tooltip[0].offsetWidth)) - - $target.bootstrapTooltip('hide') - assert.strictEqual($('.tooltip').length, 0, 'tooltip removed from dom') - - $container.remove() - $styles.remove() - }) - - QUnit.test('should adjust the tip when up against the right of an arbitrary viewport', function (assert) { - assert.expect(2) - 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) - .bootstrapTooltip({ - placement: 'bottom', - viewport: '.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 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)) - - $target.bootstrapTooltip('hide') - assert.strictEqual($('.tooltip').length, 0, 'tooltip removed from dom') - - $container.remove() - $styles.remove() - }) - - QUnit.test('should not error when trying to show an auto-placed tooltip that has been removed from the dom', function (assert) { + QUnit.test('should not error when trying to show an top-placed tooltip that has been removed from the dom', function (assert) { assert.expect(1) var passed = true var $tooltip = $('<a href="#" rel="tooltip" title="Another tooltip"/>') @@ -821,7 +389,7 @@ $(function () { .one('show.bs.tooltip', function () { $(this).remove() }) - .bootstrapTooltip({ placement: 'auto' }) + .bootstrapTooltip({ placement: 'top' }) try { $tooltip.bootstrapTooltip('show') @@ -863,7 +431,7 @@ $(function () { }) .bootstrapTooltip('show') - var $tooltip = $container.find('.tooltip') + var $tooltip = $($trigger.data('bs.tooltip').tip) setTimeout(function () { assert.ok(Math.round($tooltip.offset().top + $tooltip.outerHeight()) <= Math.round($trigger.offset().top)) @@ -871,38 +439,6 @@ $(function () { }, 0) }) - QUnit.test('should place tooltip inside viewport', function (assert) { - assert.expect(1) - var done = assert.async() - - 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 () { - assert.ok($('.tooltip').offset().left >= 0) - done() - }, 0) - }) - QUnit.test('should show tooltip if leave event hasn\'t occurred before delay expires', function (assert) { assert.expect(2) var done = assert.async() @@ -1021,16 +557,16 @@ $(function () { .bootstrapTooltip({ delay: { show: 0, hide: 150 }}) setTimeout(function () { - assert.ok($tooltip.data('bs.tooltip').$tip.is('.fade.in'), '1ms: tooltip faded in') + assert.ok($($tooltip.data('bs.tooltip').tip).is('.fade.in'), '1ms: tooltip faded in') $tooltip.trigger('mouseout') setTimeout(function () { - assert.ok($tooltip.data('bs.tooltip').$tip.is('.fade.in'), '100ms: tooltip still faded in') + assert.ok($($tooltip.data('bs.tooltip').tip).is('.fade.in'), '100ms: tooltip still faded in') }, 100) setTimeout(function () { - assert.ok(!$tooltip.data('bs.tooltip').$tip.is('.in'), '200ms: tooltip removed') + assert.ok(!$($tooltip.data('bs.tooltip').tip).is('.in'), '200ms: tooltip removed') done() }, 200) @@ -1073,57 +609,15 @@ $(function () { assert.strictEqual($('.tooltip').length, 0, 'tooltip removed from dom') done() }) - .bootstrapTooltip({ container: 'body', placement: 'top', trigger: 'manual' }) + .bootstrapTooltip({ placement: 'top', trigger: 'manual' }) $circle.bootstrapTooltip('show') }) - QUnit.test('should correctly determine auto placement based on container rather than parent', function (assert) { - assert.expect(2) - var done = assert.async() - - var styles = '<style>' - + '.tooltip, .tooltip *, .tooltip *:before, .tooltip *:after { box-sizing: border-box; }' - + '.tooltip { position: absolute; display: block; font-size: 12px; line-height: 1.4; }' - + '.tooltip .tooltip-inner { max-width: 200px; padding: 3px 8px; font-family: Helvetica; text-align: center; }' - + '#trigger-parent {' - + ' position: fixed;' - + ' top: 100px;' - + ' right: 17px;' - + '}' - + '</style>' - var $styles = $(styles).appendTo('head') - - $('#qunit-fixture').append('<span id="trigger-parent"><a id="tt-trigger" title="If a_larger_text is written here, it won\'t fit using older broken version of BS">HOVER OVER ME</a></span>') - var $trigger = $('#tt-trigger') - - $trigger - .on('shown.bs.tooltip', function () { - var $tip = $('.tooltip-inner') - var tipXrightEdge = $tip.offset().left + $tip.width() - var triggerXleftEdge = $trigger.offset().left - assert.ok(tipXrightEdge < triggerXleftEdge, 'tooltip with auto left placement, when near the right edge of the viewport, gets left placement') - $trigger.bootstrapTooltip('hide') - }) - .on('hidden.bs.tooltip', function () { - $styles.remove() - $(this).remove() - assert.strictEqual($('.tooltip').length, 0, 'tooltip removed from dom') - done() - }) - .bootstrapTooltip({ - container: 'body', - placement: 'auto left', - trigger: 'manual' - }) - - $trigger.bootstrapTooltip('show') - }) - QUnit.test('should not reload the tooltip on subsequent mouseenter events', function (assert) { assert.expect(1) var titleHtml = function () { - var uid = $.fn.bootstrapTooltip.Constructor.prototype.getUID('tooltip') + var uid = Util.getUID('tooltip') return '<p id="tt-content">' + uid + '</p><p>' + uid + '</p><p>' + uid + '</p>' } @@ -1149,8 +643,9 @@ $(function () { QUnit.test('should not reload the tooltip if the mouse leaves and re-enters before hiding', function (assert) { assert.expect(4) + var titleHtml = function () { - var uid = $.fn.bootstrapTooltip.Constructor.prototype.getUID('tooltip') + var uid = Util.getUID('tooltip') return '<p id="tt-content">' + uid + '</p><p>' + uid + '</p><p>' + uid + '</p>' } @@ -1162,7 +657,6 @@ $(function () { animation: false, trigger: 'hover', delay: { show: 0, hide: 500 }, - container: $tooltip, title: titleHtml }) @@ -1175,47 +669,14 @@ $(function () { $('#tt-outer').trigger('mouseleave') assert.strictEqual(currentUid, $('#tt-content').text()) - assert.ok(obj.hoverState == 'out', 'the tooltip hoverState should be set to "out"') + assert.ok(obj._hoverState == 'out', 'the tooltip hoverState should be set to "out"') - $('#tt-content').trigger('mouseenter') - assert.ok(obj.hoverState == 'in', 'the tooltip hoverState should be set to "in"') + $('#tt-outer').trigger('mouseenter') + assert.ok(obj._hoverState == 'in', 'the tooltip hoverState should be set to "in"') assert.strictEqual(currentUid, $('#tt-content').text()) }) - QUnit.test('should position arrow correctly when tooltip is moved to not appear offscreen', function (assert) { - assert.expect(2) - var done = assert.async() - - var styles = '<style>' - + '.tooltip, .tooltip *, .tooltip *:before, .tooltip *:after { box-sizing: border-box; }' - + '.tooltip { position: absolute; }' - + '.tooltip-arrow { position: absolute; width: 0; height: 0; }' - + '.tooltip .tooltip-inner { max-width: 200px; padding: 3px 8px; }' - + '</style>' - var $styles = $(styles).appendTo('head') - - $('<a href="#" title="tooltip title" style="position: absolute; bottom: 0; right: 0;">Foobar</a>') - .appendTo('body') - .on('shown.bs.tooltip', function () { - var arrowStyles = $(this).data('bs.tooltip').$tip.find('.tooltip-arrow').attr('style') - assert.ok(/left/i.test(arrowStyles) && !/top/i.test(arrowStyles), 'arrow positioned correctly') - $(this).bootstrapTooltip('hide') - }) - .on('hidden.bs.tooltip', function () { - $styles.remove() - $(this).remove() - assert.strictEqual($('.tooltip').length, 0, 'tooltip removed from dom') - done() - }) - .bootstrapTooltip({ - container: 'body', - placement: 'top', - trigger: 'manual' - }) - .bootstrapTooltip('show') - }) - QUnit.test('should correctly position tooltips on transformed elements', function (assert) { var styleProps = document.documentElement.style if (!('transform' in styleProps) && !('webkitTransform' in styleProps) && !('msTransform' in styleProps)) { @@ -1247,21 +708,12 @@ $(function () { done() }) .bootstrapTooltip({ - container: 'body', - placement: 'top', trigger: 'manual' }) $element.bootstrapTooltip('show') }) - QUnit.test('should throw an error when initializing tooltip on the document object without specifying a delegation selector', function (assert) { - assert.expect(1) - assert.throws(function () { - $(document).bootstrapTooltip({ title: 'What am I on?' }) - }, new Error('`selector` option must be specified when initializing tooltip on the window.document object!')) - }) - QUnit.test('should do nothing when an attempt is made to hide an uninitialized tooltip', function (assert) { assert.expect(1) @@ -1274,25 +726,15 @@ $(function () { assert.strictEqual($tooltip.data('bs.tooltip'), undefined, 'should not initialize the tooltip') }) - QUnit.test('should throw an error when template contains multiple top-level elements', function (assert) { - assert.expect(1) - assert.throws(function () { - $('<a href="#" data-toggle="tooltip" title="Another tooltip"></a>') - .appendTo('#qunit-fixture') - .bootstrapTooltip({ template: '<div>Foo</div><div>Bar</div>' }) - .bootstrapTooltip('show') - }, new Error('tooltip `template` option must consist of exactly 1 top-level element!')) - }) - QUnit.test('should not remove tooltip if multiple triggers are set and one is still active', function (assert) { assert.expect(41) var $el = $('<button>Trigger</button>') .appendTo('#qunit-fixture') .bootstrapTooltip({ trigger: 'click hover focus', animation: false }) var tooltip = $el.data('bs.tooltip') - var $tooltip = tooltip.tip() + var $tooltip = $(tooltip.getTipElement()) - function showingTooltip() { return $tooltip.hasClass('in') || tooltip.hoverState == 'in' } + function showingTooltip() { return $tooltip.hasClass('in') || tooltip._hoverState == 'in' } var tests = [ ['mouseenter', 'mouseleave'], |
