apiLifecycle.spec.ts 9.4 KB

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