diff --git a/src/lib/addProvide.js b/src/lib/addProvide.js new file mode 100644 index 000000000..5c60a5ac5 --- /dev/null +++ b/src/lib/addProvide.js @@ -0,0 +1,15 @@ +function addProvide (component, options) { + const provide = typeof options.provide === 'function' + ? options.provide + : Object.assign({}, options.provide) + + delete options.provide + + options.beforeCreate = function vueTestUtilBeforeCreate () { + this._provided = typeof provide === 'function' + ? provide.call(this) + : provide + } +} + +export default addProvide diff --git a/src/mount.js b/src/mount.js index 4821b25f8..bc2591bf6 100644 --- a/src/mount.js +++ b/src/mount.js @@ -2,6 +2,7 @@ import Vue from 'vue' import VueWrapper from './VueWrapper' import addSlots from './lib/addSlots' import addGlobals from './lib/addGlobals' +import addProvide from './lib/addProvide' Vue.config.productionTip = false @@ -31,7 +32,12 @@ export default function mount (component, options = {}) { // Remove cached constructor delete component._Ctor // eslint-disable-line no-param-reassign + if (options.provide) { + addProvide(component, options) + } + const Constructor = Vue.extend(component) + const vm = new Constructor(options) if (options.slots) { diff --git a/test/resources/components/component-with-inject.vue b/test/resources/components/component-with-inject.vue new file mode 100644 index 000000000..ab5178e0b --- /dev/null +++ b/test/resources/components/component-with-inject.vue @@ -0,0 +1,15 @@ + + + diff --git a/test/unit/specs/mount/options/provide.spec.js b/test/unit/specs/mount/options/provide.spec.js new file mode 100644 index 000000000..9d3a3aad8 --- /dev/null +++ b/test/unit/specs/mount/options/provide.spec.js @@ -0,0 +1,32 @@ +import mount from '../../../../../src/mount' +import ComponentWithInject from '../../../../resources/components/component-with-inject.vue' + +describe('provide option in mount', () => { + it('provides objects which is injected by mounted component', () => { + const wrapper = mount(ComponentWithInject, { + provide: { fromMount: 'objectValue' } + }) + + expect(wrapper.text()).to.contain('objectValue') + }) + + it('provides function which is injected by mounted component', () => { + const wrapper = mount(ComponentWithInject, { + provide () { + return { + fromMount: 'functionValue' + } + } + }) + + expect(wrapper.text()).to.contain('functionValue') + }) + + it('supports beforeCreate in component', () => { + const wrapper = mount(ComponentWithInject, { + provide: { fromMount: '_' } + }) + + expect(wrapper.vm.setInBeforeCreate).to.equal('created') + }) +})