lifecycle.spec.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. beforeMount () {
  45. spy()
  46. expect(this._isMounted).toBe(false)
  47. expect(this.$el).toBeUndefined() // due to empty mount
  48. expect(this._vnode).toBeNull()
  49. expect(this._watcher).toBeNull()
  50. }
  51. })
  52. expect(spy).not.toHaveBeenCalled()
  53. vm.$mount()
  54. expect(spy).toHaveBeenCalled()
  55. })
  56. })
  57. describe('mounted', () => {
  58. it('should have mounted', () => {
  59. const vm = new Vue({
  60. template: '<div></div>',
  61. mounted () {
  62. spy()
  63. expect(this._isMounted).toBe(true)
  64. expect(this.$el.tagName).toBe('DIV')
  65. expect(this._vnode.tag).toBe('div')
  66. }
  67. })
  68. expect(spy).not.toHaveBeenCalled()
  69. vm.$mount()
  70. expect(spy).toHaveBeenCalled()
  71. })
  72. // #3898
  73. it('should call for manually mounted instance with parent', () => {
  74. const parent = new Vue()
  75. expect(spy).not.toHaveBeenCalled()
  76. new Vue({
  77. parent,
  78. template: '<div></div>',
  79. mounted () {
  80. spy()
  81. }
  82. }).$mount()
  83. expect(spy).toHaveBeenCalled()
  84. })
  85. it('should mount child parent in correct order', () => {
  86. const calls = []
  87. new Vue({
  88. template: '<div><test></test><div>',
  89. mounted () {
  90. calls.push('parent')
  91. },
  92. components: {
  93. test: {
  94. template: '<nested></nested>',
  95. mounted () {
  96. expect(this.$el.parentNode).toBeTruthy()
  97. calls.push('child')
  98. },
  99. components: {
  100. nested: {
  101. template: '<div></div>',
  102. mounted () {
  103. expect(this.$el.parentNode).toBeTruthy()
  104. calls.push('nested')
  105. }
  106. }
  107. }
  108. }
  109. }
  110. }).$mount()
  111. expect(calls).toEqual(['nested', 'child', 'parent'])
  112. })
  113. })
  114. describe('beforeUpdate', () => {
  115. it('should be called before update', done => {
  116. const vm = new Vue({
  117. template: '<div>{{ msg }}</div>',
  118. data: { msg: 'foo' },
  119. beforeUpdate () {
  120. spy()
  121. expect(this.$el.textContent).toBe('foo')
  122. }
  123. }).$mount()
  124. expect(spy).not.toHaveBeenCalled()
  125. vm.msg = 'bar'
  126. expect(spy).not.toHaveBeenCalled() // should be async
  127. waitForUpdate(() => {
  128. expect(spy).toHaveBeenCalled()
  129. }).then(done)
  130. })
  131. })
  132. describe('updated', () => {
  133. it('should be called after update', done => {
  134. const vm = new Vue({
  135. template: '<div>{{ msg }}</div>',
  136. data: { msg: 'foo' },
  137. updated () {
  138. spy()
  139. expect(this.$el.textContent).toBe('bar')
  140. }
  141. }).$mount()
  142. expect(spy).not.toHaveBeenCalled()
  143. vm.msg = 'bar'
  144. expect(spy).not.toHaveBeenCalled() // should be async
  145. waitForUpdate(() => {
  146. expect(spy).toHaveBeenCalled()
  147. }).then(done)
  148. })
  149. })
  150. describe('beforeDestroy', () => {
  151. it('should be called before destroy', () => {
  152. const vm = new Vue({
  153. beforeDestroy () {
  154. spy()
  155. expect(this._isBeingDestroyed).toBe(false)
  156. expect(this._isDestroyed).toBe(false)
  157. }
  158. }).$mount()
  159. expect(spy).not.toHaveBeenCalled()
  160. vm.$destroy()
  161. vm.$destroy()
  162. expect(spy).toHaveBeenCalled()
  163. expect(spy.calls.count()).toBe(1)
  164. })
  165. })
  166. describe('destroyed', () => {
  167. it('should be called after destroy', () => {
  168. const vm = new Vue({
  169. destroyed () {
  170. spy()
  171. expect(this._isBeingDestroyed).toBe(true)
  172. expect(this._isDestroyed).toBe(true)
  173. }
  174. }).$mount()
  175. expect(spy).not.toHaveBeenCalled()
  176. vm.$destroy()
  177. vm.$destroy()
  178. expect(spy).toHaveBeenCalled()
  179. expect(spy.calls.count()).toBe(1)
  180. })
  181. })
  182. })