rendererComponent.spec.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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(() => props.value, (val: number) => emit('update', val))
  195. return () => {
  196. return h('div', props.value)
  197. }
  198. }
  199. }
  200. const root = nodeOps.createElement('div')
  201. render(h(App), root)
  202. expect(serializeInner(root)).toBe(`<div>0</div><div>0</div>`)
  203. outer.value++
  204. await nextTick()
  205. expect(serializeInner(root)).toBe(`<div>1</div><div>1</div>`)
  206. })
  207. // #2521
  208. test('should pause tracking deps when initializing legacy options', async () => {
  209. let childInstance = null as any
  210. const Child = {
  211. props: ['foo'],
  212. data() {
  213. return {
  214. count: 0
  215. }
  216. },
  217. watch: {
  218. foo: {
  219. immediate: true,
  220. handler() {
  221. ;(this as any).count
  222. }
  223. }
  224. },
  225. created() {
  226. childInstance = this as any
  227. childInstance.count
  228. },
  229. render() {
  230. return h('h1', (this as any).count)
  231. }
  232. }
  233. const App = {
  234. setup() {
  235. return () => h(Child)
  236. },
  237. updated: jest.fn()
  238. }
  239. const root = nodeOps.createElement('div')
  240. render(h(App), root)
  241. expect(App.updated).toHaveBeenCalledTimes(0)
  242. childInstance.count++
  243. await nextTick()
  244. expect(App.updated).toHaveBeenCalledTimes(0)
  245. })
  246. describe('render with access caches', () => {
  247. // #3297
  248. test('should not set the access cache in the data() function (production mode)', () => {
  249. const Comp = {
  250. data() {
  251. ;(this as any).foo
  252. return { foo: 1 }
  253. },
  254. render() {
  255. return h('h1', (this as any).foo)
  256. }
  257. }
  258. const root = nodeOps.createElement('div')
  259. __DEV__ = false
  260. render(h(Comp), root)
  261. __DEV__ = true
  262. expect(serializeInner(root)).toBe(`<h1>1</h1>`)
  263. })
  264. })
  265. })