lifecycle.spec.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import Vue from 'vue'
  2. describe('Options lifecyce hooks', () => {
  3. let spy
  4. beforeEach(() => {
  5. spy = jasmine.createSpy('hook')
  6. })
  7. describe('init', () => {
  8. it('should allow modifying options', () => {
  9. const vm = new Vue({
  10. data: {
  11. a: 1
  12. },
  13. init () {
  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. })
  73. describe('beforeUpdate', () => {
  74. it('should be called before update', done => {
  75. const vm = new Vue({
  76. template: '<div>{{ msg }}</div>',
  77. data: { msg: 'foo' },
  78. beforeUpdate () {
  79. spy()
  80. expect(this.$el.textContent).toBe('foo')
  81. }
  82. }).$mount()
  83. expect(spy).not.toHaveBeenCalled()
  84. vm.msg = 'bar'
  85. expect(spy).not.toHaveBeenCalled() // should be async
  86. waitForUpdate(() => {
  87. expect(spy).toHaveBeenCalled()
  88. }).then(done)
  89. })
  90. })
  91. describe('updated', () => {
  92. it('should be called after update', done => {
  93. const vm = new Vue({
  94. template: '<div>{{ msg }}</div>',
  95. data: { msg: 'foo' },
  96. updated () {
  97. spy()
  98. expect(this.$el.textContent).toBe('bar')
  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('beforeDestroy', () => {
  110. it('should be called before destroy', () => {
  111. const vm = new Vue({
  112. beforeDestroy () {
  113. spy()
  114. expect(this._isBeingDestroyed).toBe(false)
  115. expect(this._isDestroyed).toBe(false)
  116. }
  117. }).$mount()
  118. expect(spy).not.toHaveBeenCalled()
  119. vm.$destroy()
  120. vm.$destroy()
  121. expect(spy).toHaveBeenCalled()
  122. expect(spy.calls.count()).toBe(1)
  123. })
  124. })
  125. describe('destroyed', () => {
  126. it('should be called after destroy', () => {
  127. const vm = new Vue({
  128. destroyed () {
  129. spy()
  130. expect(this._isBeingDestroyed).toBe(true)
  131. expect(this._isDestroyed).toBe(true)
  132. }
  133. }).$mount()
  134. expect(spy).not.toHaveBeenCalled()
  135. vm.$destroy()
  136. vm.$destroy()
  137. expect(spy).toHaveBeenCalled()
  138. expect(spy.calls.count()).toBe(1)
  139. })
  140. })
  141. })