You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

164 lines
3.9 KiB

6 years ago
6 years ago
6 years ago
  1. import { expectError, expectAssignable } from 'tsd'
  2. import {
  3. describe,
  4. h,
  5. defineComponent,
  6. ref,
  7. Fragment,
  8. Teleport,
  9. Suspense,
  10. Component
  11. } from './index'
  12. describe('h inference w/ element', () => {
  13. // key
  14. h('div', { key: 1 })
  15. h('div', { key: 'foo' })
  16. expectError(h('div', { key: [] }))
  17. expectError(h('div', { key: {} }))
  18. // ref
  19. h('div', { ref: 'foo' })
  20. h('div', { ref: ref(null) })
  21. h('div', { ref: el => {} })
  22. expectError(h('div', { ref: [] }))
  23. expectError(h('div', { ref: {} }))
  24. expectError(h('div', { ref: 123 }))
  25. })
  26. describe('h inference w/ Fragment', () => {
  27. // only accepts array children
  28. h(Fragment, ['hello'])
  29. h(Fragment, { key: 123 }, ['hello'])
  30. expectError(h(Fragment, 'foo'))
  31. expectError(h(Fragment, { key: 123 }, 'bar'))
  32. })
  33. describe('h inference w/ Teleport', () => {
  34. h(Teleport, { to: '#foo' }, 'hello')
  35. expectError(h(Teleport))
  36. expectError(h(Teleport, {}))
  37. expectError(h(Teleport, { to: '#foo' }))
  38. })
  39. describe('h inference w/ Suspense', () => {
  40. h(Suspense, { onRecede: () => {}, onResolve: () => {} }, 'hello')
  41. h(Suspense, 'foo')
  42. h(Suspense, () => 'foo')
  43. h(Suspense, null, {
  44. default: () => 'foo'
  45. })
  46. expectError(h(Suspense, { onResolve: 1 }))
  47. })
  48. describe('h inference w/ functional component', () => {
  49. const Func = (_props: { foo: string; bar?: number }) => ''
  50. h(Func, { foo: 'hello' })
  51. h(Func, { foo: 'hello', bar: 123 })
  52. expectError(h(Func, { foo: 123 }))
  53. expectError(h(Func, {}))
  54. expectError(h(Func, { bar: 123 }))
  55. })
  56. describe('h support w/ plain object component', () => {
  57. const Foo = {
  58. props: {
  59. foo: String
  60. }
  61. }
  62. h(Foo, { foo: 'ok' })
  63. h(Foo, { foo: 'ok', class: 'extra' })
  64. // no inference in this case
  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. })
  112. // #922
  113. describe('h support for generic component type', () => {
  114. function foo(bar: Component) {
  115. h(bar)
  116. h(bar, 'hello')
  117. h(bar, { id: 'ok' }, 'hello')
  118. }
  119. foo({})
  120. })
  121. // #993
  122. describe('describeComponent extends Component', () => {
  123. // functional
  124. expectAssignable<Component>(
  125. defineComponent((_props: { foo?: string; bar: number }) => {})
  126. )
  127. // typed props
  128. expectAssignable<Component>(defineComponent({}))
  129. // prop arrays
  130. expectAssignable<Component>(
  131. defineComponent({
  132. props: ['a', 'b']
  133. })
  134. )
  135. // prop object
  136. expectAssignable<Component>(
  137. defineComponent({
  138. props: {
  139. foo: String,
  140. bar: {
  141. type: Number,
  142. required: true
  143. }
  144. }
  145. })
  146. )
  147. })