Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

types(setProps): setProps to Partial props #2241

Merged
merged 1 commit into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
})