From: Johann-S Date: Wed, 25 Oct 2017 07:32:21 +0000 (+0200) Subject: Add unit tests for util.js X-Git-Tag: v4.0.0-beta.3~173^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=988327032d6b76fdb70075feb7254bcb053ec117;p=thirdparty%2Fbootstrap.git Add unit tests for util.js --- diff --git a/js/tests/index.html b/js/tests/index.html index 2383fce6e6..0385b8a2ba 100644 --- a/js/tests/index.html +++ b/js/tests/index.html @@ -119,6 +119,7 @@ +
diff --git a/js/tests/unit/util.js b/js/tests/unit/util.js new file mode 100644 index 0000000000..372c6e3f71 --- /dev/null +++ b/js/tests/unit/util.js @@ -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 = $('
').appendTo($('#qunit-fixture')) + assert.strictEqual(Util.getSelectorFromElement($el[0]), 'body') + + // not found element + var $el2 = $('
').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 = $('
').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') + $('
').appendTo($('#qunit-fixture')) + + id2 = Util.getUID('test') + assert.ok(id !== id2, id + ' !== ' + id2) + }) +})