Skip to content

Commit 980434b

Browse files
authored
feat(test-utils): add type definitions for ExtendedVue (#1789)
add mount & shallowMount type definitions for ExtendedVue, return type of Vue.extend() method this fixes #1781
1 parent 6f574eb commit 980434b

File tree

4 files changed

+62
-5
lines changed

4 files changed

+62
-5
lines changed

packages/test-utils/types/index.d.ts

+15-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import Vue, { VNodeData, ComponentOptions, FunctionalComponentOptions, Component } from 'vue'
1+
import Vue, { VNodeData, ComponentOptions, FunctionalComponentOptions, Component, RenderContext } from 'vue'
22
import { DefaultProps, PropsDefinition } from 'vue/types/options'
3+
import { ExtendedVue, CombinedVueInstance } from 'vue/types/vue'
34

45
/**
56
* Utility type to declare an extended Vue constructor
@@ -161,10 +162,18 @@ interface MountOptions<V extends Vue> extends ComponentOptions<V> {
161162

162163
type ThisTypedMountOptions<V extends Vue> = MountOptions<V> & ThisType<V>
163164

165+
interface FunctionalComponentMountOptions<V extends Vue> extends MountOptions<V> {
166+
context?: Partial<RenderContext>
167+
}
168+
164169
type ShallowMountOptions<V extends Vue> = MountOptions<V>
165170

166171
type ThisTypedShallowMountOptions<V extends Vue> = ShallowMountOptions<V> & ThisType<V>
167172

173+
interface FunctionalComponentShallowMountOptions<V extends Vue> extends ShallowMountOptions<V> {
174+
context?: Partial<RenderContext>
175+
}
176+
168177
interface VueTestUtilsConfigOptions {
169178
stubs: Record<string, Component | boolean | string>
170179
mocks: Record<string, any>
@@ -179,11 +188,15 @@ export declare let config: VueTestUtilsConfigOptions
179188

180189
export declare function mount<V extends Vue> (component: VueClass<V>, options?: ThisTypedMountOptions<V>): Wrapper<V>
181190
export declare function mount<V extends Vue> (component: ComponentOptions<V>, options?: ThisTypedMountOptions<V>): Wrapper<V>
182-
export declare function mount<Props = DefaultProps, PropDefs = PropsDefinition<Props>>(component: FunctionalComponentOptions<Props, PropDefs>, options?: MountOptions<Vue>): Wrapper<Vue>
191+
export declare function mount<V extends Vue, Data, Methods, Computed, Props> (component: ExtendedVue<V, Data, Methods, Computed, Props>, options?: ThisTypedMountOptions<V>): Wrapper<CombinedVueInstance<V, Data, Methods, Computed, Props> & Vue>
192+
export declare function mount<Props = DefaultProps, PropDefs = PropsDefinition<Props>> (component: FunctionalComponentOptions<Props, PropDefs>, options?: MountOptions<Vue>): Wrapper<Vue>
193+
export declare function mount<V extends Vue, Props = DefaultProps> (component: ExtendedVue<V, {}, {}, {}, Props>, options?: FunctionalComponentMountOptions<V>): Wrapper<CombinedVueInstance<V, {}, {}, {}, Props> & Vue>
183194

184195
export declare function shallowMount<V extends Vue> (component: VueClass<V>, options?: ThisTypedShallowMountOptions<V>): Wrapper<V>
185196
export declare function shallowMount<V extends Vue> (component: ComponentOptions<V>, options?: ThisTypedShallowMountOptions<V>): Wrapper<V>
197+
export declare function shallowMount<V extends Vue, Data, Methods, Computed, Props> (component: ExtendedVue<V, Data, Methods, Computed, Props>, options?: ThisTypedShallowMountOptions<V>): Wrapper<CombinedVueInstance<V, Data, Methods, Computed, Props> & Vue>
186198
export declare function shallowMount<Props = DefaultProps, PropDefs = PropsDefinition<Props>>(component: FunctionalComponentOptions<Props, PropDefs>, options?: ShallowMountOptions<Vue>): Wrapper<Vue>
199+
export declare function shallowMount<V extends Vue, Props = DefaultProps> (component: ExtendedVue<V, {}, {}, {}, Props>, options?: FunctionalComponentShallowMountOptions<V>): Wrapper<CombinedVueInstance<V, {}, {}, {}, Props> & Vue>
187200

188201
export declare function createWrapper(node: Vue, options?: WrapperOptions): Wrapper<Vue>
189202
export declare function createWrapper(node: HTMLElement, options?: WrapperOptions): Wrapper<null>

packages/test-utils/types/test/mount.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Vuex from 'vuex'
22
import VueTestUtils, { mount, createLocalVue, config } from '../'
3-
import { normalOptions, functionalOptions, ClassComponent } from './resources'
3+
import { normalOptions, functionalOptions, ClassComponent, extendedFunctionalComponent, extendedNormalComponent } from './resources'
44

55
/**
66
* Should create wrapper vm based on (function) component options or constructors
@@ -9,10 +9,14 @@ import { normalOptions, functionalOptions, ClassComponent } from './resources'
99
const normalWrapper = mount(normalOptions)
1010
const normalFoo: string = normalWrapper.vm.foo
1111

12+
const extendedNormalWrapper = mount(extendedNormalComponent)
13+
const extendedNormalFoo: string = extendedNormalWrapper.vm.foo
14+
1215
const classWrapper = mount(ClassComponent)
1316
const classFoo: string = classWrapper.vm.bar
1417

1518
const functionalWrapper = mount(functionalOptions)
19+
const extendedFunctionalWrapper = mount(extendedFunctionalComponent)
1620

1721
/**
1822
* Test for mount options
@@ -62,6 +66,15 @@ mount(functionalOptions, {
6266
stubs: ['child']
6367
})
6468

69+
mount(extendedFunctionalComponent, {
70+
context: {
71+
props: { foo: 'test' },
72+
data: {}
73+
},
74+
attachTo: document.createElement('div'),
75+
stubs: ['child']
76+
})
77+
6578
/**
6679
* MountOptions should receive Vue's component options
6780
*/

packages/test-utils/types/test/resources.ts

+20-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Vue, { ComponentOptions, FunctionalComponentOptions } from 'vue'
1+
import Vue, { ComponentOptions, FunctionalComponentOptions, CreateElement, RenderContext, VNode } from 'vue'
22

33
/**
44
* Normal component options
@@ -15,6 +15,15 @@ export const normalOptions: ComponentOptions<Normal> = {
1515
}
1616
}
1717

18+
export const extendedNormalComponent = Vue.extend({
19+
name: 'normal',
20+
data() {
21+
return {
22+
foo: 'bar'
23+
}
24+
}
25+
})
26+
1827
/**
1928
* Functional component options
2029
*/
@@ -25,6 +34,16 @@ export const functionalOptions: FunctionalComponentOptions = {
2534
}
2635
}
2736

