/
defineComponent.spec.ts
221 lines (207 loc) · 5.26 KB
/
defineComponent.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import { defineComponent, h, ref, SetupContext, PropType } from '../../src'
import Router from 'vue-router'
const Vue = require('vue/dist/vue.common.js')
type Equal<Left, Right> = (<U>() => U extends Left ? 1 : 0) extends <
U
>() => U extends Right ? 1 : 0
? true
: false
const isTypeEqual = <Left, Right>(shouldBeEqual: Equal<Left, Right>) => {
void shouldBeEqual
expect(true).toBe(true)
}
const isSubType = <SuperType, SubType>(
shouldBeEqual: SubType extends SuperType ? true : false
) => {
void shouldBeEqual
expect(true).toBe(true)
}
describe('defineComponent', () => {
it('should work', () => {
const Child = defineComponent({
props: { msg: String },
setup(props) {
return () => h('span', props.msg)
},
})
const App = defineComponent({
setup() {
const msg = ref('hello')
return () =>
h('div', [
h(Child, {
props: {
msg: msg.value,
},
}),
])
},
})
const vm = new Vue(App).$mount()
expect(vm.$el.querySelector('span').textContent).toBe('hello')
})
it('should infer props type', () => {
const App = defineComponent({
props: {
a: {
type: Number,
default: 0,
},
b: String,
},
setup(props, ctx) {
type PropsType = typeof props
isTypeEqual<SetupContext, typeof ctx>(true)
isSubType<PropsType, { readonly b?: string; readonly a: number }>(true)
isSubType<{ readonly b?: string; readonly a: number }, PropsType>(true)
return () => null
},
})
new Vue(App)
expect.assertions(3)
})
it('custom props interface', () => {
interface IPropsType {
b: string
}
const App = defineComponent<IPropsType>({
props: {
b: {},
},
setup(props, ctx) {
type PropsType = typeof props
isTypeEqual<SetupContext, typeof ctx>(true)
isSubType<PropsType, { b: string }>(true)
isSubType<{ b: string }, PropsType>(true)
return () => null
},
})
new Vue(App)
expect.assertions(3)
})
it('custom props type function', () => {
interface IPropsTypeFunction {
fn: (arg: boolean) => void
}
const App = defineComponent<IPropsTypeFunction>({
props: {
fn: Function as PropType<(arg: boolean) => void>,
},
setup(props, ctx) {
type PropsType = typeof props
isTypeEqual<SetupContext, typeof ctx>(true)
isSubType<PropsType, { fn: (arg: boolean) => void }>(true)
isSubType<{ fn: (arg: boolean) => void }, PropsType>(true)
return () => null
},
})
new Vue(App)
expect.assertions(3)
})
it('custom props type inferred from PropType', () => {
interface User {
name: string
}
const App = defineComponent({
props: {
user: Object as PropType<User>,
func: Function as PropType<() => boolean>,
userFunc: Function as PropType<(u: User) => User>,
},
setup(props) {
type PropsType = typeof props
isSubType<
{ user?: User; func?: () => boolean; userFunc?: (u: User) => User },
PropsType
>(true)
isSubType<
PropsType,
{ user?: User; func?: () => boolean; userFunc?: (u: User) => User }
>(true)
},
})
new Vue(App)
expect.assertions(2)
})
it('no props', () => {
const App = defineComponent({
setup(props, ctx) {
isTypeEqual<SetupContext, typeof ctx>(true)
isTypeEqual<{}, typeof props>(true)
return () => null
},
})
new Vue(App)
expect.assertions(2)
})
it('should accept tuple props', () => {
const App = defineComponent({
props: ['p1', 'p2'],
setup(props) {
props.p1
props.p2
type PropsType = typeof props
type Expected = { readonly p1?: any; readonly p2?: any }
isSubType<Expected, PropsType>(true)
isSubType<PropsType, Expected>(true)
},
})
new Vue(App)
expect.assertions(2)
})
it('should allow any custom options', () => {
const App = defineComponent({
foo: 'foo',
bar: 'bar',
})
new Vue(App)
})
it('infer the required prop', () => {
const App = defineComponent({
props: {
foo: {
type: String,
required: true,
},
bar: {
type: String,
default: 'default',
},
zoo: {
type: String,
required: false,
},
},
propsData: {
foo: 'foo',
},
setup(props) {
type PropsType = typeof props
isSubType<
{ readonly foo: string; readonly bar: string; readonly zoo?: string },
PropsType
>(true)
isSubType<
PropsType,
{ readonly foo: string; readonly bar: string; readonly zoo?: string }
>(true)
return () => null
},
})
new Vue(App)
expect.assertions(2)
})
describe('compatible with vue router', () => {
it('RouteConfig.component', () => {
new Router({
routes: [
{
path: '/',
name: 'root',
component: defineComponent({}),
},
],
})
})
})
})