h.test-d.ts 3.2 KB

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