From 33130696b6597e9a80cefe47ab8af7eaa95d56ce Mon Sep 17 00:00:00 2001 From: dobromir-hristov Date: Sat, 11 Apr 2020 18:47:05 +0300 Subject: [PATCH 1/2] feat: extend natively --- src/index.ts | 3 ++- tests/features/plugins.spec.ts | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 tests/features/plugins.spec.ts diff --git a/src/index.ts b/src/index.ts index 891f92be2..da2ebe0c3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,5 @@ import { mount } from './mount' import { RouterLinkStub } from './components/RouterLinkStub' +import { VueWrapper } from './vue-wrapper' -export { mount, RouterLinkStub } +export { mount, RouterLinkStub, VueWrapper } diff --git a/tests/features/plugins.spec.ts b/tests/features/plugins.spec.ts new file mode 100644 index 000000000..930c6027a --- /dev/null +++ b/tests/features/plugins.spec.ts @@ -0,0 +1,19 @@ +import { mount, VueWrapper } from '../../src' +import Hello from '../components/Hello.vue' +// add a method to the VueWrapper +declare module '../../src/vue-wrapper' { + interface VueWrapper { + name(): string + } +} + +VueWrapper.prototype.name = function () { + return this.componentVM.$.type.name +} + +describe('Plugin', () => { + it('adds name property', () => { + const wrapper = mount(Hello) + expect(wrapper.name()).toEqual('Hello') + }) +}) From c8f119e2a65e15aadd2adfe1f809ddcf3c9ce3fb Mon Sep 17 00:00:00 2001 From: dobromir-hristov Date: Sat, 11 Apr 2020 19:26:32 +0300 Subject: [PATCH 2/2] feat: extend natively --- src/config.ts | 6 ++++++ src/index.ts | 4 +++- src/vue-wrapper.ts | 8 ++++++++ tests/features/plugins.spec.ts | 15 ++++++++++++++- 4 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 src/config.ts diff --git a/src/config.ts b/src/config.ts new file mode 100644 index 000000000..4a394cfa4 --- /dev/null +++ b/src/config.ts @@ -0,0 +1,6 @@ +export const config = { + plugins: { + VueWrapper: {} as any, + DOMWrapper: {} + } +} diff --git a/src/index.ts b/src/index.ts index da2ebe0c3..a3fae534f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,4 +2,6 @@ import { mount } from './mount' import { RouterLinkStub } from './components/RouterLinkStub' import { VueWrapper } from './vue-wrapper' -export { mount, RouterLinkStub, VueWrapper } +import { config } from './config' + +export { mount, RouterLinkStub, VueWrapper, config } diff --git a/src/vue-wrapper.ts b/src/vue-wrapper.ts index 11d6249b0..cc6ffb0f5 100644 --- a/src/vue-wrapper.ts +++ b/src/vue-wrapper.ts @@ -1,5 +1,6 @@ import { ComponentPublicInstance, nextTick } from 'vue' import { ShapeFlags } from '@vue/shared' +import { config } from './config' import { DOMWrapper } from './dom-wrapper' import { WrapperAPI } from './types' @@ -21,6 +22,13 @@ export class VueWrapper implements WrapperAPI { this.__setProps = setProps this.componentVM = this.vm.$refs['VTU_COMPONENT'] as ComponentPublicInstance this.__emitted = events + + // plugins hook + Object.entries(config.plugins.VueWrapper).forEach( + ([name, handler]: [string, () => any]) => { + this[name] = handler.bind(this) + } + ) } private get appRootNode() { diff --git a/tests/features/plugins.spec.ts b/tests/features/plugins.spec.ts index 930c6027a..5b9f6cfb4 100644 --- a/tests/features/plugins.spec.ts +++ b/tests/features/plugins.spec.ts @@ -1,9 +1,11 @@ -import { mount, VueWrapper } from '../../src' +import { mount, VueWrapper, config } from '../../src' import Hello from '../components/Hello.vue' // add a method to the VueWrapper declare module '../../src/vue-wrapper' { interface VueWrapper { name(): string + + lowerCaseName(): string[] } } @@ -11,9 +13,20 @@ VueWrapper.prototype.name = function () { return this.componentVM.$.type.name } +config.plugins.VueWrapper.lowerCaseName = function lowerCaseName( + this: VueWrapper +) { + return this.name().toLocaleLowerCase() +} + describe('Plugin', () => { it('adds name property', () => { const wrapper = mount(Hello) expect(wrapper.name()).toEqual('Hello') }) + + it('adds loweCase name property', () => { + const wrapper = mount(Hello) + expect(wrapper.lowerCaseName()).toEqual('hello') + }) })