forked from vuejs/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathh.test-d.ts
235 lines (211 loc) · 5.34 KB
/
h.test-d.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import {
describe,
h,
defineComponent,
ref,
Fragment,
Teleport,
Suspense,
Component,
expectError,
expectAssignable,
resolveComponent
} from './index'
describe('h inference w/ element', () => {
// key
h('div', { key: 1 })
h('div', { key: 'foo' })
// @ts-expect-error
expectError(h('div', { key: [] }))
// @ts-expect-error
expectError(h('div', { key: {} }))
// ref
h('div', { ref: 'foo' })
h('div', { ref: ref(null) })
h('div', { ref: _el => {} })
// @ts-expect-error
expectError(h('div', { ref: [] }))
// @ts-expect-error
expectError(h('div', { ref: {} }))
// @ts-expect-error
expectError(h('div', { ref: 123 }))
// slots
const slots = { default: () => {} } // RawSlots
h('div', {}, slots)
})
describe('h inference w/ Fragment', () => {
// only accepts array children
h(Fragment, ['hello'])
h(Fragment, { key: 123 }, ['hello'])
// @ts-expect-error
expectError(h(Fragment, 'foo'))
// @ts-expect-error
expectError(h(Fragment, { key: 123 }, 'bar'))
})
describe('h inference w/ Teleport', () => {
h(Teleport, { to: '#foo' }, 'hello')
// @ts-expect-error
expectError(h(Teleport))
// @ts-expect-error
expectError(h(Teleport, {}))
// @ts-expect-error
expectError(h(Teleport, { to: '#foo' }))
})
describe('h inference w/ Suspense', () => {
h(Suspense, { onRecede: () => {}, onResolve: () => {} }, 'hello')
h(Suspense, 'foo')
h(Suspense, () => 'foo')
h(Suspense, null, {
default: () => 'foo'
})
// @ts-expect-error
expectError(h(Suspense, { onResolve: 1 }))
})
describe('h inference w/ functional component', () => {
const Func = (_props: { foo: string; bar?: number }) => ''
h(Func, { foo: 'hello' })
h(Func, { foo: 'hello', bar: 123 })
// @ts-expect-error
expectError(h(Func, { foo: 123 }))
// @ts-expect-error
expectError(h(Func, {}))
// @ts-expect-error
expectError(h(Func, { bar: 123 }))
})
describe('h support w/ plain object component', () => {
const Foo = {
props: {
foo: String
}
}
h(Foo, { foo: 'ok' })
h(Foo, { foo: 'ok', class: 'extra' })
// no inference in this case
})
describe('h inference w/ defineComponent', () => {
const Foo = defineComponent({
props: {
foo: String,
bar: {
type: Number,
required: true
}
}
})
h(Foo, { bar: 1 })
h(Foo, { bar: 1, foo: 'ok' })
// should allow extraneous props (attrs fallthrough)
h(Foo, { bar: 1, foo: 'ok', class: 'extra' })
// @ts-expect-error should fail on missing required prop
expectError(h(Foo, {}))
// @ts-expect-error
expectError(h(Foo, { foo: 'ok' }))
// @ts-expect-error should fail on wrong type
expectError(h(Foo, { bar: 1, foo: 1 }))
})
// describe('h inference w/ defineComponent + optional props', () => {
// const Foo = defineComponent({
// 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' })
// // @ts-expect-error should fail on missing required prop
// expectError(h(Foo, {}))
// // @ts-expect-error
// expectError(h(Foo, { foo: 'ok' }))
// // @ts-expect-error should fail on wrong type
// expectError(h(Foo, { bar: 1, foo: 1 }))
// })
// describe('h inference w/ defineComponent + direct function', () => {
// const Foo = defineComponent((_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' })
// // @ts-expect-error should fail on missing required prop
// expectError(h(Foo, {}))
// // @ts-expect-error
// expectError(h(Foo, { foo: 'ok' }))
// // @ts-expect-error should fail on wrong type
// expectError(h(Foo, { bar: 1, foo: 1 }))
// })
// #922
describe('h support for generic component type', () => {
function foo(bar: Component) {
h(bar)
h(bar, 'hello')
// @ts-expect-error
h(bar, { id: 'ok' }, 'hello')
}
foo({})
})
// #993
describe('describeComponent extends Component', () => {
// functional
expectAssignable<Component>(
defineComponent((_props: { foo?: string; bar: number }) => {})
)
// typed props
expectAssignable<Component>(defineComponent({}))
// prop arrays
expectAssignable<Component>(
defineComponent({
props: ['a', 'b']
})
)
// prop object
expectAssignable<Component>(
defineComponent({
props: {
foo: String,
bar: {
type: Number,
required: true
}
}
})
)
})
// #1385
describe('component w/ props w/ default value', () => {
const MyComponent = defineComponent({
props: {
message: {
type: String,
default: 'hello'
}
}
})
h(MyComponent, {})
})
// #2338
describe('Boolean prop implicit false', () => {
const MyComponent = defineComponent({
props: {
visible: Boolean
}
})
h(MyComponent, {})
const RequiredComponent = defineComponent({
props: {
visible: {
type: Boolean,
required: true
}
}
})
h(RequiredComponent, {
visible: true
})
// @ts-expect-error
expectError(h(RequiredComponent, {}))
})
// #2357
describe('resolveComponent should work', () => {
h(resolveComponent('test'))
h(resolveComponent('test'), {
message: '1'
})
})