@@ -13,6 +13,10 @@ import {
13
13
ComponentOptionsWithoutProps ,
14
14
ExtractPropTypes ,
15
15
Component ,
16
+ WritableComputedOptions ,
17
+ SetupContext ,
18
+ RenderFunction ,
19
+ ComponentPropsOptions ,
16
20
AppConfig ,
17
21
VNodeProps
18
22
} from 'vue'
@@ -37,8 +41,8 @@ type SlotDictionary = {
37
41
[ key : string ] : Slot
38
42
}
39
43
40
- interface MountingOptions < Props > {
41
- data ?: ( ) => Record < string , unknown >
44
+ interface MountingOptions < Props , Data = { } > {
45
+ data ?: ( ) => Data extends object ? Partial < Data > : never
42
46
props ?: Props
43
47
attrs ?: Record < string , unknown >
44
48
slots ?: SlotDictionary & {
@@ -49,40 +53,116 @@ interface MountingOptions<Props> {
49
53
shallow ?: boolean
50
54
}
51
55
52
- // TODO improve the typings of the overloads
53
-
54
- type ExtractComponent < T > = T extends { new ( ) : infer PublicInstance }
55
- ? PublicInstance
56
- : any
56
+ export type ComputedOptions = Record <
57
+ string ,
58
+ ( ( ctx ?: any ) => any ) | WritableComputedOptions < any >
59
+ >
60
+ export type ObjectEmitsOptions = Record <
61
+ string ,
62
+ ( ( ...args : any [ ] ) => any ) | null
63
+ >
64
+ export type EmitsOptions = ObjectEmitsOptions | string [ ]
57
65
58
66
// Functional component
59
- export function mount < TestedComponent extends FunctionalComponent > (
67
+ export function mount <
68
+ TestedComponent extends FunctionalComponent < Props > ,
69
+ Props
70
+ > (
60
71
originalComponent : TestedComponent ,
61
- options ?: MountingOptions < any >
62
- ) : VueWrapper < ComponentPublicInstance >
72
+ options ?: MountingOptions < Props >
73
+ ) : VueWrapper < ComponentPublicInstance < Props > >
74
+
63
75
// Component declared with defineComponent
64
76
export function mount < TestedComponent extends ComponentPublicInstance > (
65
77
originalComponent : { new ( ) : TestedComponent } & Component ,
66
- options ?: MountingOptions < TestedComponent [ '$props' ] >
78
+ options ?: MountingOptions < TestedComponent [ '$props' ] , TestedComponent [ '$data' ] >
67
79
) : VueWrapper < TestedComponent >
68
- // Component declared with { props: { ... } }
69
- export function mount < TestedComponent extends ComponentOptionsWithObjectProps > (
70
- originalComponent : TestedComponent ,
71
- options ?: MountingOptions < ExtractPropTypes < TestedComponent [ 'props' ] , false > >
72
- ) : VueWrapper < ExtractComponent < TestedComponent > >
73
- // Component declared with { props: [] }
74
- export function mount < TestedComponent extends ComponentOptionsWithArrayProps > (
75
- originalComponent : TestedComponent ,
76
- options ?: MountingOptions < Record < string , any > >
77
- ) : VueWrapper < ExtractComponent < TestedComponent > >
80
+
78
81
// Component declared with no props
79
82
export function mount <
80
- TestedComponent extends ComponentOptionsWithoutProps ,
81
- ComponentT extends ComponentOptionsWithoutProps & { }
83
+ Props = { } ,
84
+ RawBindings = { } ,
85
+ D = { } ,
86
+ C extends ComputedOptions = { } ,
87
+ M extends Record < string , Function > = { } ,
88
+ E extends EmitsOptions = Record < string , any > ,
89
+ EE extends string = string
90
+ > (
91
+ componentOptions : ComponentOptionsWithoutProps <
92
+ Props ,
93
+ RawBindings ,
94
+ D ,
95
+ C ,
96
+ M ,
97
+ E ,
98
+ EE
99
+ > ,
100
+ options ?: MountingOptions < never , D >
101
+ ) : VueWrapper <
102
+ ComponentPublicInstance < Props , RawBindings , D , C , M , E , VNodeProps & Props >
103
+ >
104
+
105
+ // Component declared with { props: [] }
106
+ export function mount <
107
+ PropNames extends string ,
108
+ RawBindings ,
109
+ D ,
110
+ C extends ComputedOptions = { } ,
111
+ M extends Record < string , Function > = { } ,
112
+ E extends EmitsOptions = Record < string , any > ,
113
+ EE extends string = string ,
114
+ Props extends Readonly < { [ key in PropNames ] ?: any } > = Readonly <
115
+ { [ key in PropNames ] ?: any }
116
+ >
117
+ > (
118
+ componentOptions : ComponentOptionsWithArrayProps <
119
+ PropNames ,
120
+ RawBindings ,
121
+ D ,
122
+ C ,
123
+ M ,
124
+ E ,
125
+ EE ,
126
+ Props
127
+ > ,
128
+ options ?: MountingOptions < Props , D >
129
+ ) : VueWrapper < ComponentPublicInstance < Props , RawBindings , D , C , M , E > >
130
+
131
+ // Component declared with { props: { ... } }
132
+ export function mount <
133
+ // the Readonly constraint allows TS to treat the type of { required: true }
134
+ // as constant instead of boolean.
135
+ PropsOptions extends Readonly < ComponentPropsOptions > ,
136
+ RawBindings ,
137
+ D ,
138
+ C extends ComputedOptions = { } ,
139
+ M extends Record < string , Function > = { } ,
140
+ E extends EmitsOptions = Record < string , any > ,
141
+ EE extends string = string
82
142
> (
83
- originalComponent : ComponentT extends { new ( ) : any } ? never : ComponentT ,
84
- options ?: MountingOptions < never >
85
- ) : VueWrapper < ExtractComponent < TestedComponent > >
143
+ componentOptions : ComponentOptionsWithObjectProps <
144
+ PropsOptions ,
145
+ RawBindings ,
146
+ D ,
147
+ C ,
148
+ M ,
149
+ E ,
150
+ EE
151
+ > ,
152
+ options ?: MountingOptions < ExtractPropTypes < PropsOptions > , D >
153
+ ) : VueWrapper <
154
+ ComponentPublicInstance <
155
+ ExtractPropTypes < PropsOptions > ,
156
+ RawBindings ,
157
+ D ,
158
+ C ,
159
+ M ,
160
+ E ,
161
+ VNodeProps & ExtractPropTypes < PropsOptions , false >
162
+ >
163
+ >
164
+
165
+ // implementation
86
166
export function mount (
87
167
originalComponent : any ,
88
168
options ?: MountingOptions < any >
0 commit comments