Skip to content

Respect provide from parentComponent #1301

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions packages/create-instance/create-instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ function createChildren(vm, h, { slots, context }) {
)
}

function getValuesFromCallableOption(optionValue) {
if (typeof optionValue === 'function') {
return optionValue.call(this)
}
return optionValue
}

export default function createInstance(
component: Component,
options: NormalizedOptions,
Expand Down Expand Up @@ -99,8 +106,14 @@ export default function createInstance(

const parentComponentOptions = options.parentComponent || {}

parentComponentOptions.provide = options.provide
parentComponentOptions._provided = options.provide
const originalParentComponentProvide = parentComponentOptions.provide
parentComponentOptions.provide = function() {
return {
...getValuesFromCallableOption.call(this, originalParentComponentProvide),
...getValuesFromCallableOption.call(this, options.provide)
}
}

parentComponentOptions.$_doNotStubChildren = true
parentComponentOptions._isFunctionalContainer = componentOptions.functional
parentComponentOptions.render = function(h) {
Expand Down
11 changes: 11 additions & 0 deletions test/specs/mounting-options/methods.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@ import { config } from '@vue/test-utils'
import { describeWithShallowAndMount } from '~resources/utils'

describeWithShallowAndMount('options.methods', mountingMethod => {
let configMethodsSave

beforeEach(() => {
configMethodsSave = config.methods
config.methods = {}
})

afterEach(() => {
config.methods = configMethodsSave
})

it('prioritize mounting options over config', () => {
config.methods['val'] = () => 'methodFromConfig'

Expand Down
80 changes: 80 additions & 0 deletions test/specs/mounting-options/provide.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,86 @@ describeWithShallowAndMount('options.provide', mountingMethod => {
}
)

itDoNotRunIf(
!injectSupported,
'respects provide fooues from parentComponent',
() => {
const parentComponent = {
provide: {
foo: 'from parent'
}
}

const child = {
inject: ['foo', 'foo2'],
template: '<div></div>'
}

const wrapper = mountingMethod(child, {
parentComponent,
provide: {
foo2: 'from config'
}
})

expect(wrapper.vm.foo).to.equal('from parent')
expect(wrapper.vm.foo2).to.equal('from config')
}
)

itDoNotRunIf(
!injectSupported,
'respects provide function from parentComponent',
() => {
const parentComponent = {
provide() {
return { foo: 'from parent' }
}
}

const child = {
inject: ['foo', 'foo2'],
template: '<div></div>'
}

const wrapper = mountingMethod(child, {
parentComponent,
provide: {
foo2: 'from config'
}
})

expect(wrapper.vm.foo).to.equal('from parent')
expect(wrapper.vm.foo2).to.equal('from config')
}
)

itDoNotRunIf(
!injectSupported,
'prioritize mounting options over parentComponent provide',
() => {
const parentComponent = {
provide() {
return { foo: 'from parent' }
}
}

const child = {
inject: ['foo'],
template: '<div></div>'
}

const wrapper = mountingMethod(child, {
parentComponent,
provide: {
foo: 'from config'
}
})

expect(wrapper.vm.foo).to.equal('from config')
}
)

itDoNotRunIf(
!injectSupported,
'provides function which is injected by mounted component',
Expand Down