dom_spec.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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('on/off', function () {
  66. // IE requires element to be in document to fire events
  67. document.body.appendChild(target)
  68. var spy = jasmine.createSpy()
  69. _.on(target, 'click', spy)
  70. var e = document.createEvent('HTMLEvents')
  71. e.initEvent('click', true, true)
  72. target.dispatchEvent(e)
  73. expect(spy.calls.count()).toBe(1)
  74. expect(spy).toHaveBeenCalledWith(e)
  75. _.off(target, 'click', spy)
  76. target.dispatchEvent(e)
  77. expect(spy.calls.count()).toBe(1)
  78. document.body.removeChild(target)
  79. })
  80. it('addClass/removeClass', function () {
  81. var el = document.createElement('div')
  82. el.className = 'aa bb cc'
  83. _.removeClass(el, 'bb')
  84. expect(el.className).toBe('aa cc')
  85. _.removeClass(el, 'aa')
  86. expect(el.className).toBe('cc')
  87. _.addClass(el, 'bb')
  88. expect(el.className).toBe('cc bb')
  89. _.addClass(el, 'bb')
  90. expect(el.className).toBe('cc bb')
  91. })
  92. it('addClass/removeClass for SVG/IE9', function () {
  93. var el = document.createElementNS('http://www.w3.org/2000/svg', 'circle')
  94. el.setAttribute('class', 'aa bb cc')
  95. _.removeClass(el, 'bb')
  96. expect(el.getAttribute('class')).toBe('aa cc')
  97. _.removeClass(el, 'aa')
  98. expect(el.getAttribute('class')).toBe('cc')
  99. _.addClass(el, 'bb')
  100. expect(el.getAttribute('class')).toBe('cc bb')
  101. _.addClass(el, 'bb')
  102. expect(el.getAttribute('class')).toBe('cc bb')
  103. })
  104. })
  105. }