dom_spec.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. var _ = require('src/util')
  2. describe('Util - DOM', function () {
  3. var parent, child, target
  4. function div () {
  5. return document.createElement('div')
  6. }
  7. beforeEach(function () {
  8. parent = div()
  9. child = div()
  10. target = div()
  11. parent.appendChild(child)
  12. })
  13. it('inDoc', function () {
  14. expect(_.inDoc(target)).toBe(false)
  15. document.body.appendChild(target)
  16. expect(_.inDoc(target)).toBe(true)
  17. document.body.removeChild(target)
  18. expect(_.inDoc(target)).toBe(false)
  19. })
  20. it('inDoc (iframe)', function (done) {
  21. var f = document.createElement('iframe')
  22. f.onload = function () {
  23. f.contentWindow.document.body.appendChild(target)
  24. expect(_.inDoc(target)).toBe(true)
  25. document.body.removeChild(f)
  26. done()
  27. }
  28. document.body.appendChild(f)
  29. f.src = 'about:blank'
  30. })
  31. it('getAttr', function () {
  32. target.setAttribute('v-test', 'ok')
  33. var val = _.getAttr(target, 'v-test')
  34. expect(val).toBe('ok')
  35. expect(target.hasAttribute('v-test')).toBe(false)
  36. })
  37. it('before', function () {
  38. _.before(target, child)
  39. expect(target.parentNode).toBe(parent)
  40. expect(target.nextSibling).toBe(child)
  41. })
  42. it('after', function () {
  43. _.after(target, child)
  44. expect(target.parentNode).toBe(parent)
  45. expect(child.nextSibling).toBe(target)
  46. })
  47. it('after with sibling', function () {
  48. var sibling = div()
  49. parent.appendChild(sibling)
  50. _.after(target, child)
  51. expect(target.parentNode).toBe(parent)
  52. expect(child.nextSibling).toBe(target)
  53. })
  54. it('remove', function () {
  55. _.remove(child)
  56. expect(child.parentNode).toBeNull()
  57. expect(parent.childNodes.length).toBe(0)
  58. })
  59. it('prepend', function () {
  60. _.prepend(target, parent)
  61. expect(target.parentNode).toBe(parent)
  62. expect(parent.firstChild).toBe(target)
  63. })
  64. it('prepend to empty node', function () {
  65. parent.removeChild(child)
  66. _.prepend(target, parent)
  67. expect(target.parentNode).toBe(parent)
  68. expect(parent.firstChild).toBe(target)
  69. })
  70. it('replace', function () {
  71. _.replace(child, target)
  72. expect(parent.childNodes.length).toBe(1)
  73. expect(parent.firstChild).toBe(target)
  74. })
  75. it('on/off', function () {
  76. // IE requires element to be in document to fire events
  77. document.body.appendChild(target)
  78. var spy = jasmine.createSpy()
  79. _.on(target, 'click', spy)
  80. var e = document.createEvent('HTMLEvents')
  81. e.initEvent('click', true, true)
  82. target.dispatchEvent(e)
  83. expect(spy.calls.count()).toBe(1)
  84. expect(spy).toHaveBeenCalledWith(e)
  85. _.off(target, 'click', spy)
  86. target.dispatchEvent(e)
  87. expect(spy.calls.count()).toBe(1)
  88. document.body.removeChild(target)
  89. })
  90. it('addClass/removeClass', function () {
  91. var el = document.createElement('div')
  92. el.className = 'aa bb cc'
  93. _.removeClass(el, 'bb')
  94. expect(el.className).toBe('aa cc')
  95. _.removeClass(el, 'aa')
  96. expect(el.className).toBe('cc')
  97. _.addClass(el, 'bb')
  98. expect(el.className).toBe('cc bb')
  99. _.addClass(el, 'bb')
  100. expect(el.className).toBe('cc bb')
  101. })
  102. it('addClass/removeClass for SVG/IE9', function () {
  103. var el = document.createElementNS('http://www.w3.org/2000/svg', 'circle')
  104. el.setAttribute('class', 'aa bb cc')
  105. _.removeClass(el, 'bb')
  106. expect(el.getAttribute('class')).toBe('aa cc')
  107. _.removeClass(el, 'aa')
  108. expect(el.getAttribute('class')).toBe('cc')
  109. _.addClass(el, 'bb')
  110. expect(el.getAttribute('class')).toBe('cc bb')
  111. _.addClass(el, 'bb')
  112. expect(el.getAttribute('class')).toBe('cc bb')
  113. })
  114. it('getOuterHTML for SVG', function () {
  115. var el = document.createElementNS('http://www.w3.org/2000/svg', 'circle')
  116. el.setAttribute('class', 'aa bb cc')
  117. var html = _.getOuterHTML(el)
  118. var re = /<circle (xmlns="http:\/\/www\.w3\.org\/2000\/svg"\s)?class="aa bb cc"(\s?\/>|><\/circle>)/
  119. expect(re.test(html)).toBe(true)
  120. })
  121. })