2017-10-25 09:32:21 +02:00
$ ( function ( ) {
'use strict'
2018-03-29 22:16:56 +02:00
window . Util = typeof bootstrap !== 'undefined' ? bootstrap . Util : Util
2018-05-07 15:56:24 +02:00
QUnit . module ( 'util' , {
afterEach : function ( ) {
$ ( '#qunit-fixture' ) . html ( '' )
}
} )
2017-10-25 09:32:21 +02:00
QUnit . test ( 'Util.getSelectorFromElement should return the correct element' , function ( assert ) {
2018-01-21 21:02:16 +01:00
assert . expect ( 2 )
2017-11-07 12:41:06 +01:00
2017-10-25 09:32:21 +02:00
var $el = $ ( '<div data-target="body"></div>' ) . appendTo ( $ ( '#qunit-fixture' ) )
assert . strictEqual ( Util . getSelectorFromElement ( $el [ 0 ] ) , 'body' )
2017-12-16 13:00:38 +01:00
// Not found element
2017-10-25 09:32:21 +02:00
var $el2 = $ ( '<div data-target="#fakeDiv"></div>' ) . appendTo ( $ ( '#qunit-fixture' ) )
assert . strictEqual ( Util . getSelectorFromElement ( $el2 [ 0 ] ) , null )
} )
2018-12-23 13:30:35 +01:00
QUnit . test ( 'Util.getSelectorFromElement should return null when there is a bad selector' , function ( assert ) {
2018-09-12 10:08:39 +02:00
assert . expect ( 2 )
var $el = $ ( '<div data-target="#1"></div>' ) . appendTo ( $ ( '#qunit-fixture' ) )
2018-12-23 13:30:35 +01:00
assert . strictEqual ( Util . getSelectorFromElement ( $el [ 0 ] ) , null )
var $el2 = $ ( '<a href="/posts"></a>' ) . appendTo ( $ ( '#qunit-fixture' ) )
assert . strictEqual ( Util . getSelectorFromElement ( $el2 [ 0 ] ) , null )
2018-09-12 10:08:39 +02:00
} )
2017-10-25 09:32:21 +02: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 13:00:38 +01:00
toggle : 'boolean' ,
parent : '(string|element)'
2017-10-25 09:32:21 +02:00
}
var config = {
toggle : true ,
parent : 777
}
try {
Util . typeCheckConfig ( namePlugin , config , defaultType )
2017-12-16 13:00:38 +01:00
} catch ( err ) {
assert . strictEqual ( err . message , 'COLLAPSE: Option "parent" provided type "number" but expected type "(string|element)".' )
2017-10-25 09:32:21 +02: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 09:59:20 +01: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 10:38:36 +01:00
assert . strictEqual ( Util . getTransitionDurationFromElement ( $div [ 0 ] ) , 300 )
2018-03-13 09:59:20 +01: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 10:38:36 +01:00
assert . strictEqual ( Util . getTransitionDurationFromElement ( $div [ 0 ] ) , 400 )
2018-03-13 09:59:20 +01:00
} )
2018-10-29 21:11:50 +01:00
QUnit . test ( 'Util.getTransitionDurationFromElement should return the addition of transition-delay and transition-duration' , function ( assert ) {
assert . expect ( 2 )
var $fixture = $ ( '#qunit-fixture' )
2018-10-30 14:29:04 +01:00
var $div = $ ( '<div style="transition: all 0s 150ms ease-out;"></div>' ) . appendTo ( $fixture )
2018-10-29 21:11:50 +01:00
var $div2 = $ ( '<div style="transition: all .25s 30ms ease-out;"></div>' ) . appendTo ( $fixture )
2018-10-30 14:29:04 +01:00
assert . strictEqual ( Util . getTransitionDurationFromElement ( $div [ 0 ] ) , 150 )
2018-10-29 21:11:50 +01:00
assert . strictEqual ( Util . getTransitionDurationFromElement ( $div2 [ 0 ] ) , 280 )
} )
2018-03-13 09:59:20 +01: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 10:38:36 +01:00
assert . strictEqual ( Util . getTransitionDurationFromElement ( $div [ 0 ] ) , 300 )
2018-03-13 09:59:20 +01: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 10:38:36 +01:00
assert . strictEqual ( Util . getTransitionDurationFromElement ( $div [ 0 ] ) , 0 )
2018-03-13 09:59:20 +01: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 10:38:36 +01:00
assert . strictEqual ( Util . getTransitionDurationFromElement ( $div [ 0 ] ) , 0 )
2018-03-13 09:59:20 +01:00
} )
2017-10-25 09:32:21 +02: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 11:07:58 +01:00
QUnit . test ( 'Util.supportsTransitionEnd should return true' , function ( assert ) {
assert . expect ( 1 )
assert . ok ( Util . supportsTransitionEnd ( ) )
} )
2017-11-30 10:54:27 +01:00
QUnit . test ( 'Util.findShadowRoot should find the shadow DOM root' , function ( assert ) {
// Only for newer browsers
if ( ! document . documentElement . attachShadow ) {
assert . expect ( 0 )
return
}
assert . expect ( 2 )
var $div = $ ( '<div id="test"></div>' ) . appendTo ( $ ( '#qunit-fixture' ) )
var shadowRoot = $div [ 0 ] . attachShadow ( {
mode : 'open'
} )
assert . equal ( shadowRoot , Util . findShadowRoot ( shadowRoot ) )
shadowRoot . innerHTML = '<button>Shadow Button</button>'
assert . equal ( shadowRoot , Util . findShadowRoot ( shadowRoot . firstChild ) )
} )
QUnit . test ( 'Util.findShadowRoot should return null when attachShadow is not available' , function ( assert ) {
assert . expect ( 1 )
var $div = $ ( '<div id="test"></div>' ) . appendTo ( $ ( '#qunit-fixture' ) )
if ( ! document . documentElement . attachShadow ) {
assert . equal ( null , Util . findShadowRoot ( $div [ 0 ] ) )
} else {
var sandbox = sinon . createSandbox ( )
sandbox . replace ( document . documentElement , 'attachShadow' , function ( ) {
// to avoid empty function
return $div
} )
assert . equal ( null , Util . findShadowRoot ( $div [ 0 ] ) )
sandbox . restore ( )
}
} )
2017-10-25 09:32:21 +02:00
} )