util.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. $(function () {
  2. 'use strict'
  3. window.Util = typeof bootstrap !== 'undefined' ? bootstrap.Util : Util
  4. QUnit.module('util', {
  5. afterEach: function () {
  6. $('#qunit-fixture').html('')
  7. }
  8. })
  9. QUnit.test('Util.getSelectorFromElement should return the correct element', function (assert) {
  10. assert.expect(2)
  11. var $el = $('<div data-target="body"></div>').appendTo($('#qunit-fixture'))
  12. assert.strictEqual(Util.getSelectorFromElement($el[0]), 'body')
  13. // Not found element
  14. var $el2 = $('<div data-target="#fakeDiv"></div>').appendTo($('#qunit-fixture'))
  15. assert.strictEqual(Util.getSelectorFromElement($el2[0]), null)
  16. })
  17. QUnit.test('Util.getSelectorFromElement should return null when there is a bad selector', function (assert) {
  18. assert.expect(2)
  19. var $el = $('<div data-target="#1"></div>').appendTo($('#qunit-fixture'))
  20. assert.strictEqual(Util.getSelectorFromElement($el[0]), null)
  21. var $el2 = $('<a href="/posts"></a>').appendTo($('#qunit-fixture'))
  22. assert.strictEqual(Util.getSelectorFromElement($el2[0]), null)
  23. })
  24. QUnit.test('Util.typeCheckConfig should thrown an error when a bad config is passed', function (assert) {
  25. assert.expect(1)
  26. var namePlugin = 'collapse'
  27. var defaultType = {
  28. toggle: 'boolean',
  29. parent: '(string|element)'
  30. }
  31. var config = {
  32. toggle: true,
  33. parent: 777
  34. }
  35. try {
  36. Util.typeCheckConfig(namePlugin, config, defaultType)
  37. } catch (err) {
  38. assert.strictEqual(err.message, 'COLLAPSE: Option "parent" provided type "number" but expected type "(string|element)".')
  39. }
  40. })
  41. QUnit.test('Util.isElement should check if we passed an element or not', function (assert) {
  42. assert.expect(3)
  43. var $div = $('<div id="test"></div>').appendTo($('#qunit-fixture'))
  44. assert.strictEqual(Util.isElement($div), 1)
  45. assert.strictEqual(Util.isElement($div[0]), 1)
  46. assert.strictEqual(typeof Util.isElement({}) === 'undefined', true)
  47. })
  48. QUnit.test('Util.getTransitionDurationFromElement should accept transition durations in milliseconds', function (assert) {
  49. assert.expect(1)
  50. var $div = $('<div style="transition: all 300ms ease-out;"></div>').appendTo($('#qunit-fixture'))
  51. assert.strictEqual(Util.getTransitionDurationFromElement($div[0]), 300)
  52. })
  53. QUnit.test('Util.getTransitionDurationFromElement should accept transition durations in seconds', function (assert) {
  54. assert.expect(1)
  55. var $div = $('<div style="transition: all .4s ease-out;"></div>').appendTo($('#qunit-fixture'))
  56. assert.strictEqual(Util.getTransitionDurationFromElement($div[0]), 400)
  57. })
  58. QUnit.test('Util.getTransitionDurationFromElement should return the addition of transition-delay and transition-duration', function (assert) {
  59. assert.expect(2)
  60. var $fixture = $('#qunit-fixture')
  61. var $div = $('<div style="transition: all 0s 150ms ease-out;"></div>').appendTo($fixture)
  62. var $div2 = $('<div style="transition: all .25s 30ms ease-out;"></div>').appendTo($fixture)
  63. assert.strictEqual(Util.getTransitionDurationFromElement($div[0]), 150)
  64. assert.strictEqual(Util.getTransitionDurationFromElement($div2[0]), 280)
  65. })
  66. QUnit.test('Util.getTransitionDurationFromElement should get the first transition duration if multiple transition durations are defined', function (assert) {
  67. assert.expect(1)
  68. var $div = $('<div style="transition: transform .3s ease-out, opacity .2s;"></div>').appendTo($('#qunit-fixture'))
  69. assert.strictEqual(Util.getTransitionDurationFromElement($div[0]), 300)
  70. })
  71. QUnit.test('Util.getTransitionDurationFromElement should return 0 if transition duration is not defined', function (assert) {
  72. assert.expect(1)
  73. var $div = $('<div></div>').appendTo($('#qunit-fixture'))
  74. assert.strictEqual(Util.getTransitionDurationFromElement($div[0]), 0)
  75. })
  76. QUnit.test('Util.getTransitionDurationFromElement should return 0 if element is not found in DOM', function (assert) {
  77. assert.expect(1)
  78. var $div = $('#fake-id')
  79. assert.strictEqual(Util.getTransitionDurationFromElement($div[0]), 0)
  80. })
  81. QUnit.test('Util.getUID should generate a new id uniq', function (assert) {
  82. assert.expect(2)
  83. var id = Util.getUID('test')
  84. var id2 = Util.getUID('test')
  85. assert.ok(id !== id2, id + ' !== ' + id2)
  86. id = Util.getUID('test')
  87. $('<div id="' + id + '"></div>').appendTo($('#qunit-fixture'))
  88. id2 = Util.getUID('test')
  89. assert.ok(id !== id2, id + ' !== ' + id2)
  90. })
  91. QUnit.test('Util.supportsTransitionEnd should return true', function (assert) {
  92. assert.expect(1)
  93. assert.ok(Util.supportsTransitionEnd())
  94. })
  95. QUnit.test('Util.findShadowRoot should find the shadow DOM root', function (assert) {
  96. // Only for newer browsers
  97. if (!document.documentElement.attachShadow) {
  98. assert.expect(0)
  99. return
  100. }
  101. assert.expect(2)
  102. var $div = $('<div id="test"></div>').appendTo($('#qunit-fixture'))
  103. var shadowRoot = $div[0].attachShadow({
  104. mode: 'open'
  105. })
  106. assert.equal(shadowRoot, Util.findShadowRoot(shadowRoot))
  107. shadowRoot.innerHTML = '<button>Shadow Button</button>'
  108. assert.equal(shadowRoot, Util.findShadowRoot(shadowRoot.firstChild))
  109. })
  110. QUnit.test('Util.findShadowRoot should return null when attachShadow is not available', function (assert) {
  111. assert.expect(1)
  112. var $div = $('<div id="test"></div>').appendTo($('#qunit-fixture'))
  113. if (!document.documentElement.attachShadow) {
  114. assert.equal(null, Util.findShadowRoot($div[0]))
  115. } else {
  116. var sandbox = sinon.createSandbox()
  117. sandbox.replace(document.documentElement, 'attachShadow', function () {
  118. // to avoid empty function
  119. return $div
  120. })
  121. assert.equal(null, Util.findShadowRoot($div[0]))
  122. sandbox.restore()
  123. }
  124. })
  125. })