dom_spec.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /**
  2. * We are not testing transition-related stuff here,
  3. * those are tested in transition_spec.js.
  4. */
  5. var Vue = require('src')
  6. var _ = require('src/util')
  7. describe('DOM API', function () {
  8. var vm, vm2, parent, target, sibling, empty, spy
  9. beforeEach(function () {
  10. spy = jasmine.createSpy('dom')
  11. parent = document.createElement('div')
  12. target = document.createElement('div')
  13. sibling = document.createElement('div')
  14. empty = document.createElement('div')
  15. parent.appendChild(target)
  16. parent.appendChild(sibling)
  17. var el = document.createElement('div')
  18. vm = new Vue({ el: el })
  19. // fragment instance
  20. var frag = document.createDocumentFragment()
  21. frag.appendChild(document.createElement('p'))
  22. frag.appendChild(document.createElement('span'))
  23. vm2 = new Vue({
  24. el: frag
  25. })
  26. })
  27. describe('$appendTo', function () {
  28. it('normal instance', function () {
  29. vm.$appendTo(parent, spy)
  30. expect(parent.childNodes.length).toBe(3)
  31. expect(parent.lastChild).toBe(vm.$el)
  32. expect(spy.calls.count()).toBe(1)
  33. })
  34. it('fragment instance', function () {
  35. vm2.$appendTo(parent, spy)
  36. expect(parent.childNodes.length).toBe(6)
  37. expect(parent.childNodes[2]).toBe(vm2._fragmentStart)
  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._fragmentEnd)
  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('fragment instance', function () {
  57. vm2.$prependTo(parent, spy)
  58. expect(parent.childNodes.length).toBe(6)
  59. expect(parent.childNodes[0]).toBe(vm2._fragmentStart)
  60. expect(parent.childNodes[0]).toBe(vm2.$el)
  61. expect(parent.childNodes[1].tagName).toBe('P')
  62. expect(parent.childNodes[2].tagName).toBe('SPAN')
  63. expect(parent.childNodes[3]).toBe(vm2._fragmentEnd)
  64. expect(spy.calls.count()).toBe(1)
  65. // empty
  66. vm2.$prependTo(empty, spy)
  67. expect(empty.childNodes.length).toBe(4)
  68. expect(empty.childNodes[0]).toBe(vm2._fragmentStart)
  69. expect(empty.childNodes[0]).toBe(vm2.$el)
  70. expect(empty.childNodes[1].tagName).toBe('P')
  71. expect(empty.childNodes[2].tagName).toBe('SPAN')
  72. expect(empty.childNodes[3]).toBe(vm2._fragmentEnd)
  73. expect(spy.calls.count()).toBe(2)
  74. })
  75. })
  76. describe('$before', function () {
  77. it('normal instance', function () {
  78. vm.$before(sibling, spy)
  79. expect(parent.childNodes.length).toBe(3)
  80. expect(parent.childNodes[1]).toBe(vm.$el)
  81. expect(spy.calls.count()).toBe(1)
  82. })
  83. it('fragment instance', function () {
  84. vm2.$before(sibling, spy)
  85. expect(parent.childNodes.length).toBe(6)
  86. expect(parent.childNodes[1]).toBe(vm2._fragmentStart)
  87. expect(parent.childNodes[1]).toBe(vm2.$el)
  88. expect(parent.childNodes[2].tagName).toBe('P')
  89. expect(parent.childNodes[3].tagName).toBe('SPAN')
  90. expect(parent.childNodes[4]).toBe(vm2._fragmentEnd)
  91. expect(spy.calls.count()).toBe(1)
  92. })
  93. })
  94. describe('$after', function () {
  95. it('normal instance', function () {
  96. vm.$after(target, spy)
  97. expect(parent.childNodes.length).toBe(3)
  98. expect(parent.childNodes[1]).toBe(vm.$el)
  99. expect(spy.calls.count()).toBe(1)
  100. })
  101. it('normal instance no next sibling', function () {
  102. vm.$after(sibling, spy)
  103. expect(parent.childNodes.length).toBe(3)
  104. expect(parent.lastChild).toBe(vm.$el)
  105. expect(spy.calls.count()).toBe(1)
  106. })
  107. it('fragment instance', function () {
  108. vm2.$after(target, spy)
  109. expect(parent.childNodes.length).toBe(6)
  110. expect(parent.childNodes[1]).toBe(vm2._fragmentStart)
  111. expect(parent.childNodes[1]).toBe(vm2.$el)
  112. expect(parent.childNodes[2].tagName).toBe('P')
  113. expect(parent.childNodes[3].tagName).toBe('SPAN')
  114. expect(parent.childNodes[4]).toBe(vm2._fragmentEnd)
  115. expect(spy.calls.count()).toBe(1)
  116. })
  117. it('fragment instance no next sibling', function () {
  118. vm2.$after(sibling, spy)
  119. expect(parent.childNodes.length).toBe(6)
  120. expect(parent.childNodes[2]).toBe(vm2._fragmentStart)
  121. expect(parent.childNodes[2]).toBe(vm2.$el)
  122. expect(parent.childNodes[3].tagName).toBe('P')
  123. expect(parent.childNodes[4].tagName).toBe('SPAN')
  124. expect(parent.childNodes[5]).toBe(vm2._fragmentEnd)
  125. expect(spy.calls.count()).toBe(1)
  126. })
  127. })
  128. describe('$remove', function () {
  129. it('normal instance', function () {
  130. vm.$before(sibling)
  131. expect(parent.childNodes.length).toBe(3)
  132. expect(parent.childNodes[1]).toBe(vm.$el)
  133. vm.$remove(spy)
  134. expect(parent.childNodes.length).toBe(2)
  135. expect(parent.childNodes[0]).toBe(target)
  136. expect(parent.childNodes[1]).toBe(sibling)
  137. expect(spy.calls.count()).toBe(1)
  138. })
  139. it('fragment instance', function () {
  140. vm2.$before(sibling)
  141. expect(parent.childNodes.length).toBe(6)
  142. expect(parent.childNodes[1]).toBe(vm2._fragmentStart)
  143. expect(parent.childNodes[1]).toBe(vm2.$el)
  144. expect(parent.childNodes[2].tagName).toBe('P')
  145. expect(parent.childNodes[3].tagName).toBe('SPAN')
  146. expect(parent.childNodes[4]).toBe(vm2._fragmentEnd)
  147. vm2.$remove(spy)
  148. expect(parent.childNodes.length).toBe(2)
  149. expect(parent.childNodes[0]).toBe(target)
  150. expect(parent.childNodes[1]).toBe(sibling)
  151. expect(spy.calls.count()).toBe(1)
  152. })
  153. it('detached', function () {
  154. vm.$remove(spy)
  155. expect(spy.calls.count()).toBe(1)
  156. })
  157. })
  158. describe('$nextTick', function () {
  159. it('should work', function (done) {
  160. var context
  161. var called = false
  162. vm.$nextTick(function () {
  163. called = true
  164. context = this
  165. })
  166. expect(called).toBe(false)
  167. _.nextTick(function () {
  168. expect(called).toBe(true)
  169. expect(context).toBe(vm)
  170. done()
  171. })
  172. })
  173. })
  174. })