Skip to content

Commit 205acf8

Browse files
committed
fix: respect provide from parentComponent
1 parent 9dc90a3 commit 205acf8

File tree

4 files changed

+110
-1
lines changed

4 files changed

+110
-1
lines changed

Diff for: packages/create-instance/create-instance.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ function createChildren(vm, h, { slots, context }) {
3939
)
4040
}
4141

42+
function getValuesFromCallableOption(optionValue) {
43+
if (typeof optionValue === 'function') {
44+
return optionValue.call(this)
45+
}
46+
return optionValue
47+
}
48+
4249
export default function createInstance(
4350
component: Component,
4451
options: NormalizedOptions,
@@ -81,7 +88,14 @@ export default function createInstance(
8188

8289
const parentComponentOptions = options.parentComponent || {}
8390

84-
parentComponentOptions.provide = options.provide
91+
const originalParentComponentProvide = parentComponentOptions.provide
92+
parentComponentOptions.provide = function() {
93+
return {
94+
...getValuesFromCallableOption.call(this, originalParentComponentProvide),
95+
...getValuesFromCallableOption.call(this, options.provide)
96+
}
97+
}
98+
8599
parentComponentOptions.$_doNotStubChildren = true
86100
parentComponentOptions._isFunctionalContainer = componentOptions.functional
87101
parentComponentOptions.render = function(h) {

Diff for: test/specs/config.spec.js

+4
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ describeWithShallowAndMount('config', mountingMethod => {
6464
})
6565

6666
it('overrides a method', () => {
67+
afterEach(() => {
68+
delete config.methods['val']
69+
})
70+
6771
const testComponent = {
6872
template: `
6973
<div>{{ val() }}</div>

Diff for: test/specs/mounting-options/methods.spec.js

+11
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@ import { config } from '~vue/test-utils'
22
import { describeWithShallowAndMount } from '~resources/utils'
33

44
describeWithShallowAndMount('options.methods', mountingMethod => {
5+
let configMethodsSave
6+
7+
beforeEach(() => {
8+
configMethodsSave = config.methods
9+
config.methods = {}
10+
})
11+
12+
afterEach(() => {
13+
config.methods = configMethodsSave
14+
})
15+
516
it('prioritize mounting options over config', () => {
617
config.methods['val'] = () => 'methodFromConfig'
718

Diff for: test/specs/mounting-options/provide.spec.js

+80
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,86 @@ describeWithShallowAndMount('options.provide', mountingMethod => {
2929
}
3030
)
3131

32+
itDoNotRunIf(
33+
!injectSupported,
34+
'respects provide values from parentComponent',
35+
() => {
36+
const parentComponent = {
37+
provide: {
38+
val: 'from parent'
39+
}
40+
}
41+
42+
const child = {
43+
inject: ['val', 'val2'],
44+
template: '<div></div>'
45+
}
46+
47+
const wrapper = mountingMethod(child, {
48+
parentComponent,
49+
provide: {
50+
val2: 'from config'
51+
}
52+
})
53+
54+
expect(wrapper.vm.val).to.equal('from parent')
55+
expect(wrapper.vm.val2).to.equal('from config')
56+
}
57+
)
58+
59+
itDoNotRunIf(
60+
!injectSupported,
61+
'respects provide function from parentComponent',
62+
() => {
63+
const parentComponent = {
64+
provide() {
65+
return { val: 'from parent' }
66+
}
67+
}
68+
69+
const child = {
70+
inject: ['val', 'val2'],
71+
template: '<div></div>'
72+
}
73+
74+
const wrapper = mountingMethod(child, {
75+
parentComponent,
76+
provide: {
77+
val2: 'from config'
78+
}
79+
})
80+
81+
expect(wrapper.vm.val).to.equal('from parent')
82+
expect(wrapper.vm.val2).to.equal('from config')
83+
}
84+
)
85+
86+
itDoNotRunIf(
87+
!injectSupported,
88+
'prioritize mounting options over parentComponent provide',
89+
() => {
90+
const parentComponent = {
91+
provide() {
92+
return { val: 'from parent' }
93+
}
94+
}
95+
96+
const child = {
97+
inject: ['val'],
98+
template: '<div></div>'
99+
}
100+
101+
const wrapper = mountingMethod(child, {
102+
parentComponent,
103+
provide: {
104+
val: 'from config'
105+
}
106+
})
107+
108+
expect(wrapper.vm.val).to.equal('from config')
109+
}
110+
)
111+
32112
itDoNotRunIf(
33113
!injectSupported,
34114
'provides function which is injected by mounted component',

0 commit comments

Comments
 (0)