vnode.spec.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import { createVNode } from '@vue/runtime-test'
  2. import { ShapeFlags } from '@vue/runtime-core'
  3. describe('vnode', () => {
  4. test('create with just tag', () => {
  5. const vnode = createVNode('p')
  6. expect(vnode.type).toBe('p')
  7. expect(vnode.props).toBe(null)
  8. })
  9. test('create with tag and props', () => {
  10. const vnode = createVNode('p', {})
  11. expect(vnode.type).toBe('p')
  12. expect(vnode.props).toMatchObject({})
  13. })
  14. test('create with tag, props and children', () => {
  15. const vnode = createVNode('p', {}, ['foo'])
  16. expect(vnode.type).toBe('p')
  17. expect(vnode.props).toMatchObject({})
  18. expect(vnode.children).toMatchObject(['foo'])
  19. })
  20. test('create with 0 as props', () => {
  21. const vnode = createVNode('p', null)
  22. expect(vnode.type).toBe('p')
  23. expect(vnode.props).toBe(null)
  24. })
  25. describe('class normalization', () => {
  26. test('string', () => {
  27. const vnode = createVNode('p', { class: 'foo baz' })
  28. expect(vnode.props).toMatchObject({ class: 'foo baz' })
  29. })
  30. test('array<string>', () => {
  31. const vnode = createVNode('p', { class: ['foo', 'baz'] })
  32. expect(vnode.props).toMatchObject({ class: 'foo baz' })
  33. })
  34. test('array<object>', () => {
  35. const vnode = createVNode('p', {
  36. class: [{ foo: 'foo' }, { baz: 'baz' }]
  37. })
  38. expect(vnode.props).toMatchObject({ class: 'foo baz' })
  39. })
  40. test('object', () => {
  41. const vnode = createVNode('p', { class: { foo: 'foo', baz: 'baz' } })
  42. expect(vnode.props).toMatchObject({ class: 'foo baz' })
  43. })
  44. })
  45. describe('style normalization', () => {
  46. test('array', () => {
  47. const vnode = createVNode('p', {
  48. style: [{ foo: 'foo' }, { baz: 'baz' }]
  49. })
  50. expect(vnode.props).toMatchObject({ style: { foo: 'foo', baz: 'baz' } })
  51. })
  52. test('object', () => {
  53. const vnode = createVNode('p', { style: { foo: 'foo', baz: 'baz' } })
  54. expect(vnode.props).toMatchObject({ style: { foo: 'foo', baz: 'baz' } })
  55. })
  56. })
  57. describe('children normalization', () => {
  58. const nop = jest.fn
  59. test('null', () => {
  60. const vnode = createVNode('p', null, null)
  61. expect(vnode.children).toBe(null)
  62. expect(vnode.shapeFlag).toBe(ShapeFlags.ELEMENT)
  63. })
  64. test('array', () => {
  65. const vnode = createVNode('p', null, ['foo'])
  66. expect(vnode.children).toMatchObject(['foo'])
  67. expect(vnode.shapeFlag).toBe(
  68. ShapeFlags.ELEMENT + ShapeFlags.ARRAY_CHILDREN
  69. )
  70. })
  71. test('object', () => {
  72. const vnode = createVNode('p', null, { foo: 'foo' })
  73. expect(vnode.children).toMatchObject({ foo: 'foo' })
  74. expect(vnode.shapeFlag).toBe(
  75. ShapeFlags.ELEMENT + ShapeFlags.SLOTS_CHILDREN
  76. )
  77. })
  78. test('function', () => {
  79. const vnode = createVNode('p', null, nop)
  80. expect(vnode.children).toMatchObject({ default: nop })
  81. expect(vnode.shapeFlag).toBe(
  82. ShapeFlags.ELEMENT + ShapeFlags.SLOTS_CHILDREN
  83. )
  84. })
  85. test('string', () => {
  86. const vnode = createVNode('p', null, 'foo')
  87. expect(vnode.children).toBe('foo')
  88. expect(vnode.shapeFlag).toBe(
  89. ShapeFlags.ELEMENT + ShapeFlags.TEXT_CHILDREN
  90. )
  91. })
  92. })
  93. test.todo('normalizeVNode')
  94. test.todo('node type/shapeFlag inference')
  95. test.todo('cloneVNode')
  96. test.todo('mergeProps')
  97. })