diff options
author | Johann-S <johann.servoire@gmail.com> | 2018-04-10 10:28:29 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-04-10 10:28:29 +0300 |
commit | 0871d69ec975b51472a451ed63ffa2af874606d2 (patch) | |
tree | 977e0bcbcde804c5a90da0fc28db41a6748b8b82 | |
parent | 875ed0b975a587fffcc1b0c632e1cc8f612ebc5a (diff) |
v4 improve coverage (#25945)
* Improve code coverage for our Button plugin
* improve coverage for our Alert plugin
* test tooltip update method
* test update and dispose for dropdown
* increase code coverage requirements
-rw-r--r-- | js/src/alert.js | 6 | ||||
-rw-r--r-- | js/tests/karma.conf.js | 8 | ||||
-rw-r--r-- | js/tests/unit/.eslintrc.json | 4 | ||||
-rw-r--r-- | js/tests/unit/alert.js | 39 | ||||
-rw-r--r-- | js/tests/unit/button.js | 23 | ||||
-rw-r--r-- | js/tests/unit/dropdown.js | 112 | ||||
-rw-r--r-- | js/tests/unit/tooltip.js | 29 |
7 files changed, 214 insertions, 7 deletions
diff --git a/js/src/alert.js b/js/src/alert.js index cd31c00999..8772c0de7c 100644 --- a/js/src/alert.js +++ b/js/src/alert.js @@ -58,9 +58,11 @@ const Alert = (($) => { // Public close(element) { - element = element || this._element + let rootElement = this._element + if (element) { + rootElement = this._getRootElement(element) + } - const rootElement = this._getRootElement(element) const customEvent = this._triggerCloseEvent(rootElement) if (customEvent.isDefaultPrevented()) { diff --git a/js/tests/karma.conf.js b/js/tests/karma.conf.js index a2b0a676bd..eceaa7ff72 100644 --- a/js/tests/karma.conf.js +++ b/js/tests/karma.conf.js @@ -60,10 +60,10 @@ module.exports = (config) => { thresholds: { emitWarning: false, global: { - statements: 89, - lines: 89, - branches: 83, - functions: 84 + statements: 90, + branches: 84, + functions: 87, + lines: 90 } } } diff --git a/js/tests/unit/.eslintrc.json b/js/tests/unit/.eslintrc.json index 93aaa05f82..0896f406d0 100644 --- a/js/tests/unit/.eslintrc.json +++ b/js/tests/unit/.eslintrc.json @@ -7,7 +7,9 @@ "globals": { "bootstrap": false, "sinon": false, - "Util": false + "Util": false, + "Alert": false, + "Button": false }, "parserOptions": { "ecmaVersion": 5, diff --git a/js/tests/unit/alert.js b/js/tests/unit/alert.js index 165c91f1e1..fc7727fb1c 100644 --- a/js/tests/unit/alert.js +++ b/js/tests/unit/alert.js @@ -80,4 +80,43 @@ $(function () { }) .bootstrapAlert('close') }) + + QUnit.test('close should use internal _element if no element provided', function (assert) { + assert.expect(1) + + var done = assert.async() + var $el = $('<div/>') + var $alert = $el.bootstrapAlert() + var alertInstance = $alert.data('bs.alert') + + $alert.one('closed.bs.alert', function () { + assert.ok('alert closed') + done() + }) + + alertInstance.close() + }) + + QUnit.test('dispose should remove data and the element', function (assert) { + assert.expect(2) + + var $el = $('<div/>') + var $alert = $el.bootstrapAlert() + + assert.ok(typeof $alert.data('bs.alert') !== 'undefined') + + $alert.data('bs.alert').dispose() + + assert.ok(typeof $alert.data('bs.button') === 'undefined') + }) + + QUnit.test('should return alert version', function (assert) { + assert.expect(1) + + if (typeof Alert !== 'undefined') { + assert.ok(typeof Alert.VERSION === 'string') + } else { + assert.notOk() + } + }) }) diff --git a/js/tests/unit/button.js b/js/tests/unit/button.js index cc2719c3a0..1fdcce95fd 100644 --- a/js/tests/unit/button.js +++ b/js/tests/unit/button.js @@ -172,4 +172,27 @@ $(function () { assert.ok($btn.is(':not(.active)'), 'button did not become active') assert.ok(!$input.is(':checked'), 'checkbox did not get checked') }) + + QUnit.test('dispose should remove data and the element', function (assert) { + assert.expect(2) + + var $el = $('<div/>') + var $button = $el.bootstrapButton() + + assert.ok(typeof $button.data('bs.button') !== 'undefined') + + $button.data('bs.button').dispose() + + assert.ok(typeof $button.data('bs.button') === 'undefined') + }) + + QUnit.test('should return button version', function (assert) { + assert.expect(1) + + if (typeof Button !== 'undefined') { + assert.ok(typeof Button.VERSION === 'string') + } else { + assert.notOk() + } + }) }) diff --git a/js/tests/unit/dropdown.js b/js/tests/unit/dropdown.js index 0e3d370413..726655e9cc 100644 --- a/js/tests/unit/dropdown.js +++ b/js/tests/unit/dropdown.js @@ -940,4 +940,116 @@ $(function () { $dropdown.trigger('click') }) + + QUnit.test('should call Popper.js and detect navbar on update', function (assert) { + assert.expect(3) + + var dropdownHTML = + '<div class="dropdown">' + + ' <a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown</a>' + + ' <div class="dropdown-menu">' + + ' <a class="dropdown-item" href="#">Another link</a>' + + ' </div>' + + '</div>' + + var $dropdown = $(dropdownHTML) + .appendTo('#qunit-fixture') + .find('[data-toggle="dropdown"]') + .bootstrapDropdown() + + var dropdown = $dropdown.data('bs.dropdown') + dropdown.toggle() + assert.ok(dropdown._popper) + + var spyPopper = sinon.spy(dropdown._popper, 'scheduleUpdate') + var spyDetectNavbar = sinon.spy(dropdown, '_detectNavbar') + dropdown.update() + + assert.ok(spyPopper.called) + assert.ok(spyDetectNavbar.called) + }) + + QUnit.test('should just detect navbar on update', function (assert) { + assert.expect(2) + + var dropdownHTML = + '<div class="dropdown">' + + ' <a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown</a>' + + ' <div class="dropdown-menu">' + + ' <a class="dropdown-item" href="#">Another link</a>' + + ' </div>' + + '</div>' + + var $dropdown = $(dropdownHTML) + .appendTo('#qunit-fixture') + .find('[data-toggle="dropdown"]') + .bootstrapDropdown() + + var dropdown = $dropdown.data('bs.dropdown') + var spyDetectNavbar = sinon.spy(dropdown, '_detectNavbar') + + dropdown.update() + + assert.notOk(dropdown._popper) + assert.ok(spyDetectNavbar.called) + }) + + QUnit.test('should dispose dropdown with Popper', function (assert) { + assert.expect(6) + + var dropdownHTML = + '<div class="dropdown">' + + ' <a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown</a>' + + ' <div class="dropdown-menu">' + + ' <a class="dropdown-item" href="#">Another link</a>' + + ' </div>' + + '</div>' + + var $dropdown = $(dropdownHTML) + .appendTo('#qunit-fixture') + .find('[data-toggle="dropdown"]') + .bootstrapDropdown() + + var dropdown = $dropdown.data('bs.dropdown') + dropdown.toggle() + + assert.ok(dropdown._popper) + assert.ok(dropdown._menu !== null) + assert.ok(dropdown._element !== null) + var spyDestroy = sinon.spy(dropdown._popper, 'destroy') + + dropdown.dispose() + + assert.ok(spyDestroy.called) + assert.ok(dropdown._menu === null) + assert.ok(dropdown._element === null) + }) + + QUnit.test('should dispose dropdown', function (assert) { + assert.expect(5) + + var dropdownHTML = + '<div class="dropdown">' + + ' <a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown</a>' + + ' <div class="dropdown-menu">' + + ' <a class="dropdown-item" href="#">Another link</a>' + + ' </div>' + + '</div>' + + var $dropdown = $(dropdownHTML) + .appendTo('#qunit-fixture') + .find('[data-toggle="dropdown"]') + .bootstrapDropdown() + + var dropdown = $dropdown.data('bs.dropdown') + + assert.notOk(dropdown._popper) + assert.ok(dropdown._menu !== null) + assert.ok(dropdown._element !== null) + + dropdown.dispose() + + assert.ok(dropdown._menu === null) + assert.ok(dropdown._element === null) + }) }) diff --git a/js/tests/unit/tooltip.js b/js/tests/unit/tooltip.js index 20d022c5dc..a2a2395309 100644 --- a/js/tests/unit/tooltip.js +++ b/js/tests/unit/tooltip.js @@ -953,4 +953,33 @@ $(function () { $trigger.trigger($.Event('click')) }, 200) }) + + QUnit.test('should call Popper.js to update', function (assert) { + assert.expect(2) + + var $tooltip = $('<a href="#" rel="tooltip" data-trigger="click" title="Another tooltip"/>') + .appendTo('#qunit-fixture') + .bootstrapTooltip() + + var tooltip = $tooltip.data('bs.tooltip') + tooltip.show() + assert.ok(tooltip._popper) + + var spyPopper = sinon.spy(tooltip._popper, 'scheduleUpdate') + tooltip.update() + assert.ok(spyPopper.called) + }) + + QUnit.test('should not call Popper.js to update', function (assert) { + assert.expect(1) + + var $tooltip = $('<a href="#" rel="tooltip" data-trigger="click" title="Another tooltip"/>') + .appendTo('#qunit-fixture') + .bootstrapTooltip() + + var tooltip = $tooltip.data('bs.tooltip') + tooltip.update() + + assert.ok(tooltip._popper === null) + }) }) |