Skip to content

Commit 44b5f48

Browse files
authored
Merge pull request #63 from cexbrayat/fix/vm
fix: vm should be properly typed
2 parents 08e2881 + 8f0d12a commit 44b5f48

File tree

3 files changed

+32
-17
lines changed

3 files changed

+32
-17
lines changed

src/mount.ts

+16-5
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ import {
99
Plugin,
1010
Directive,
1111
Component,
12-
reactive
12+
reactive,
13+
ComponentPublicInstance
1314
} from 'vue'
1415

15-
import { createWrapper } from './vue-wrapper'
16+
import { createWrapper, VueWrapper } from './vue-wrapper'
1617
import { createEmitMixin } from './emitMixin'
1718
import { createDataMixin } from './dataMixin'
1819
import { MOUNT_ELEMENT_ID } from './constants'
@@ -40,7 +41,14 @@ interface MountingOptions {
4041
stubs?: Record<string, any>
4142
}
4243

43-
export function mount(originalComponent: any, options?: MountingOptions) {
44+
export function mount<T extends any>(
45+
originalComponent: any,
46+
options?: MountingOptions
47+
): VueWrapper<any>
48+
export function mount<T extends ComponentPublicInstance>(
49+
originalComponent: new () => T,
50+
options?: MountingOptions
51+
): VueWrapper<T> {
4452
const component = { ...originalComponent }
4553

4654
// Reset the document.body
@@ -66,7 +74,10 @@ export function mount(originalComponent: any, options?: MountingOptions) {
6674
// override component data with mounting options data
6775
if (options?.data) {
6876
const dataMixin = createDataMixin(options.data())
69-
component.mixins = [...(component.mixins || []), dataMixin]
77+
;(component as any).mixins = [
78+
...((component as any).mixins || []),
79+
dataMixin
80+
]
7081
}
7182

7283
// we define props as reactive so that way when we update them with `setProps`
@@ -147,5 +158,5 @@ export function mount(originalComponent: any, options?: MountingOptions) {
147158
// mount the app!
148159
const app = vm.mount(el)
149160

150-
return createWrapper(app, events, setProps)
161+
return createWrapper<T>(app, events, setProps)
151162
}

src/vue-wrapper.ts

+8-9
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ import { WrapperAPI } from './types'
66
import { ErrorWrapper } from './error-wrapper'
77
import { MOUNT_ELEMENT_ID } from './constants'
88

9-
export class VueWrapper implements WrapperAPI {
10-
private componentVM: ComponentPublicInstance
9+
export class VueWrapper<T extends ComponentPublicInstance>
10+
implements WrapperAPI {
11+
private componentVM: T
1112
private __emitted: Record<string, unknown[]> = {}
1213
private __vm: ComponentPublicInstance
1314
private __setProps: (props: Record<string, any>) => void
@@ -19,9 +20,7 @@ export class VueWrapper implements WrapperAPI {
1920
) {
2021
this.__vm = vm
2122
this.__setProps = setProps
22-
this.componentVM = this.__vm.$refs[
23-
'VTU_COMPONENT'
24-
] as ComponentPublicInstance
23+
this.componentVM = this.__vm.$refs['VTU_COMPONENT'] as T
2524
this.__emitted = events
2625
}
2726

@@ -43,7 +42,7 @@ export class VueWrapper implements WrapperAPI {
4342
return this.hasMultipleRoots ? this.parentElement : this.componentVM.$el
4443
}
4544

46-
get vm(): ComponentPublicInstance {
45+
get vm(): T {
4746
return this.componentVM
4847
}
4948

@@ -106,10 +105,10 @@ export class VueWrapper implements WrapperAPI {
106105
}
107106
}
108107

109-
export function createWrapper(
108+
export function createWrapper<T extends ComponentPublicInstance>(
110109
vm: ComponentPublicInstance,
111110
events: Record<string, unknown[]>,
112111
setProps: (props: Record<string, any>) => void
113-
): VueWrapper {
114-
return new VueWrapper(vm, events, setProps)
112+
): VueWrapper<T> {
113+
return new VueWrapper<T>(vm, events, setProps)
115114
}

tests/vm.spec.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,18 @@ describe('vm', () => {
99
setup() {
1010
const msg = 'hello'
1111
const isEnabled = ref(true)
12-
return { msg, isEnabled }
12+
const toggle = () => (isEnabled.value = !isEnabled.value)
13+
return { msg, isEnabled, toggle }
1314
}
1415
})
1516

1617
const wrapper = mount(Component)
1718

18-
expect((wrapper.vm as any).msg).toBe('hello')
19-
expect((wrapper.vm as any).isEnabled).toBe(true)
19+
expect(wrapper.vm.msg).toBe('hello')
20+
expect(wrapper.vm.isEnabled).toBe(true)
21+
22+
wrapper.vm.toggle()
23+
24+
expect(wrapper.vm.isEnabled).toBe(false)
2025
})
2126
})

0 commit comments

Comments
 (0)