37+
/**
38+
* Functional component with Vue.extend()
39+
*/
40+
export const extendedFunctionalComponent = Vue.extend({
41+
functional: true,
42+
render: (createElement: CreateElement, context: RenderContext): VNode => {
43+
return createElement('div')
44+
}
45+
})
46+
2847
/**
2948
* Component constructor declared with vue-class-component etc.
3049
*/

packages/test-utils/types/test/shallow.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Vuex from 'vuex'
22
import { shallowMount, createLocalVue } from '../'
3-
import { normalOptions, functionalOptions, ClassComponent } from './resources'
3+
import { normalOptions, functionalOptions, ClassComponent, extendedFunctionalComponent, extendedNormalComponent } from './resources'
44

55
/**
66
* Should create wrapper vm based on (function) component options or constructors
@@ -9,10 +9,14 @@ import { normalOptions, functionalOptions, ClassComponent } from './resources'
99
const normalWrapper = shallowMount(normalOptions)
1010
const normalFoo: string = normalWrapper.vm.foo
1111

12+
const extendedNormalWrapper = shallowMount(extendedNormalComponent)
13+
const extendedNormalFoo: string = extendedNormalWrapper.vm.foo
14+
1215
const classWrapper = shallowMount(ClassComponent)
1316
const classFoo: string = classWrapper.vm.bar
1417

1518
const functinalWrapper = shallowMount(functionalOptions)
19+
const extendedFunctionalWrapper = shallowMount(extendedFunctionalComponent)
1620

1721
/**
1822
* Test for shallowMount options
@@ -54,6 +58,14 @@ shallowMount(functionalOptions, {
5458
stubs: ['child']
5559
})
5660

61+
shallowMount(extendedFunctionalComponent, {
62+
context: {
63+
props: { foo: 'test' },
64+
data: {}
65+
},
66+
stubs: ['child']
67+
})
68+
5769
/**
5870
* ShallowMountOptions should receive Vue's component options
5971
*/

0 commit comments

Comments
 (0)