Skip to content

Commit 431cc9f

Browse files
committed
Move provide work to addProvide function
1 parent 24e842f commit 431cc9f

File tree

4 files changed

+27
-12
lines changed

4 files changed

+27
-12
lines changed

Diff for: src/lib/addProvide.js

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

Diff for: src/mount.js

+2-6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import Vue from 'vue'
22
import VueWrapper from './VueWrapper'
33
import addSlots from './lib/addSlots'
44
import addGlobals from './lib/addGlobals'
5+
import addProvide from './lib/addProvide'
56

67
Vue.config.productionTip = false
78

@@ -32,12 +33,7 @@ export default function mount (component, options = {}) {
3233
delete component._Ctor // eslint-disable-line no-param-reassign
3334

3435
if (options.provide) {
35-
const provide = Object.assign({}, options.provide)
36-
delete options.provide
37-
38-
component.beforeCreate = function beforeCreate() {
39-
this._provided = provide
40-
}
36+
addProvide(component, options)
4137
}
4238

4339
const Constructor = Vue.extend(component)

Diff for: test/resources/components/component-with-inject.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
name: 'component-with-inject',
1010
inject: ['fromMount'],
1111
beforeCreate() {
12-
this.setByOriginalBeforeCreate = 'created'
12+
this.setInBeforeCreate = 'created'
1313
}
1414
};
1515
</script>

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

+9-5
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,19 @@ import mount from '../../../../../src/mount'
22
import ComponentWithInject from '../../../../resources/components/component-with-inject.vue'
33

44
describe('provide option in mount', () => {
5-
const wrapper = mount(ComponentWithInject, {
6-
provide: { fromMount: 'providedValue' }
7-
})
8-
95
it('provides value which is injected by mounted component', () => {
6+
const wrapper = mount(ComponentWithInject, {
7+
provide: { fromMount: 'providedValue' }
8+
})
9+
1010
expect(wrapper.text()).to.contain('providedValue')
1111
})
1212

1313
it('supports beforeCreate in component', () => {
14-
expect(wrapper.vm.setByOriginalBeforeCreate).to.toEqual('created')
14+
const wrapper = mount(ComponentWithInject, {
15+
provide: { fromMount: 'providedValue' }
16+
})
17+
18+
expect(wrapper.vm.setInBeforeCreate).to.equal('created')
1519
})
1620
})

0 commit comments

Comments
 (0)