From 48b12939b0b673149864f3494aa7ea9fadc82eb6 Mon Sep 17 00:00:00 2001 From: pikax Date: Sat, 17 Oct 2020 07:57:44 +0100 Subject: [PATCH 1/4] fix: support for DefineComponent shim --- src/mount.ts | 68 +++++++++++++++++++++++++++++++++++++--- src/vueShims.d.ts | 5 +-- test-dts/mount.d-test.ts | 19 ++++++++++- 3 files changed, 84 insertions(+), 8 deletions(-) diff --git a/src/mount.ts b/src/mount.ts index ada6d81b5..32e573b9c 100644 --- a/src/mount.ts +++ b/src/mount.ts @@ -17,7 +17,12 @@ import { ComponentPropsOptions, AppConfig, VNodeProps, - ComponentOptionsMixin + ComponentOptionsMixin, + DefineComponent, + MethodOptions, + AllowedComponentProps, + ComponentCustomProps, + ExtractDefaultPropTypes } from 'vue' import { config } from './config' @@ -34,6 +39,8 @@ import { } from './constants' import { stubComponents } from './stubs' +type PublicProps = VNodeProps & AllowedComponentProps & ComponentCustomProps + type Slot = VNode | string | { render: Function } | Function | Component type SlotDictionary = { @@ -46,6 +53,7 @@ interface MountingOptions { : Data extends object ? Partial : never + // data?: {} extends Data ? never : () => Partial props?: Props /** @deprecated */ propsData?: Props @@ -78,10 +86,60 @@ export function mount< ): VueWrapper> // Component declared with defineComponent -export function mount( - originalComponent: { new (): TestedComponent } & Component, - options?: MountingOptions -): VueWrapper +// export function mount( +// originalComponent: { new (): TestedComponent } & Component, +// options?: MountingOptions +// ): VueWrapper + +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, + EE extends string = string, + PP = PublicProps, + Props = Readonly>, + Defaults = ExtractDefaultPropTypes +>( + component: DefineComponent< + PropsOrPropOptions, + RawBindings, + D, + C, + M, + Mixin, + Extends, + E, + EE, + PP, + Props, + Defaults + >, + options?: MountingOptions +): VueWrapper< + InstanceType< + DefineComponent< + PropsOrPropOptions, + RawBindings, + D, + C, + M, + Mixin, + Extends, + E, + EE, + PP, + Props, + Defaults + > + > +> + +//Props //VueWrapper> //ExtractPropTypes //PropsOrOptions //VueWrapper> // Component declared with no props export function mount< diff --git a/src/vueShims.d.ts b/src/vueShims.d.ts index e875bbea9..1fe418c70 100644 --- a/src/vueShims.d.ts +++ b/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 } diff --git a/test-dts/mount.d-test.ts b/test-dts/mount.d-test.ts index 2b49d054c..3db3d9651 100644 --- a/test-dts/mount.d-test.ts +++ b/test-dts/mount.d-test.ts @@ -1,5 +1,5 @@ import { expectError, expectType } from 'tsd' -import { defineComponent } from 'vue' +import { DefineComponent, defineComponent } from 'vue' import { mount } from '../src' const AppWithDefine = defineComponent({ @@ -160,3 +160,20 @@ mount(AppWithProps, { } } }) + +declare const ShimComponent: DefineComponent + +mount(ShimComponent, { + props: { + msg: 1 + } +}) + +// TODO it should work +// mount(ShimedComponent, { +// data() { +// return { +// a: 1 +// } +// } +// }) From cf25fe0ec5d42cc2f452f67c52727187ed8d441c Mon Sep 17 00:00:00 2001 From: pikax Date: Sat, 17 Oct 2020 07:59:56 +0100 Subject: [PATCH 2/4] removed comments --- src/mount.ts | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/mount.ts b/src/mount.ts index 32e573b9c..d1c02f40d 100644 --- a/src/mount.ts +++ b/src/mount.ts @@ -53,7 +53,6 @@ interface MountingOptions { : Data extends object ? Partial : never - // data?: {} extends Data ? never : () => Partial props?: Props /** @deprecated */ propsData?: Props @@ -86,11 +85,6 @@ export function mount< ): VueWrapper> // Component declared with defineComponent -// export function mount( -// originalComponent: { new (): TestedComponent } & Component, -// options?: MountingOptions -// ): VueWrapper - export function mount< PropsOrPropOptions = {}, RawBindings = {}, From aa9fbe86f190a18617401095a11f1b97a0b8563f Mon Sep 17 00:00:00 2001 From: pikax Date: Sat, 17 Oct 2020 08:12:11 +0100 Subject: [PATCH 3/4] more comments removed --- src/mount.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/mount.ts b/src/mount.ts index d1c02f40d..edb38deb0 100644 --- a/src/mount.ts +++ b/src/mount.ts @@ -39,6 +39,7 @@ 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 @@ -133,8 +134,6 @@ export function mount< > > -//Props //VueWrapper> //ExtractPropTypes //PropsOrOptions //VueWrapper> - // Component declared with no props export function mount< Props = {}, From cc1c94343c0e2aea131a9a9340f69d45dc1f388e Mon Sep 17 00:00:00 2001 From: pikax Date: Sat, 17 Oct 2020 20:23:57 +0100 Subject: [PATCH 4/4] fix data issue --- src/mount.ts | 6 +----- test-dts/mount.d-test.ts | 36 ++++++++++++++++++------------------ 2 files changed, 19 insertions(+), 23 deletions(-) diff --git a/src/mount.ts b/src/mount.ts index edb38deb0..77c390c42 100644 --- a/src/mount.ts +++ b/src/mount.ts @@ -49,11 +49,7 @@ type SlotDictionary = { } interface MountingOptions { - data?: () => {} extends Data - ? never - : Data extends object - ? Partial - : never + data?: () => {} extends Data ? any : Data extends object ? Partial : any props?: Props /** @deprecated */ propsData?: Props diff --git a/test-dts/mount.d-test.ts b/test-dts/mount.d-test.ts index 3db3d9651..8a9557c06 100644 --- a/test-dts/mount.d-test.ts +++ b/test-dts/mount.d-test.ts @@ -1,5 +1,5 @@ import { expectError, expectType } from 'tsd' -import { DefineComponent, defineComponent } from 'vue' +import { DefineComponent, defineComponent, reactive } from 'vue' import { mount } from '../src' const AppWithDefine = defineComponent({ @@ -27,16 +27,16 @@ expectType( }).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( @@ -170,10 +170,10 @@ mount(ShimComponent, { }) // TODO it should work -// mount(ShimedComponent, { -// data() { -// return { -// a: 1 -// } -// } -// }) +mount(ShimComponent, { + data() { + return { + a: 1 + } + } +})