lifecycle.spec.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. it('should mount child parent in correct order', () => {
  73. const calls = []
  74. new Vue({
  75. template: '<test></test>',
  76. mounted () {
  77. calls.push('parent')
  78. },
  79. components: {
  80. test: {
  81. template: '<div></div>',
  82. mounted () {
  83. calls.push('child')
  84. }
  85. }
  86. }
  87. }).$mount()
  88. expect(calls).toEqual(['child', 'parent'])
  89. })
  90. })
  91. describe('beforeUpdate', () => {
  92. it('should be called before update', done => {
  93. const vm = new Vue({
  94. template: '<div>{{ msg }}</div>',
  95. data: { msg: 'foo' },
  96. beforeUpdate () {
  97. spy()
  98. expect(this.$el.textContent).toBe('foo')
  99. }
  100. }).$mount()
  101. expect(spy).not.toHaveBeenCalled()
  102. vm.msg = 'bar'
  103. expect(spy).not.toHaveBeenCalled() // should be async
  104. waitForUpdate(() => {
  105. expect(spy).toHaveBeenCalled()
  106. }).then(done)
  107. })
  108. })
  109. describe('updated', () => {
  110. it('should be called after update', done => {
  111. const vm = new Vue({
  112. template: '<div>{{ msg }}</div>',
  113. data: { msg: 'foo' },
  114. updated () {
  115. spy()
  116. expect(this.$el.textContent).toBe('bar')
  117. }
  118. }).$mount()
  119. expect(spy).not.toHaveBeenCalled()
  120. vm.msg = 'bar'
  121. expect(spy).not.toHaveBeenCalled() // should be async
  122. waitForUpdate(() => {
  123. expect(spy).toHaveBeenCalled()
  124. }).then(done)
  125. })
  126. })
  127. describe('beforeDestroy', () => {
  128. it('should be called before destroy', () => {
  129. const vm = new Vue({
  130. beforeDestroy () {
  131. spy()
  132. expect(this._isBeingDestroyed).toBe(false)
  133. expect(this._isDestroyed).toBe(false)
  134. }
  135. }).$mount()
  136. expect(spy).not.toHaveBeenCalled()
  137. vm.$destroy()
  138. vm.$destroy()
  139. expect(spy).toHaveBeenCalled()
  140. expect(spy.calls.count()).toBe(1)
  141. })
  142. })
  143. describe('destroyed', () => {
  144. it('should be called after destroy', () => {
  145. const vm = new Vue({
  146. destroyed () {
  147. spy()
  148. expect(this._isBeingDestroyed).toBe(true)
  149. expect(this._isDestroyed).toBe(true)
  150. }
  151. }).$mount()
  152. expect(spy).not.toHaveBeenCalled()
  153. vm.$destroy()
  154. vm.$destroy()
  155. expect(spy).toHaveBeenCalled()
  156. expect(spy.calls.count()).toBe(1)
  157. })
  158. })
  159. })