Skip to content

Add provide option to mount #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 7, 2017
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/lib/addProvide.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function addProvide (component, options) {
const provideOption = Object.assign({}, options.provide)
delete options.provide

const originalBeforeCreate = component.beforeCreate
component.beforeCreate = function beforeCreate () {
this._provided = provideOption

if (originalBeforeCreate) {
originalBeforeCreate.call(this)
}
}
}

export default addProvide
6 changes: 6 additions & 0 deletions src/mount.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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) {
Expand Down
15 changes: 15 additions & 0 deletions test/resources/components/component-with-inject.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<template>
<div>
{{ fromMount }}
</div>
</template>

<script>
export default {
name: 'component-with-inject',
inject: ['fromMount'],
beforeCreate() {
this.setInBeforeCreate = 'created'
}
};
</script>
20 changes: 20 additions & 0 deletions test/unit/specs/mount/options/provide.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import mount from '../../../../../src/mount'
import ComponentWithInject from '../../../../resources/components/component-with-inject.vue'

describe('provide option in mount', () => {
it('provides value which is injected by mounted component', () => {
const wrapper = mount(ComponentWithInject, {
provide: { fromMount: 'providedValue' }
})

expect(wrapper.text()).to.contain('providedValue')
})

it('supports beforeCreate in component', () => {
const wrapper = mount(ComponentWithInject, {
provide: { fromMount: 'providedValue' }
})

expect(wrapper.vm.setInBeforeCreate).to.equal('created')
})
})