Skip to content

Commit

Permalink
v4 improve coverage (#25945)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Johann-S authored Apr 10, 2018
1 parent 875ed0b commit 0871d69
Show file tree
Hide file tree
Showing 7 changed files with 214 additions and 7 deletions.
6 changes: 4 additions & 2 deletions js/src/alert.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
8 changes: 4 additions & 4 deletions js/tests/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion js/tests/unit/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
"globals": {
"bootstrap": false,
"sinon": false,
"Util": false
"Util": false,
"Alert": false,
"Button": false
},
"parserOptions": {
"ecmaVersion": 5,
Expand Down
39 changes: 39 additions & 0 deletions js/tests/unit/alert.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
})
})
23 changes: 23 additions & 0 deletions js/tests/unit/button.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
})
})
112 changes: 112 additions & 0 deletions js/tests/unit/dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
29 changes: 29 additions & 0 deletions js/tests/unit/tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})

0 comments on commit 0871d69

Please sign in to comment.