rendererComponent.spec.ts 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. import {
  2. ref,
  3. h,
  4. render,
  5. nodeOps,
  6. serializeInner,
  7. nextTick,
  8. VNode,
  9. provide,
  10. inject,
  11. Ref,
  12. watch,
  13. SetupContext,
  14. computed
  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: jest.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 = jest.fn(returnThis)
  135. const dataWatchSpy = jest.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(() => props.value, (val: number) => emit('update', val))
  196. return () => {
  197. return h('div', props.value)
  198. }
  199. }
  200. }
  201. const root = nodeOps.createElement('div')
  202. render(h(App), root)
  203. expect(serializeInner(root)).toBe(`<div>0</div><div>0</div>`)
  204. outer.value++
  205. await nextTick()
  206. expect(serializeInner(root)).toBe(`<div>1</div><div>1</div>`)
  207. })
  208. // #2521
  209. test('should pause tracking deps when initializing legacy options', async () => {
  210. let childInstance = null as any
  211. const Child = {
  212. props: ['foo'],
  213. data() {
  214. return {
  215. count: 0
  216. }
  217. },
  218. watch: {
  219. foo: {
  220. immediate: true,
  221. handler() {
  222. ;(this as any).count
  223. }
  224. }
  225. },
  226. created() {
  227. childInstance = this as any
  228. childInstance.count
  229. },
  230. render() {
  231. return h('h1', (this as any).count)
  232. }
  233. }
  234. const App = {
  235. setup() {
  236. return () => h(Child)
  237. },
  238. updated: jest.fn()
  239. }
  240. const root = nodeOps.createElement('div')
  241. render(h(App), root)
  242. expect(App.updated).toHaveBeenCalledTimes(0)
  243. childInstance.count++
  244. await nextTick()
  245. expect(App.updated).toHaveBeenCalledTimes(0)
  246. })
  247. describe('render with access caches', () => {
  248. // #3297
  249. test('should not set the access cache in the data() function (production mode)', () => {
  250. const Comp = {
  251. data() {
  252. ;(this as any).foo
  253. return { foo: 1 }
  254. },
  255. render() {
  256. return h('h1', (this as any).foo)
  257. }
  258. }
  259. const root = nodeOps.createElement('div')
  260. __DEV__ = false
  261. render(h(Comp), root)
  262. __DEV__ = true
  263. expect(serializeInner(root)).toBe(`<h1>1</h1>`)
  264. })
  265. })
  266. test('the component VNode should be cloned when reusing it', () => {
  267. const App = {
  268. render() {
  269. const c = [h(Comp)]
  270. return [c, c, c]
  271. }
  272. }
  273. const ids: number[] = []
  274. const Comp = {
  275. render: () => h('h1'),
  276. beforeUnmount() {
  277. ids.push((this as any).$.uid)
  278. }
  279. }
  280. const root = nodeOps.createElement('div')
  281. render(h(App), root)
  282. expect(serializeInner(root)).toBe(`<h1></h1><h1></h1><h1></h1>`)
  283. render(null, root)
  284. expect(serializeInner(root)).toBe(``)
  285. expect(ids).toEqual([ids[0], ids[0] + 1, ids[0] + 2])
  286. })
  287. test('computed that did not change should not trigger re-render', async () => {
  288. const src = ref(0)
  289. const c = computed(() => src.value % 2)
  290. const spy = jest.fn()
  291. const App = {
  292. render() {
  293. spy()
  294. return c.value
  295. }
  296. }
  297. const root = nodeOps.createElement('div')
  298. render(h(App), root)
  299. expect(serializeInner(root)).toBe(`0`)
  300. expect(spy).toHaveBeenCalledTimes(1)
  301. // verify it updates
  302. src.value = 1
  303. src.value = 2
  304. src.value = 3
  305. await nextTick()
  306. expect(serializeInner(root)).toBe(`1`)
  307. expect(spy).toHaveBeenCalledTimes(2) // should only update once
  308. // verify it updates
  309. src.value = 4
  310. src.value = 5
  311. await nextTick()
  312. expect(serializeInner(root)).toBe(`1`)
  313. expect(spy).toHaveBeenCalledTimes(2) // should not need to update
  314. })
  315. })