2017-10-25 15:32:21 +08:00
$ ( function ( ) {
'use strict'
2018-03-30 04:16:56 +08:00
window . Util = typeof bootstrap !== 'undefined' ? bootstrap . Util : Util
2018-05-07 21:56:24 +08:00
QUnit . module ( 'util' , {
afterEach : function ( ) {
$ ( '#qunit-fixture' ) . html ( '' )
}
} )
2017-10-25 15:32:21 +08:00
QUnit . test ( 'Util.getSelectorFromElement should return the correct element' , function ( assert ) {
2018-01-22 04:02:16 +08:00
assert . expect ( 2 )
2017-11-07 19:41:06 +08:00
2017-10-25 15:32:21 +08:00
var $el = $ ( '<div data-target="body"></div>' ) . appendTo ( $ ( '#qunit-fixture' ) )
assert . strictEqual ( Util . getSelectorFromElement ( $el [ 0 ] ) , 'body' )
2017-12-16 20:00:38 +08:00
// Not found element
2017-10-25 15:32:21 +08:00
var $el2 = $ ( '<div data-target="#fakeDiv"></div>' ) . appendTo ( $ ( '#qunit-fixture' ) )
assert . strictEqual ( Util . getSelectorFromElement ( $el2 [ 0 ] ) , null )
} )
2018-08-08 00:37:46 +08:00
QUnit . test ( 'Util.getSelectorFromElement should use getElementById' , function ( assert ) {
assert . expect ( 2 )
var spy = sinon . spy ( document , 'getElementById' )
var $el = $ ( '<div data-target="#7"></div>' ) . appendTo ( $ ( '#qunit-fixture' ) )
$ ( '<div id="7" />' ) . appendTo ( $ ( '#qunit-fixture' ) )
assert . strictEqual ( Util . getSelectorFromElement ( $el [ 0 ] ) , '#7' )
2018-08-12 04:40:47 +08:00
assert . ok ( spy . called )
} )
QUnit . test ( 'Util.getSelectorFromElement should use querySelector when there are multi ids' , function ( assert ) {
assert . expect ( 2 )
var spy = sinon . spy ( document , 'querySelector' )
var $el = $ ( '<div data-target="#j7, #j8"></div>' ) . appendTo ( $ ( '#qunit-fixture' ) )
$ ( '<div id="j7" />' ) . appendTo ( $ ( '#qunit-fixture' ) )
$ ( '<div id="j8" />' ) . appendTo ( $ ( '#qunit-fixture' ) )
assert . strictEqual ( Util . getSelectorFromElement ( $el [ 0 ] ) , '#j7, #j8' )
2018-08-08 00:37:46 +08:00
assert . ok ( spy . called )
} )
2017-10-25 15:32:21 +08:00
QUnit . test ( 'Util.typeCheckConfig should thrown an error when a bad config is passed' , function ( assert ) {
assert . expect ( 1 )
var namePlugin = 'collapse'
var defaultType = {
2017-12-16 20:00:38 +08:00
toggle : 'boolean' ,
parent : '(string|element)'
2017-10-25 15:32:21 +08:00
}
var config = {
toggle : true ,
parent : 777
}
try {
Util . typeCheckConfig ( namePlugin , config , defaultType )
2017-12-16 20:00:38 +08:00
} catch ( err ) {
assert . strictEqual ( err . message , 'COLLAPSE: Option "parent" provided type "number" but expected type "(string|element)".' )
2017-10-25 15:32:21 +08:00
}
} )
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 )
} )
2018-03-13 16:59:20 +08:00
QUnit . test ( 'Util.getTransitionDurationFromElement should accept transition durations in milliseconds' , function ( assert ) {
assert . expect ( 1 )
var $div = $ ( '<div style="transition: all 300ms ease-out;"></div>' ) . appendTo ( $ ( '#qunit-fixture' ) )
2018-03-13 17:38:36 +08:00
assert . strictEqual ( Util . getTransitionDurationFromElement ( $div [ 0 ] ) , 300 )
2018-03-13 16:59:20 +08:00
} )
QUnit . test ( 'Util.getTransitionDurationFromElement should accept transition durations in seconds' , function ( assert ) {
assert . expect ( 1 )
var $div = $ ( '<div style="transition: all .4s ease-out;"></div>' ) . appendTo ( $ ( '#qunit-fixture' ) )
2018-03-13 17:38:36 +08:00
assert . strictEqual ( Util . getTransitionDurationFromElement ( $div [ 0 ] ) , 400 )
2018-03-13 16:59:20 +08:00
} )
QUnit . test ( 'Util.getTransitionDurationFromElement should get the first transition duration if multiple transition durations are defined' , function ( assert ) {
assert . expect ( 1 )
var $div = $ ( '<div style="transition: transform .3s ease-out, opacity .2s;"></div>' ) . appendTo ( $ ( '#qunit-fixture' ) )
2018-03-13 17:38:36 +08:00
assert . strictEqual ( Util . getTransitionDurationFromElement ( $div [ 0 ] ) , 300 )
2018-03-13 16:59:20 +08:00
} )
QUnit . test ( 'Util.getTransitionDurationFromElement should return 0 if transition duration is not defined' , function ( assert ) {
assert . expect ( 1 )
var $div = $ ( '<div></div>' ) . appendTo ( $ ( '#qunit-fixture' ) )
2018-03-13 17:38:36 +08:00
assert . strictEqual ( Util . getTransitionDurationFromElement ( $div [ 0 ] ) , 0 )
2018-03-13 16:59:20 +08:00
} )
QUnit . test ( 'Util.getTransitionDurationFromElement should return 0 if element is not found in DOM' , function ( assert ) {
assert . expect ( 1 )
var $div = $ ( '#fake-id' )
2018-03-13 17:38:36 +08:00
assert . strictEqual ( Util . getTransitionDurationFromElement ( $div [ 0 ] ) , 0 )
2018-03-13 16:59:20 +08:00
} )
2017-10-25 15:32:21 +08:00
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 )
} )
2018-03-20 18:07:58 +08:00
QUnit . test ( 'Util.supportsTransitionEnd should return true' , function ( assert ) {
assert . expect ( 1 )
assert . ok ( Util . supportsTransitionEnd ( ) )
} )
2017-10-25 15:32:21 +08:00
} )