lifecycle.spec.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. import Vue from 'vue'
  2. describe('Options lifecycle 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. it('should be called before render and allow mutating state', done => {
  133. const vm = new Vue({
  134. template: '<div>{{ msg }}</div>',
  135. data: { msg: 'foo' },
  136. beforeUpdate () {
  137. this.msg += '!'
  138. }
  139. }).$mount()
  140. expect(vm.$el.textContent).toBe('foo')
  141. vm.msg = 'bar'
  142. waitForUpdate(() => {
  143. expect(vm.$el.textContent).toBe('bar!')
  144. }).then(done)
  145. })
  146. })
  147. describe('updated', () => {
  148. it('should be called after update', done => {
  149. const vm = new Vue({
  150. template: '<div>{{ msg }}</div>',
  151. data: { msg: 'foo' },
  152. updated () {
  153. spy()
  154. expect(this.$el.textContent).toBe('bar')
  155. }
  156. }).$mount()
  157. expect(spy).not.toHaveBeenCalled()
  158. vm.msg = 'bar'
  159. expect(spy).not.toHaveBeenCalled() // should be async
  160. waitForUpdate(() => {
  161. expect(spy).toHaveBeenCalled()
  162. }).then(done)
  163. })
  164. it('should be called after children are updated', done => {
  165. const calls = []
  166. const vm = new Vue({
  167. template: '<div><test ref="child">{{ msg }}</test></div>',
  168. data: { msg: 'foo' },
  169. components: {
  170. test: {
  171. template: `<div><slot></slot></div>`,
  172. updated () {
  173. expect(this.$el.textContent).toBe('bar')
  174. calls.push('child')
  175. }
  176. }
  177. },
  178. updated () {
  179. expect(this.$el.textContent).toBe('bar')
  180. calls.push('parent')
  181. }
  182. }).$mount()
  183. expect(calls).toEqual([])
  184. vm.msg = 'bar'
  185. expect(calls).toEqual([])
  186. waitForUpdate(() => {
  187. expect(calls).toEqual(['child', 'parent'])
  188. }).then(done)
  189. })
  190. })
  191. describe('beforeDestroy', () => {
  192. it('should be called before destroy', () => {
  193. const vm = new Vue({
  194. render () {},
  195. beforeDestroy () {
  196. spy()
  197. expect(this._isBeingDestroyed).toBe(false)
  198. expect(this._isDestroyed).toBe(false)
  199. }
  200. }).$mount()
  201. expect(spy).not.toHaveBeenCalled()
  202. vm.$destroy()
  203. vm.$destroy()
  204. expect(spy).toHaveBeenCalled()
  205. expect(spy.calls.count()).toBe(1)
  206. })
  207. })
  208. describe('destroyed', () => {
  209. it('should be called after destroy', () => {
  210. const vm = new Vue({
  211. render () {},
  212. destroyed () {
  213. spy()
  214. expect(this._isBeingDestroyed).toBe(true)
  215. expect(this._isDestroyed).toBe(true)
  216. }
  217. }).$mount()
  218. expect(spy).not.toHaveBeenCalled()
  219. vm.$destroy()
  220. vm.$destroy()
  221. expect(spy).toHaveBeenCalled()
  222. expect(spy.calls.count()).toBe(1)
  223. })
  224. })
  225. it('should emit hook events', () => {
  226. const created = jasmine.createSpy()
  227. const mounted = jasmine.createSpy()
  228. const destroyed = jasmine.createSpy()
  229. const vm = new Vue({
  230. render () {},
  231. beforeCreate () {
  232. this.$on('hook:created', created)
  233. this.$on('hook:mounted', mounted)
  234. this.$on('hook:destroyed', destroyed)
  235. }
  236. })
  237. expect(created).toHaveBeenCalled()
  238. expect(mounted).not.toHaveBeenCalled()
  239. expect(destroyed).not.toHaveBeenCalled()
  240. vm.$mount()
  241. expect(mounted).toHaveBeenCalled()
  242. expect(destroyed).not.toHaveBeenCalled()
  243. vm.$destroy()
  244. expect(destroyed).toHaveBeenCalled()
  245. })
  246. })