Skip to content

Commit de333f0

Browse files
committed
Add provide function value and test
1 parent 431cc9f commit de333f0

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

Diff for: src/lib/addProvide.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
function addProvide (component, options) {
2-
const provideOption = Object.assign({}, options.provide)
2+
const provide = typeof options.provide === 'function'
3+
? options.provide
4+
: Object.assign({}, options.provide)
5+
36
delete options.provide
47

58
const originalBeforeCreate = component.beforeCreate
69
component.beforeCreate = function beforeCreate () {
7-
this._provided = provideOption
10+
this._provided = typeof provide === 'function'
11+
? provide.call(this)
12+
: provide
813

914
if (originalBeforeCreate) {
1015
originalBeforeCreate.call(this)

Diff for: test/unit/specs/mount/options/provide.spec.js

+16-4
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,29 @@ import mount from '../../../../../src/mount'
22
import ComponentWithInject from '../../../../resources/components/component-with-inject.vue'
33

44
describe('provide option in mount', () => {
5-
it('provides value which is injected by mounted component', () => {
5+
it('provides objects which is injected by mounted component', () => {
66
const wrapper = mount(ComponentWithInject, {
7-
provide: { fromMount: 'providedValue' }
7+
provide: { fromMount: 'objectValue' }
88
})
99

10-
expect(wrapper.text()).to.contain('providedValue')
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')
1123
})
1224

1325
it('supports beforeCreate in component', () => {
1426
const wrapper = mount(ComponentWithInject, {
15-
provide: { fromMount: 'providedValue' }
27+
provide: { fromMount: '_' }
1628
})
1729

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

0 commit comments

Comments
 (0)