Skip to content

Commit 0b4f762

Browse files
authored
Merge pull request #254 from pikax/fix/class-component
fix(types): Fix mount and shallowMount types
2 parents d15875e + 87eaaab commit 0b4f762

File tree

3 files changed

+62
-5
lines changed

3 files changed

+62
-5
lines changed

src/mount.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ import { attachEmitListener } from './emitMixin'
3535
import { createDataMixin } from './dataMixin'
3636
import { MOUNT_COMPONENT_REF, MOUNT_PARENT_NAME } from './constants'
3737
import { stubComponents } from './stubs'
38-
import { VueConstructor } from 'vue-class-component'
3938

4039
// NOTE this should come from `vue`
4140
type PublicProps = VNodeProps & AllowedComponentProps & ComponentCustomProps
@@ -50,11 +49,24 @@ export type ObjectEmitsOptions = Record<
5049
>
5150
export type EmitsOptions = ObjectEmitsOptions | string[]
5251

53-
// Class component
54-
export function mount(
55-
originalComponent: VueConstructor,
52+
// Class component - no props
53+
export function mount<V>(
54+
originalComponent: {
55+
new (...args: any[]): V
56+
registerHooks(keys: string[]): void
57+
},
5658
options?: MountingOptions<any>
57-
): VueWrapper<ComponentPublicInstance<any>>
59+
): VueWrapper<ComponentPublicInstance<V>>
60+
61+
// Class component - props
62+
export function mount<V, P>(
63+
originalComponent: {
64+
new (...args: any[]): V
65+
props(Props: P): any
66+
registerHooks(keys: string[]): void
67+
},
68+
options?: MountingOptions<P>
69+
): VueWrapper<ComponentPublicInstance<V>>
5870

5971
// Functional component with emits
6072
export function mount<Props, E extends EmitsOptions = {}>(

test-dts/mount.d-test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
FunctionalComponent,
66
reactive
77
} from 'vue'
8+
import { Options, Vue } from 'vue-class-component'
89
import { mount } from '../src'
910

1011
const AppWithDefine = defineComponent({
@@ -203,3 +204,25 @@ mount(FunctionalComponentEmit)
203204

204205
// @ts-ignore vue 3.0.2 doesn't work. FIX: https://github.com/vuejs/vue-next/pull/2494
205206
mount(defineComponent(FunctionalComponentEmit))
207+
208+
// class component
209+
210+
@Options({
211+
props: {
212+
msg: String
213+
}
214+
})
215+
class ClassComponent extends Vue {
216+
dataText: string = ''
217+
get computedMsg(): string {
218+
return `Message: ${(this.$props as any).msg}`
219+
}
220+
221+
changeMessage(text: string): void {
222+
this.dataText = 'Updated'
223+
}
224+
}
225+
226+
// @ts-expect-error it requires an argument
227+
expectError(mount(ClassComponent, {}).vm.changeMessage())
228+
mount(ClassComponent, {}).vm.changeMessage('')

test-dts/shallowMount.d-test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { expectError, expectType } from 'tsd'
22
import { defineComponent } from 'vue'
3+
import { Options, Vue } from 'vue-class-component'
34
import { shallowMount } from '../src'
45

56
const AppWithDefine = defineComponent({
@@ -99,3 +100,24 @@ expectError(
99100
shallowMount(AppWithoutProps, {
100101
props: { b: 'Hello' } as never
101102
})
103+
104+
// class component
105+
@Options({
106+
props: {
107+
msg: String
108+
}
109+
})
110+
class ClassComponent extends Vue {
111+
dataText: string = ''
112+
get computedMsg(): string {
113+
return `Message: ${(this.$props as any).msg}`
114+
}
115+
116+
changeMessage(text: string): void {
117+
this.dataText = 'Updated'
118+
}
119+
}
120+
121+
// @ts-expect-error it requires an argument
122+
expectError(shallowMount(ClassComponent, {}).vm.changeMessage())
123+
shallowMount(ClassComponent, {}).vm.changeMessage('')

0 commit comments

Comments
 (0)