2019-11-02 10:54:01 +08:00
|
|
|
import { expectError } from 'tsd'
|
2019-11-23 07:09:26 +08:00
|
|
|
import {
|
|
|
|
describe,
|
|
|
|
h,
|
2019-12-22 23:58:12 +08:00
|
|
|
defineComponent,
|
2019-11-23 07:09:26 +08:00
|
|
|
ref,
|
|
|
|
Fragment,
|
2020-04-02 10:04:55 +08:00
|
|
|
Teleport,
|
2019-11-23 07:09:26 +08:00
|
|
|
Suspense
|
|
|
|
} from './index'
|
2019-11-02 10:54:01 +08:00
|
|
|
|
2019-11-05 07:38:55 +08:00
|
|
|
describe('h inference w/ element', () => {
|
|
|
|
// key
|
|
|
|
h('div', { key: 1 })
|
|
|
|
h('div', { key: 'foo' })
|
|
|
|
expectError(h('div', { key: [] }))
|
|
|
|
expectError(h('div', { key: {} }))
|
|
|
|
// ref
|
|
|
|
h('div', { ref: 'foo' })
|
|
|
|
h('div', { ref: ref(null) })
|
|
|
|
h('div', { ref: el => {} })
|
|
|
|
expectError(h('div', { ref: [] }))
|
|
|
|
expectError(h('div', { ref: {} }))
|
|
|
|
expectError(h('div', { ref: 123 }))
|
|
|
|
})
|
2019-11-02 10:54:01 +08:00
|
|
|
|
2019-11-05 07:38:55 +08:00
|
|
|
describe('h inference w/ Fragment', () => {
|
|
|
|
// only accepts array children
|
|
|
|
h(Fragment, ['hello'])
|
|
|
|
h(Fragment, { key: 123 }, ['hello'])
|
|
|
|
expectError(h(Fragment, 'foo'))
|
|
|
|
expectError(h(Fragment, { key: 123 }, 'bar'))
|
|
|
|
})
|
2019-11-02 10:54:01 +08:00
|
|
|
|
2020-04-02 10:04:55 +08:00
|
|
|
describe('h inference w/ Teleport', () => {
|
|
|
|
h(Teleport, { to: '#foo' }, 'hello')
|
|
|
|
expectError(h(Teleport))
|
|
|
|
expectError(h(Teleport, {}))
|
|
|
|
expectError(h(Teleport, { to: '#foo' }))
|
2019-11-05 07:38:55 +08:00
|
|
|
})
|
2019-11-02 10:54:01 +08:00
|
|
|
|
2019-11-05 07:38:55 +08:00
|
|
|
describe('h inference w/ Suspense', () => {
|
|
|
|
h(Suspense, { onRecede: () => {}, onResolve: () => {} }, 'hello')
|
|
|
|
h(Suspense, 'foo')
|
|
|
|
h(Suspense, () => 'foo')
|
|
|
|
h(Suspense, null, {
|
|
|
|
default: () => 'foo'
|
|
|
|
})
|
|
|
|
expectError(h(Suspense, { onResolve: 1 }))
|
2019-11-02 10:54:01 +08:00
|
|
|
})
|
|
|
|
|
2019-11-05 07:38:55 +08:00
|
|
|
describe('h inference w/ functional component', () => {
|
|
|
|
const Func = (_props: { foo: string; bar?: number }) => ''
|
|
|
|
h(Func, { foo: 'hello' })
|
|
|
|
h(Func, { foo: 'hello', bar: 123 })
|
|
|
|
expectError(h(Func, { foo: 123 }))
|
|
|
|
expectError(h(Func, {}))
|
|
|
|
expectError(h(Func, { bar: 123 }))
|
|
|
|
})
|
2019-11-02 10:54:01 +08:00
|
|
|
|
2019-11-05 07:38:55 +08:00
|
|
|
describe('h inference w/ plain object component', () => {
|
|
|
|
const Foo = {
|
|
|
|
props: {
|
|
|
|
foo: String
|
|
|
|
}
|
2019-11-02 10:54:01 +08:00
|
|
|
}
|
|
|
|
|
2019-11-05 07:38:55 +08:00
|
|
|
h(Foo, { foo: 'ok' })
|
|
|
|
h(Foo, { foo: 'ok', class: 'extra' })
|
|
|
|
// should fail on wrong type
|
|
|
|
expectError(h(Foo, { foo: 1 }))
|
|
|
|
})
|
2019-11-02 10:54:01 +08:00
|
|
|
|
2019-12-22 23:58:12 +08:00
|
|
|
describe('h inference w/ defineComponent', () => {
|
|
|
|
const Foo = defineComponent({
|
2019-11-05 07:38:55 +08:00
|
|
|
props: {
|
|
|
|
foo: String,
|
|
|
|
bar: {
|
|
|
|
type: Number,
|
|
|
|
required: true
|
|
|
|
}
|
2019-11-02 10:54:01 +08:00
|
|
|
}
|
2019-11-05 07:38:55 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
h(Foo, { bar: 1 })
|
|
|
|
h(Foo, { bar: 1, foo: 'ok' })
|
|
|
|
// should allow extraneous props (attrs fallthrough)
|
|
|
|
h(Foo, { bar: 1, foo: 'ok', class: 'extra' })
|
|
|
|
// should fail on missing required prop
|
|
|
|
expectError(h(Foo, {}))
|
|
|
|
expectError(h(Foo, { foo: 'ok' }))
|
|
|
|
// should fail on wrong type
|
|
|
|
expectError(h(Foo, { bar: 1, foo: 1 }))
|
2019-11-02 10:54:01 +08:00
|
|
|
})
|
|
|
|
|
2019-12-22 23:58:12 +08:00
|
|
|
describe('h inference w/ defineComponent + optional props', () => {
|
|
|
|
const Foo = defineComponent({
|
2019-11-05 07:38:55 +08:00
|
|
|
setup(_props: { foo?: string; bar: number }) {}
|
|
|
|
})
|
|
|
|
|
|
|
|
h(Foo, { bar: 1 })
|
|
|
|
h(Foo, { bar: 1, foo: 'ok' })
|
|
|
|
// should allow extraneous props (attrs fallthrough)
|
|
|
|
h(Foo, { bar: 1, foo: 'ok', class: 'extra' })
|
|
|
|
// should fail on missing required prop
|
|
|
|
expectError(h(Foo, {}))
|
|
|
|
expectError(h(Foo, { foo: 'ok' }))
|
|
|
|
// should fail on wrong type
|
|
|
|
expectError(h(Foo, { bar: 1, foo: 1 }))
|
|
|
|
})
|
|
|
|
|
2019-12-22 23:58:12 +08:00
|
|
|
describe('h inference w/ defineComponent + direct function', () => {
|
|
|
|
const Foo = defineComponent((_props: { foo?: string; bar: number }) => {})
|
2019-11-05 07:38:55 +08:00
|
|
|
|
|
|
|
h(Foo, { bar: 1 })
|
|
|
|
h(Foo, { bar: 1, foo: 'ok' })
|
|
|
|
// should allow extraneous props (attrs fallthrough)
|
|
|
|
h(Foo, { bar: 1, foo: 'ok', class: 'extra' })
|
|
|
|
// should fail on missing required prop
|
|
|
|
expectError(h(Foo, {}))
|
|
|
|
expectError(h(Foo, { foo: 'ok' }))
|
|
|
|
// should fail on wrong type
|
|
|
|
expectError(h(Foo, { bar: 1, foo: 1 }))
|
|
|
|
})
|