lifecycle.spec.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. // #8076
  191. it('should not be called after destroy', done => {
  192. const updated = jasmine.createSpy('updated')
  193. const destroyed = jasmine.createSpy('destroyed')
  194. Vue.component('todo', {
  195. template: '<div>{{todo.done}}</div>',
  196. props: ['todo'],
  197. destroyed,
  198. updated
  199. })
  200. const vm = new Vue({
  201. template: `
  202. <div>
  203. <todo v-for="t in pendingTodos" :todo="t" :key="t.id"></todo>
  204. </div>
  205. `,
  206. data () {
  207. return {
  208. todos: [{ id: 1, done: false }]
  209. }
  210. },
  211. computed: {
  212. pendingTodos () {
  213. return this.todos.filter(t => !t.done)
  214. }
  215. }
  216. }).$mount()
  217. vm.todos[0].done = true
  218. waitForUpdate(() => {
  219. expect(destroyed).toHaveBeenCalled()
  220. expect(updated).not.toHaveBeenCalled()
  221. }).then(done)
  222. })
  223. })
  224. describe('beforeDestroy', () => {
  225. it('should be called before destroy', () => {
  226. const vm = new Vue({
  227. render () {},
  228. beforeDestroy () {
  229. spy()
  230. expect(this._isBeingDestroyed).toBe(false)
  231. expect(this._isDestroyed).toBe(false)
  232. }
  233. }).$mount()
  234. expect(spy).not.toHaveBeenCalled()
  235. vm.$destroy()
  236. vm.$destroy()
  237. expect(spy).toHaveBeenCalled()
  238. expect(spy.calls.count()).toBe(1)
  239. })
  240. })
  241. describe('destroyed', () => {
  242. it('should be called after destroy', () => {
  243. const vm = new Vue({
  244. render () {},
  245. destroyed () {
  246. spy()
  247. expect(this._isBeingDestroyed).toBe(true)
  248. expect(this._isDestroyed).toBe(true)
  249. }
  250. }).$mount()
  251. expect(spy).not.toHaveBeenCalled()
  252. vm.$destroy()
  253. vm.$destroy()
  254. expect(spy).toHaveBeenCalled()
  255. expect(spy.calls.count()).toBe(1)
  256. })
  257. })
  258. it('should emit hook events', () => {
  259. const created = jasmine.createSpy()
  260. const mounted = jasmine.createSpy()
  261. const destroyed = jasmine.createSpy()
  262. const vm = new Vue({
  263. render () {},
  264. beforeCreate () {
  265. this.$on('hook:created', created)
  266. this.$on('hook:mounted', mounted)
  267. this.$on('hook:destroyed', destroyed)
  268. }
  269. })
  270. expect(created).toHaveBeenCalled()
  271. expect(mounted).not.toHaveBeenCalled()
  272. expect(destroyed).not.toHaveBeenCalled()
  273. vm.$mount()
  274. expect(mounted).toHaveBeenCalled()
  275. expect(destroyed).not.toHaveBeenCalled()
  276. vm.$destroy()
  277. expect(destroyed).toHaveBeenCalled()
  278. })
  279. })