h.spec.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import { h } from '../src/h'
  2. import { createVNode } from '../src/vnode'
  3. import type { RawSlots } from '../src/componentSlots'
  4. // Since h is a thin layer on top of createVNode, we are only testing its
  5. // own logic here. Details of vnode creation is tested in vnode.spec.ts.
  6. describe('renderer: h', () => {
  7. test('type only', () => {
  8. expect(h('div')).toMatchObject(createVNode('div'))
  9. })
  10. test('type + props', () => {
  11. expect(h('div', { id: 'foo' })).toMatchObject(
  12. createVNode('div', { id: 'foo' }),
  13. )
  14. })
  15. test('type + omit props', () => {
  16. // array
  17. expect(h('div', ['foo'])).toMatchObject(createVNode('div', null, ['foo']))
  18. // default slot
  19. const Component = { template: '<br />' }
  20. const slot = () => {}
  21. expect(h(Component, slot)).toMatchObject(createVNode(Component, null, slot))
  22. // single vnode
  23. const vnode = h('div')
  24. expect(h('div', vnode)).toMatchObject(createVNode('div', null, [vnode]))
  25. // text
  26. expect(h('div', 'foo')).toMatchObject(createVNode('div', null, 'foo'))
  27. })
  28. test('type + props + children', () => {
  29. // array
  30. expect(h('div', {}, ['foo'])).toMatchObject(createVNode('div', {}, ['foo']))
  31. // slots
  32. const slots = {} as RawSlots
  33. expect(h('div', {}, slots)).toMatchObject(createVNode('div', {}, slots))
  34. const Component = { template: '<br />' }
  35. expect(h(Component, {}, slots)).toMatchObject(
  36. createVNode(Component, {}, slots),
  37. )
  38. // default slot
  39. const slot = () => {}
  40. expect(h(Component, {}, slot)).toMatchObject(
  41. createVNode(Component, {}, slot),
  42. )
  43. // single vnode
  44. const vnode = h('div')
  45. expect(h('div', {}, vnode)).toMatchObject(createVNode('div', {}, [vnode]))
  46. // text
  47. expect(h('div', {}, 'foo')).toMatchObject(createVNode('div', {}, 'foo'))
  48. })
  49. test('named slots with null props', () => {
  50. const Component = { template: '<br />' }
  51. const slot = () => {}
  52. expect(
  53. h(Component, null, {
  54. foo: slot,
  55. }),
  56. ).toMatchObject(
  57. createVNode(Component, null, {
  58. foo: slot,
  59. }),
  60. )
  61. })
  62. // for simple JSX compat
  63. // note this signature is not supported in types; it's purely for usage with
  64. // compiled code.
  65. test('support variadic children', () => {
  66. // @ts-expect-error
  67. const vnode = h('div', null, h('span'), h('span'))
  68. expect(vnode.children).toMatchObject([
  69. {
  70. type: 'span',
  71. },
  72. {
  73. type: 'span',
  74. },
  75. ])
  76. })
  77. })