Skip to content

Commit

Permalink
types(setProps): setProps to Partial props
Browse files Browse the repository at this point in the history
  • Loading branch information
pikax committed Nov 14, 2023
1 parent eb4a23a commit f59f8a6
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 12 deletions.
4 changes: 1 addition & 3 deletions src/mount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ export function mount<
ComponentProps<C>,
ComponentData<C> & ComponentExposed<C> & Omit<P, keyof ComponentProps<C>>
>
> & {
LOOL: Exclude<P, ComponentProps<C>>
}
>

// implementation
export function mount(
Expand Down
2 changes: 1 addition & 1 deletion src/vueWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ export class VueWrapper<
return nextTick()
}

setProps(props: T['$props']): Promise<void> {
setProps(props: Partial<T['$props']>): Promise<void> {
// if this VM's parent is not the root or if setProps does not exist, error out
if (this.vm.$parent !== this.rootVM || !this.__setProps) {
throw Error('You can only use setProps on your mounted component')
Expand Down
40 changes: 32 additions & 8 deletions test-dts/wrapper.d-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,25 +120,49 @@ expectType<{ [key: string]: any }>(wrapper.props())
const ComponentWithProps = defineComponent({
props: {
foo: String,
bar: Number,
},
bar: Number
}
})

const propsWrapper = mount(ComponentWithProps);
const propsWrapper = mount(ComponentWithProps)

propsWrapper.setProps({foo: 'abc'})
propsWrapper.setProps({foo: 'abc', bar: 123})
propsWrapper.setProps({ foo: 'abc' })
propsWrapper.setProps({ foo: 'abc', bar: 123 })
// @ts-expect-error :: should require string
propsWrapper.setProps({foo: 123})
propsWrapper.setProps({ foo: 123 })
// @ts-expect-error :: unknown prop
propsWrapper.setProps({badProp: true})
propsWrapper.setProps({ badProp: true })

expectType<string | undefined>(propsWrapper.props().foo)
expectType<number | undefined>(propsWrapper.props().bar)
// @ts-expect-error :: unknown prop
propsWrapper.props().badProp;
propsWrapper.props().badProp

expectType<string | undefined>(propsWrapper.props('foo'))
expectType<number | undefined>(propsWrapper.props('bar'))
// @ts-expect-error :: unknown prop
propsWrapper.props('badProp')

const requiredPropsWrapper = mount(
defineComponent({
props: {
foo: { type: String, required: true },
bar: { type: Number, required: true }
}
}),
{
props: {
foo: 'abc',
bar: 123
}
}
)

requiredPropsWrapper.setProps({
foo: 'abc'
})

requiredPropsWrapper.setProps({
// @ts-expect-error wrong type
foo: 1
})

0 comments on commit f59f8a6

Please sign in to comment.