From 980434b9eac6a6ee5ba770ae839a19805f344a93 Mon Sep 17 00:00:00 2001 From: Yasuhiro SHIMIZU Date: Wed, 24 Feb 2021 16:33:03 +0900 Subject: [PATCH] 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 --- packages/test-utils/types/index.d.ts | 17 +++++++++++++++-- packages/test-utils/types/test/mount.ts | 15 ++++++++++++++- packages/test-utils/types/test/resources.ts | 21 ++++++++++++++++++++- packages/test-utils/types/test/shallow.ts | 14 +++++++++++++- 4 files changed, 62 insertions(+), 5 deletions(-) diff --git a/packages/test-utils/types/index.d.ts b/packages/test-utils/types/index.d.ts index 05b78fbca..ee3938787 100644 --- a/packages/test-utils/types/index.d.ts +++ b/packages/test-utils/types/index.d.ts @@ -1,5 +1,6 @@ -import Vue, { VNodeData, ComponentOptions, FunctionalComponentOptions, Component } from 'vue' +import Vue, { VNodeData, ComponentOptions, FunctionalComponentOptions, Component, RenderContext } from 'vue' import { DefaultProps, PropsDefinition } from 'vue/types/options' +import { ExtendedVue, CombinedVueInstance } from 'vue/types/vue' /** * Utility type to declare an extended Vue constructor @@ -161,10 +162,18 @@ interface MountOptions extends ComponentOptions { type ThisTypedMountOptions = MountOptions & ThisType +interface FunctionalComponentMountOptions extends MountOptions { + context?: Partial +} + type ShallowMountOptions = MountOptions type ThisTypedShallowMountOptions = ShallowMountOptions & ThisType +interface FunctionalComponentShallowMountOptions extends ShallowMountOptions { + context?: Partial +} + interface VueTestUtilsConfigOptions { stubs: Record mocks: Record @@ -179,11 +188,15 @@ export declare let config: VueTestUtilsConfigOptions export declare function mount (component: VueClass, options?: ThisTypedMountOptions): Wrapper export declare function mount (component: ComponentOptions, options?: ThisTypedMountOptions): Wrapper -export declare function mount>(component: FunctionalComponentOptions, options?: MountOptions): Wrapper +export declare function mount (component: ExtendedVue, options?: ThisTypedMountOptions): Wrapper & Vue> +export declare function mount> (component: FunctionalComponentOptions, options?: MountOptions): Wrapper +export declare function mount (component: ExtendedVue, options?: FunctionalComponentMountOptions): Wrapper & Vue> export declare function shallowMount (component: VueClass, options?: ThisTypedShallowMountOptions): Wrapper export declare function shallowMount (component: ComponentOptions, options?: ThisTypedShallowMountOptions): Wrapper +export declare function shallowMount (component: ExtendedVue, options?: ThisTypedShallowMountOptions): Wrapper & Vue> export declare function shallowMount>(component: FunctionalComponentOptions, options?: ShallowMountOptions): Wrapper +export declare function shallowMount (component: ExtendedVue, options?: FunctionalComponentShallowMountOptions): Wrapper & Vue> export declare function createWrapper(node: Vue, options?: WrapperOptions): Wrapper export declare function createWrapper(node: HTMLElement, options?: WrapperOptions): Wrapper diff --git a/packages/test-utils/types/test/mount.ts b/packages/test-utils/types/test/mount.ts index 038b08205..726337259 100644 --- a/packages/test-utils/types/test/mount.ts +++ b/packages/test-utils/types/test/mount.ts @@ -1,6 +1,6 @@ import Vuex from 'vuex' import VueTestUtils, { mount, createLocalVue, config } from '../' -import { normalOptions, functionalOptions, ClassComponent } from './resources' +import { normalOptions, functionalOptions, ClassComponent, extendedFunctionalComponent, extendedNormalComponent } from './resources' /** * Should create wrapper vm based on (function) component options or constructors @@ -9,10 +9,14 @@ import { normalOptions, functionalOptions, ClassComponent } from './resources' const normalWrapper = mount(normalOptions) const normalFoo: string = normalWrapper.vm.foo +const extendedNormalWrapper = mount(extendedNormalComponent) +const extendedNormalFoo: string = extendedNormalWrapper.vm.foo + const classWrapper = mount(ClassComponent) const classFoo: string = classWrapper.vm.bar const functionalWrapper = mount(functionalOptions) +const extendedFunctionalWrapper = mount(extendedFunctionalComponent) /** * Test for mount options @@ -62,6 +66,15 @@ mount(functionalOptions, { stubs: ['child'] }) +mount(extendedFunctionalComponent, { + context: { + props: { foo: 'test' }, + data: {} + }, + attachTo: document.createElement('div'), + stubs: ['child'] +}) + /** * MountOptions should receive Vue's component options */ diff --git a/packages/test-utils/types/test/resources.ts b/packages/test-utils/types/test/resources.ts index 4195c5c8d..045a31604 100644 --- a/packages/test-utils/types/test/resources.ts +++ b/packages/test-utils/types/test/resources.ts @@ -1,4 +1,4 @@ -import Vue, { ComponentOptions, FunctionalComponentOptions } from 'vue' +import Vue, { ComponentOptions, FunctionalComponentOptions, CreateElement, RenderContext, VNode } from 'vue' /** * Normal component options @@ -15,6 +15,15 @@ export const normalOptions: ComponentOptions = { } } +export const extendedNormalComponent = Vue.extend({ + name: 'normal', + data() { + return { + foo: 'bar' + } + } +}) + /** * Functional component options */ @@ -25,6 +34,16 @@ export const functionalOptions: FunctionalComponentOptions = { } } +/** + * Functional component with Vue.extend() + */ +export const extendedFunctionalComponent = Vue.extend({ + functional: true, + render: (createElement: CreateElement, context: RenderContext): VNode => { + return createElement('div') + } +}) + /** * Component constructor declared with vue-class-component etc. */ diff --git a/packages/test-utils/types/test/shallow.ts b/packages/test-utils/types/test/shallow.ts index d8e96d787..dd6ccff77 100644 --- a/packages/test-utils/types/test/shallow.ts +++ b/packages/test-utils/types/test/shallow.ts @@ -1,6 +1,6 @@ import Vuex from 'vuex' import { shallowMount, createLocalVue } from '../' -import { normalOptions, functionalOptions, ClassComponent } from './resources' +import { normalOptions, functionalOptions, ClassComponent, extendedFunctionalComponent, extendedNormalComponent } from './resources' /** * Should create wrapper vm based on (function) component options or constructors @@ -9,10 +9,14 @@ import { normalOptions, functionalOptions, ClassComponent } from './resources' const normalWrapper = shallowMount(normalOptions) const normalFoo: string = normalWrapper.vm.foo +const extendedNormalWrapper = shallowMount(extendedNormalComponent) +const extendedNormalFoo: string = extendedNormalWrapper.vm.foo + const classWrapper = shallowMount(ClassComponent) const classFoo: string = classWrapper.vm.bar const functinalWrapper = shallowMount(functionalOptions) +const extendedFunctionalWrapper = shallowMount(extendedFunctionalComponent) /** * Test for shallowMount options @@ -54,6 +58,14 @@ shallowMount(functionalOptions, { stubs: ['child'] }) +shallowMount(extendedFunctionalComponent, { + context: { + props: { foo: 'test' }, + data: {} + }, + stubs: ['child'] +}) + /** * ShallowMountOptions should receive Vue's component options */