2019-09-06 23:19:22 +08:00
|
|
|
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'
|
2019-05-30 22:09:48 +08:00
|
|
|
|
2019-06-12 15:43:19 +08:00
|
|
|
// mock React just for TSX testing purposes
|
|
|
|
const React = {
|
|
|
|
createElement: () => {}
|
|
|
|
}
|
|
|
|
|
2019-05-30 22:09:48 +08:00
|
|
|
test('createComponent type inference', () => {
|
2019-06-12 15:43:19 +08:00
|
|
|
const MyComponent = createComponent({
|
2019-05-30 22:09:48 +08:00
|
|
|
props: {
|
|
|
|
a: Number,
|
2019-06-01 00:47:05 +08:00
|
|
|
// required should make property non-void
|
2019-05-30 22:09:48 +08:00
|
|
|
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
|
2019-10-06 03:37:55 +08:00
|
|
|
cc: Array as PropType<string[]>,
|
2019-06-01 00:47:05 +08:00
|
|
|
// required + type casting
|
|
|
|
dd: {
|
2019-10-06 03:37:55 +08:00
|
|
|
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-05-30 22:09:48 +08:00
|
|
|
}
|
2019-06-01 00:47:05 +08:00
|
|
|
} as const, // required to narrow for conditional check
|
2019-05-30 22:09:48 +08:00
|
|
|
setup(props) {
|
2019-06-01 00:47:05 +08:00
|
|
|
props.a && props.a * 2
|
2019-05-30 22:09:48 +08:00
|
|
|
props.b.slice()
|
2019-06-01 00:47:05 +08:00
|
|
|
props.bb.slice()
|
|
|
|
props.cc && props.cc.push('hoo')
|
|
|
|
props.dd.push('dd')
|
2019-05-30 22:09:48 +08:00
|
|
|
return {
|
2019-08-22 05:10:37 +08:00
|
|
|
c: ref(1),
|
2019-05-30 22:09:48 +08:00
|
|
|
d: {
|
2019-08-22 05:10:37 +08:00
|
|
|
e: ref('hi')
|
2019-05-30 22:09:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2019-09-04 03:27:59 +08:00
|
|
|
render() {
|
|
|
|
const props = this.$props
|
2019-06-01 00:47:05 +08:00
|
|
|
props.a && props.a * 2
|
2019-05-30 22:09:48 +08:00
|
|
|
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
|
2019-05-30 22:09:48 +08:00
|
|
|
this.b.slice()
|
2019-06-01 00:47:05 +08:00
|
|
|
this.bb.slice()
|
2019-05-30 22:09:48 +08:00
|
|
|
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')
|
2019-09-06 06:48:49 +08:00
|
|
|
return h('div', this.bb)
|
2019-05-30 22:09:48 +08:00
|
|
|
}
|
|
|
|
})
|
2019-06-12 15:43:19 +08:00
|
|
|
// test TSX props inference
|
2019-10-15 03:07:43 +08:00
|
|
|
;<MyComponent a={1} b="foo" dd={['foo']} ddd={['foo']} />
|
2019-05-30 22:09:48 +08:00
|
|
|
})
|
2019-06-01 00:47:05 +08:00
|
|
|
|
|
|
|
test('type inference w/ optional props declaration', () => {
|
2019-06-12 15:43:19 +08:00
|
|
|
const Comp = createComponent({
|
|
|
|
setup(props: { msg: string }) {
|
|
|
|
props.msg
|
2019-06-01 00:47:05 +08:00
|
|
|
return {
|
|
|
|
a: 1
|
|
|
|
}
|
|
|
|
},
|
2019-09-04 03:27:59 +08:00
|
|
|
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
|
2019-09-06 06:48:49 +08:00
|
|
|
return h('div', this.msg)
|
2019-06-01 00:47:05 +08:00
|
|
|
}
|
|
|
|
})
|
2019-10-15 03:07:43 +08:00
|
|
|
;<Comp msg="hello" />
|
2019-06-01 00:47:05 +08:00
|
|
|
})
|
|
|
|
|
2019-06-12 15:43:19 +08:00
|
|
|
test('type inference w/ direct setup function', () => {
|
|
|
|
const Comp = createComponent((props: { msg: string }) => {
|
|
|
|
return () => <div>{props.msg}</div>
|
|
|
|
})
|
2019-10-15 03:07:43 +08:00
|
|
|
;<Comp msg="hello" />
|
2019-06-12 15:43:19 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
test('type inference w/ array props declaration', () => {
|
|
|
|
const Comp = createComponent({
|
|
|
|
props: ['a', 'b'],
|
|
|
|
setup(props) {
|
|
|
|
props.a
|
|
|
|
props.b
|
|
|
|
return {
|
|
|
|
c: 1
|
|
|
|
}
|
|
|
|
},
|
2019-09-04 03:27:59 +08:00
|
|
|
render() {
|
|
|
|
this.$props.a
|
|
|
|
this.$props.b
|
2019-06-12 15:43:19 +08:00
|
|
|
this.a
|
|
|
|
this.b
|
|
|
|
this.c
|
|
|
|
}
|
|
|
|
})
|
2019-10-15 03:07:43 +08:00
|
|
|
;<Comp a={1} b={2} />
|
2019-06-12 15:43:19 +08:00
|
|
|
})
|
2019-09-06 04:09:30 +08:00
|
|
|
|
|
|
|
test('with legacy options', () => {
|
|
|
|
createComponent({
|
|
|
|
props: { a: Number },
|
|
|
|
setup() {
|
|
|
|
return {
|
|
|
|
b: 123
|
|
|
|
}
|
|
|
|
},
|
|
|
|
data() {
|
2019-09-06 06:48:49 +08:00
|
|
|
// 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 {
|
2019-09-06 06:48:49 +08:00
|
|
|
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
|
|
|
|
}
|
2019-09-06 06:48:49 +08:00
|
|
|
},
|
|
|
|
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
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|