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 891f92be2..a3fae534f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,7 @@ import { mount } from './mount' import { RouterLinkStub } from './components/RouterLinkStub' +import { VueWrapper } from './vue-wrapper' -export { mount, RouterLinkStub } +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 new file mode 100644 index 000000000..5b9f6cfb4 --- /dev/null +++ b/tests/features/plugins.spec.ts @@ -0,0 +1,32 @@ +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[] + } +} + +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') + }) +})