dom_spec.js 3.7 KB

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