diff options
| author | Mark Otto <[email protected]> | 2017-05-26 22:28:09 -0700 |
|---|---|---|
| committer | Mark Otto <[email protected]> | 2017-05-26 22:28:09 -0700 |
| commit | 6c3f833076a9fa68601741e3e21bd07ad79b7d8a (patch) | |
| tree | fe016946d77f9ffff15bbe9cdc593fd098b5bcc7 /js/tests/unit | |
| parent | c581564a780974c6430ac5897740006f623f2277 (diff) | |
| parent | 5d7db507396275fcda96935aff47b09e1d79ddc1 (diff) | |
| download | bootstrap-6c3f833076a9fa68601741e3e21bd07ad79b7d8a.tar.xz bootstrap-6c3f833076a9fa68601741e3e21bd07ad79b7d8a.zip | |
Merge branch 'v4-docs-streamlined' of https://github.com/twbs/bootstrap into v4-docs-streamlined
Diffstat (limited to 'js/tests/unit')
| -rw-r--r-- | js/tests/unit/alert.js | 8 | ||||
| -rw-r--r-- | js/tests/unit/button.js | 22 | ||||
| -rw-r--r-- | js/tests/unit/carousel.js | 65 | ||||
| -rw-r--r-- | js/tests/unit/collapse.js | 51 | ||||
| -rw-r--r-- | js/tests/unit/dropdown.js | 36 | ||||
| -rw-r--r-- | js/tests/unit/modal.js | 75 | ||||
| -rw-r--r-- | js/tests/unit/phantom.js | 31 | ||||
| -rw-r--r-- | js/tests/unit/popover.js | 16 | ||||
| -rw-r--r-- | js/tests/unit/scrollspy.js | 58 | ||||
| -rw-r--r-- | js/tests/unit/tab.js | 88 | ||||
| -rw-r--r-- | js/tests/unit/tooltip.js | 90 |
11 files changed, 434 insertions, 106 deletions
diff --git a/js/tests/unit/alert.js b/js/tests/unit/alert.js index 97818960a..e078082c3 100644 --- a/js/tests/unit/alert.js +++ b/js/tests/unit/alert.js @@ -1,5 +1,5 @@ $(function () { - 'use strict'; + 'use strict' QUnit.module('alert plugin') @@ -34,7 +34,7 @@ $(function () { QUnit.test('should fade element out on clicking .close', function (assert) { assert.expect(1) - var alertHTML = '<div class="alert alert-danger fade in">' + var alertHTML = '<div class="alert alert-danger fade show">' + '<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>' @@ -43,12 +43,12 @@ $(function () { $alert.find('.close').trigger('click') - assert.strictEqual($alert.hasClass('in'), false, 'remove .in class on .close click') + assert.strictEqual($alert.hasClass('show'), false, 'remove .show class on .close click') }) QUnit.test('should remove element when clicking .close', function (assert) { assert.expect(2) - var alertHTML = '<div class="alert alert-danger fade in">' + var alertHTML = '<div class="alert alert-danger fade show">' + '<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>' diff --git a/js/tests/unit/button.js b/js/tests/unit/button.js index 5648506cf..d87b57610 100644 --- a/js/tests/unit/button.js +++ b/js/tests/unit/button.js @@ -1,5 +1,5 @@ $(function () { - 'use strict'; + 'use strict' QUnit.module('button plugin') @@ -72,6 +72,26 @@ $(function () { assert.strictEqual($btn.attr('aria-pressed'), 'true', 'btn aria-pressed state is true') }) + QUnit.test('should trigger input change event when toggled button has input field', function (assert) { + assert.expect(1) + var done = assert.async() + + var groupHTML = '<div class="btn-group" data-toggle="buttons">' + + '<label class="btn btn-primary">' + + '<input type="radio" id="radio" autocomplete="off">Radio' + + '</label>' + + '</div>' + var $group = $(groupHTML).appendTo('#qunit-fixture') + + $group.find('input').on('change', function (e) { + e.preventDefault() + assert.ok(true, 'change event fired') + done() + }) + + $group.find('label').trigger('click') + }) + QUnit.test('should check for closest matching toggle', function (assert) { assert.expect(12) var groupHTML = '<div class="btn-group" data-toggle="buttons">' diff --git a/js/tests/unit/carousel.js b/js/tests/unit/carousel.js index d6d0186e1..dbdea921a 100644 --- a/js/tests/unit/carousel.js +++ b/js/tests/unit/carousel.js @@ -1,5 +1,5 @@ $(function () { - 'use strict'; + 'use strict' QUnit.module('carousel plugin') @@ -507,6 +507,37 @@ $(function () { assert.strictEqual($template.find('.carousel-item')[1], $template.find('.active')[0], 'second item active') }) + QUnit.test('should not prevent keydown if key is not ARROW_LEFT or ARROW_RIGHT', function (assert) { + assert.expect(2) + var templateHTML = '<div id="myCarousel" class="carousel" data-interval="false">' + + '<div class="carousel-inner">' + + '<div id="first" class="carousel-item active">' + + '<img alt="">' + + '</div>' + + '</div>' + + '</div>' + var $template = $(templateHTML) + + $template.bootstrapCarousel() + var done = assert.async() + + var eventArrowDown = $.Event('keydown', { which: 40 }) + var eventArrowUp = $.Event('keydown', { which: 38 }) + + $template.one('keydown', function (event) { + assert.strictEqual(event.isDefaultPrevented(), false) + }) + + $template.trigger(eventArrowDown) + + $template.one('keydown', function (event) { + assert.strictEqual(event.isDefaultPrevented(), false) + done() + }) + + $template.trigger(eventArrowUp) + }) + 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">' @@ -759,4 +790,36 @@ $(function () { .bootstrapCarousel('prev') assert.strictEqual($carousel.find('.carousel-item.active').attr('id'), 'one', 'carousel did not wrap around and stayed on 1st slide') }) + + QUnit.test('should not prevent keydown for inputs and textareas', function (assert) { + assert.expect(2) + var templateHTML = '<div id="myCarousel" class="carousel" data-interval="false">' + + '<div class="carousel-inner">' + + '<div id="first" class="carousel-item">' + + '<input type="text" id="inputText" />' + + '</div>' + + '<div id="second" class="carousel-item active">' + + '<textarea id="txtArea"></textarea>' + + '</div>' + + '</div>' + + '</div>' + var $template = $(templateHTML) + var done = assert.async() + $template.appendTo('#qunit-fixture') + var $inputText = $template.find('#inputText') + var $textArea = $template.find('#txtArea') + $template.bootstrapCarousel() + + var eventKeyDown = $.Event('keydown', { which: 65 }) // 65 for "a" + $inputText.on('keydown', function (event) { + assert.strictEqual(event.isDefaultPrevented(), false) + }) + $inputText.trigger(eventKeyDown) + + $textArea.on('keydown', function (event) { + assert.strictEqual(event.isDefaultPrevented(), false) + done() + }) + $textArea.trigger(eventKeyDown) + }) }) diff --git a/js/tests/unit/collapse.js b/js/tests/unit/collapse.js index 892da52ed..713930433 100644 --- a/js/tests/unit/collapse.js +++ b/js/tests/unit/collapse.js @@ -1,5 +1,5 @@ $(function () { - 'use strict'; + 'use strict' QUnit.module('collapse plugin') @@ -48,15 +48,38 @@ $(function () { assert.expect(2) var $el = $('<div class="collapse"/>').bootstrapCollapse('show') - assert.ok($el.hasClass('in'), 'has class "in"') + assert.ok($el.hasClass('show'), 'has class "show"') assert.ok(!/height/i.test($el.attr('style')), 'has height reset') }) + QUnit.test('should collapse only the first collapse', function (assert) { + assert.expect(2) + var html = [ + '<div class="panel-group" id="accordion1">', + '<div class="panel">', + '<div id="collapse1" class="collapse"/>', + '</div>', + '</div>', + '<div class="panel-group" id="accordion2">', + '<div class="panel">', + '<div id="collapse2" class="collapse show"/>', + '</div>', + '</div>' + ].join('') + $(html).appendTo('#qunit-fixture') + var $el1 = $('#collapse1') + var $el2 = $('#collapse2') + $el1.bootstrapCollapse('show') + + assert.ok($el1.hasClass('show')) + assert.ok($el2.hasClass('show')) + }) + QUnit.test('should hide a collapsed element', function (assert) { assert.expect(1) var $el = $('<div class="collapse"/>').bootstrapCollapse('hide') - assert.ok(!$el.hasClass('in'), 'does not have class "in"') + assert.ok(!$el.hasClass('show'), 'does not have class "show"') }) QUnit.test('should not fire shown when show is prevented', function (assert) { @@ -127,7 +150,7 @@ $(function () { var $target = $('<a role="button" data-toggle="collapse" href="#test1"/>').appendTo('#qunit-fixture') - $('<div id="test1" class="in"/>') + $('<div id="test1" class="show"/>') .appendTo('#qunit-fixture') .on('hidden.bs.collapse', function () { assert.ok($target.hasClass('collapsed'), 'target has collapsed class') @@ -162,7 +185,7 @@ $(function () { var $target = $('<a role="button" data-toggle="collapse" href="#test1"/>').appendTo('#qunit-fixture') var $alt = $('<a role="button" data-toggle="collapse" href="#test1"/>').appendTo('#qunit-fixture') - $('<div id="test1" class="in"/>') + $('<div id="test1" class="show"/>') .appendTo('#qunit-fixture') .on('hidden.bs.collapse', function () { assert.ok($target.hasClass('collapsed'), 'target has collapsed class') @@ -177,7 +200,7 @@ $(function () { assert.expect(0) var done = assert.async() - var $test = $('<div id="test1" class="in"/>') + var $test = $('<div id="test1" class="show"/>') .appendTo('#qunit-fixture') .on('hide.bs.collapse', function () { assert.ok(false) @@ -221,7 +244,7 @@ $(function () { assert.expect(1) var done = assert.async() - $('<div class="collapse in"></div>') + $('<div class="collapse show"></div>') .appendTo('#qunit-fixture') .on('hide.bs.collapse', function () { assert.ok(true, 'hiding a previously-uninitialized shown collapse when the "hide" method is called') @@ -244,7 +267,7 @@ $(function () { var $target1 = $('<a role="button" data-toggle="collapse" href="#body1" data-parent="#accordion"/>').appendTo($groups.eq(0)) - $('<div id="body1" class="in"/>').appendTo($groups.eq(0)) + $('<div id="body1" class="show"/>').appendTo($groups.eq(0)) var $target2 = $('<a class="collapsed" data-toggle="collapse" role="button" href="#body2" data-parent="#accordion"/>').appendTo($groups.eq(1)) @@ -278,7 +301,7 @@ $(function () { var $target1 = $('<a role="button" data-toggle="collapse" href="#body1" data-parent=".accordion"/>').appendTo($groups.eq(0)) - $('<div id="body1" class="in"/>').appendTo($groups.eq(0)) + $('<div id="body1" class="show"/>').appendTo($groups.eq(0)) var $target2 = $('<a class="collapsed" data-toggle="collapse" role="button" href="#body2" data-parent=".accordion"/>').appendTo($groups.eq(1)) @@ -321,7 +344,7 @@ $(function () { var $target = $('<a role="button" data-toggle="collapse" href="#test1" aria-expanded="true"/>').appendTo('#qunit-fixture') - $('<div id="test1" class="in"/>') + $('<div id="test1" class="show"/>') .appendTo('#qunit-fixture') .on('hidden.bs.collapse', function () { assert.strictEqual($target.attr('aria-expanded'), 'false', 'aria-expanded on target is "false"') @@ -356,7 +379,7 @@ $(function () { var $target = $('<a role="button" data-toggle="collapse" href="#test1" aria-expanded="true"/>').appendTo('#qunit-fixture') var $alt = $('<a role="button" data-toggle="collapse" href="#test1" aria-expanded="true"/>').appendTo('#qunit-fixture') - $('<div id="test1" class="in"/>') + $('<div id="test1" class="show"/>') .appendTo('#qunit-fixture') .on('hidden.bs.collapse', function () { assert.strictEqual($target.attr('aria-expanded'), 'false', 'aria-expanded on target is "false"') @@ -380,7 +403,7 @@ $(function () { var $target1 = $('<a role="button" data-toggle="collapse" href="#body1" data-parent="#accordion"/>').appendTo($groups.eq(0)) - $('<div id="body1" aria-expanded="true" class="in"/>').appendTo($groups.eq(0)) + $('<div id="body1" aria-expanded="true" class="show"/>').appendTo($groups.eq(0)) var $target2 = $('<a role="button" data-toggle="collapse" href="#body2" data-parent="#accordion" class="collapsed" />').appendTo($groups.eq(1)) @@ -426,7 +449,7 @@ $(function () { $target2.trigger('click') $body2 - .toggleClass('in collapsing') + .toggleClass('show collapsing') .data('bs.collapse')._isTransitioning = 1 $target1.trigger('click') @@ -443,7 +466,7 @@ $(function () { var $target = $('<a role="button" data-toggle="collapse" href="#test1"/>').appendTo('#qunit-fixture') - $('<div id="test1" class="in"/>') + $('<div id="test1" class="show"/>') .appendTo('#qunit-fixture') .on('hidden.bs.collapse', function () { assert.ok($target.hasClass('collapsed')) diff --git a/js/tests/unit/dropdown.js b/js/tests/unit/dropdown.js index e6cda58d4..53455c2a6 100644 --- a/js/tests/unit/dropdown.js +++ b/js/tests/unit/dropdown.js @@ -1,5 +1,5 @@ $(function () { - 'use strict'; + 'use strict' QUnit.module('dropdowns plugin') @@ -59,7 +59,7 @@ $(function () { + '</ul>' var $dropdown = $(dropdownHTML).find('[data-toggle="dropdown"]').bootstrapDropdown().trigger('click') - assert.ok(!$dropdown.parent('.dropdown').hasClass('open'), '"open" class added on click') + assert.ok(!$dropdown.parent('.dropdown').hasClass('show'), '"show" class added on click') }) QUnit.test('should set aria-expanded="true" on target when dropdown menu is shown', function (assert) { @@ -128,10 +128,10 @@ $(function () { + '</ul>' var $dropdown = $(dropdownHTML).find('[data-toggle="dropdown"]').bootstrapDropdown().trigger('click') - assert.ok(!$dropdown.parent('.dropdown').hasClass('open'), '"open" class added on click') + assert.ok(!$dropdown.parent('.dropdown').hasClass('show'), '"show" class added on click') }) - QUnit.test('should add class open to menu if clicked', function (assert) { + QUnit.test('should add class show to menu if clicked', function (assert) { assert.expect(1) var dropdownHTML = '<ul class="tabs">' + '<li class="dropdown">' @@ -146,7 +146,7 @@ $(function () { + '</ul>' var $dropdown = $(dropdownHTML).find('[data-toggle="dropdown"]').bootstrapDropdown().trigger('click') - assert.ok($dropdown.parent('.dropdown').hasClass('open'), '"open" class added on click') + assert.ok($dropdown.parent('.dropdown').hasClass('show'), '"show" class added on click') }) QUnit.test('should test if element has a # before assuming it\'s a selector', function (assert) { @@ -164,11 +164,11 @@ $(function () { + '</ul>' var $dropdown = $(dropdownHTML).find('[data-toggle="dropdown"]').bootstrapDropdown().trigger('click') - assert.ok($dropdown.parent('.dropdown').hasClass('open'), '"open" class added on click') + assert.ok($dropdown.parent('.dropdown').hasClass('show'), '"show" class added on click') }) - QUnit.test('should remove "open" class if body is clicked', function (assert) { + QUnit.test('should remove "show" class if body is clicked', function (assert) { assert.expect(2) var dropdownHTML = '<ul class="tabs">' + '<li class="dropdown">' @@ -187,12 +187,12 @@ $(function () { .bootstrapDropdown() .trigger('click') - assert.ok($dropdown.parent('.dropdown').hasClass('open'), '"open" class added on click') + assert.ok($dropdown.parent('.dropdown').hasClass('show'), '"show" class added on click') $(document.body).trigger('click') - assert.ok(!$dropdown.parent('.dropdown').hasClass('open'), '"open" class removed') + assert.ok(!$dropdown.parent('.dropdown').hasClass('show'), '"show" class removed') }) - QUnit.test('should remove "open" class if body is clicked, with multiple dropdowns', function (assert) { + QUnit.test('should remove "show" class if body is clicked, with multiple dropdowns', function (assert) { assert.expect(7) var dropdownHTML = '<ul class="nav">' + '<li><a href="#menu1">Menu 1</a></li>' @@ -217,16 +217,16 @@ $(function () { assert.strictEqual($dropdowns.length, 2, 'two dropdowns') $first.trigger('click') - assert.strictEqual($first.parents('.open').length, 1, '"open" class added on click') - assert.strictEqual($('#qunit-fixture .open').length, 1, 'only one dropdown is open') + assert.strictEqual($first.parents('.show').length, 1, '"show" class added on click') + assert.strictEqual($('#qunit-fixture .show').length, 1, 'only one dropdown is shown') $(document.body).trigger('click') - assert.strictEqual($('#qunit-fixture .open').length, 0, '"open" class removed') + assert.strictEqual($('#qunit-fixture .show').length, 0, '"show" class removed') $last.trigger('click') - assert.strictEqual($last.parent('.open').length, 1, '"open" class added on click') - assert.strictEqual($('#qunit-fixture .open').length, 1, 'only one dropdown is open') + assert.strictEqual($last.parent('.show').length, 1, '"show" class added on click') + assert.strictEqual($('#qunit-fixture .show').length, 1, 'only one dropdown is shown') $(document.body).trigger('click') - assert.strictEqual($('#qunit-fixture .open').length, 0, '"open" class removed') + assert.strictEqual($('#qunit-fixture .show').length, 0, '"show" class removed') }) QUnit.test('should fire show and hide event', function (assert) { @@ -411,7 +411,7 @@ $(function () { $('#textField').trigger('click') - assert.ok($dropdown.parent('.btn-group').hasClass('open'), 'dropdown menu is open') + assert.ok($dropdown.parent('.btn-group').hasClass('show'), 'dropdown menu is shown') }) QUnit.test('should not close the dropdown if the user clicks on a textarea', function (assert) { @@ -430,6 +430,6 @@ $(function () { $('#textArea').trigger('click') - assert.ok($dropdown.parent('.btn-group').hasClass('open'), 'dropdown menu is open') + assert.ok($dropdown.parent('.btn-group').hasClass('show'), 'dropdown menu is shown') }) }) diff --git a/js/tests/unit/modal.js b/js/tests/unit/modal.js index 28c1e0ec7..7af5aeb27 100644 --- a/js/tests/unit/modal.js +++ b/js/tests/unit/modal.js @@ -1,5 +1,5 @@ $(function () { - 'use strict'; + 'use strict' QUnit.module('modal plugin') @@ -174,6 +174,19 @@ $(function () { .bootstrapModal('show') }) + QUnit.test('should not close modal when clicking outside of modal-content if data-backdrop="true"', function (assert) { + assert.expect(1) + var done = assert.async() + + $('<div id="modal-test" data-backdrop="false"><div class="contents"/></div>') + .on('shown.bs.modal', function () { + $('#modal-test').trigger('click') + assert.ok($('#modal-test').is(':visible'), 'modal not hidden') + done() + }) + .bootstrapModal('show') + }) + QUnit.test('should close modal when escape key is pressed via keydown', function (assert) { assert.expect(3) var done = assert.async() @@ -363,6 +376,44 @@ $(function () { .bootstrapModal('show') }) + QUnit.test('should have a paddingRight when the modal is taller than the viewport', function (assert) { + assert.expect(2) + var done = assert.async() + $('<div class="navbar-fixed-top navbar-fixed-bottom is-fixed">@Johann-S</div>').appendTo('#qunit-fixture') + $('.navbar-fixed-top, .navbar-fixed-bottom, .is-fixed').css('padding-right', '10px') + + $('<div id="modal-test"/>') + .on('shown.bs.modal', function () { + var paddingRight = parseInt($(document.body).css('padding-right'), 10) + assert.strictEqual(isNaN(paddingRight), false) + assert.strictEqual(paddingRight !== 0, true) + $(document.body).css('padding-right', '') // Because test case "should ignore other inline styles when trying to restore body padding after closing" fail if not + done() + }) + .bootstrapModal('show') + }) + + QUnit.test('should remove padding-right on modal after closing', function (assert) { + assert.expect(3) + var done = assert.async() + $('<div class="navbar-fixed-top navbar-fixed-bottom is-fixed">@Johann-S</div>').appendTo('#qunit-fixture') + $('.navbar-fixed-top, .navbar-fixed-bottom, .is-fixed').css('padding-right', '10px') + + $('<div id="modal-test"/>') + .on('shown.bs.modal', function () { + var paddingRight = parseInt($(document.body).css('padding-right'), 10) + assert.strictEqual(isNaN(paddingRight), false) + assert.strictEqual(paddingRight !== 0, true) + $(this).bootstrapModal('hide') + }) + .on('hidden.bs.modal', function () { + var paddingRight = parseInt($(document.body).css('padding-right'), 10) + assert.strictEqual(paddingRight, 0) + done() + }) + .bootstrapModal('show') + }) + QUnit.test('should ignore other inline styles when trying to restore body padding after closing', function (assert) { assert.expect(2) var done = assert.async() @@ -403,4 +454,26 @@ $(function () { }) .bootstrapModal('show') }) + + QUnit.test('should not follow link in area tag', function (assert) { + assert.expect(2) + var done = assert.async() + + $('<map><area id="test" shape="default" data-toggle="modal" data-target="#modal-test" href="demo.html"/></map>') + .appendTo('#qunit-fixture') + + $('<div id="modal-test"><div class="contents"><div id="close" data-dismiss="modal"/></div></div>') + .appendTo('#qunit-fixture') + + $('#test') + .on('click.bs.modal.data-api', function (event) { + assert.notOk(event.isDefaultPrevented(), 'navigating to href will happen') + + setTimeout(function () { + assert.ok(event.isDefaultPrevented(), 'model shown instead of navigating to href') + done() + }, 1) + }) + .trigger('click') + }) }) diff --git a/js/tests/unit/phantom.js b/js/tests/unit/phantom.js index 525aea002..eea7486a4 100644 --- a/js/tests/unit/phantom.js +++ b/js/tests/unit/phantom.js @@ -7,7 +7,7 @@ */ (function () { - 'use strict'; + 'use strict' // Don't re-order tests. QUnit.config.reorder = false @@ -70,32 +70,3 @@ }) }()) - - -// bind polyfill -// shoutout mdn: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind#Polyfill - -if (!Function.prototype.bind) { - Function.prototype.bind = function (oThis) { - if (typeof this !== 'function') { - // closest thing possible to the ECMAScript 5 - // internal IsCallable function - throw new TypeError('Function.prototype.bind - what is trying to be bound is not callable'); - } - - var aArgs = Array.prototype.slice.call(arguments, 1) - var fToBind = this - var FNOP = function () {} - var fBound = function () { - return fToBind.apply(this instanceof FNOP ? this : oThis, aArgs.concat(Array.prototype.slice.call(arguments))) - } - - if (this.prototype) { - // native functions don't have a prototype - FNOP.prototype = this.prototype - } - fBound.prototype = new FNOP() - - return fBound - } -} diff --git a/js/tests/unit/popover.js b/js/tests/unit/popover.js index fcd7791d2..5452def58 100644 --- a/js/tests/unit/popover.js +++ b/js/tests/unit/popover.js @@ -1,5 +1,5 @@ $(function () { - 'use strict'; + 'use strict' QUnit.module('popover plugin') @@ -229,7 +229,7 @@ $(function () { $popover.bootstrapPopover('show') $popover.bootstrapPopover('dispose') - assert.ok(!$popover.hasClass('in'), 'popover is hidden') + assert.ok(!$popover.hasClass('show'), 'popover is hidden') assert.ok(!$popover.data('popover'), 'popover does not have data') assert.strictEqual($._data($popover[0], 'events').click[0].namespace, 'foo', 'popover still has click.foo') assert.ok(!$._data($popover[0], 'events').mouseover && !$._data($popover[0], 'events').mouseout, 'popover does not have any events') @@ -320,4 +320,16 @@ $(function () { .bootstrapPopover('show') }) + QUnit.test('should throw an error when show is called on hidden elements', function (assert) { + assert.expect(1) + var done = assert.async() + + try { + $('<div data-toggle="popover" data-title="some title" data-content="@Johann-S" style="display: none"/>').bootstrapPopover('show') + } + catch (err) { + assert.strictEqual(err.message, 'Please use show on visible elements') + done() + } + }) }) diff --git a/js/tests/unit/scrollspy.js b/js/tests/unit/scrollspy.js index 97ddd16ec..877ec67a2 100644 --- a/js/tests/unit/scrollspy.js +++ b/js/tests/unit/scrollspy.js @@ -1,5 +1,5 @@ $(function () { - 'use strict'; + 'use strict' QUnit.module('scrollspy plugin') @@ -231,8 +231,8 @@ $(function () { .appendTo('#qunit-fixture') .bootstrapScrollspy({ offset: 0, target: '#navigation' }) - !function testActiveElements() { - if (++times > 3) return done() + function testActiveElements() { + if (++times > 3) { return done() } $content.one('scroll', function () { assert.ok($('#a-1').hasClass('active'), 'nav item for outer element has "active" class') @@ -241,7 +241,9 @@ $(function () { }) $content.scrollTop($content.scrollTop() + 10) - }() + } + + testActiveElements() }) QUnit.test('should clear selection if above the first section', function (assert) { @@ -285,6 +287,50 @@ $(function () { .scrollTop(201) }) + QUnit.test('should NOT clear selection if above the first section and first section is at the top', function (assert) { + assert.expect(4) + var done = assert.async() + + var sectionHTML = '<div id="header" style="height: 500px;"></div>' + + '<nav id="navigation" class="navbar">' + + '<ul class="nav navbar-nav">' + + '<li><a id="one-link" class="nav-link active" href="#one">One</a></li>' + + '<li><a id="two-link" class="nav-link" href="#two">Two</a></li>' + + '<li><a id="three-link" class="nav-link" href="#three">Three</a></li>' + + '</ul>' + + '</nav>' + $(sectionHTML).appendTo('#qunit-fixture') + + var negativeHeight = -10 + var startOfSectionTwo = 101 + + var scrollspyHTML = '<div id="content" style="height: 200px; overflow-y: auto;">' + + '<div id="one" style="height: 100px;"/>' + + '<div id="two" style="height: 100px;"/>' + + '<div id="three" style="height: 100px;"/>' + + '<div id="spacer" style="height: 100px;"/>' + + '</div>' + var $scrollspy = $(scrollspyHTML).appendTo('#qunit-fixture') + + $scrollspy + .bootstrapScrollspy({ + target: '#navigation', + offset: $scrollspy.position().top + }) + .one('scroll', function () { + assert.strictEqual($('.active').length, 1, '"active" class on only one element present') + assert.strictEqual($('.active').is('#two-link'), true, '"active" class on second section') + $scrollspy + .one('scroll', function () { + assert.strictEqual($('.active').length, 1, '"active" class on only one element present') + assert.strictEqual($('.active').is('#one-link'), true, '"active" class on first section') + done() + }) + .scrollTop(negativeHeight) + }) + .scrollTop(startOfSectionTwo) + }) + QUnit.test('should correctly select navigation element on backward scrolling when each target section height is 100%', function (assert) { assert.expect(5) var navbarHtml = @@ -399,8 +445,8 @@ $(function () { $navbar.appendTo('#qunit-fixture') $content.appendTo('#qunit-fixture') - if (type === 'js') $content.bootstrapScrollspy({ target: '.navbar', offset: 0, method: 'position' }) - else if (type === 'data') $(window).trigger('load') + if (type === 'js') { $content.bootstrapScrollspy({ target: '.navbar', offset: 0, method: 'position' }) } + else if (type === 'data') { $(window).trigger('load') } var $target = $('#div-' + type + 'm-2') var scrollspy = $content.data('bs.scrollspy') diff --git a/js/tests/unit/tab.js b/js/tests/unit/tab.js index 2e0143293..62eece896 100644 --- a/js/tests/unit/tab.js +++ b/js/tests/unit/tab.js @@ -1,5 +1,5 @@ $(function () { - 'use strict'; + 'use strict' QUnit.module('tabs plugin') @@ -76,6 +76,22 @@ $(function () { assert.strictEqual($('#qunit-fixture').find('.active').attr('id'), 'home') }) + QUnit.test('should activate element by tab id in ordered list', function (assert) { + assert.expect(2) + var pillsHTML = '<ol class="pills">' + + '<li><a href="#home">Home</a></li>' + + '<li><a href="#profile">Profile</a></li>' + + '</ol>' + + $('<ol><li id="home"/><li id="profile"/></ol>').appendTo('#qunit-fixture') + + $(pillsHTML).find('li:last a').bootstrapTab('show') + assert.strictEqual($('#qunit-fixture').find('.active').attr('id'), 'profile') + + $(pillsHTML).find('li:first a').bootstrapTab('show') + assert.strictEqual($('#qunit-fixture').find('.active').attr('id'), 'home') + }) + QUnit.test('should not fire shown when show is prevented', function (assert) { assert.expect(1) var done = assert.async() @@ -92,6 +108,44 @@ $(function () { .bootstrapTab('show') }) + QUnit.test('should not fire shown when tab is already active', function (assert) { + assert.expect(0) + var tabsHTML = '<ul class="nav nav-tabs" role="tablist">' + + '<li class="nav-item"><a href="#home" class="nav-link active" role="tab">Home</a></li>' + + '<li class="nav-item"><a href="#profile" class="nav-link" role="tab">Profile</a></li>' + + '</ul>' + + '<div class="tab-content">' + + '<div class="tab-pane active" id="home" role="tabpanel"></div>' + + '<div class="tab-pane" id="profile" role="tabpanel"></div>' + + '</div>' + + $(tabsHTML) + .find('a.active') + .on('shown.bs.tab', function () { + assert.ok(true, 'shown event fired') + }) + .bootstrapTab('show') + }) + + QUnit.test('should not fire shown when tab is disabled', function (assert) { + assert.expect(0) + var tabsHTML = '<ul class="nav nav-tabs" role="tablist">' + + '<li class="nav-item"><a href="#home" class="nav-link active" role="tab">Home</a></li>' + + '<li class="nav-item"><a href="#profile" class="nav-link disabled" role="tab">Profile</a></li>' + + '</ul>' + + '<div class="tab-content">' + + '<div class="tab-pane active" id="home" role="tabpanel"></div>' + + '<div class="tab-pane" id="profile" role="tabpanel"></div>' + + '</div>' + + $(tabsHTML) + .find('a.disabled') + .on('shown.bs.tab', function () { + assert.ok(true, 'shown event fired') + }) + .bootstrapTab('show') + }) + QUnit.test('show and shown events should reference correct relatedTarget', function (assert) { assert.expect(2) var done = assert.async() @@ -225,4 +279,36 @@ $(function () { assert.strictEqual($tabs.find('a:not(.active)').attr('aria-expanded'), 'false', 'after second show event, hidden tab has aria-expanded = false') }) + QUnit.test('selected tab should deactivate previous selected tab', function (assert) { + assert.expect(2) + var tabsHTML = '<ul class="nav nav-tabs">' + + '<li class="nav-item"><a class="nav-link active" href="#home" data-toggle="tab">Home</a></li>' + + '<li class="nav-item"><a class="nav-link" href="#profile" data-toggle="tab">Profile</a></li>' + + '</ul>' + var $tabs = $(tabsHTML).appendTo('#qunit-fixture') + + $tabs.find('li:last a').trigger('click') + assert.notOk($tabs.find('li:first a').hasClass('active')) + assert.ok($tabs.find('li:last a').hasClass('active')) + }) + + QUnit.test('selected tab should deactivate previous selected link in dropdown', function (assert) { + assert.expect(3) + var tabsHTML = '<ul class="nav nav-tabs">' + + '<li class="nav-item"><a class="nav-link" href="#home" data-toggle="tab">Home</a></li>' + + '<li class="nav-item"><a class="nav-link" href="#profile" data-toggle="tab">Profile</a></li>' + + '<li class="nav-item dropdown"><a class="nav-link dropdown-toggle active" data-toggle="dropdown" href="#">Dropdown</a>' + + '<div class="dropdown-menu">' + + '<a class="dropdown-item active" href="#dropdown1" id="dropdown1-tab" data-toggle="tab">@fat</a>' + + '<a class="dropdown-item" href="#dropdown2" id="dropdown2-tab" data-toggle="tab">@mdo</a>' + + '</div>' + + '</li>' + + '</ul>' + var $tabs = $(tabsHTML).appendTo('#qunit-fixture') + + $tabs.find('li:first > a').trigger('click') + assert.ok($tabs.find('li:first a').hasClass('active')) + assert.notOk($tabs.find('li:last > a').hasClass('active')) + assert.notOk($tabs.find('li:last > .dropdown-menu > a:first').hasClass('active')) + }) }) diff --git a/js/tests/unit/tooltip.js b/js/tests/unit/tooltip.js index 6cf870551..7ff967fab 100644 --- a/js/tests/unit/tooltip.js +++ b/js/tests/unit/tooltip.js @@ -1,5 +1,5 @@ $(function () { - 'use strict'; + 'use strict' QUnit.module('tooltip plugin') @@ -111,7 +111,7 @@ $(function () { assert .ok($('.tooltip') - .is('.fade.bs-tether-element-attached-top.bs-tether-element-attached-center.in'), 'has correct classes applied') + .is('.fade.bs-tether-element-attached-top.bs-tether-element-attached-center.show'), 'has correct classes applied') $tooltip.bootstrapTooltip('hide') @@ -185,6 +185,19 @@ $(function () { .bootstrapTooltip('show') }) + QUnit.test('should throw an error when show is called on hidden elements', function (assert) { + assert.expect(1) + var done = assert.async() + + try { + $('<div title="tooltip title" style="display: none"/>').bootstrapTooltip('show') + } + catch (err) { + assert.strictEqual(err.message, 'Please use show on visible elements') + done() + } + }) + QUnit.test('should fire inserted event', function (assert) { assert.expect(2) var done = assert.async() @@ -293,7 +306,7 @@ $(function () { $tooltip.bootstrapTooltip('show') $tooltip.bootstrapTooltip('dispose') - assert.ok(!$tooltip.hasClass('in'), 'tooltip is hidden') + assert.ok(!$tooltip.hasClass('show'), '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') @@ -322,7 +335,7 @@ $(function () { .bootstrapTooltip({ trigger: 'manual' }) .bootstrapTooltip('toggle') - assert.ok($('.tooltip').is('.fade.in'), 'tooltip is faded in') + assert.ok($('.tooltip').is('.fade.show'), 'tooltip is faded active') }) QUnit.test('should hide previously shown tooltip when toggle is called on tooltip', function (assert) { @@ -333,7 +346,7 @@ $(function () { .bootstrapTooltip('show') $('.tooltip').bootstrapTooltip('toggle') - assert.ok($('.tooltip').not('.fade.in'), 'tooltip was faded out') + assert.ok($('.tooltip').not('.fade.show'), 'tooltip was faded out') }) QUnit.test('should place tooltips inside body when container is body', function (assert) { @@ -369,7 +382,7 @@ $(function () { var $tooltip = $($target.data('bs.tooltip').tip) // this is some dumb hack stuff because sub pixels in firefox - var top = Math.round($target.offset().top + ($target[0].offsetHeight / 2) - ($tooltip[0].offsetHeight / 2)) + var top = Math.round($target.offset().top + $target[0].offsetHeight / 2 - $tooltip[0].offsetHeight / 2) var top2 = Math.round($tooltip.offset().top) var topDiff = top - top2 assert.ok(topDiff <= 1 && topDiff >= -1) @@ -489,11 +502,11 @@ $(function () { .bootstrapTooltip({ delay: 150 }) setTimeout(function () { - assert.ok(!$('.tooltip').is('.fade.in'), '100ms: tooltip is not faded in') + assert.ok(!$('.tooltip').is('.fade.show'), '100ms: tooltip is not faded active') }, 100) setTimeout(function () { - assert.ok($('.tooltip').is('.fade.in'), '200ms: tooltip is faded in') + assert.ok($('.tooltip').is('.fade.show'), '200ms: tooltip is faded active') done() }, 200) @@ -509,12 +522,12 @@ $(function () { .bootstrapTooltip({ delay: 150 }) setTimeout(function () { - assert.ok(!$('.tooltip').is('.fade.in'), '100ms: tooltip not faded in') + assert.ok(!$('.tooltip').is('.fade.show'), '100ms: tooltip not faded active') $tooltip.trigger('mouseout') }, 100) setTimeout(function () { - assert.ok(!$('.tooltip').is('.fade.in'), '200ms: tooltip not faded in') + assert.ok(!$('.tooltip').is('.fade.show'), '200ms: tooltip not faded active') done() }, 200) @@ -527,19 +540,19 @@ $(function () { var $tooltip = $('<a href="#" rel="tooltip" title="Another tooltip"/>') .appendTo('#qunit-fixture') - .bootstrapTooltip({ delay: { show: 0, hide: 150 }}) + .bootstrapTooltip({ delay: { show: 0, hide: 150 } }) setTimeout(function () { - assert.ok($('.tooltip').is('.fade.in'), '1ms: tooltip faded in') + assert.ok($('.tooltip').is('.fade.show'), '1ms: tooltip faded active') $tooltip.trigger('mouseout') setTimeout(function () { - assert.ok($('.tooltip').is('.fade.in'), '100ms: tooltip still faded in') + assert.ok($('.tooltip').is('.fade.show'), '100ms: tooltip still faded active') $tooltip.trigger('mouseenter') }, 100) setTimeout(function () { - assert.ok($('.tooltip').is('.fade.in'), '200ms: tooltip still faded in') + assert.ok($('.tooltip').is('.fade.show'), '200ms: tooltip still faded active') done() }, 200) }, 0) @@ -556,12 +569,12 @@ $(function () { .bootstrapTooltip({ delay: 150 }) setTimeout(function () { - assert.ok(!$('.tooltip').is('.fade.in'), '100ms: tooltip not faded in') + assert.ok(!$('.tooltip').is('.fade.show'), '100ms: tooltip not faded active') $tooltip.trigger('mouseout') }, 100) setTimeout(function () { - assert.ok(!$('.tooltip').is('.fade.in'), '200ms: tooltip not faded in') + assert.ok(!$('.tooltip').is('.fade.show'), '200ms: tooltip not faded active') done() }, 200) @@ -574,15 +587,15 @@ $(function () { var $tooltip = $('<a href="#" rel="tooltip" title="Another tooltip"/>') .appendTo('#qunit-fixture') - .bootstrapTooltip({ delay: { show: 150, hide: 0 }}) + .bootstrapTooltip({ delay: { show: 150, hide: 0 } }) setTimeout(function () { - assert.ok(!$('.tooltip').is('.fade.in'), '100ms: tooltip not faded in') + assert.ok(!$('.tooltip').is('.fade.show'), '100ms: tooltip not faded active') $tooltip.trigger('mouseout') }, 100) setTimeout(function () { - assert.ok(!$('.tooltip').is('.fade.in'), '250ms: tooltip not faded in') + assert.ok(!$('.tooltip').is('.fade.show'), '250ms: tooltip not faded active') done() }, 250) @@ -595,19 +608,19 @@ $(function () { var $tooltip = $('<a href="#" rel="tooltip" title="Another tooltip"/>') .appendTo('#qunit-fixture') - .bootstrapTooltip({ delay: { show: 0, hide: 150 }}) + .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.show'), '1ms: tooltip faded active') $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.show'), '100ms: tooltip still faded active') }, 100) setTimeout(function () { - assert.ok(!$($tooltip.data('bs.tooltip').tip).is('.in'), '200ms: tooltip removed') + assert.ok(!$($tooltip.data('bs.tooltip').tip).is('.show'), '200ms: tooltip removed') done() }, 200) @@ -710,10 +723,10 @@ $(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-outer').trigger('mouseenter') - assert.ok(obj._hoverState == 'in', 'the tooltip hoverState should be set to "in"') + assert.ok(obj._hoverState === 'show', 'the tooltip hoverState should be set to "show"') assert.strictEqual(currentUid, $('#tt-content').text()) }) @@ -775,7 +788,7 @@ $(function () { var tooltip = $el.data('bs.tooltip') var $tooltip = $(tooltip.getTipElement()) - function showingTooltip() { return $tooltip.hasClass('in') || tooltip._hoverState == 'in' } + function showingTooltip() { return $tooltip.hasClass('show') || tooltip._hoverState === 'show' } var tests = [ ['mouseenter', 'mouseleave'], @@ -799,10 +812,31 @@ $(function () { $.each(tests, function (idx, triggers) { for (var i = 0, len = triggers.length; i < len; i++) { - $el.trigger(triggers[i]); - assert.equal(i < (len - 1), showingTooltip()) + $el.trigger(triggers[i]) + assert.equal(i < len - 1, showingTooltip()) } }) }) + QUnit.test('should show on first trigger after hide', function (assert) { + assert.expect(3) + var $el = $('<a href="#" rel="tooltip" title="Test tooltip"/>') + .appendTo('#qunit-fixture') + .bootstrapTooltip({ trigger: 'click hover focus', animation: false }) + + var tooltip = $el.data('bs.tooltip') + var $tooltip = $(tooltip.getTipElement()) + + function showingTooltip() { return $tooltip.hasClass('show') || tooltip._hoverState === 'show' } + + $el.trigger('click') + assert.ok(showingTooltip(), 'tooltip is faded in') + + $el.bootstrapTooltip('hide') + assert.ok(!showingTooltip(), 'tooltip was faded out') + + $el.trigger('click') + assert.ok(showingTooltip(), 'tooltip is faded in again') + }) + }) |
