events_spec.js 5.7 KB

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