rendererComponent.spec.ts 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. import { vi } from 'vitest'
  2. import {
  3. ref,
  4. h,
  5. render,
  6. nodeOps,
  7. serializeInner,
  8. nextTick,
  9. VNode,
  10. provide,
  11. inject,
  12. Ref,
  13. watch,
  14. SetupContext
  15. } from '@vue/runtime-test'
  16. describe('renderer: component', () => {
  17. test('should update parent(hoc) component host el when child component self update', async () => {
  18. const value = ref(true)
  19. let parentVnode: VNode
  20. let childVnode1: VNode
  21. let childVnode2: VNode
  22. const Parent = {
  23. render: () => {
  24. // let Parent first rerender
  25. return (parentVnode = h(Child))
  26. }
  27. }
  28. const Child = {
  29. render: () => {
  30. return value.value
  31. ? (childVnode1 = h('div'))
  32. : (childVnode2 = h('span'))
  33. }
  34. }
  35. const root = nodeOps.createElement('div')
  36. render(h(Parent), root)
  37. expect(serializeInner(root)).toBe(`<div></div>`)
  38. expect(parentVnode!.el).toBe(childVnode1!.el)
  39. value.value = false
  40. await nextTick()
  41. expect(serializeInner(root)).toBe(`<span></span>`)
  42. expect(parentVnode!.el).toBe(childVnode2!.el)
  43. })
  44. it('should create an Component with props', () => {
  45. const Comp = {
  46. render: () => {
  47. return h('div')
  48. }
  49. }
  50. const root = nodeOps.createElement('div')
  51. render(h(Comp, { id: 'foo', class: 'bar' }), root)
  52. expect(serializeInner(root)).toBe(`<div id="foo" class="bar"></div>`)
  53. })
  54. it('should create an Component with direct text children', () => {
  55. const Comp = {
  56. render: () => {
  57. return h('div', 'test')
  58. }
  59. }
  60. const root = nodeOps.createElement('div')
  61. render(h(Comp, { id: 'foo', class: 'bar' }), root)
  62. expect(serializeInner(root)).toBe(`<div id="foo" class="bar">test</div>`)
  63. })
  64. it('should update an Component tag which is already mounted', () => {
  65. const Comp1 = {
  66. render: () => {
  67. return h('div', 'foo')
  68. }
  69. }
  70. const root = nodeOps.createElement('div')
  71. render(h(Comp1), root)
  72. expect(serializeInner(root)).toBe('<div>foo</div>')
  73. const Comp2 = {
  74. render: () => {
  75. return h('span', 'foo')
  76. }
  77. }
  78. render(h(Comp2), root)
  79. expect(serializeInner(root)).toBe('<span>foo</span>')
  80. })
  81. // #2072
  82. it('should not update Component if only changed props are declared emit listeners', () => {
  83. const Comp1 = {
  84. emits: ['foo'],
  85. updated: vi.fn(),
  86. render: () => null
  87. }
  88. const root = nodeOps.createElement('div')
  89. render(
  90. h(Comp1, {
  91. onFoo: () => {}
  92. }),
  93. root
  94. )
  95. render(
  96. h(Comp1, {
  97. onFoo: () => {}
  98. }),
  99. root
  100. )
  101. expect(Comp1.updated).not.toHaveBeenCalled()
  102. })
  103. // #2043
  104. test('component child synchronously updating parent state should trigger parent re-render', async () => {
  105. const App = {
  106. setup() {
  107. const n = ref(0)
  108. provide('foo', n)
  109. return () => {
  110. return [h('div', n.value), h(Child)]
  111. }
  112. }
  113. }
  114. const Child = {
  115. setup() {
  116. const n = inject<Ref<number>>('foo')!
  117. n.value++
  118. return () => {
  119. return h('div', n.value)
  120. }
  121. }
  122. }
  123. const root = nodeOps.createElement('div')
  124. render(h(App), root)
  125. expect(serializeInner(root)).toBe(`<div>0</div><div>1</div>`)
  126. await nextTick()
  127. expect(serializeInner(root)).toBe(`<div>1</div><div>1</div>`)
  128. })
  129. // #2170
  130. test('instance.$el should be exposed to watch options', async () => {
  131. function returnThis(this: any, _arg: any) {
  132. return this
  133. }
  134. const propWatchSpy = vi.fn(returnThis)
  135. const dataWatchSpy = vi.fn(returnThis)
  136. let instance: any
  137. const Comp = {
  138. props: {
  139. testProp: String
  140. },
  141. data() {
  142. return {
  143. testData: undefined
  144. }
  145. },
  146. watch: {
  147. testProp() {
  148. // @ts-ignore
  149. propWatchSpy(this.$el)
  150. },
  151. testData() {
  152. // @ts-ignore
  153. dataWatchSpy(this.$el)
  154. }
  155. },
  156. created() {
  157. instance = this
  158. },
  159. render() {
  160. return h('div')
  161. }
  162. }
  163. const root = nodeOps.createElement('div')
  164. render(h(Comp), root)
  165. await nextTick()
  166. expect(propWatchSpy).not.toHaveBeenCalled()
  167. expect(dataWatchSpy).not.toHaveBeenCalled()
  168. render(h(Comp, { testProp: 'prop ' }), root)
  169. await nextTick()
  170. expect(propWatchSpy).toHaveBeenCalledWith(instance.$el)
  171. instance.testData = 1
  172. await nextTick()
  173. expect(dataWatchSpy).toHaveBeenCalledWith(instance.$el)
  174. })
  175. // #2200
  176. test('component child updating parent state in pre-flush should trigger parent re-render', async () => {
  177. const outer = ref(0)
  178. const App = {
  179. setup() {
  180. const inner = ref(0)
  181. return () => {
  182. return [
  183. h('div', inner.value),
  184. h(Child, {
  185. value: outer.value,
  186. onUpdate: (val: number) => (inner.value = val)
  187. })
  188. ]
  189. }
  190. }
  191. }
  192. const Child = {
  193. props: ['value'],
  194. setup(props: any, { emit }: SetupContext) {
  195. watch(
  196. () => props.value,
  197. (val: number) => emit('update', val)
  198. )
  199. return () => {
  200. return h('div', props.value)
  201. }
  202. }
  203. }
  204. const root = nodeOps.createElement('div')
  205. render(h(App), root)
  206. expect(serializeInner(root)).toBe(`<div>0</div><div>0</div>`)
  207. outer.value++
  208. await nextTick()
  209. expect(serializeInner(root)).toBe(`<div>1</div><div>1</div>`)
  210. })
  211. // #2521
  212. test('should pause tracking deps when initializing legacy options', async () => {
  213. let childInstance = null as any
  214. const Child = {
  215. props: ['foo'],
  216. data() {
  217. return {
  218. count: 0
  219. }
  220. },
  221. watch: {
  222. foo: {
  223. immediate: true,
  224. handler() {
  225. ;(this as any).count
  226. }
  227. }
  228. },
  229. created() {
  230. childInstance = this as any
  231. childInstance.count
  232. },
  233. render() {
  234. return h('h1', (this as any).count)
  235. }
  236. }
  237. const App = {
  238. setup() {
  239. return () => h(Child)
  240. },
  241. updated: vi.fn()
  242. }
  243. const root = nodeOps.createElement('div')
  244. render(h(App), root)
  245. expect(App.updated).toHaveBeenCalledTimes(0)
  246. childInstance.count++
  247. await nextTick()
  248. expect(App.updated).toHaveBeenCalledTimes(0)
  249. })
  250. describe('render with access caches', () => {
  251. // #3297
  252. test('should not set the access cache in the data() function (production mode)', () => {
  253. const Comp = {
  254. data() {
  255. ;(this as any).foo
  256. return { foo: 1 }
  257. },
  258. render() {
  259. return h('h1', (this as any).foo)
  260. }
  261. }
  262. const root = nodeOps.createElement('div')
  263. __DEV__ = false
  264. render(h(Comp), root)
  265. __DEV__ = true
  266. expect(serializeInner(root)).toBe(`<h1>1</h1>`)
  267. })
  268. })
  269. test('the component VNode should be cloned when reusing it', () => {
  270. const App = {
  271. render() {
  272. const c = [h(Comp)]
  273. return [c, c, c]
  274. }
  275. }
  276. const ids: number[] = []
  277. const Comp = {
  278. render: () => h('h1'),
  279. beforeUnmount() {
  280. ids.push((this as any).$.uid)
  281. }
  282. }
  283. const root = nodeOps.createElement('div')
  284. render(h(App), root)
  285. expect(serializeInner(root)).toBe(`<h1></h1><h1></h1><h1></h1>`)
  286. render(null, root)
  287. expect(serializeInner(root)).toBe(``)
  288. expect(ids).toEqual([ids[0], ids[0] + 1, ids[0] + 2])
  289. })
  290. test('child component props update should not lead to double update', async () => {
  291. const text = ref(0)
  292. const spy = vi.fn()
  293. const App = {
  294. render() {
  295. return h(Comp, { text: text.value })
  296. }
  297. }
  298. const Comp = {
  299. props: ['text'],
  300. render(this: any) {
  301. spy()
  302. return h('h1', this.text)
  303. }
  304. }
  305. const root = nodeOps.createElement('div')
  306. render(h(App), root)
  307. expect(serializeInner(root)).toBe(`<h1>0</h1>`)
  308. expect(spy).toHaveBeenCalledTimes(1)
  309. text.value++
  310. await nextTick()
  311. expect(serializeInner(root)).toBe(`<h1>1</h1>`)
  312. expect(spy).toHaveBeenCalledTimes(2)
  313. })
  314. })