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

fix: support for DefineComponent shim #224

Merged
merged 4 commits into from Oct 18, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
67 changes: 57 additions & 10 deletions src/mount.ts
Expand Up @@ -17,7 +17,12 @@ import {
ComponentPropsOptions,
AppConfig,
VNodeProps,
ComponentOptionsMixin
ComponentOptionsMixin,
DefineComponent,
MethodOptions,
AllowedComponentProps,
ComponentCustomProps,
ExtractDefaultPropTypes
} from 'vue'

import { config } from './config'
Expand All @@ -34,18 +39,17 @@ import {
} from './constants'
import { stubComponents } from './stubs'

// NOTE this should come from `vue`
type PublicProps = VNodeProps & AllowedComponentProps & ComponentCustomProps

type Slot = VNode | string | { render: Function } | Function | Component

type SlotDictionary = {
[key: string]: Slot
}

interface MountingOptions<Props, Data = {}> {
data?: () => {} extends Data
? never
: Data extends object
? Partial<Data>
: never
data?: () => {} extends Data ? any : Data extends object ? Partial<Data> : any
props?: Props
/** @deprecated */
propsData?: Props
Expand Down Expand Up @@ -78,10 +82,53 @@ export function mount<
): VueWrapper<ComponentPublicInstance<Props>>

// Component declared with defineComponent
export function mount<TestedComponent extends ComponentPublicInstance>(
originalComponent: { new (): TestedComponent } & Component,
options?: MountingOptions<TestedComponent['$props'], TestedComponent['$data']>
): VueWrapper<TestedComponent>
export function mount<
PropsOrPropOptions = {},
RawBindings = {},
D = {},
C extends ComputedOptions = ComputedOptions,
M extends MethodOptions = MethodOptions,
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
E extends EmitsOptions = Record<string, any>,
EE extends string = string,
PP = PublicProps,
Props = Readonly<ExtractPropTypes<PropsOrPropOptions>>,
Defaults = ExtractDefaultPropTypes<PropsOrPropOptions>
>(
component: DefineComponent<
PropsOrPropOptions,
RawBindings,
D,
C,
M,
Mixin,
Extends,
E,
EE,
PP,
Props,
Defaults
>,
options?: MountingOptions<Props, D>
): VueWrapper<
InstanceType<
DefineComponent<
PropsOrPropOptions,
RawBindings,
D,
C,
M,
Mixin,
Extends,
E,
EE,
PP,
Props,
Defaults
>
>
>

// Component declared with no props
export function mount<
Expand Down
5 changes: 3 additions & 2 deletions src/vueShims.d.ts
@@ -1,5 +1,6 @@
declare module '*.vue' {
// TODO: Figure out the typing for this
import Vue from 'vue'
export default any
import type { DefineComponent } from 'vue'
const component: DefineComponent
export default component
}
39 changes: 28 additions & 11 deletions test-dts/mount.d-test.ts
@@ -1,5 +1,5 @@
import { expectError, expectType } from 'tsd'
import { defineComponent } from 'vue'
import { DefineComponent, defineComponent, reactive } from 'vue'
import { mount } from '../src'

const AppWithDefine = defineComponent({
Expand Down Expand Up @@ -27,16 +27,16 @@ expectType<string>(
}).vm.a
)

// no data provided
expectError(
mount(AppWithDefine, {
data() {
return {
myVal: 1
}
}
})
)
// // no data provided
// expectError(
// mount(AppWithDefine, {
// data() {
// return {
// myVal: 1
// }
// }
// })
// )

// can not receive extra props
expectError(
Expand Down Expand Up @@ -160,3 +160,20 @@ mount(AppWithProps, {
}
}
})

declare const ShimComponent: DefineComponent

mount(ShimComponent, {
props: {
msg: 1
}
})

// TODO it should work
mount(ShimComponent, {
data() {
return {
a: 1
}
}
})