2
0

apiLifecycle.spec.ts 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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 = jest.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 = jest.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 = jest.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 = jest.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 = jest.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 = jest.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 = jest.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 = jest.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 = jest.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. setup(props: any) {
  203. onBeforeMount(() => calls.push('mid onBeforeMount'))
  204. onMounted(() => calls.push('mid onMounted'))
  205. onBeforeUpdate(() => calls.push('mid onBeforeUpdate'))
  206. onUpdated(() => calls.push('mid onUpdated'))
  207. onBeforeUnmount(() => calls.push('mid onBeforeUnmount'))
  208. onUnmounted(() => calls.push('mid onUnmounted'))
  209. return () => h(Child, { count: props.count })
  210. }
  211. }
  212. const Child = {
  213. setup(props: any) {
  214. onBeforeMount(() => calls.push('child onBeforeMount'))
  215. onMounted(() => calls.push('child onMounted'))
  216. onBeforeUpdate(() => calls.push('child onBeforeUpdate'))
  217. onUpdated(() => calls.push('child onUpdated'))
  218. onBeforeUnmount(() => calls.push('child onBeforeUnmount'))
  219. onUnmounted(() => calls.push('child onUnmounted'))
  220. return () => h('div', props.count)
  221. }
  222. }
  223. // mount
  224. render(h(Root), root)
  225. expect(calls).toEqual([
  226. 'root onBeforeMount',
  227. 'mid onBeforeMount',
  228. 'child onBeforeMount',
  229. 'child onMounted',
  230. 'mid onMounted',
  231. 'root onMounted'
  232. ])
  233. calls.length = 0
  234. // update
  235. count.value++
  236. await nextTick()
  237. expect(calls).toEqual([
  238. 'root onBeforeUpdate',
  239. 'mid onBeforeUpdate',
  240. 'child onBeforeUpdate',
  241. 'child onUpdated',
  242. 'mid onUpdated',
  243. 'root onUpdated'
  244. ])
  245. calls.length = 0
  246. // unmount
  247. render(null, root)
  248. expect(calls).toEqual([
  249. 'root onBeforeUnmount',
  250. 'mid onBeforeUnmount',
  251. 'child onBeforeUnmount',
  252. 'child onUnmounted',
  253. 'mid onUnmounted',
  254. 'root onUnmounted'
  255. ])
  256. })
  257. it('onRenderTracked', () => {
  258. const events: DebuggerEvent[] = []
  259. const onTrack = jest.fn((e: DebuggerEvent) => {
  260. events.push(e)
  261. })
  262. const obj = reactive({ foo: 1, bar: 2 })
  263. const Comp = {
  264. setup() {
  265. onRenderTracked(onTrack)
  266. return () =>
  267. h('div', [obj.foo, 'bar' in obj, Object.keys(obj).join('')])
  268. }
  269. }
  270. render(h(Comp), nodeOps.createElement('div'))
  271. expect(onTrack).toHaveBeenCalledTimes(3)
  272. expect(events).toMatchObject([
  273. {
  274. target: obj,
  275. type: TrackOpTypes.GET,
  276. key: 'foo'
  277. },
  278. {
  279. target: obj,
  280. type: TrackOpTypes.HAS,
  281. key: 'bar'
  282. },
  283. {
  284. target: obj,
  285. type: TrackOpTypes.ITERATE,
  286. key: ITERATE_KEY
  287. }
  288. ])
  289. })
  290. it('onRenderTriggered', async () => {
  291. const events: DebuggerEvent[] = []
  292. const onTrigger = jest.fn((e: DebuggerEvent) => {
  293. events.push(e)
  294. })
  295. const obj = reactive({ foo: 1, bar: 2 })
  296. const Comp = {
  297. setup() {
  298. onRenderTriggered(onTrigger)
  299. return () =>
  300. h('div', [obj.foo, 'bar' in obj, Object.keys(obj).join('')])
  301. }
  302. }
  303. render(h(Comp), nodeOps.createElement('div'))
  304. obj.foo++
  305. await nextTick()
  306. expect(onTrigger).toHaveBeenCalledTimes(1)
  307. expect(events[0]).toMatchObject({
  308. type: TriggerOpTypes.SET,
  309. key: 'foo',
  310. oldValue: 1,
  311. newValue: 2
  312. })
  313. // @ts-ignore
  314. delete obj.bar
  315. await nextTick()
  316. expect(onTrigger).toHaveBeenCalledTimes(2)
  317. expect(events[1]).toMatchObject({
  318. type: TriggerOpTypes.DELETE,
  319. key: 'bar',
  320. oldValue: 2
  321. })
  322. ;(obj as any).baz = 3
  323. await nextTick()
  324. expect(onTrigger).toHaveBeenCalledTimes(3)
  325. expect(events[2]).toMatchObject({
  326. type: TriggerOpTypes.ADD,
  327. key: 'baz',
  328. newValue: 3
  329. })
  330. })
  331. })