test: improve tests and typing for runtime-core (#100)

* test: add test case for declaring Array prop type with constructor casting in `createComponent`

* test: add test case for `setup(props)` with explicit props declaration
This commit is contained in:
Carlos Rodrigues 2019-10-05 20:37:55 +01:00 committed by Evan You
parent f48a2ffc76
commit 8133b3867a
4 changed files with 46 additions and 6 deletions

View File

@ -124,10 +124,10 @@ describe('api: createApp', () => {
},
setup() {
// resolve in setup
const FooBar = resolveDirective('foo-bar') as any
const FooBar = resolveDirective('foo-bar')!
return () => {
// resolve in render
const BarBaz = resolveDirective('bar-baz') as any
const BarBaz = resolveDirective('bar-baz')!
return applyDirectives(h('div'), [[FooBar], [BarBaz]])
}
}

View File

@ -22,10 +22,17 @@ test('createComponent type inference', () => {
default: 'hello'
},
// explicit type casting
cc: (Array as any) as PropType<string[]>,
cc: Array as PropType<string[]>,
// required + type casting
dd: {
type: (Array as any) as PropType<string[]>,
type: Array as PropType<string[]>,
required: true
},
// explicit type casting with constructor
ccc: Array as () => string[],
// required + contructor type casting
ddd: {
type: Array as () => string[],
required: true
}
} as const, // required to narrow for conditional check
@ -60,7 +67,7 @@ test('createComponent type inference', () => {
}
})
// test TSX props inference
;(<MyComponent a={1} b="foo" dd={['foo']}/>)
;(<MyComponent a={1} b="foo" dd={['foo']} ddd={['foo']}/>)
})
test('type inference w/ optional props declaration', () => {

View File

@ -74,6 +74,39 @@ describe('api: setup context', () => {
expect(dummy).toBe(1)
})
it('setup props should resolve the correct types from props object', async () => {
const count = ref(0)
let dummy
const Parent = {
render: () => h(Child, { count: count.value })
}
const Child = createComponent({
props: {
count: Number
},
setup(props) {
watch(() => {
dummy = props.count
})
return () => h('div', props.count)
}
})
const root = nodeOps.createElement('div')
render(h(Parent), root)
expect(serializeInner(root)).toMatch(`<div>0</div>`)
expect(dummy).toBe(0)
// props should be reactive
count.value++
await nextTick()
expect(serializeInner(root)).toMatch(`<div>1</div>`)
expect(dummy).toBe(1)
})
it('context.attrs', async () => {
const toggle = ref(true)

View File

@ -517,7 +517,7 @@ describe('renderer: suspense', () => {
const Comp = {
setup() {
const error = ref<any>(null)
const error = ref<Error | null>(null)
onErrorCaptured(e => {
error.value = e
return true