File tree Expand file tree Collapse file tree 4 files changed +41
-11
lines changed Expand file tree Collapse file tree 4 files changed +41
-11
lines changed Original file line number Diff line number Diff line change @@ -215,10 +215,14 @@ export class VueWrapper<
215215 return this . componentVM
216216 }
217217
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' ]
222226 return selector ? props [ selector ] : props
223227 }
224228
@@ -240,7 +244,7 @@ export class VueWrapper<
240244 return nextTick ( )
241245 }
242246
243- setProps ( props : Record < string , unknown > ) : Promise < void > {
247+ setProps ( props : T [ '$props' ] ) : Promise < void > {
244248 // if this VM's parent is not the root or if setProps does not exist, error out
245249 if ( this . vm . $parent !== this . rootVM || ! this . __setProps ) {
246250 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'))
116116
117117// props
118118expectType < { [ 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 11import { describe , expect , it , vi } from 'vitest'
2- import { DefineComponent , defineComponent } from 'vue'
2+ import { defineComponent } from 'vue'
33import { mount , RouterLinkStub , shallowMount } from '../src'
44import Issue425 from './components/Issue425.vue'
55
@@ -70,15 +70,15 @@ describe('getComponent', () => {
7070 // https://github.com/vuejs/test-utils/issues/425
7171 it ( 'works with router-link and mount' , ( ) => {
7272 const wrapper = mount ( Issue425 , options )
73- expect ( wrapper . getComponent < DefineComponent > ( '.link' ) . props ( 'to' ) ) . toEqual ( {
73+ expect ( wrapper . getComponent < typeof RouterLinkStub > ( '.link' ) . props ( 'to' ) ) . toEqual ( {
7474 name
7575 } )
7676 } )
7777
7878 // https://github.com/vuejs/test-utils/issues/425
7979 it ( 'works with router-link and shallowMount' , ( ) => {
8080 const wrapper = shallowMount ( Issue425 , options )
81- expect ( wrapper . getComponent < DefineComponent > ( '.link' ) . props ( 'to' ) ) . toEqual ( {
81+ expect ( wrapper . getComponent < typeof RouterLinkStub > ( '.link' ) . props ( 'to' ) ) . toEqual ( {
8282 name
8383 } )
8484 } )
Original file line number Diff line number Diff line change 11import { describe , expect , it } from 'vitest'
2- import { mount , shallowMount } from '../src'
2+ import { VueWrapper , mount , shallowMount } from '../src'
33import WithProps from './components/WithProps.vue'
44import PropWithSymbol from './components/PropWithSymbol.vue'
55import Hello from './components/Hello.vue'
@@ -20,6 +20,7 @@ describe('props', () => {
2020
2121 it ( 'returns undefined if props does not exist' , ( ) => {
2222 const wrapper = mount ( WithProps , { props : { msg : 'ABC' } } )
23+ // @ts -expect-error :: non-existent prop
2324 expect ( wrapper . props ( 'foo' ) ) . toEqual ( undefined )
2425 } )
2526
@@ -342,7 +343,7 @@ describe('props', () => {
342343 } )
343344
344345 it ( 'should get props from functional component' , async ( ) => {
345- const wrapper = mount ( Title , {
346+ const wrapper : VueWrapper < any > = mount ( Title , {
346347 props : {
347348 title : 'nickname'
348349 }
You can’t perform that action at this time.
0 commit comments