dom_spec.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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('attr', function () {
  15. target.setAttribute('v-test', 'ok')
  16. var val = _.attr(target, 'test')
  17. expect(val).toBe('ok')
  18. expect(target.hasAttribute('v-test')).toBe(false)
  19. })
  20. it('before', function () {
  21. _.before(target, child)
  22. expect(target.parentNode).toBe(parent)
  23. expect(target.nextSibling).toBe(child)
  24. })
  25. it('after', function () {
  26. _.after(target, child)
  27. expect(target.parentNode).toBe(parent)
  28. expect(child.nextSibling).toBe(target)
  29. })
  30. it('after with sibling', function () {
  31. var sibling = div()
  32. parent.appendChild(sibling)
  33. _.after(target, child)
  34. expect(target.parentNode).toBe(parent)
  35. expect(child.nextSibling).toBe(target)
  36. })
  37. it('remove', function () {
  38. _.remove(child)
  39. expect(child.parentNode).toBeNull()
  40. expect(parent.childNodes.length).toBe(0)
  41. })
  42. it('prepend', function () {
  43. _.prepend(target, parent)
  44. expect(target.parentNode).toBe(parent)
  45. expect(parent.firstChild).toBe(target)
  46. })
  47. it('prepend to empty node', function () {
  48. parent.removeChild(child)
  49. _.prepend(target, parent)
  50. expect(target.parentNode).toBe(parent)
  51. expect(parent.firstChild).toBe(target)
  52. })
  53. it('replace', function () {
  54. _.replace(child, target)
  55. expect(parent.childNodes.length).toBe(1)
  56. expect(parent.firstChild).toBe(target)
  57. })
  58. it('copyAttributes', function () {
  59. parent.setAttribute('test1', 1)
  60. parent.setAttribute('test2', 2)
  61. _.copyAttributes(parent, target)
  62. expect(target.attributes.length).toBe(2)
  63. expect(target.getAttribute('test1')).toBe('1')
  64. expect(target.getAttribute('test2')).toBe('2')
  65. })
  66. })
  67. }