lifecycle.spec.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. import Vue from 'vue'
  2. describe('Options lifecyce hooks', () => {
  3. let spy
  4. beforeEach(() => {
  5. spy = jasmine.createSpy('hook')
  6. })
  7. describe('beforeCreate', () => {
  8. it('should allow modifying options', () => {
  9. const vm = new Vue({
  10. data: {
  11. a: 1
  12. },
  13. beforeCreate () {
  14. spy()
  15. expect(this.a).toBeUndefined()
  16. this.$options.computed = {
  17. b () {
  18. return this.a + 1
  19. }
  20. }
  21. }
  22. })
  23. expect(spy).toHaveBeenCalled()
  24. expect(vm.b).toBe(2)
  25. })
  26. })
  27. describe('created', () => {
  28. it('should have completed observation', () => {
  29. new Vue({
  30. data: {
  31. a: 1
  32. },
  33. created () {
  34. expect(this.a).toBe(1)
  35. spy()
  36. }
  37. })
  38. expect(spy).toHaveBeenCalled()
  39. })
  40. })
  41. describe('beforeMount', () => {
  42. it('should not have mounted', () => {
  43. const vm = new Vue({
  44. render () {},
  45. beforeMount () {
  46. spy()
  47. expect(this._isMounted).toBe(false)
  48. expect(this.$el).toBeUndefined() // due to empty mount
  49. expect(this._vnode).toBeNull()
  50. expect(this._watcher).toBeNull()
  51. }
  52. })
  53. expect(spy).not.toHaveBeenCalled()
  54. vm.$mount()
  55. expect(spy).toHaveBeenCalled()
  56. })
  57. })
  58. describe('mounted', () => {
  59. it('should have mounted', () => {
  60. const vm = new Vue({
  61. template: '<div></div>',
  62. mounted () {
  63. spy()
  64. expect(this._isMounted).toBe(true)
  65. expect(this.$el.tagName).toBe('DIV')
  66. expect(this._vnode.tag).toBe('div')
  67. }
  68. })
  69. expect(spy).not.toHaveBeenCalled()
  70. vm.$mount()
  71. expect(spy).toHaveBeenCalled()
  72. })
  73. // #3898
  74. it('should call for manually mounted instance with parent', () => {
  75. const parent = new Vue()
  76. expect(spy).not.toHaveBeenCalled()
  77. new Vue({
  78. parent,
  79. template: '<div></div>',
  80. mounted () {
  81. spy()
  82. }
  83. }).$mount()
  84. expect(spy).toHaveBeenCalled()
  85. })
  86. it('should mount child parent in correct order', () => {
  87. const calls = []
  88. new Vue({
  89. template: '<div><test></test></div>',
  90. mounted () {
  91. calls.push('parent')
  92. },
  93. components: {
  94. test: {
  95. template: '<nested></nested>',
  96. mounted () {
  97. expect(this.$el.parentNode).toBeTruthy()
  98. calls.push('child')
  99. },
  100. components: {
  101. nested: {
  102. template: '<div></div>',
  103. mounted () {
  104. expect(this.$el.parentNode).toBeTruthy()
  105. calls.push('nested')
  106. }
  107. }
  108. }
  109. }
  110. }
  111. }).$mount()
  112. expect(calls).toEqual(['nested', 'child', 'parent'])
  113. })
  114. })
  115. describe('beforeUpdate', () => {
  116. it('should be called before update', done => {
  117. const vm = new Vue({
  118. template: '<div>{{ msg }}</div>',
  119. data: { msg: 'foo' },
  120. beforeUpdate () {
  121. spy()
  122. expect(this.$el.textContent).toBe('foo')
  123. }
  124. }).$mount()
  125. expect(spy).not.toHaveBeenCalled()
  126. vm.msg = 'bar'
  127. expect(spy).not.toHaveBeenCalled() // should be async
  128. waitForUpdate(() => {
  129. expect(spy).toHaveBeenCalled()
  130. }).then(done)
  131. })
  132. })
  133. describe('updated', () => {
  134. it('should be called after update', done => {
  135. const vm = new Vue({
  136. template: '<div>{{ msg }}</div>',
  137. data: { msg: 'foo' },
  138. updated () {
  139. spy()
  140. expect(this.$el.textContent).toBe('bar')
  141. }
  142. }).$mount()
  143. expect(spy).not.toHaveBeenCalled()
  144. vm.msg = 'bar'
  145. expect(spy).not.toHaveBeenCalled() // should be async
  146. waitForUpdate(() => {
  147. expect(spy).toHaveBeenCalled()
  148. }).then(done)
  149. })
  150. it('should be called after children are updated', done => {
  151. const calls = []
  152. const vm = new Vue({
  153. template: '<div><test ref="child">{{ msg }}</test></div>',
  154. data: { msg: 'foo' },
  155. components: {
  156. test: {
  157. template: `<div><slot></slot></div>`,
  158. updated () {
  159. expect(this.$el.textContent).toBe('bar')
  160. calls.push('child')
  161. }
  162. }
  163. },
  164. updated () {
  165. expect(this.$el.textContent).toBe('bar')
  166. calls.push('parent')
  167. }
  168. }).$mount()
  169. expect(calls).toEqual([])
  170. vm.msg = 'bar'
  171. expect(calls).toEqual([])
  172. waitForUpdate(() => {
  173. expect(calls).toEqual(['child', 'parent'])
  174. }).then(done)
  175. })
  176. })
  177. describe('beforeDestroy', () => {
  178. it('should be called before destroy', () => {
  179. const vm = new Vue({
  180. render () {},
  181. beforeDestroy () {
  182. spy()
  183. expect(this._isBeingDestroyed).toBe(false)
  184. expect(this._isDestroyed).toBe(false)
  185. }
  186. }).$mount()
  187. expect(spy).not.toHaveBeenCalled()
  188. vm.$destroy()
  189. vm.$destroy()
  190. expect(spy).toHaveBeenCalled()
  191. expect(spy.calls.count()).toBe(1)
  192. })
  193. })
  194. describe('destroyed', () => {
  195. it('should be called after destroy', () => {
  196. const vm = new Vue({
  197. render () {},
  198. destroyed () {
  199. spy()
  200. expect(this._isBeingDestroyed).toBe(true)
  201. expect(this._isDestroyed).toBe(true)
  202. }
  203. }).$mount()
  204. expect(spy).not.toHaveBeenCalled()
  205. vm.$destroy()
  206. vm.$destroy()
  207. expect(spy).toHaveBeenCalled()
  208. expect(spy.calls.count()).toBe(1)
  209. })
  210. })
  211. it('should emit hook events', () => {
  212. const created = jasmine.createSpy()
  213. const mounted = jasmine.createSpy()
  214. const destroyed = jasmine.createSpy()
  215. const vm = new Vue({
  216. render () {},
  217. beforeCreate () {
  218. this.$on('hook:created', created)
  219. this.$on('hook:mounted', mounted)
  220. this.$on('hook:destroyed', destroyed)
  221. }
  222. })
  223. expect(created).toHaveBeenCalled()
  224. expect(mounted).not.toHaveBeenCalled()
  225. expect(destroyed).not.toHaveBeenCalled()
  226. vm.$mount()
  227. expect(mounted).toHaveBeenCalled()
  228. expect(destroyed).not.toHaveBeenCalled()
  229. vm.$destroy()
  230. expect(destroyed).toHaveBeenCalled()
  231. })
  232. })