dom_spec.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /**
  2. * We are not testing transition-related stuff here,
  3. * those are tested in transition_spec.js.
  4. */
  5. var Vue = require('../../../../src/vue')
  6. var _ = require('../../../../src/util')
  7. if (_.inBrowser) {
  8. describe('DOM API', function () {
  9. var vm, vm2, parent, target, sibling, empty, spy
  10. beforeEach(function () {
  11. spy = jasmine.createSpy('dom')
  12. parent = document.createElement('div')
  13. target = document.createElement('div')
  14. sibling = document.createElement('div')
  15. empty = document.createElement('div')
  16. parent.appendChild(target)
  17. parent.appendChild(sibling)
  18. var el = document.createElement('div')
  19. vm = new Vue({ el: el })
  20. // block instance
  21. var frag = document.createDocumentFragment()
  22. frag.appendChild(document.createElement('p'))
  23. frag.appendChild(document.createElement('span'))
  24. vm2 = new Vue({
  25. el: frag
  26. })
  27. })
  28. describe('$appendTo', function () {
  29. it('normal instance', function () {
  30. vm.$appendTo(parent, spy)
  31. expect(parent.childNodes.length).toBe(3)
  32. expect(parent.lastChild).toBe(vm.$el)
  33. expect(spy.calls.count()).toBe(1)
  34. })
  35. it('block instance', function () {
  36. vm2.$appendTo(parent, spy)
  37. expect(parent.childNodes.length).toBe(6)
  38. expect(parent.childNodes[2]).toBe(vm2.$el)
  39. expect(parent.childNodes[3].tagName).toBe('P')
  40. expect(parent.childNodes[4].tagName).toBe('SPAN')
  41. expect(parent.childNodes[5]).toBe(vm2._blockEnd)
  42. expect(spy.calls.count()).toBe(1)
  43. })
  44. })
  45. describe('$prependTo', function () {
  46. it('normal instance', function () {
  47. vm.$prependTo(parent, spy)
  48. expect(parent.childNodes.length).toBe(3)
  49. expect(parent.firstChild).toBe(vm.$el)
  50. expect(spy.calls.count()).toBe(1)
  51. vm.$prependTo(empty, spy)
  52. expect(empty.childNodes.length).toBe(1)
  53. expect(empty.firstChild).toBe(vm.$el)
  54. expect(spy.calls.count()).toBe(2)
  55. })
  56. it('block instance', function () {
  57. vm2.$prependTo(parent, spy)
  58. expect(parent.childNodes.length).toBe(6)
  59. expect(parent.childNodes[0]).toBe(vm2.$el)
  60. expect(parent.childNodes[1].tagName).toBe('P')
  61. expect(parent.childNodes[2].tagName).toBe('SPAN')
  62. expect(parent.childNodes[3]).toBe(vm2._blockEnd)
  63. expect(spy.calls.count()).toBe(1)
  64. // empty
  65. vm2.$prependTo(empty, spy)
  66. expect(empty.childNodes.length).toBe(4)
  67. expect(empty.childNodes[0]).toBe(vm2.$el)
  68. expect(empty.childNodes[1].tagName).toBe('P')
  69. expect(empty.childNodes[2].tagName).toBe('SPAN')
  70. expect(empty.childNodes[3]).toBe(vm2._blockEnd)
  71. expect(spy.calls.count()).toBe(2)
  72. })
  73. })
  74. describe('$before', function () {
  75. it('normal instance', function () {
  76. vm.$before(sibling, spy)
  77. expect(parent.childNodes.length).toBe(3)
  78. expect(parent.childNodes[1]).toBe(vm.$el)
  79. expect(spy.calls.count()).toBe(1)
  80. })
  81. it('block instance', function () {
  82. vm2.$before(sibling, spy)
  83. expect(parent.childNodes.length).toBe(6)
  84. expect(parent.childNodes[1]).toBe(vm2.$el)
  85. expect(parent.childNodes[2].tagName).toBe('P')
  86. expect(parent.childNodes[3].tagName).toBe('SPAN')
  87. expect(parent.childNodes[4]).toBe(vm2._blockEnd)
  88. expect(spy.calls.count()).toBe(1)
  89. })
  90. })
  91. describe('$after', function () {
  92. it('normal instance', function () {
  93. vm.$after(target, spy)
  94. expect(parent.childNodes.length).toBe(3)
  95. expect(parent.childNodes[1]).toBe(vm.$el)
  96. expect(spy.calls.count()).toBe(1)
  97. })
  98. it('normal instance no next sibling', function () {
  99. vm.$after(sibling, spy)
  100. expect(parent.childNodes.length).toBe(3)
  101. expect(parent.lastChild).toBe(vm.$el)
  102. expect(spy.calls.count()).toBe(1)
  103. })
  104. it('block instance', function () {
  105. vm2.$after(target, spy)
  106. expect(parent.childNodes.length).toBe(6)
  107. expect(parent.childNodes[1]).toBe(vm2.$el)
  108. expect(parent.childNodes[2].tagName).toBe('P')
  109. expect(parent.childNodes[3].tagName).toBe('SPAN')
  110. expect(parent.childNodes[4]).toBe(vm2._blockEnd)
  111. expect(spy.calls.count()).toBe(1)
  112. })
  113. it('block instance no next sibling', function () {
  114. vm2.$after(sibling, spy)
  115. expect(parent.childNodes.length).toBe(6)
  116. expect(parent.childNodes[2]).toBe(vm2.$el)
  117. expect(parent.childNodes[3].tagName).toBe('P')
  118. expect(parent.childNodes[4].tagName).toBe('SPAN')
  119. expect(parent.childNodes[5]).toBe(vm2._blockEnd)
  120. expect(spy.calls.count()).toBe(1)
  121. })
  122. })
  123. describe('$remove', function () {
  124. it('normal instance', function () {
  125. vm.$before(sibling)
  126. expect(parent.childNodes.length).toBe(3)
  127. expect(parent.childNodes[1]).toBe(vm.$el)
  128. vm.$remove(spy)
  129. expect(parent.childNodes.length).toBe(2)
  130. expect(parent.childNodes[0]).toBe(target)
  131. expect(parent.childNodes[1]).toBe(sibling)
  132. expect(spy.calls.count()).toBe(1)
  133. })
  134. it('block instance', function () {
  135. vm2.$before(sibling)
  136. expect(parent.childNodes.length).toBe(6)
  137. expect(parent.childNodes[1]).toBe(vm2.$el)
  138. expect(parent.childNodes[2].tagName).toBe('P')
  139. expect(parent.childNodes[3].tagName).toBe('SPAN')
  140. expect(parent.childNodes[4]).toBe(vm2._blockEnd)
  141. vm2.$remove(spy)
  142. expect(parent.childNodes.length).toBe(2)
  143. expect(parent.childNodes[0]).toBe(target)
  144. expect(parent.childNodes[1]).toBe(sibling)
  145. expect(spy.calls.count()).toBe(1)
  146. })
  147. })
  148. })
  149. }