h.test-d.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import { describe } from './util'
  2. import { expectError } from 'tsd'
  3. import { h, createComponent, ref, Fragment, Portal, Suspense } from './index'
  4. describe('h inference w/ element', () => {
  5. // key
  6. h('div', { key: 1 })
  7. h('div', { key: 'foo' })
  8. expectError(h('div', { key: [] }))
  9. expectError(h('div', { key: {} }))
  10. // ref
  11. h('div', { ref: 'foo' })
  12. h('div', { ref: ref(null) })
  13. h('div', { ref: el => {} })
  14. expectError(h('div', { ref: [] }))
  15. expectError(h('div', { ref: {} }))
  16. expectError(h('div', { ref: 123 }))
  17. })
  18. describe('h inference w/ Fragment', () => {
  19. // only accepts array children
  20. h(Fragment, ['hello'])
  21. h(Fragment, { key: 123 }, ['hello'])
  22. expectError(h(Fragment, 'foo'))
  23. expectError(h(Fragment, { key: 123 }, 'bar'))
  24. })
  25. describe('h inference w/ Portal', () => {
  26. h(Portal, { target: '#foo' }, 'hello')
  27. expectError(h(Portal))
  28. expectError(h(Portal, {}))
  29. expectError(h(Portal, { target: '#foo' }))
  30. })
  31. describe('h inference w/ Suspense', () => {
  32. h(Suspense, { onRecede: () => {}, onResolve: () => {} }, 'hello')
  33. h(Suspense, 'foo')
  34. h(Suspense, () => 'foo')
  35. h(Suspense, null, {
  36. default: () => 'foo'
  37. })
  38. expectError(h(Suspense, { onResolve: 1 }))
  39. })
  40. describe('h inference w/ functional component', () => {
  41. const Func = (_props: { foo: string; bar?: number }) => ''
  42. h(Func, { foo: 'hello' })
  43. h(Func, { foo: 'hello', bar: 123 })
  44. expectError(h(Func, { foo: 123 }))
  45. expectError(h(Func, {}))
  46. expectError(h(Func, { bar: 123 }))
  47. })
  48. describe('h inference w/ plain object component', () => {
  49. const Foo = {
  50. props: {
  51. foo: String
  52. }
  53. }
  54. h(Foo, { foo: 'ok' })
  55. h(Foo, { foo: 'ok', class: 'extra' })
  56. // should fail on wrong type
  57. expectError(h(Foo, { foo: 1 }))
  58. })
  59. describe('h inference w/ createComponent', () => {
  60. const Foo = createComponent({
  61. props: {
  62. foo: String,
  63. bar: {
  64. type: Number,
  65. required: true
  66. }
  67. }
  68. })
  69. h(Foo, { bar: 1 })
  70. h(Foo, { bar: 1, foo: 'ok' })
  71. // should allow extraneous props (attrs fallthrough)
  72. h(Foo, { bar: 1, foo: 'ok', class: 'extra' })
  73. // should fail on missing required prop
  74. expectError(h(Foo, {}))
  75. expectError(h(Foo, { foo: 'ok' }))
  76. // should fail on wrong type
  77. expectError(h(Foo, { bar: 1, foo: 1 }))
  78. })
  79. describe('h inference w/ createComponent + optional props', () => {
  80. const Foo = createComponent({
  81. setup(_props: { foo?: string; bar: number }) {}
  82. })
  83. h(Foo, { bar: 1 })
  84. h(Foo, { bar: 1, foo: 'ok' })
  85. // should allow extraneous props (attrs fallthrough)
  86. h(Foo, { bar: 1, foo: 'ok', class: 'extra' })
  87. // should fail on missing required prop
  88. expectError(h(Foo, {}))
  89. expectError(h(Foo, { foo: 'ok' }))
  90. // should fail on wrong type
  91. expectError(h(Foo, { bar: 1, foo: 1 }))
  92. })
  93. describe('h inference w/ createComponent + direct function', () => {
  94. const Foo = createComponent((_props: { foo?: string; bar: number }) => {})
  95. h(Foo, { bar: 1 })
  96. h(Foo, { bar: 1, foo: 'ok' })
  97. // should allow extraneous props (attrs fallthrough)
  98. h(Foo, { bar: 1, foo: 'ok', class: 'extra' })
  99. // should fail on missing required prop
  100. expectError(h(Foo, {}))
  101. expectError(h(Foo, { foo: 'ok' }))
  102. // should fail on wrong type
  103. expectError(h(Foo, { bar: 1, foo: 1 }))
  104. })