lifecycle.spec.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. })
  151. describe('beforeDestroy', () => {
  152. it('should be called before destroy', () => {
  153. const vm = new Vue({
  154. render () {},
  155. beforeDestroy () {
  156. spy()
  157. expect(this._isBeingDestroyed).toBe(false)
  158. expect(this._isDestroyed).toBe(false)
  159. }
  160. }).$mount()
  161. expect(spy).not.toHaveBeenCalled()
  162. vm.$destroy()
  163. vm.$destroy()
  164. expect(spy).toHaveBeenCalled()
  165. expect(spy.calls.count()).toBe(1)
  166. })
  167. })
  168. describe('destroyed', () => {
  169. it('should be called after destroy', () => {
  170. const vm = new Vue({
  171. render () {},
  172. destroyed () {
  173. spy()
  174. expect(this._isBeingDestroyed).toBe(true)
  175. expect(this._isDestroyed).toBe(true)
  176. }
  177. }).$mount()
  178. expect(spy).not.toHaveBeenCalled()
  179. vm.$destroy()
  180. vm.$destroy()
  181. expect(spy).toHaveBeenCalled()
  182. expect(spy.calls.count()).toBe(1)
  183. })
  184. })
  185. })