vue3-yuanma/packages/runtime-core/__tests__/apiCreateComponent.spec.tsx

168 lines
3.5 KiB
TypeScript
Raw Normal View History

import { createComponent } from '../src/apiCreateComponent'
2019-08-22 05:10:37 +08:00
import { ref } from '@vue/reactivity'
2019-06-01 00:47:05 +08:00
import { PropType } from '../src/componentProps'
2019-09-06 04:09:30 +08:00
import { h } from '../src/h'
// mock React just for TSX testing purposes
const React = {
createElement: () => {}
}
test('createComponent type inference', () => {
const MyComponent = createComponent({
props: {
a: Number,
2019-06-01 00:47:05 +08:00
// required should make property non-void
b: {
2019-06-01 00:47:05 +08:00
type: String,
required: true
},
// default value should infer type and make it non-void
bb: {
default: 'hello'
},
// explicit type casting
cc: Array as PropType<string[]>,
2019-06-01 00:47:05 +08:00
// required + type casting
dd: {
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[],
2019-06-01 00:47:05 +08:00
required: true
}
2019-06-01 00:47:05 +08:00
} as const, // required to narrow for conditional check
setup(props) {
2019-06-01 00:47:05 +08:00
props.a && props.a * 2
props.b.slice()
2019-06-01 00:47:05 +08:00
props.bb.slice()
props.cc && props.cc.push('hoo')
props.dd.push('dd')
return {
2019-08-22 05:10:37 +08:00
c: ref(1),
d: {
2019-08-22 05:10:37 +08:00
e: ref('hi')
}
}
},
render() {
const props = this.$props
2019-06-01 00:47:05 +08:00
props.a && props.a * 2
props.b.slice()
2019-06-01 00:47:05 +08:00
props.bb.slice()
props.cc && props.cc.push('hoo')
props.dd.push('dd')
this.a && this.a * 2
this.b.slice()
2019-06-01 00:47:05 +08:00
this.bb.slice()
this.c * 2
this.d.e.slice()
2019-06-01 00:47:05 +08:00
this.cc && this.cc.push('hoo')
this.dd.push('dd')
return h('div', this.bb)
}
})
// test TSX props inference
;(<MyComponent a={1} b="foo" dd={['foo']} ddd={['foo']}/>)
})
2019-06-01 00:47:05 +08:00
test('type inference w/ optional props declaration', () => {
const Comp = createComponent({
setup(props: { msg: string }) {
props.msg
2019-06-01 00:47:05 +08:00
return {
a: 1
}
},
render() {
this.$props.msg
2019-06-19 16:43:34 +08:00
this.msg
2019-06-01 00:47:05 +08:00
this.a * 2
return h('div', this.msg)
2019-06-01 00:47:05 +08:00
}
})
;(<Comp msg="hello"/>)
2019-06-01 00:47:05 +08:00
})
test('type inference w/ direct setup function', () => {
const Comp = createComponent((props: { msg: string }) => {
return () => <div>{props.msg}</div>
})
;(<Comp msg="hello"/>)
})
test('type inference w/ array props declaration', () => {
const Comp = createComponent({
props: ['a', 'b'],
setup(props) {
props.a
props.b
return {
c: 1
}
},
render() {
this.$props.a
this.$props.b
this.a
this.b
this.c
}
})
;(<Comp a={1} b={2}/>)
})
2019-09-06 04:09:30 +08:00
test('with legacy options', () => {
createComponent({
props: { a: Number },
setup() {
return {
b: 123
}
},
data() {
// Limitation: we cannot expose the return result of setup() on `this`
// here in data() - somehow that would mess up the inference
2019-09-06 04:09:30 +08:00
return {
c: this.a || 123
2019-09-06 04:09:30 +08:00
}
},
computed: {
d(): number {
return this.b + 1
}
},
watch: {
a() {
this.b + 1
}
},
created() {
this.a && this.a * 2
this.b * 2
this.c * 2
this.d * 2
},
methods: {
doSomething() {
this.a && this.a * 2
this.b * 2
this.c * 2
this.d * 2
return (this.a || 0) + this.b + this.c + this.d
}
},
render() {
this.a && this.a * 2
this.b * 2
this.c * 2
this.d * 2
return h('div', (this.a || 0) + this.b + this.c + this.d)
2019-09-06 04:09:30 +08:00
}
})
})