Skip to content

Commit 51bc025

Browse files
authored
Merge pull request #3 from Austio/provideInject
Add provide option to mount
2 parents 8a46240 + 9b5e557 commit 51bc025

File tree

4 files changed

+68
-0
lines changed

4 files changed

+68
-0
lines changed

src/lib/addProvide.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function addProvide (component, options) {
2+
const provide = typeof options.provide === 'function'
3+
? options.provide
4+
: Object.assign({}, options.provide)
5+
6+
delete options.provide
7+
8+
options.beforeCreate = function vueTestUtilBeforeCreate () {
9+
this._provided = typeof provide === 'function'
10+
? provide.call(this)
11+
: provide
12+
}
13+
}
14+
15+
export default addProvide

src/mount.js

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import Vue from 'vue'
44
import VueWrapper from './VueWrapper'
55
import addSlots from './lib/addSlots'
66
import addGlobals from './lib/addGlobals'
7+
import addProvide from './lib/addProvide'
78

89
Vue.config.productionTip = false
910

@@ -42,7 +43,12 @@ export default function mount (component: Component, options: MountOptions = {})
4243
// Remove cached constructor
4344
delete component._Ctor // eslint-disable-line no-param-reassign
4445

46+
if (options.provide) {
47+
addProvide(component, options)
48+
}
49+
4550
const Constructor = Vue.extend(component)
51+
4652
const vm = new Constructor(options)
4753

4854
if (options.slots) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<template>
2+
<div>
3+
{{ fromMount }}
4+
</div>
5+
</template>
6+
7+
<script>
8+
export default {
9+
name: 'component-with-inject',
10+
inject: ['fromMount'],
11+
beforeCreate() {
12+
this.setInBeforeCreate = 'created'
13+
}
14+
};
15+
</script>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import mount from '../../../../../src/mount'
2+
import ComponentWithInject from '../../../../resources/components/component-with-inject.vue'
3+
4+
describe('provide option in mount', () => {
5+
it('provides objects which is injected by mounted component', () => {
6+
const wrapper = mount(ComponentWithInject, {
7+
provide: { fromMount: 'objectValue' }
8+
})
9+
10+
expect(wrapper.text()).to.contain('objectValue')
11+
})
12+
13+
it('provides function which is injected by mounted component', () => {
14+
const wrapper = mount(ComponentWithInject, {
15+
provide () {
16+
return {
17+
fromMount: 'functionValue'
18+
}
19+
}
20+
})
21+
22+
expect(wrapper.text()).to.contain('functionValue')
23+
})
24+
25+
it('supports beforeCreate in component', () => {
26+
const wrapper = mount(ComponentWithInject, {
27+
provide: { fromMount: '_' }
28+
})
29+
30+
expect(wrapper.vm.setInBeforeCreate).to.equal('created')
31+
})
32+
})

0 commit comments

Comments
 (0)