File tree 4 files changed +45
-11
lines changed
4 files changed +45
-11
lines changed Original file line number Diff line number Diff line change @@ -215,10 +215,14 @@ export class VueWrapper<
215
215
return this . componentVM
216
216
}
217
217
218
- props ( ) : { [ key : string ] : any }
219
- props ( selector : string ) : any
220
- props ( selector ?: string ) : { [ key : string ] : any } | any {
221
- const props = this . componentVM . $props as { [ key : string ] : any }
218
+ props ( ) : T [ '$props' ]
219
+ props < Selector extends keyof T [ '$props' ] > (
220
+ selector : Selector
221
+ ) : T [ '$props' ] [ Selector ]
222
+ props < Selector extends keyof T [ '$props' ] > (
223
+ selector ?: Selector
224
+ ) : T [ '$props' ] | T [ '$props' ] [ Selector ] {
225
+ const props = this . componentVM . $props as T [ '$props' ]
222
226
return selector ? props [ selector ] : props
223
227
}
224
228
@@ -240,7 +244,7 @@ export class VueWrapper<
240
244
return nextTick ( )
241
245
}
242
246
243
- setProps ( props : Record < string , unknown > ) : Promise < void > {
247
+ setProps ( props : T [ '$props' ] ) : Promise < void > {
244
248
// if this VM's parent is not the root or if setProps does not exist, error out
245
249
if ( this . vm . $parent !== this . rootVM || ! this . __setProps ) {
246
250
throw Error ( 'You can only use setProps on your mounted component' )
Original file line number Diff line number Diff line change @@ -116,4 +116,29 @@ expectType<boolean>(domWrapper.classes('class'))
116
116
117
117
// props
118
118
expectType < { [ key : string ] : any } > ( wrapper . props ( ) )
119
- expectType < any > ( wrapper . props ( 'prop' ) )
119
+
120
+ const ComponentWithProps = defineComponent ( {
121
+ props : {
122
+ foo : String ,
123
+ bar : Number ,
124
+ } ,
125
+ } )
126
+
127
+ const propsWrapper = mount ( ComponentWithProps ) ;
128
+
129
+ propsWrapper . setProps ( { foo : 'abc' } )
130
+ propsWrapper . setProps ( { foo : 'abc' , bar : 123 } )
131
+ // @ts -expect-error :: should require string
132
+ propsWrapper . setProps ( { foo : 123 } )
133
+ // @ts -expect-error :: unknown prop
134
+ propsWrapper . setProps ( { badProp : true } )
135
+
136
+ expectType < string | undefined > ( propsWrapper . props ( ) . foo )
137
+ expectType < number | undefined > ( propsWrapper . props ( ) . bar )
138
+ // @ts -expect-error :: unknown prop
139
+ propsWrapper . props ( ) . badProp ;
140
+
141
+ expectType < string | undefined > ( propsWrapper . props ( 'foo' ) )
142
+ expectType < number | undefined > ( propsWrapper . props ( 'bar' ) )
143
+ // @ts -expect-error :: unknown prop
144
+ propsWrapper . props ( 'badProp' )
Original file line number Diff line number Diff line change 1
1
import { describe , expect , it , vi } from 'vitest'
2
- import { DefineComponent , defineComponent } from 'vue'
2
+ import { defineComponent } from 'vue'
3
3
import { mount , RouterLinkStub , shallowMount } from '../src'
4
4
import Issue425 from './components/Issue425.vue'
5
5
@@ -70,15 +70,19 @@ describe('getComponent', () => {
70
70
// https://github.com/vuejs/test-utils/issues/425
71
71
it ( 'works with router-link and mount' , ( ) => {
72
72
const wrapper = mount ( Issue425 , options )
73
- expect ( wrapper . getComponent < DefineComponent > ( '.link' ) . props ( 'to' ) ) . toEqual ( {
73
+ expect (
74
+ wrapper . getComponent < typeof RouterLinkStub > ( '.link' ) . props ( 'to' )
75
+ ) . toEqual ( {
74
76
name
75
77
} )
76
78
} )
77
79
78
80
// https://github.com/vuejs/test-utils/issues/425
79
81
it ( 'works with router-link and shallowMount' , ( ) => {
80
82
const wrapper = shallowMount ( Issue425 , options )
81
- expect ( wrapper . getComponent < DefineComponent > ( '.link' ) . props ( 'to' ) ) . toEqual ( {
83
+ expect (
84
+ wrapper . getComponent < typeof RouterLinkStub > ( '.link' ) . props ( 'to' )
85
+ ) . toEqual ( {
82
86
name
83
87
} )
84
88
} )
Original file line number Diff line number Diff line change 1
1
import { describe , expect , it } from 'vitest'
2
- import { mount , shallowMount } from '../src'
2
+ import { VueWrapper , mount , shallowMount } from '../src'
3
3
import WithProps from './components/WithProps.vue'
4
4
import PropWithSymbol from './components/PropWithSymbol.vue'
5
5
import Hello from './components/Hello.vue'
@@ -20,6 +20,7 @@ describe('props', () => {
20
20
21
21
it ( 'returns undefined if props does not exist' , ( ) => {
22
22
const wrapper = mount ( WithProps , { props : { msg : 'ABC' } } )
23
+ // @ts -expect-error :: non-existent prop
23
24
expect ( wrapper . props ( 'foo' ) ) . toEqual ( undefined )
24
25
} )
25
26
@@ -342,7 +343,7 @@ describe('props', () => {
342
343
} )
343
344
344
345
it ( 'should get props from functional component' , async ( ) => {
345
- const wrapper = mount ( Title , {
346
+ const wrapper : VueWrapper < any > = mount ( Title , {
346
347
props : {
347
348
title : 'nickname'
348
349
}
You can’t perform that action at this time.
0 commit comments