apiLifecycle.spec.ts 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. import Vue from 'vue'
  2. import {
  3. h,
  4. onBeforeMount,
  5. onMounted,
  6. ref,
  7. reactive,
  8. onBeforeUpdate,
  9. onUpdated,
  10. onBeforeUnmount,
  11. onUnmounted,
  12. onRenderTracked,
  13. onRenderTriggered,
  14. DebuggerEvent,
  15. TrackOpTypes,
  16. TriggerOpTypes
  17. } from 'v3'
  18. import { nextTick } from 'core/util'
  19. describe('api: lifecycle hooks', () => {
  20. it('onBeforeMount', () => {
  21. const fn = vi.fn(() => {
  22. // should be called before root is replaced
  23. expect(vm.$el).toBeUndefined()
  24. })
  25. const Comp = {
  26. setup() {
  27. onBeforeMount(fn)
  28. return () => h('div', 'hello')
  29. }
  30. }
  31. const vm = new Vue(Comp)
  32. vm.$mount()
  33. expect(fn).toHaveBeenCalledTimes(1)
  34. expect(vm.$el.innerHTML).toBe(`hello`)
  35. })
  36. it('onMounted', () => {
  37. const fn = vi.fn(() => {
  38. // should be called after inner div is rendered
  39. expect(vm.$el.outerHTML).toBe(`<div></div>`)
  40. })
  41. const Comp = {
  42. setup() {
  43. onMounted(fn)
  44. return () => h('div')
  45. }
  46. }
  47. const vm = new Vue(Comp)
  48. vm.$mount()
  49. expect(fn).toHaveBeenCalledTimes(1)
  50. })
  51. it('onBeforeUpdate', async () => {
  52. const count = ref(0)
  53. const fn = vi.fn(() => {
  54. // should be called before inner div is updated
  55. expect(vm.$el.outerHTML).toBe(`<div>0</div>`)
  56. })
  57. const Comp = {
  58. setup() {
  59. onBeforeUpdate(fn)
  60. return () => h('div', count.value)
  61. }
  62. }
  63. const vm = new Vue(Comp).$mount()
  64. count.value++
  65. await nextTick()
  66. expect(fn).toHaveBeenCalledTimes(1)
  67. expect(vm.$el.outerHTML).toBe(`<div>1</div>`)
  68. })
  69. it('state mutation in onBeforeUpdate', async () => {
  70. const count = ref(0)
  71. const fn = vi.fn(() => {
  72. // should be called before inner div is updated
  73. expect(vm.$el.outerHTML).toBe(`<div>0</div>`)
  74. count.value++
  75. })
  76. const renderSpy = vi.fn()
  77. const Comp = {
  78. setup() {
  79. onBeforeUpdate(fn)
  80. return () => {
  81. renderSpy()
  82. return h('div', count.value)
  83. }
  84. }
  85. }
  86. const vm = new Vue(Comp).$mount()
  87. expect(renderSpy).toHaveBeenCalledTimes(1)
  88. count.value++
  89. await nextTick()
  90. expect(fn).toHaveBeenCalledTimes(1)
  91. expect(renderSpy).toHaveBeenCalledTimes(2)
  92. expect(vm.$el.outerHTML).toBe(`<div>2</div>`)
  93. })
  94. it('onUpdated', async () => {
  95. const count = ref(0)
  96. const fn = vi.fn(() => {
  97. // should be called after inner div is updated
  98. expect(vm.$el.outerHTML).toBe(`<div>1</div>`)
  99. })
  100. const Comp = {
  101. setup() {
  102. onUpdated(fn)
  103. return () => h('div', count.value)
  104. }
  105. }
  106. const vm = new Vue(Comp).$mount()
  107. count.value++
  108. await nextTick()
  109. expect(fn).toHaveBeenCalledTimes(1)
  110. })
  111. it('onBeforeUnmount', async () => {
  112. const toggle = ref(true)
  113. const root = document.createElement('div')
  114. const fn = vi.fn(() => {
  115. // should be called before inner div is removed
  116. expect(root.outerHTML).toBe(`<div></div>`)
  117. })
  118. const Comp = {
  119. setup() {
  120. return () => (toggle.value ? h(Child) : null)
  121. }
  122. }
  123. const Child = {
  124. setup() {
  125. onBeforeUnmount(fn)
  126. return () => h('div')
  127. }
  128. }
  129. new Vue(Comp).$mount(root)
  130. toggle.value = false
  131. await nextTick()
  132. expect(fn).toHaveBeenCalledTimes(1)
  133. })
  134. it('onUnmounted', async () => {
  135. const toggle = ref(true)
  136. const fn = vi.fn(() => {
  137. // @discrepancy should be called after inner div is removed
  138. // expect(vm.$el.outerHTML).toBe(`<span></span>`)
  139. })
  140. const Comp = {
  141. setup() {
  142. return () => (toggle.value ? h(Child) : h('span'))
  143. }
  144. }
  145. const Child = {
  146. setup() {
  147. onUnmounted(fn)
  148. return () => h('div')
  149. }
  150. }
  151. new Vue(Comp).$mount()
  152. toggle.value = false
  153. await nextTick()
  154. expect(fn).toHaveBeenCalledTimes(1)
  155. })
  156. it('onBeforeUnmount in onMounted', async () => {
  157. const toggle = ref(true)
  158. const fn = vi.fn(() => {
  159. // should be called before inner div is removed
  160. expect(vm.$el.outerHTML).toBe(`<div></div>`)
  161. })
  162. const Comp = {
  163. setup() {
  164. return () => (toggle.value ? h(Child) : null)
  165. }
  166. }
  167. const Child = {
  168. setup() {
  169. onMounted(() => {
  170. onBeforeUnmount(fn)
  171. })
  172. return () => h('div')
  173. }
  174. }
  175. const vm = new Vue(Comp).$mount()
  176. toggle.value = false
  177. await nextTick()
  178. expect(fn).toHaveBeenCalledTimes(1)
  179. })
  180. it('lifecycle call order', async () => {
  181. const count = ref(0)
  182. const calls: string[] = []
  183. const Root = {
  184. setup() {
  185. onBeforeMount(() => calls.push('root onBeforeMount'))
  186. onMounted(() => calls.push('root onMounted'))
  187. onBeforeUpdate(() => calls.push('root onBeforeUpdate'))
  188. onUpdated(() => calls.push('root onUpdated'))
  189. onBeforeUnmount(() => calls.push('root onBeforeUnmount'))
  190. onUnmounted(() => calls.push('root onUnmounted'))
  191. return () => h(Mid, { props: { count: count.value } })
  192. }
  193. }
  194. const Mid = {
  195. props: ['count'],
  196. setup(props: any) {
  197. onBeforeMount(() => calls.push('mid onBeforeMount'))
  198. onMounted(() => calls.push('mid onMounted'))
  199. onBeforeUpdate(() => calls.push('mid onBeforeUpdate'))
  200. onUpdated(() => calls.push('mid onUpdated'))
  201. onBeforeUnmount(() => calls.push('mid onBeforeUnmount'))
  202. onUnmounted(() => calls.push('mid onUnmounted'))
  203. return () => h(Child, { props: { count: props.count } })
  204. }
  205. }
  206. const Child = {
  207. props: ['count'],
  208. setup(props: any) {
  209. onBeforeMount(() => calls.push('child onBeforeMount'))
  210. onMounted(() => calls.push('child onMounted'))
  211. onBeforeUpdate(() => calls.push('child onBeforeUpdate'))
  212. onUpdated(() => calls.push('child onUpdated'))
  213. onBeforeUnmount(() => calls.push('child onBeforeUnmount'))
  214. onUnmounted(() => calls.push('child onUnmounted'))
  215. return () => h('div', props.count)
  216. }
  217. }
  218. // mount
  219. const vm = new Vue(Root)
  220. vm.$mount()
  221. expect(calls).toEqual([
  222. 'root onBeforeMount',
  223. 'mid onBeforeMount',
  224. 'child onBeforeMount',
  225. 'child onMounted',
  226. 'mid onMounted',
  227. 'root onMounted'
  228. ])
  229. calls.length = 0
  230. // update
  231. count.value++
  232. await nextTick()
  233. expect(calls).toEqual([
  234. 'root onBeforeUpdate',
  235. 'mid onBeforeUpdate',
  236. 'child onBeforeUpdate',
  237. 'child onUpdated',
  238. 'mid onUpdated',
  239. 'root onUpdated'
  240. ])
  241. calls.length = 0
  242. // unmount
  243. vm.$destroy()
  244. expect(calls).toEqual([
  245. 'root onBeforeUnmount',
  246. 'mid onBeforeUnmount',
  247. 'child onBeforeUnmount',
  248. 'child onUnmounted',
  249. 'mid onUnmounted',
  250. 'root onUnmounted'
  251. ])
  252. })
  253. it('onRenderTracked', () => {
  254. const events: DebuggerEvent[] = []
  255. const onTrack = vi.fn((e: DebuggerEvent) => {
  256. events.push(e)
  257. })
  258. const obj = reactive({ foo: 1, bar: 2 })
  259. const Comp = {
  260. setup() {
  261. onRenderTracked(onTrack)
  262. return () => h('div', [obj.foo + obj.bar])
  263. }
  264. }
  265. new Vue(Comp).$mount()
  266. expect(onTrack).toHaveBeenCalledTimes(2)
  267. expect(events).toMatchObject([
  268. {
  269. target: obj,
  270. type: TrackOpTypes.GET,
  271. key: 'foo'
  272. },
  273. {
  274. target: obj,
  275. type: TrackOpTypes.GET,
  276. key: 'bar'
  277. }
  278. ])
  279. })
  280. it('onRenderTriggered', async () => {
  281. const events: DebuggerEvent[] = []
  282. const onTrigger = vi.fn((e: DebuggerEvent) => {
  283. events.push(e)
  284. })
  285. const obj = reactive<{
  286. foo: number
  287. bar: number
  288. }>({ foo: 1, bar: 2 })
  289. const Comp = {
  290. setup() {
  291. onRenderTriggered(onTrigger)
  292. return () => h('div', [obj.foo + obj.bar])
  293. }
  294. }
  295. new Vue(Comp).$mount()
  296. obj.foo++
  297. await nextTick()
  298. expect(onTrigger).toHaveBeenCalledTimes(1)
  299. expect(events[0]).toMatchObject({
  300. type: TriggerOpTypes.SET,
  301. key: 'foo',
  302. oldValue: 1,
  303. newValue: 2
  304. })
  305. obj.bar++
  306. await nextTick()
  307. expect(onTrigger).toHaveBeenCalledTimes(2)
  308. expect(events[1]).toMatchObject({
  309. type: TriggerOpTypes.SET,
  310. key: 'bar',
  311. oldValue: 2,
  312. newValue: 3
  313. })
  314. })
  315. })