events_spec.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. var Vue = require('../../../../src/vue')
  2. describe('Instance Events', function () {
  3. var spy, spy2
  4. beforeEach(function () {
  5. spy = jasmine.createSpy()
  6. spy2 = jasmine.createSpy()
  7. })
  8. describe('events', function () {
  9. it('normal events', function () {
  10. var vm = new Vue({
  11. events: {
  12. test: spy,
  13. test2: [spy, spy]
  14. }
  15. })
  16. vm.$emit('test', 123)
  17. expect(spy).toHaveBeenCalledWith(123)
  18. vm.$emit('test2')
  19. expect(spy.calls.count()).toBe(3)
  20. })
  21. it('hook events', function () {
  22. var vm = new Vue({
  23. events: {
  24. 'hook:created': spy
  25. }
  26. })
  27. expect(spy).toHaveBeenCalled()
  28. })
  29. it('method name strings', function () {
  30. var vm = new Vue({
  31. events: {
  32. 'test': 'doSomething'
  33. },
  34. methods: {
  35. doSomething: spy
  36. }
  37. })
  38. vm.$emit('test', 123)
  39. expect(spy).toHaveBeenCalledWith(123)
  40. })
  41. })
  42. describe('hooks', function () {
  43. it('created', function () {
  44. var ctx
  45. var vm = new Vue({
  46. created: function () {
  47. // can't assert this === vm here
  48. // because the constructor hasn't returned yet
  49. ctx = this
  50. // should have observed data
  51. expect(this._data.__ob__).toBeTruthy()
  52. spy()
  53. }
  54. })
  55. expect(ctx).toBe(vm)
  56. expect(spy).toHaveBeenCalled()
  57. })
  58. it('beforeDestroy', function () {
  59. var vm = new Vue({
  60. beforeDestroy: function () {
  61. expect(this).toBe(vm)
  62. expect(this._isDestroyed).toBe(false)
  63. spy()
  64. }
  65. })
  66. vm.$destroy()
  67. expect(spy).toHaveBeenCalled()
  68. })
  69. it('destroyed', function () {
  70. var vm = new Vue({
  71. destroyed: function () {
  72. expect(this).toBe(vm)
  73. expect(this._isDestroyed).toBe(true)
  74. expect(this._data).toBeNull()
  75. spy()
  76. }
  77. })
  78. vm.$destroy()
  79. expect(spy).toHaveBeenCalled()
  80. })
  81. if (Vue.util.inBrowser) {
  82. it('beforeCompile', function () {
  83. var vm = new Vue({
  84. template: '{{a}}',
  85. data: { a: 1 },
  86. beforeCompile: function () {
  87. expect(this).toBe(vm)
  88. expect(this.$el).toBe(el)
  89. expect(this.$el.textContent).toBe('{{a}}')
  90. spy()
  91. }
  92. })
  93. var el = document.createElement('div')
  94. vm.$mount(el)
  95. expect(spy).toHaveBeenCalled()
  96. })
  97. it('compiled', function () {
  98. var vm = new Vue({
  99. template: '{{a}}',
  100. data: { a: 1 },
  101. compiled: function () {
  102. expect(this.$el).toBe(el)
  103. expect(this.$el.textContent).toBe('1')
  104. spy()
  105. }
  106. })
  107. var el = document.createElement('div')
  108. vm.$mount(el)
  109. expect(spy).toHaveBeenCalled()
  110. })
  111. it('ready', function () {
  112. var vm = new Vue({
  113. ready: spy
  114. })
  115. expect(spy).not.toHaveBeenCalled()
  116. var el = document.createElement('div')
  117. vm.$mount(el)
  118. expect(spy).not.toHaveBeenCalled()
  119. vm.$appendTo(document.body)
  120. expect(spy).toHaveBeenCalled()
  121. vm.$remove()
  122. // try mounting on something already in dom
  123. el = document.createElement('div')
  124. document.body.appendChild(el)
  125. vm = new Vue({
  126. el: el,
  127. ready: spy2
  128. })
  129. expect(spy2).toHaveBeenCalled()
  130. vm.$remove()
  131. })
  132. describe('attached/detached', function () {
  133. it('in DOM', function () {
  134. var el = document.createElement('div')
  135. var childEl = document.createElement('div')
  136. el.appendChild(childEl)
  137. document.body.appendChild(el)
  138. var parentVm = new Vue({
  139. el: el,
  140. attached: spy,
  141. detached: spy2
  142. })
  143. var childVm = parentVm.$addChild({
  144. el: childEl,
  145. attached: spy,
  146. detached: spy2
  147. })
  148. expect(spy.calls.count()).toBe(2)
  149. parentVm.$remove()
  150. expect(spy2.calls.count()).toBe(2)
  151. // child should be already detached
  152. // so the hook should not fire again
  153. childVm.$remove()
  154. expect(spy2.calls.count()).toBe(2)
  155. })
  156. it('create then attach', function () {
  157. var el = document.createElement('div')
  158. var childEl = document.createElement('div')
  159. el.appendChild(childEl)
  160. var parentVm = new Vue({
  161. el: el,
  162. attached: spy,
  163. detached: spy2
  164. })
  165. var childVm = parentVm.$addChild({
  166. el: childEl,
  167. attached: spy,
  168. detached: spy2
  169. })
  170. parentVm.$appendTo(document.body)
  171. expect(spy.calls.count()).toBe(2)
  172. // detach child first
  173. childVm.$remove()
  174. expect(spy2.calls.count()).toBe(1)
  175. // should only fire parent detach
  176. parentVm.$remove()
  177. expect(spy2.calls.count()).toBe(2)
  178. })
  179. it('should not fire on detached child', function () {
  180. var el = document.createElement('div')
  181. var childEl = document.createElement('div')
  182. var parentVm = new Vue({
  183. el: el,
  184. attached: spy
  185. })
  186. var childVm = parentVm.$addChild({
  187. el: childEl,
  188. attached: spy
  189. })
  190. parentVm.$appendTo(document.body)
  191. expect(spy.calls.count()).toBe(1)
  192. childVm.$appendTo(el)
  193. expect(spy.calls.count()).toBe(2)
  194. })
  195. })
  196. }
  197. })
  198. })