2
0

apiLifecycle.spec.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. import {
  2. KeepAlive,
  3. TrackOpTypes,
  4. h,
  5. nextTick,
  6. nodeOps,
  7. onActivated,
  8. onBeforeMount,
  9. onBeforeUnmount,
  10. onBeforeUpdate,
  11. onMounted,
  12. onRenderTracked,
  13. onRenderTriggered,
  14. onUnmounted,
  15. onUpdated,
  16. reactive,
  17. ref,
  18. render,
  19. serializeInner,
  20. } from '@vue/runtime-test'
  21. import {
  22. type DebuggerEvent,
  23. ITERATE_KEY,
  24. TriggerOpTypes,
  25. } from '@vue/reactivity'
  26. // reference: https://vue-composition-api-rfc.netlify.com/api.html#lifecycle-hooks
  27. describe('api: lifecycle hooks', () => {
  28. it('onBeforeMount', () => {
  29. const root = nodeOps.createElement('div')
  30. const fn = vi.fn(() => {
  31. // should be called before inner div is rendered
  32. expect(serializeInner(root)).toBe(``)
  33. })
  34. const Comp = {
  35. setup() {
  36. onBeforeMount(fn)
  37. return () => h('div')
  38. },
  39. }
  40. render(h(Comp), root)
  41. expect(fn).toHaveBeenCalledTimes(1)
  42. // #10863
  43. expect(fn).toHaveBeenCalledWith()
  44. })
  45. it('onMounted', () => {
  46. const root = nodeOps.createElement('div')
  47. const fn = vi.fn(() => {
  48. // should be called after inner div is rendered
  49. expect(serializeInner(root)).toBe(`<div></div>`)
  50. })
  51. const Comp = {
  52. setup() {
  53. onMounted(fn)
  54. return () => h('div')
  55. },
  56. }
  57. render(h(Comp), root)
  58. expect(fn).toHaveBeenCalledTimes(1)
  59. })
  60. it('onBeforeUpdate', async () => {
  61. const count = ref(0)
  62. const root = nodeOps.createElement('div')
  63. const fn = vi.fn(() => {
  64. // should be called before inner div is updated
  65. expect(serializeInner(root)).toBe(`<div>0</div>`)
  66. })
  67. const Comp = {
  68. setup() {
  69. onBeforeUpdate(fn)
  70. return () => h('div', count.value)
  71. },
  72. }
  73. render(h(Comp), root)
  74. count.value++
  75. await nextTick()
  76. expect(fn).toHaveBeenCalledTimes(1)
  77. expect(serializeInner(root)).toBe(`<div>1</div>`)
  78. })
  79. it('state mutation in onBeforeUpdate', async () => {
  80. const count = ref(0)
  81. const root = nodeOps.createElement('div')
  82. const fn = vi.fn(() => {
  83. // should be called before inner div is updated
  84. expect(serializeInner(root)).toBe(`<div>0</div>`)
  85. count.value++
  86. })
  87. const renderSpy = vi.fn()
  88. const Comp = {
  89. setup() {
  90. onBeforeUpdate(fn)
  91. return () => {
  92. renderSpy()
  93. return h('div', count.value)
  94. }
  95. },
  96. }
  97. render(h(Comp), root)
  98. expect(renderSpy).toHaveBeenCalledTimes(1)
  99. count.value++
  100. await nextTick()
  101. expect(fn).toHaveBeenCalledTimes(1)
  102. expect(renderSpy).toHaveBeenCalledTimes(2)
  103. expect(serializeInner(root)).toBe(`<div>2</div>`)
  104. })
  105. it('onUpdated', async () => {
  106. const count = ref(0)
  107. const root = nodeOps.createElement('div')
  108. const fn = vi.fn(() => {
  109. // should be called after inner div is updated
  110. expect(serializeInner(root)).toBe(`<div>1</div>`)
  111. })
  112. const Comp = {
  113. setup() {
  114. onUpdated(fn)
  115. return () => h('div', count.value)
  116. },
  117. }
  118. render(h(Comp), root)
  119. count.value++
  120. await nextTick()
  121. expect(fn).toHaveBeenCalledTimes(1)
  122. })
  123. it('onBeforeUnmount', async () => {
  124. const toggle = ref(true)
  125. const root = nodeOps.createElement('div')
  126. const fn = vi.fn(() => {
  127. // should be called before inner div is removed
  128. expect(serializeInner(root)).toBe(`<div></div>`)
  129. })
  130. const Comp = {
  131. setup() {
  132. return () => (toggle.value ? h(Child) : null)
  133. },
  134. }
  135. const Child = {
  136. setup() {
  137. onBeforeUnmount(fn)
  138. return () => h('div')
  139. },
  140. }
  141. render(h(Comp), root)
  142. toggle.value = false
  143. await nextTick()
  144. expect(fn).toHaveBeenCalledTimes(1)
  145. })
  146. it('onUnmounted', async () => {
  147. const toggle = ref(true)
  148. const root = nodeOps.createElement('div')
  149. const fn = vi.fn(() => {
  150. // should be called after inner div is removed
  151. expect(serializeInner(root)).toBe(`<!---->`)
  152. })
  153. const Comp = {
  154. setup() {
  155. return () => (toggle.value ? h(Child) : null)
  156. },
  157. }
  158. const Child = {
  159. setup() {
  160. onUnmounted(fn)
  161. return () => h('div')
  162. },
  163. }
  164. render(h(Comp), root)
  165. toggle.value = false
  166. await nextTick()
  167. expect(fn).toHaveBeenCalledTimes(1)
  168. })
  169. it('onBeforeUnmount in onMounted', async () => {
  170. const toggle = ref(true)
  171. const root = nodeOps.createElement('div')
  172. const fn = vi.fn(() => {
  173. // should be called before inner div is removed
  174. expect(serializeInner(root)).toBe(`<div></div>`)
  175. })
  176. const Comp = {
  177. setup() {
  178. return () => (toggle.value ? h(Child) : null)
  179. },
  180. }
  181. const Child = {
  182. setup() {
  183. onMounted(() => {
  184. onBeforeUnmount(fn)
  185. })
  186. return () => h('div')
  187. },
  188. }
  189. render(h(Comp), root)
  190. toggle.value = false
  191. await nextTick()
  192. expect(fn).toHaveBeenCalledTimes(1)
  193. })
  194. it('lifecycle call order', async () => {
  195. const count = ref(0)
  196. const root = nodeOps.createElement('div')
  197. const calls: string[] = []
  198. const Root = {
  199. setup() {
  200. onBeforeMount(() => calls.push('root onBeforeMount'))
  201. onMounted(() => calls.push('root onMounted'))
  202. onBeforeUpdate(() => calls.push('root onBeforeUpdate'))
  203. onUpdated(() => calls.push('root onUpdated'))
  204. onBeforeUnmount(() => calls.push('root onBeforeUnmount'))
  205. onUnmounted(() => calls.push('root onUnmounted'))
  206. return () => h(Mid, { count: count.value })
  207. },
  208. }
  209. const Mid = {
  210. props: ['count'],
  211. setup(props: any) {
  212. onBeforeMount(() => calls.push('mid onBeforeMount'))
  213. onMounted(() => calls.push('mid onMounted'))
  214. onBeforeUpdate(() => calls.push('mid onBeforeUpdate'))
  215. onUpdated(() => calls.push('mid onUpdated'))
  216. onBeforeUnmount(() => calls.push('mid onBeforeUnmount'))
  217. onUnmounted(() => calls.push('mid onUnmounted'))
  218. return () => h(Child, { count: props.count })
  219. },
  220. }
  221. const Child = {
  222. props: ['count'],
  223. setup(props: any) {
  224. onBeforeMount(() => calls.push('child onBeforeMount'))
  225. onMounted(() => calls.push('child onMounted'))
  226. onBeforeUpdate(() => calls.push('child onBeforeUpdate'))
  227. onUpdated(() => calls.push('child onUpdated'))
  228. onBeforeUnmount(() => calls.push('child onBeforeUnmount'))
  229. onUnmounted(() => calls.push('child onUnmounted'))
  230. return () => h('div', props.count)
  231. },
  232. }
  233. // mount
  234. render(h(Root), root)
  235. expect(calls).toEqual([
  236. 'root onBeforeMount',
  237. 'mid onBeforeMount',
  238. 'child onBeforeMount',
  239. 'child onMounted',
  240. 'mid onMounted',
  241. 'root onMounted',
  242. ])
  243. calls.length = 0
  244. // update
  245. count.value++
  246. await nextTick()
  247. expect(calls).toEqual([
  248. 'root onBeforeUpdate',
  249. 'mid onBeforeUpdate',
  250. 'child onBeforeUpdate',
  251. 'child onUpdated',
  252. 'mid onUpdated',
  253. 'root onUpdated',
  254. ])
  255. calls.length = 0
  256. // unmount
  257. render(null, root)
  258. expect(calls).toEqual([
  259. 'root onBeforeUnmount',
  260. 'mid onBeforeUnmount',
  261. 'child onBeforeUnmount',
  262. 'child onUnmounted',
  263. 'mid onUnmounted',
  264. 'root onUnmounted',
  265. ])
  266. })
  267. it('onRenderTracked', () => {
  268. const events: DebuggerEvent[] = []
  269. const onTrack = vi.fn((e: DebuggerEvent) => {
  270. events.push(e)
  271. })
  272. const obj = reactive({ foo: 1, bar: 2 })
  273. const Comp = {
  274. setup() {
  275. onRenderTracked(onTrack)
  276. return () =>
  277. h('div', [obj.foo, 'bar' in obj, Object.keys(obj).join('')])
  278. },
  279. }
  280. render(h(Comp), nodeOps.createElement('div'))
  281. expect(onTrack).toHaveBeenCalledTimes(3)
  282. expect(events).toMatchObject([
  283. {
  284. target: obj,
  285. type: TrackOpTypes.GET,
  286. key: 'foo',
  287. },
  288. {
  289. target: obj,
  290. type: TrackOpTypes.HAS,
  291. key: 'bar',
  292. },
  293. {
  294. target: obj,
  295. type: TrackOpTypes.ITERATE,
  296. key: ITERATE_KEY,
  297. },
  298. ])
  299. })
  300. it('onRenderTriggered', async () => {
  301. const events: DebuggerEvent[] = []
  302. const onTrigger = vi.fn((e: DebuggerEvent) => {
  303. events.push(e)
  304. })
  305. const obj = reactive<{
  306. foo: number
  307. bar?: number
  308. }>({ foo: 1, bar: 2 })
  309. const Comp = {
  310. setup() {
  311. onRenderTriggered(onTrigger)
  312. return () =>
  313. h('div', [obj.foo, 'bar' in obj, Object.keys(obj).join('')])
  314. },
  315. }
  316. render(h(Comp), nodeOps.createElement('div'))
  317. obj.foo++
  318. await nextTick()
  319. expect(onTrigger).toHaveBeenCalledTimes(1)
  320. expect(events[0]).toMatchObject({
  321. type: TriggerOpTypes.SET,
  322. key: 'foo',
  323. oldValue: 1,
  324. newValue: 2,
  325. })
  326. delete obj.bar
  327. await nextTick()
  328. expect(onTrigger).toHaveBeenCalledTimes(2)
  329. expect(events[1]).toMatchObject({
  330. type: TriggerOpTypes.DELETE,
  331. key: 'bar',
  332. oldValue: 2,
  333. })
  334. ;(obj as any).baz = 3
  335. await nextTick()
  336. expect(onTrigger).toHaveBeenCalledTimes(3)
  337. expect(events[2]).toMatchObject({
  338. type: TriggerOpTypes.ADD,
  339. key: 'baz',
  340. newValue: 3,
  341. })
  342. })
  343. it('runs shared hook fn for each instance', async () => {
  344. const fn = vi.fn()
  345. const toggle = ref(true)
  346. const Comp = {
  347. setup() {
  348. return () => (toggle.value ? [h(Child), h(Child)] : null)
  349. },
  350. }
  351. const Child = {
  352. setup() {
  353. onMounted(fn)
  354. onBeforeUnmount(fn)
  355. return () => h('div')
  356. },
  357. }
  358. render(h(Comp), nodeOps.createElement('div'))
  359. expect(fn).toHaveBeenCalledTimes(2)
  360. toggle.value = false
  361. await nextTick()
  362. expect(fn).toHaveBeenCalledTimes(4)
  363. })
  364. it('immediately trigger unmount during rendering', async () => {
  365. const fn = vi.fn()
  366. const toggle = ref(false)
  367. const Child = {
  368. setup() {
  369. onMounted(fn)
  370. // trigger unmount immediately
  371. toggle.value = false
  372. return () => h('div')
  373. },
  374. }
  375. const Comp = {
  376. setup() {
  377. return () => (toggle.value ? [h(Child)] : null)
  378. },
  379. }
  380. render(h(Comp), nodeOps.createElement('div'))
  381. toggle.value = true
  382. await nextTick()
  383. expect(fn).toHaveBeenCalledTimes(0)
  384. })
  385. it('immediately trigger unmount during rendering(with KeepAlive)', async () => {
  386. const mountedSpy = vi.fn()
  387. const activeSpy = vi.fn()
  388. const toggle = ref(false)
  389. const Child = {
  390. setup() {
  391. onMounted(mountedSpy)
  392. onActivated(activeSpy)
  393. // trigger unmount immediately
  394. toggle.value = false
  395. return () => h('div')
  396. },
  397. }
  398. const Comp = {
  399. setup() {
  400. return () => h(KeepAlive, [toggle.value ? h(Child) : null])
  401. },
  402. }
  403. render(h(Comp), nodeOps.createElement('div'))
  404. toggle.value = true
  405. await nextTick()
  406. expect(mountedSpy).toHaveBeenCalledTimes(0)
  407. expect(activeSpy).toHaveBeenCalledTimes(0)
  408. })
  409. })