Skip to content

Commit 351f573

Browse files
xanflmiller1990
andauthored
Respect provide from parentComponent (#1301)
* fix: respect provide from parentComponent * fix: remove afterEach from spec * refactor: rename variable Co-authored-by: Lachlan Miller <[email protected]>
1 parent a4cbe93 commit 351f573

File tree

3 files changed

+106
-2
lines changed

3 files changed

+106
-2
lines changed

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

+15-2
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,
@@ -99,8 +106,14 @@ export default function createInstance(
99106

100107
const parentComponentOptions = options.parentComponent || {}
101108

102-
parentComponentOptions.provide = options.provide
103-
parentComponentOptions._provided = options.provide
109+
const originalParentComponentProvide = parentComponentOptions.provide
110+
parentComponentOptions.provide = function() {
111+
return {
112+
...getValuesFromCallableOption.call(this, originalParentComponentProvide),
113+
...getValuesFromCallableOption.call(this, options.provide)
114+
}
115+
}
116+
104117
parentComponentOptions.$_doNotStubChildren = true
105118
parentComponentOptions._isFunctionalContainer = componentOptions.functional
106119
parentComponentOptions.render = function(h) {

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
@@ -32,6 +32,86 @@ describeWithShallowAndMount('options.provide', mountingMethod => {
3232
}
3333
)
3434

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

0 commit comments

Comments
 (0)