diff --git a/js/src/alert.js b/js/src/alert.js index cd31c00999b5..8772c0de7c33 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 a2b0a676bdcd..eceaa7ff7275 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 93aaa05f821a..0896f406d0f5 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 165c91f1e14f..fc7727fb1c05 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 = $('
') + 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 = $('
') + 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 cc2719c3a085..1fdcce95fd7c 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 = $('
') + 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 0e3d3704137d..726655e9ccce 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 = + '' + + 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 = + '' + + 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 = + '' + + 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 = + '' + + 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 20d022c5dcd2..a2a239530967 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 = $('') + .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 = $('') + .appendTo('#qunit-fixture') + .bootstrapTooltip() + + var tooltip = $tooltip.data('bs.tooltip') + tooltip.update() + + assert.ok(tooltip._popper === null) + }) })