vnode.spec.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. import { createVNode } from '@vue/runtime-test'
  2. import {
  3. ShapeFlags,
  4. Comment,
  5. Fragment,
  6. Text,
  7. cloneVNode
  8. } from '@vue/runtime-core'
  9. import { mergeProps, normalizeVNode } from '../src/vnode'
  10. import { Data } from '../src/component'
  11. describe('vnode', () => {
  12. test('create with just tag', () => {
  13. const vnode = createVNode('p')
  14. expect(vnode.type).toBe('p')
  15. expect(vnode.props).toBe(null)
  16. })
  17. test('create with tag and props', () => {
  18. const vnode = createVNode('p', {})
  19. expect(vnode.type).toBe('p')
  20. expect(vnode.props).toMatchObject({})
  21. })
  22. test('create with tag, props and children', () => {
  23. const vnode = createVNode('p', {}, ['foo'])
  24. expect(vnode.type).toBe('p')
  25. expect(vnode.props).toMatchObject({})
  26. expect(vnode.children).toMatchObject(['foo'])
  27. })
  28. test('create with 0 as props', () => {
  29. const vnode = createVNode('p', null)
  30. expect(vnode.type).toBe('p')
  31. expect(vnode.props).toBe(null)
  32. })
  33. describe('class normalization', () => {
  34. test('string', () => {
  35. const vnode = createVNode('p', { class: 'foo baz' })
  36. expect(vnode.props).toMatchObject({ class: 'foo baz' })
  37. })
  38. test('array<string>', () => {
  39. const vnode = createVNode('p', { class: ['foo', 'baz'] })
  40. expect(vnode.props).toMatchObject({ class: 'foo baz' })
  41. })
  42. test('array<object>', () => {
  43. const vnode = createVNode('p', {
  44. class: [{ foo: 'foo' }, { baz: 'baz' }]
  45. })
  46. expect(vnode.props).toMatchObject({ class: 'foo baz' })
  47. })
  48. test('object', () => {
  49. const vnode = createVNode('p', { class: { foo: 'foo', baz: 'baz' } })
  50. expect(vnode.props).toMatchObject({ class: 'foo baz' })
  51. })
  52. })
  53. describe('style normalization', () => {
  54. test('array', () => {
  55. const vnode = createVNode('p', {
  56. style: [{ foo: 'foo' }, { baz: 'baz' }]
  57. })
  58. expect(vnode.props).toMatchObject({ style: { foo: 'foo', baz: 'baz' } })
  59. })
  60. test('object', () => {
  61. const vnode = createVNode('p', { style: { foo: 'foo', baz: 'baz' } })
  62. expect(vnode.props).toMatchObject({ style: { foo: 'foo', baz: 'baz' } })
  63. })
  64. })
  65. describe('children normalization', () => {
  66. const nop = jest.fn
  67. test('null', () => {
  68. const vnode = createVNode('p', null, null)
  69. expect(vnode.children).toBe(null)
  70. expect(vnode.shapeFlag).toBe(ShapeFlags.ELEMENT)
  71. })
  72. test('array', () => {
  73. const vnode = createVNode('p', null, ['foo'])
  74. expect(vnode.children).toMatchObject(['foo'])
  75. expect(vnode.shapeFlag).toBe(
  76. ShapeFlags.ELEMENT + ShapeFlags.ARRAY_CHILDREN
  77. )
  78. })
  79. test('object', () => {
  80. const vnode = createVNode('p', null, { foo: 'foo' })
  81. expect(vnode.children).toMatchObject({ foo: 'foo' })
  82. expect(vnode.shapeFlag).toBe(
  83. ShapeFlags.ELEMENT + ShapeFlags.SLOTS_CHILDREN
  84. )
  85. })
  86. test('function', () => {
  87. const vnode = createVNode('p', null, nop)
  88. expect(vnode.children).toMatchObject({ default: nop })
  89. expect(vnode.shapeFlag).toBe(
  90. ShapeFlags.ELEMENT + ShapeFlags.SLOTS_CHILDREN
  91. )
  92. })
  93. test('string', () => {
  94. const vnode = createVNode('p', null, 'foo')
  95. expect(vnode.children).toBe('foo')
  96. expect(vnode.shapeFlag).toBe(
  97. ShapeFlags.ELEMENT + ShapeFlags.TEXT_CHILDREN
  98. )
  99. })
  100. })
  101. test('normalizeVNode', () => {
  102. // null / undefined -> Comment
  103. expect(normalizeVNode(null)).toMatchObject({ type: Comment })
  104. expect(normalizeVNode(undefined)).toMatchObject({ type: Comment })
  105. // array -> Fragment
  106. expect(normalizeVNode(['foo'])).toMatchObject({ type: Fragment })
  107. // VNode -> VNode
  108. const vnode = createVNode('div')
  109. expect(normalizeVNode(vnode)).toBe(vnode)
  110. // mounted VNode -> cloned VNode
  111. const mounted = createVNode('div')
  112. mounted.el = {}
  113. const normalized = normalizeVNode(mounted)
  114. expect(normalized).not.toBe(mounted)
  115. expect(normalized).toEqual(mounted)
  116. // primitive types
  117. expect(normalizeVNode('foo')).toMatchObject({ type: Text, children: `foo` })
  118. expect(normalizeVNode(1)).toMatchObject({ type: Text, children: `1` })
  119. expect(normalizeVNode(true)).toMatchObject({ type: Text, children: `true` })
  120. })
  121. test('type shapeFlag inference', () => {
  122. expect(createVNode('div').shapeFlag).toBe(ShapeFlags.ELEMENT)
  123. expect(createVNode({}).shapeFlag).toBe(ShapeFlags.STATEFUL_COMPONENT)
  124. expect(createVNode(() => {}).shapeFlag).toBe(
  125. ShapeFlags.FUNCTIONAL_COMPONENT
  126. )
  127. expect(createVNode(Text).shapeFlag).toBe(0)
  128. })
  129. test('cloneVNode', () => {
  130. const node1 = createVNode('div', { foo: 1 }, null)
  131. expect(cloneVNode(node1)).toEqual(node1)
  132. const node2 = createVNode({}, null, [node1])
  133. const cloned2 = cloneVNode(node2)
  134. expect(cloned2).toEqual(node2)
  135. expect(cloneVNode(node2)).toEqual(node2)
  136. expect(cloneVNode(node2)).toEqual(cloned2)
  137. })
  138. describe('mergeProps', () => {
  139. test('class', () => {
  140. let props1: Data = { class: 'c' }
  141. let props2: Data = { class: ['cc'] }
  142. let props3: Data = { class: [{ ccc: true }] }
  143. let props4: Data = { class: { cccc: true } }
  144. expect(mergeProps(props1, props2, props3, props4)).toMatchObject({
  145. class: 'c cc ccc cccc'
  146. })
  147. })
  148. test('style', () => {
  149. let props1: Data = {
  150. style: {
  151. color: 'red',
  152. fontSize: 10
  153. }
  154. }
  155. let props2: Data = {
  156. style: [
  157. {
  158. color: 'blue',
  159. with: '200px'
  160. },
  161. {
  162. with: '300px',
  163. height: '300px',
  164. fontSize: 30
  165. }
  166. ]
  167. }
  168. expect(mergeProps(props1, props2)).toMatchObject({
  169. style: {
  170. color: 'blue',
  171. with: '300px',
  172. height: '300px',
  173. fontSize: 30
  174. }
  175. })
  176. })
  177. test('handlers', () => {
  178. let clickHander1 = function() {}
  179. let clickHander2 = function() {}
  180. let focusHander2 = function() {}
  181. let props1: Data = { onClick: clickHander1 }
  182. let props2: Data = { onClick: clickHander2, onFocus: focusHander2 }
  183. expect(mergeProps(props1, props2)).toMatchObject({
  184. onClick: [clickHander1, clickHander2],
  185. onFocus: focusHander2
  186. })
  187. })
  188. test('default', () => {
  189. let props1: Data = { foo: 'c' }
  190. let props2: Data = { foo: {}, bar: ['cc'] }
  191. let props3: Data = { baz: { ccc: true } }
  192. expect(mergeProps(props1, props2, props3)).toMatchObject({
  193. foo: {},
  194. bar: ['cc'],
  195. baz: { ccc: true }
  196. })
  197. })
  198. })
  199. })