Skip to content

Commit

Permalink
Add unit tests for util.js
Browse files Browse the repository at this point in the history
  • Loading branch information
Johann-S committed Oct 25, 2017
1 parent 9611089 commit 9883270
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
1 change: 1 addition & 0 deletions js/tests/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
<script src="unit/tab.js"></script>
<script src="unit/tooltip.js"></script>
<script src="unit/popover.js"></script>
<script src="unit/util.js"></script>
</head>
<body>
<div id="qunit-container">
Expand Down
57 changes: 57 additions & 0 deletions js/tests/unit/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
$(function () {
'use strict'

QUnit.module('util')

QUnit.test('Util.getSelectorFromElement should return the correct element', function (assert) {
assert.expect(2)
var $el = $('<div data-target="body"></div>').appendTo($('#qunit-fixture'))
assert.strictEqual(Util.getSelectorFromElement($el[0]), 'body')

// not found element
var $el2 = $('<div data-target="#fakeDiv"></div>').appendTo($('#qunit-fixture'))
assert.strictEqual(Util.getSelectorFromElement($el2[0]), null)
})

QUnit.test('Util.typeCheckConfig should thrown an error when a bad config is passed', function (assert) {
assert.expect(1)
var namePlugin = 'collapse'
var defaultType = {
toggle : 'boolean',
parent : '(string|element)'
}
var config = {
toggle: true,
parent: 777
}

try {
Util.typeCheckConfig(namePlugin, config, defaultType)
} catch (e) {
assert.strictEqual(e.message, 'COLLAPSE: Option "parent" provided type "number" but expected type "(string|element)".')
}
})

QUnit.test('Util.isElement should check if we passed an element or not', function (assert) {
assert.expect(3)
var $div = $('<div id="test"></div>').appendTo($('#qunit-fixture'))

assert.strictEqual(Util.isElement($div), 1)
assert.strictEqual(Util.isElement($div[0]), 1)
assert.strictEqual(typeof Util.isElement({}) === 'undefined', true)
})

QUnit.test('Util.getUID should generate a new id uniq', function (assert) {
assert.expect(2)
var id = Util.getUID('test')
var id2 = Util.getUID('test')

assert.ok(id !== id2, id + ' !== ' + id2)

id = Util.getUID('test')
$('<div id="' + id + '"></div>').appendTo($('#qunit-fixture'))

id2 = Util.getUID('test')
assert.ok(id !== id2, id + ' !== ' + id2)
})
})

0 comments on commit 9883270

Please sign in to comment.