From 205acf8f7af61b3811bcc88cbf73181710df7858 Mon Sep 17 00:00:00 2001 From: Illya Klymov Date: Tue, 3 Sep 2019 14:18:24 +0300 Subject: [PATCH 1/3] fix: respect provide from parentComponent --- packages/create-instance/create-instance.js | 16 ++++- test/specs/config.spec.js | 4 ++ test/specs/mounting-options/methods.spec.js | 11 +++ test/specs/mounting-options/provide.spec.js | 80 +++++++++++++++++++++ 4 files changed, 110 insertions(+), 1 deletion(-) diff --git a/packages/create-instance/create-instance.js b/packages/create-instance/create-instance.js index 562c1f852..160c98a80 100644 --- a/packages/create-instance/create-instance.js +++ b/packages/create-instance/create-instance.js @@ -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, @@ -81,7 +88,14 @@ export default function createInstance( const parentComponentOptions = options.parentComponent || {} - parentComponentOptions.provide = 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) { diff --git a/test/specs/config.spec.js b/test/specs/config.spec.js index dc078413d..53725c52e 100644 --- a/test/specs/config.spec.js +++ b/test/specs/config.spec.js @@ -64,6 +64,10 @@ describeWithShallowAndMount('config', mountingMethod => { }) it('overrides a method', () => { + afterEach(() => { + delete config.methods['val'] + }) + const testComponent = { template: `
{{ val() }}
diff --git a/test/specs/mounting-options/methods.spec.js b/test/specs/mounting-options/methods.spec.js index 352f1643c..6cbaa5300 100644 --- a/test/specs/mounting-options/methods.spec.js +++ b/test/specs/mounting-options/methods.spec.js @@ -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' diff --git a/test/specs/mounting-options/provide.spec.js b/test/specs/mounting-options/provide.spec.js index 97bb9d32c..acd80df0c 100644 --- a/test/specs/mounting-options/provide.spec.js +++ b/test/specs/mounting-options/provide.spec.js @@ -29,6 +29,86 @@ describeWithShallowAndMount('options.provide', mountingMethod => { } ) + itDoNotRunIf( + !injectSupported, + 'respects provide values from parentComponent', + () => { + const parentComponent = { + provide: { + val: 'from parent' + } + } + + const child = { + inject: ['val', 'val2'], + template: '
' + } + + const wrapper = mountingMethod(child, { + parentComponent, + provide: { + val2: 'from config' + } + }) + + expect(wrapper.vm.val).to.equal('from parent') + expect(wrapper.vm.val2).to.equal('from config') + } + ) + + itDoNotRunIf( + !injectSupported, + 'respects provide function from parentComponent', + () => { + const parentComponent = { + provide() { + return { val: 'from parent' } + } + } + + const child = { + inject: ['val', 'val2'], + template: '
' + } + + const wrapper = mountingMethod(child, { + parentComponent, + provide: { + val2: 'from config' + } + }) + + expect(wrapper.vm.val).to.equal('from parent') + expect(wrapper.vm.val2).to.equal('from config') + } + ) + + itDoNotRunIf( + !injectSupported, + 'prioritize mounting options over parentComponent provide', + () => { + const parentComponent = { + provide() { + return { val: 'from parent' } + } + } + + const child = { + inject: ['val'], + template: '
' + } + + const wrapper = mountingMethod(child, { + parentComponent, + provide: { + val: 'from config' + } + }) + + expect(wrapper.vm.val).to.equal('from config') + } + ) + itDoNotRunIf( !injectSupported, 'provides function which is injected by mounted component', From 2658607537d2df41adf88649dad50b591bfa2bf1 Mon Sep 17 00:00:00 2001 From: Lachlan Miller Date: Tue, 7 Apr 2020 22:12:58 +1000 Subject: [PATCH 2/3] fix: remove afterEach from spec --- test/specs/config.spec.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/test/specs/config.spec.js b/test/specs/config.spec.js index b0b393eb4..660aafabc 100644 --- a/test/specs/config.spec.js +++ b/test/specs/config.spec.js @@ -45,10 +45,6 @@ describeWithShallowAndMount('config', mountingMethod => { }) it('overrides a method', () => { - afterEach(() => { - delete config.methods['val'] - }) - const testComponent = { template: `
{{ val() }}
From def9a18933cc07883186c74e25edbf34af485e53 Mon Sep 17 00:00:00 2001 From: Lachlan Miller Date: Tue, 7 Apr 2020 23:17:55 +1000 Subject: [PATCH 3/3] refactor: rename variable --- test/specs/mounting-options/provide.spec.js | 30 ++++++++++----------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/test/specs/mounting-options/provide.spec.js b/test/specs/mounting-options/provide.spec.js index c8217c53a..904958611 100644 --- a/test/specs/mounting-options/provide.spec.js +++ b/test/specs/mounting-options/provide.spec.js @@ -34,28 +34,28 @@ describeWithShallowAndMount('options.provide', mountingMethod => { itDoNotRunIf( !injectSupported, - 'respects provide values from parentComponent', + 'respects provide fooues from parentComponent', () => { const parentComponent = { provide: { - val: 'from parent' + foo: 'from parent' } } const child = { - inject: ['val', 'val2'], + inject: ['foo', 'foo2'], template: '
' } const wrapper = mountingMethod(child, { parentComponent, provide: { - val2: 'from config' + foo2: 'from config' } }) - expect(wrapper.vm.val).to.equal('from parent') - expect(wrapper.vm.val2).to.equal('from config') + expect(wrapper.vm.foo).to.equal('from parent') + expect(wrapper.vm.foo2).to.equal('from config') } ) @@ -65,24 +65,24 @@ describeWithShallowAndMount('options.provide', mountingMethod => { () => { const parentComponent = { provide() { - return { val: 'from parent' } + return { foo: 'from parent' } } } const child = { - inject: ['val', 'val2'], + inject: ['foo', 'foo2'], template: '
' } const wrapper = mountingMethod(child, { parentComponent, provide: { - val2: 'from config' + foo2: 'from config' } }) - expect(wrapper.vm.val).to.equal('from parent') - expect(wrapper.vm.val2).to.equal('from config') + expect(wrapper.vm.foo).to.equal('from parent') + expect(wrapper.vm.foo2).to.equal('from config') } ) @@ -92,23 +92,23 @@ describeWithShallowAndMount('options.provide', mountingMethod => { () => { const parentComponent = { provide() { - return { val: 'from parent' } + return { foo: 'from parent' } } } const child = { - inject: ['val'], + inject: ['foo'], template: '
' } const wrapper = mountingMethod(child, { parentComponent, provide: { - val: 'from config' + foo: 'from config' } }) - expect(wrapper.vm.val).to.equal('from config') + expect(wrapper.vm.foo).to.equal('from config') } )