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