vnode.spec.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. import {
  2. createBlock,
  3. createVNode,
  4. openBlock,
  5. Comment,
  6. Fragment,
  7. Text,
  8. cloneVNode,
  9. mergeProps,
  10. normalizeVNode,
  11. transformVNodeArgs
  12. } from '../src/vnode'
  13. import { Data } from '../src/component'
  14. import { ShapeFlags, PatchFlags } from '@vue/shared'
  15. import { h, reactive, isReactive } from '../src'
  16. import { createApp, nodeOps, serializeInner } from '@vue/runtime-test'
  17. import { setCurrentRenderingInstance } from '../src/componentRenderUtils'
  18. describe('vnode', () => {
  19. test('create with just tag', () => {
  20. const vnode = createVNode('p')
  21. expect(vnode.type).toBe('p')
  22. expect(vnode.props).toBe(null)
  23. })
  24. test('create with tag and props', () => {
  25. const vnode = createVNode('p', {})
  26. expect(vnode.type).toBe('p')
  27. expect(vnode.props).toMatchObject({})
  28. })
  29. test('create with tag, props and children', () => {
  30. const vnode = createVNode('p', {}, ['foo'])
  31. expect(vnode.type).toBe('p')
  32. expect(vnode.props).toMatchObject({})
  33. expect(vnode.children).toMatchObject(['foo'])
  34. })
  35. test('create with 0 as props', () => {
  36. const vnode = createVNode('p', null)
  37. expect(vnode.type).toBe('p')
  38. expect(vnode.props).toBe(null)
  39. })
  40. test('vnode keys', () => {
  41. for (const key of ['', 'a', 0, 1, NaN]) {
  42. expect(createVNode('div', { key }).key).toBe(key)
  43. }
  44. expect(createVNode('div').key).toBe(null)
  45. expect(createVNode('div', { key: undefined }).key).toBe(null)
  46. })
  47. test('create with class component', () => {
  48. class Component {
  49. $props: any
  50. static __vccOpts = { template: '<div />' }
  51. }
  52. const vnode = createVNode(Component)
  53. expect(vnode.type).toEqual(Component.__vccOpts)
  54. })
  55. describe('class normalization', () => {
  56. test('string', () => {
  57. const vnode = createVNode('p', { class: 'foo baz' })
  58. expect(vnode.props).toMatchObject({ class: 'foo baz' })
  59. })
  60. test('array<string>', () => {
  61. const vnode = createVNode('p', { class: ['foo', 'baz'] })
  62. expect(vnode.props).toMatchObject({ class: 'foo baz' })
  63. })
  64. test('array<object>', () => {
  65. const vnode = createVNode('p', {
  66. class: [{ foo: 'foo' }, { baz: 'baz' }]
  67. })
  68. expect(vnode.props).toMatchObject({ class: 'foo baz' })
  69. })
  70. test('object', () => {
  71. const vnode = createVNode('p', { class: { foo: 'foo', baz: 'baz' } })
  72. expect(vnode.props).toMatchObject({ class: 'foo baz' })
  73. })
  74. })
  75. describe('style normalization', () => {
  76. test('array', () => {
  77. const vnode = createVNode('p', {
  78. style: [{ foo: 'foo' }, { baz: 'baz' }]
  79. })
  80. expect(vnode.props).toMatchObject({ style: { foo: 'foo', baz: 'baz' } })
  81. })
  82. test('object', () => {
  83. const vnode = createVNode('p', { style: { foo: 'foo', baz: 'baz' } })
  84. expect(vnode.props).toMatchObject({ style: { foo: 'foo', baz: 'baz' } })
  85. })
  86. })
  87. describe('children normalization', () => {
  88. const nop = jest.fn
  89. test('null', () => {
  90. const vnode = createVNode('p', null, null)
  91. expect(vnode.children).toBe(null)
  92. expect(vnode.shapeFlag).toBe(ShapeFlags.ELEMENT)
  93. })
  94. test('array', () => {
  95. const vnode = createVNode('p', null, ['foo'])
  96. expect(vnode.children).toMatchObject(['foo'])
  97. expect(vnode.shapeFlag).toBe(
  98. ShapeFlags.ELEMENT | ShapeFlags.ARRAY_CHILDREN
  99. )
  100. })
  101. test('object', () => {
  102. const vnode = createVNode('p', null, { foo: 'foo' })
  103. expect(vnode.children).toMatchObject({ foo: 'foo' })
  104. expect(vnode.shapeFlag).toBe(
  105. ShapeFlags.ELEMENT | ShapeFlags.SLOTS_CHILDREN
  106. )
  107. })
  108. test('function', () => {
  109. const vnode = createVNode('p', null, nop)
  110. expect(vnode.children).toMatchObject({ default: nop })
  111. expect(vnode.shapeFlag).toBe(
  112. ShapeFlags.ELEMENT | ShapeFlags.SLOTS_CHILDREN
  113. )
  114. })
  115. test('string', () => {
  116. const vnode = createVNode('p', null, 'foo')
  117. expect(vnode.children).toBe('foo')
  118. expect(vnode.shapeFlag).toBe(
  119. ShapeFlags.ELEMENT | ShapeFlags.TEXT_CHILDREN
  120. )
  121. })
  122. test('element with slots', () => {
  123. const children = [createVNode('span', null, 'hello')]
  124. const vnode = createVNode('div', null, {
  125. default: () => children
  126. })
  127. expect(vnode.children).toBe(children)
  128. expect(vnode.shapeFlag).toBe(
  129. ShapeFlags.ELEMENT | ShapeFlags.ARRAY_CHILDREN
  130. )
  131. })
  132. })
  133. test('normalizeVNode', () => {
  134. // null / undefined -> Comment
  135. expect(normalizeVNode(null)).toMatchObject({ type: Comment })
  136. expect(normalizeVNode(undefined)).toMatchObject({ type: Comment })
  137. // boolean -> Comment
  138. // this is for usage like `someBoolean && h('div')` and behavior consistency
  139. // with 2.x (#574)
  140. expect(normalizeVNode(true)).toMatchObject({ type: Comment })
  141. expect(normalizeVNode(false)).toMatchObject({ type: Comment })
  142. // array -> Fragment
  143. expect(normalizeVNode(['foo'])).toMatchObject({ type: Fragment })
  144. // VNode -> VNode
  145. const vnode = createVNode('div')
  146. expect(normalizeVNode(vnode)).toBe(vnode)
  147. // mounted VNode -> cloned VNode
  148. const mounted = createVNode('div')
  149. mounted.el = {}
  150. const normalized = normalizeVNode(mounted)
  151. expect(normalized).not.toBe(mounted)
  152. expect(normalized).toEqual(mounted)
  153. // primitive types
  154. expect(normalizeVNode('foo')).toMatchObject({ type: Text, children: `foo` })
  155. expect(normalizeVNode(1)).toMatchObject({ type: Text, children: `1` })
  156. })
  157. test('type shapeFlag inference', () => {
  158. expect(createVNode('div').shapeFlag).toBe(ShapeFlags.ELEMENT)
  159. expect(createVNode({}).shapeFlag).toBe(ShapeFlags.STATEFUL_COMPONENT)
  160. expect(createVNode(() => {}).shapeFlag).toBe(
  161. ShapeFlags.FUNCTIONAL_COMPONENT
  162. )
  163. expect(createVNode(Text).shapeFlag).toBe(0)
  164. })
  165. test('cloneVNode', () => {
  166. const node1 = createVNode('div', { foo: 1 }, null)
  167. expect(cloneVNode(node1)).toEqual(node1)
  168. const node2 = createVNode({}, null, [node1])
  169. const cloned2 = cloneVNode(node2)
  170. expect(cloned2).toEqual(node2)
  171. expect(cloneVNode(node2)).toEqual(node2)
  172. expect(cloneVNode(node2)).toEqual(cloned2)
  173. })
  174. test('cloneVNode key normalization', () => {
  175. // #1041 should use resolved key/ref
  176. expect(cloneVNode(createVNode('div', { key: 1 })).key).toBe(1)
  177. expect(cloneVNode(createVNode('div', { key: 1 }), { key: 2 }).key).toBe(2)
  178. expect(cloneVNode(createVNode('div'), { key: 2 }).key).toBe(2)
  179. })
  180. // ref normalizes to [currentRenderingInstance, ref]
  181. test('cloneVNode ref normalization', () => {
  182. const mockInstance1 = {} as any
  183. const mockInstance2 = {} as any
  184. setCurrentRenderingInstance(mockInstance1)
  185. const original = createVNode('div', { ref: 'foo' })
  186. expect(original.ref).toEqual([mockInstance1, 'foo'])
  187. // clone and preserve original ref
  188. const cloned1 = cloneVNode(original)
  189. expect(cloned1.ref).toEqual([mockInstance1, 'foo'])
  190. // cloning with new ref, but with same context instance
  191. const cloned2 = cloneVNode(original, { ref: 'bar' })
  192. expect(cloned2.ref).toEqual([mockInstance1, 'bar'])
  193. // cloning and adding ref to original that has no ref
  194. const original2 = createVNode('div')
  195. const cloned3 = cloneVNode(original2, { ref: 'bar' })
  196. expect(cloned3.ref).toEqual([mockInstance1, 'bar'])
  197. // cloning with different context instance
  198. setCurrentRenderingInstance(mockInstance2)
  199. // clone and preserve original ref
  200. const cloned4 = cloneVNode(original)
  201. // #1311 should preserve original context instance!
  202. expect(cloned4.ref).toEqual([mockInstance1, 'foo'])
  203. // cloning with new ref, but with same context instance
  204. const cloned5 = cloneVNode(original, { ref: 'bar' })
  205. // new ref should use current context instance and overwrite orgiinal
  206. expect(cloned5.ref).toEqual([mockInstance2, 'bar'])
  207. // cloning and adding ref to original that has no ref
  208. const cloned6 = cloneVNode(original2, { ref: 'bar' })
  209. expect(cloned6.ref).toEqual([mockInstance2, 'bar'])
  210. setCurrentRenderingInstance(null)
  211. })
  212. describe('mergeProps', () => {
  213. test('class', () => {
  214. let props1: Data = { class: 'c' }
  215. let props2: Data = { class: ['cc'] }
  216. let props3: Data = { class: [{ ccc: true }] }
  217. let props4: Data = { class: { cccc: true } }
  218. expect(mergeProps(props1, props2, props3, props4)).toMatchObject({
  219. class: 'c cc ccc cccc'
  220. })
  221. })
  222. test('style', () => {
  223. let props1: Data = {
  224. style: {
  225. color: 'red',
  226. fontSize: 10
  227. }
  228. }
  229. let props2: Data = {
  230. style: [
  231. {
  232. color: 'blue',
  233. width: '200px'
  234. },
  235. {
  236. width: '300px',
  237. height: '300px',
  238. fontSize: 30
  239. }
  240. ]
  241. }
  242. expect(mergeProps(props1, props2)).toMatchObject({
  243. style: {
  244. color: 'blue',
  245. width: '300px',
  246. height: '300px',
  247. fontSize: 30
  248. }
  249. })
  250. })
  251. test('style w/ strings', () => {
  252. let props1: Data = {
  253. style: 'width:100px;right:10;top:10'
  254. }
  255. let props2: Data = {
  256. style: [
  257. {
  258. color: 'blue',
  259. width: '200px'
  260. },
  261. {
  262. width: '300px',
  263. height: '300px',
  264. fontSize: 30
  265. }
  266. ]
  267. }
  268. expect(mergeProps(props1, props2)).toMatchObject({
  269. style: {
  270. color: 'blue',
  271. width: '300px',
  272. height: '300px',
  273. fontSize: 30,
  274. right: '10',
  275. top: '10'
  276. }
  277. })
  278. })
  279. test('handlers', () => {
  280. let clickHandler1 = function() {}
  281. let clickHandler2 = function() {}
  282. let focusHandler2 = function() {}
  283. let props1: Data = { onClick: clickHandler1 }
  284. let props2: Data = { onClick: clickHandler2, onFocus: focusHandler2 }
  285. expect(mergeProps(props1, props2)).toMatchObject({
  286. onClick: [clickHandler1, clickHandler2],
  287. onFocus: focusHandler2
  288. })
  289. })
  290. test('default', () => {
  291. let props1: Data = { foo: 'c' }
  292. let props2: Data = { foo: {}, bar: ['cc'] }
  293. let props3: Data = { baz: { ccc: true } }
  294. expect(mergeProps(props1, props2, props3)).toMatchObject({
  295. foo: {},
  296. bar: ['cc'],
  297. baz: { ccc: true }
  298. })
  299. })
  300. })
  301. describe('dynamic children', () => {
  302. test('with patchFlags', () => {
  303. const hoist = createVNode('div')
  304. let vnode1
  305. const vnode = (openBlock(),
  306. createBlock('div', null, [
  307. hoist,
  308. (vnode1 = createVNode('div', null, 'text', PatchFlags.TEXT))
  309. ]))
  310. expect(vnode.dynamicChildren).toStrictEqual([vnode1])
  311. })
  312. test('should not track vnodes with only HYDRATE_EVENTS flag', () => {
  313. const hoist = createVNode('div')
  314. const vnode = (openBlock(),
  315. createBlock('div', null, [
  316. hoist,
  317. createVNode('div', null, 'text', PatchFlags.HYDRATE_EVENTS)
  318. ]))
  319. expect(vnode.dynamicChildren).toStrictEqual([])
  320. })
  321. test('many times call openBlock', () => {
  322. const hoist = createVNode('div')
  323. let vnode1, vnode2, vnode3
  324. const vnode = (openBlock(),
  325. createBlock('div', null, [
  326. hoist,
  327. (vnode1 = createVNode('div', null, 'text', PatchFlags.TEXT)),
  328. (vnode2 = (openBlock(),
  329. createBlock('div', null, [
  330. hoist,
  331. (vnode3 = createVNode('div', null, 'text', PatchFlags.TEXT))
  332. ])))
  333. ]))
  334. expect(vnode.dynamicChildren).toStrictEqual([vnode1, vnode2])
  335. expect(vnode2.dynamicChildren).toStrictEqual([vnode3])
  336. })
  337. test('with stateful component', () => {
  338. const hoist = createVNode('div')
  339. let vnode1
  340. const vnode = (openBlock(),
  341. createBlock('div', null, [
  342. hoist,
  343. (vnode1 = createVNode({}, null, 'text'))
  344. ]))
  345. expect(vnode.dynamicChildren).toStrictEqual([vnode1])
  346. })
  347. test('with functional component', () => {
  348. const hoist = createVNode('div')
  349. let vnode1
  350. const vnode = (openBlock(),
  351. createBlock('div', null, [
  352. hoist,
  353. (vnode1 = createVNode(() => {}, null, 'text'))
  354. ]))
  355. expect(vnode.dynamicChildren).toStrictEqual([vnode1])
  356. })
  357. test('with suspense', () => {
  358. const hoist = createVNode('div')
  359. let vnode1
  360. const vnode = (openBlock(),
  361. createBlock('div', null, [
  362. hoist,
  363. (vnode1 = createVNode(() => {}, null, 'text'))
  364. ]))
  365. expect(vnode.dynamicChildren).toStrictEqual([vnode1])
  366. })
  367. // #1039
  368. // <component :is="foo">{{ bar }}</component>
  369. // - content is compiled as slot
  370. // - dynamic component resolves to plain element, but as a block
  371. // - block creation disables its own tracking, accidentally causing the
  372. // slot content (called during the block node creation) to be missed
  373. test('element block should track normalized slot children', () => {
  374. const hoist = createVNode('div')
  375. let vnode1
  376. const vnode = (openBlock(),
  377. createBlock('div', null, {
  378. default: () => {
  379. return [
  380. hoist,
  381. (vnode1 = createVNode('div', null, 'text', PatchFlags.TEXT))
  382. ]
  383. }
  384. }))
  385. expect(vnode.dynamicChildren).toStrictEqual([vnode1])
  386. })
  387. test('openBlock w/ disableTracking: true', () => {
  388. const hoist = createVNode('div')
  389. let vnode1
  390. const vnode = (openBlock(),
  391. createBlock('div', null, [
  392. // a v-for fragment block generated by the compiler
  393. // disables tracking because it always diffs its
  394. // children.
  395. (vnode1 = (openBlock(true),
  396. createBlock(Fragment, null, [
  397. hoist,
  398. /*vnode2*/ createVNode(() => {}, null, 'text')
  399. ])))
  400. ]))
  401. expect(vnode.dynamicChildren).toStrictEqual([vnode1])
  402. expect(vnode1.dynamicChildren).toStrictEqual([])
  403. })
  404. test('openBlock without disableTracking: true', () => {
  405. const hoist = createVNode('div')
  406. let vnode1, vnode2
  407. const vnode = (openBlock(),
  408. createBlock('div', null, [
  409. (vnode1 = (openBlock(),
  410. createBlock(Fragment, null, [
  411. hoist,
  412. (vnode2 = createVNode(() => {}, null, 'text'))
  413. ])))
  414. ]))
  415. expect(vnode.dynamicChildren).toStrictEqual([vnode1])
  416. expect(vnode1.dynamicChildren).toStrictEqual([vnode2])
  417. })
  418. })
  419. describe('transformVNodeArgs', () => {
  420. afterEach(() => {
  421. // reset
  422. transformVNodeArgs()
  423. })
  424. test('no-op pass through', () => {
  425. transformVNodeArgs(args => args)
  426. const vnode = createVNode('div', { id: 'foo' }, 'hello')
  427. expect(vnode).toMatchObject({
  428. type: 'div',
  429. props: { id: 'foo' },
  430. children: 'hello',
  431. shapeFlag: ShapeFlags.ELEMENT | ShapeFlags.TEXT_CHILDREN
  432. })
  433. })
  434. test('direct override', () => {
  435. transformVNodeArgs(() => ['div', { id: 'foo' }, 'hello'])
  436. const vnode = createVNode('p')
  437. expect(vnode).toMatchObject({
  438. type: 'div',
  439. props: { id: 'foo' },
  440. children: 'hello',
  441. shapeFlag: ShapeFlags.ELEMENT | ShapeFlags.TEXT_CHILDREN
  442. })
  443. })
  444. test('receive component instance as 2nd arg', () => {
  445. transformVNodeArgs((args, instance) => {
  446. if (instance) {
  447. return ['h1', null, instance.type.name]
  448. } else {
  449. return args
  450. }
  451. })
  452. const App = {
  453. // this will be the name of the component in the h1
  454. name: 'Root Component',
  455. render() {
  456. return h('p') // this will be overwritten by the transform
  457. }
  458. }
  459. const root = nodeOps.createElement('div')
  460. createApp(App).mount(root)
  461. expect(serializeInner(root)).toBe('<h1>Root Component</h1>')
  462. })
  463. test('should not be observable', () => {
  464. const a = createVNode('div')
  465. const b = reactive(a)
  466. expect(b).toBe(a)
  467. expect(isReactive(b)).toBe(false)
  468. })
  469. })
  470. })