h.test-d.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. import {
  2. describe,
  3. h,
  4. defineComponent,
  5. ref,
  6. Fragment,
  7. Teleport,
  8. Suspense,
  9. Component,
  10. expectError,
  11. expectAssignable,
  12. resolveComponent
  13. } from './index'
  14. describe('h inference w/ element', () => {
  15. // key
  16. h('div', { key: 1 })
  17. h('div', { key: 'foo' })
  18. // @ts-expect-error
  19. expectError(h('div', { key: [] }))
  20. // @ts-expect-error
  21. expectError(h('div', { key: {} }))
  22. // ref
  23. h('div', { ref: 'foo' })
  24. h('div', { ref: ref(null) })
  25. h('div', { ref: _el => {} })
  26. // @ts-expect-error
  27. expectError(h('div', { ref: [] }))
  28. // @ts-expect-error
  29. expectError(h('div', { ref: {} }))
  30. // @ts-expect-error
  31. expectError(h('div', { ref: 123 }))
  32. // slots
  33. const slots = { default: () => {} } // RawSlots
  34. h('div', {}, slots)
  35. })
  36. describe('h inference w/ Fragment', () => {
  37. // only accepts array children
  38. h(Fragment, ['hello'])
  39. h(Fragment, { key: 123 }, ['hello'])
  40. // @ts-expect-error
  41. expectError(h(Fragment, 'foo'))
  42. // @ts-expect-error
  43. expectError(h(Fragment, { key: 123 }, 'bar'))
  44. })
  45. describe('h inference w/ Teleport', () => {
  46. h(Teleport, { to: '#foo' }, 'hello')
  47. // @ts-expect-error
  48. expectError(h(Teleport))
  49. // @ts-expect-error
  50. expectError(h(Teleport, {}))
  51. // @ts-expect-error
  52. expectError(h(Teleport, { to: '#foo' }))
  53. })
  54. describe('h inference w/ Suspense', () => {
  55. h(Suspense, { onRecede: () => {}, onResolve: () => {} }, 'hello')
  56. h(Suspense, 'foo')
  57. h(Suspense, () => 'foo')
  58. h(Suspense, null, {
  59. default: () => 'foo'
  60. })
  61. // @ts-expect-error
  62. expectError(h(Suspense, { onResolve: 1 }))
  63. })
  64. describe('h inference w/ functional component', () => {
  65. const Func = (_props: { foo: string; bar?: number }) => ''
  66. h(Func, { foo: 'hello' })
  67. h(Func, { foo: 'hello', bar: 123 })
  68. // @ts-expect-error
  69. expectError(h(Func, { foo: 123 }))
  70. // @ts-expect-error
  71. expectError(h(Func, {}))
  72. // @ts-expect-error
  73. expectError(h(Func, { bar: 123 }))
  74. })
  75. describe('h support w/ plain object component', () => {
  76. const Foo = {
  77. props: {
  78. foo: String
  79. }
  80. }
  81. h(Foo, { foo: 'ok' })
  82. h(Foo, { foo: 'ok', class: 'extra' })
  83. // no inference in this case
  84. })
  85. describe('h inference w/ defineComponent', () => {
  86. const Foo = defineComponent({
  87. props: {
  88. foo: String,
  89. bar: {
  90. type: Number,
  91. required: true
  92. }
  93. }
  94. })
  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. // @ts-expect-error should fail on missing required prop
  100. expectError(h(Foo, {}))
  101. // @ts-expect-error
  102. expectError(h(Foo, { foo: 'ok' }))
  103. // @ts-expect-error should fail on wrong type
  104. expectError(h(Foo, { bar: 1, foo: 1 }))
  105. })
  106. // describe('h inference w/ defineComponent + optional props', () => {
  107. // const Foo = defineComponent({
  108. // setup(_props: { foo?: string; bar: number }) {}
  109. // })
  110. // h(Foo, { bar: 1 })
  111. // h(Foo, { bar: 1, foo: 'ok' })
  112. // // should allow extraneous props (attrs fallthrough)
  113. // h(Foo, { bar: 1, foo: 'ok', class: 'extra' })
  114. // // @ts-expect-error should fail on missing required prop
  115. // expectError(h(Foo, {}))
  116. // // @ts-expect-error
  117. // expectError(h(Foo, { foo: 'ok' }))
  118. // // @ts-expect-error should fail on wrong type
  119. // expectError(h(Foo, { bar: 1, foo: 1 }))
  120. // })
  121. // describe('h inference w/ defineComponent + direct function', () => {
  122. // const Foo = defineComponent((_props: { foo?: string; bar: number }) => {})
  123. // h(Foo, { bar: 1 })
  124. // h(Foo, { bar: 1, foo: 'ok' })
  125. // // should allow extraneous props (attrs fallthrough)
  126. // h(Foo, { bar: 1, foo: 'ok', class: 'extra' })
  127. // // @ts-expect-error should fail on missing required prop
  128. // expectError(h(Foo, {}))
  129. // // @ts-expect-error
  130. // expectError(h(Foo, { foo: 'ok' }))
  131. // // @ts-expect-error should fail on wrong type
  132. // expectError(h(Foo, { bar: 1, foo: 1 }))
  133. // })
  134. // #922
  135. describe('h support for generic component type', () => {
  136. function foo(bar: Component) {
  137. h(bar)
  138. h(bar, 'hello')
  139. // @ts-expect-error
  140. h(bar, { id: 'ok' }, 'hello')
  141. }
  142. foo({})
  143. })
  144. // #993
  145. describe('describeComponent extends Component', () => {
  146. // functional
  147. expectAssignable<Component>(
  148. defineComponent((_props: { foo?: string; bar: number }) => {})
  149. )
  150. // typed props
  151. expectAssignable<Component>(defineComponent({}))
  152. // prop arrays
  153. expectAssignable<Component>(
  154. defineComponent({
  155. props: ['a', 'b']
  156. })
  157. )
  158. // prop object
  159. expectAssignable<Component>(
  160. defineComponent({
  161. props: {
  162. foo: String,
  163. bar: {
  164. type: Number,
  165. required: true
  166. }
  167. }
  168. })
  169. )
  170. })
  171. // #1385
  172. describe('component w/ props w/ default value', () => {
  173. const MyComponent = defineComponent({
  174. props: {
  175. message: {
  176. type: String,
  177. default: 'hello'
  178. }
  179. }
  180. })
  181. h(MyComponent, {})
  182. })
  183. // #2338
  184. describe('Boolean prop implicit false', () => {
  185. const MyComponent = defineComponent({
  186. props: {
  187. visible: Boolean
  188. }
  189. })
  190. h(MyComponent, {})
  191. const RequiredComponent = defineComponent({
  192. props: {
  193. visible: {
  194. type: Boolean,
  195. required: true
  196. }
  197. }
  198. })
  199. h(RequiredComponent, {
  200. visible: true
  201. })
  202. // @ts-expect-error
  203. expectError(h(RequiredComponent, {}))
  204. })
  205. // #2357
  206. describe('resolveComponent should work', () => {
  207. h(resolveComponent('test'))
  208. h(resolveComponent('test'), {
  209. message: '1'
  210. })
  211. })