apiLifecycle.spec.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. import Vue from 'vue'
  2. import {
  3. h,
  4. onBeforeMount,
  5. onMounted,
  6. ref,
  7. onBeforeUpdate,
  8. onUpdated,
  9. onBeforeUnmount,
  10. onUnmounted
  11. } from 'v3'
  12. import { nextTick } from 'core/util'
  13. describe('api: lifecycle hooks', () => {
  14. it('onBeforeMount', () => {
  15. const fn = vi.fn(() => {
  16. // should be called before root is replaced
  17. expect(vm.$el).toBeUndefined()
  18. })
  19. const Comp = {
  20. setup() {
  21. onBeforeMount(fn)
  22. return () => h('div', 'hello')
  23. }
  24. }
  25. const vm = new Vue(Comp)
  26. vm.$mount()
  27. expect(fn).toHaveBeenCalledTimes(1)
  28. expect(vm.$el.innerHTML).toBe(`hello`)
  29. })
  30. it('onMounted', () => {
  31. const fn = vi.fn(() => {
  32. // should be called after inner div is rendered
  33. expect(vm.$el.outerHTML).toBe(`<div></div>`)
  34. })
  35. const Comp = {
  36. setup() {
  37. onMounted(fn)
  38. return () => h('div')
  39. }
  40. }
  41. const vm = new Vue(Comp)
  42. vm.$mount()
  43. expect(fn).toHaveBeenCalledTimes(1)
  44. })
  45. it('onBeforeUpdate', async () => {
  46. const count = ref(0)
  47. const fn = vi.fn(() => {
  48. // should be called before inner div is updated
  49. expect(vm.$el.outerHTML).toBe(`<div>0</div>`)
  50. })
  51. const Comp = {
  52. setup() {
  53. onBeforeUpdate(fn)
  54. return () => h('div', count.value)
  55. }
  56. }
  57. const vm = new Vue(Comp).$mount()
  58. count.value++
  59. await nextTick()
  60. expect(fn).toHaveBeenCalledTimes(1)
  61. expect(vm.$el.outerHTML).toBe(`<div>1</div>`)
  62. })
  63. it('state mutation in onBeforeUpdate', async () => {
  64. const count = ref(0)
  65. const fn = vi.fn(() => {
  66. // should be called before inner div is updated
  67. expect(vm.$el.outerHTML).toBe(`<div>0</div>`)
  68. count.value++
  69. })
  70. const renderSpy = vi.fn()
  71. const Comp = {
  72. setup() {
  73. onBeforeUpdate(fn)
  74. return () => {
  75. renderSpy()
  76. return h('div', count.value)
  77. }
  78. }
  79. }
  80. const vm = new Vue(Comp).$mount()
  81. expect(renderSpy).toHaveBeenCalledTimes(1)
  82. count.value++
  83. await nextTick()
  84. expect(fn).toHaveBeenCalledTimes(1)
  85. expect(renderSpy).toHaveBeenCalledTimes(2)
  86. expect(vm.$el.outerHTML).toBe(`<div>2</div>`)
  87. })
  88. it('onUpdated', async () => {
  89. const count = ref(0)
  90. const fn = vi.fn(() => {
  91. // should be called after inner div is updated
  92. expect(vm.$el.outerHTML).toBe(`<div>1</div>`)
  93. })
  94. const Comp = {
  95. setup() {
  96. onUpdated(fn)
  97. return () => h('div', count.value)
  98. }
  99. }
  100. const vm = new Vue(Comp).$mount()
  101. count.value++
  102. await nextTick()
  103. expect(fn).toHaveBeenCalledTimes(1)
  104. })
  105. it('onBeforeUnmount', async () => {
  106. const toggle = ref(true)
  107. const root = document.createElement('div')
  108. const fn = vi.fn(() => {
  109. // should be called before inner div is removed
  110. expect(root.outerHTML).toBe(`<div></div>`)
  111. })
  112. const Comp = {
  113. setup() {
  114. return () => (toggle.value ? h(Child) : null)
  115. }
  116. }
  117. const Child = {
  118. setup() {
  119. onBeforeUnmount(fn)
  120. return () => h('div')
  121. }
  122. }
  123. new Vue(Comp).$mount(root)
  124. toggle.value = false
  125. await nextTick()
  126. expect(fn).toHaveBeenCalledTimes(1)
  127. })
  128. it('onUnmounted', async () => {
  129. const toggle = ref(true)
  130. const fn = vi.fn(() => {
  131. // @discrepancy should be called after inner div is removed
  132. // expect(vm.$el.outerHTML).toBe(`<span></span>`)
  133. })
  134. const Comp = {
  135. setup() {
  136. return () => (toggle.value ? h(Child) : h('span'))
  137. }
  138. }
  139. const Child = {
  140. setup() {
  141. onUnmounted(fn)
  142. return () => h('div')
  143. }
  144. }
  145. new Vue(Comp).$mount()
  146. toggle.value = false
  147. await nextTick()
  148. expect(fn).toHaveBeenCalledTimes(1)
  149. })
  150. it('onBeforeUnmount in onMounted', async () => {
  151. const toggle = ref(true)
  152. const fn = vi.fn(() => {
  153. // should be called before inner div is removed
  154. expect(vm.$el.outerHTML).toBe(`<div></div>`)
  155. })
  156. const Comp = {
  157. setup() {
  158. return () => (toggle.value ? h(Child) : null)
  159. }
  160. }
  161. const Child = {
  162. setup() {
  163. onMounted(() => {
  164. onBeforeUnmount(fn)
  165. })
  166. return () => h('div')
  167. }
  168. }
  169. const vm = new Vue(Comp).$mount()
  170. toggle.value = false
  171. await nextTick()
  172. expect(fn).toHaveBeenCalledTimes(1)
  173. })
  174. it('lifecycle call order', async () => {
  175. const count = ref(0)
  176. const calls: string[] = []
  177. const Root = {
  178. setup() {
  179. onBeforeMount(() => calls.push('root onBeforeMount'))
  180. onMounted(() => calls.push('root onMounted'))
  181. onBeforeUpdate(() => calls.push('root onBeforeUpdate'))
  182. onUpdated(() => calls.push('root onUpdated'))
  183. onBeforeUnmount(() => calls.push('root onBeforeUnmount'))
  184. onUnmounted(() => calls.push('root onUnmounted'))
  185. return () => h(Mid, { props: { count: count.value } })
  186. }
  187. }
  188. const Mid = {
  189. props: ['count'],
  190. setup(props: any) {
  191. onBeforeMount(() => calls.push('mid onBeforeMount'))
  192. onMounted(() => calls.push('mid onMounted'))
  193. onBeforeUpdate(() => calls.push('mid onBeforeUpdate'))
  194. onUpdated(() => calls.push('mid onUpdated'))
  195. onBeforeUnmount(() => calls.push('mid onBeforeUnmount'))
  196. onUnmounted(() => calls.push('mid onUnmounted'))
  197. return () => h(Child, { props: { count: props.count } })
  198. }
  199. }
  200. const Child = {
  201. props: ['count'],
  202. setup(props: any) {
  203. onBeforeMount(() => calls.push('child onBeforeMount'))
  204. onMounted(() => calls.push('child onMounted'))
  205. onBeforeUpdate(() => calls.push('child onBeforeUpdate'))
  206. onUpdated(() => calls.push('child onUpdated'))
  207. onBeforeUnmount(() => calls.push('child onBeforeUnmount'))
  208. onUnmounted(() => calls.push('child onUnmounted'))
  209. return () => h('div', props.count)
  210. }
  211. }
  212. // mount
  213. const vm = new Vue(Root)
  214. vm.$mount()
  215. expect(calls).toEqual([
  216. 'root onBeforeMount',
  217. 'mid onBeforeMount',
  218. 'child onBeforeMount',
  219. 'child onMounted',
  220. 'mid onMounted',
  221. 'root onMounted'
  222. ])
  223. calls.length = 0
  224. // update
  225. count.value++
  226. await nextTick()
  227. expect(calls).toEqual([
  228. 'root onBeforeUpdate',
  229. 'mid onBeforeUpdate',
  230. 'child onBeforeUpdate',
  231. 'child onUpdated',
  232. 'mid onUpdated',
  233. 'root onUpdated'
  234. ])
  235. calls.length = 0
  236. // unmount
  237. vm.$destroy()
  238. expect(calls).toEqual([
  239. 'root onBeforeUnmount',
  240. 'mid onBeforeUnmount',
  241. 'child onBeforeUnmount',
  242. 'child onUnmounted',
  243. 'mid onUnmounted',
  244. 'root onUnmounted'
  245. ])
  246. })
  247. })