vnode.spec.ts 16 KB

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