From 807d3c8110a61e6b505f3e747c956f2f3a29d55e Mon Sep 17 00:00:00 2001 From: "Jeremy \"Jay\" Zahner" Date: Wed, 25 Apr 2018 15:43:29 +0200 Subject: [PATCH] feat: add `provide` to the global config. (#555) --- docs/en/api/config.md | 19 +++++++++ packages/shared/merge-options.js | 11 +++-- packages/test-utils/src/config.js | 3 +- test/specs/mounting-options/provide.spec.js | 46 +++++++++++++++++++++ 4 files changed, 75 insertions(+), 4 deletions(-) diff --git a/docs/en/api/config.md b/docs/en/api/config.md index 9f7c51feb..a84befb06 100644 --- a/docs/en/api/config.md +++ b/docs/en/api/config.md @@ -60,3 +60,22 @@ VueTestUtils.config.methods['errors'] = () => { any: () => false } ``` + +### `provide` + +- type: `Object` +- default: `{}` + +Like `stubs` or `mocks`, the values passed to `config.provide` are used by default. Any values passed to the mounting options `provide` object will take priority over the ones declared in `config.provide`. **Please take note that it is not supported to pass a function as `config.provide`.** + +Example: + +```js +import VueTestUtils from '@vue/test-utils' + +VueTestUtils.config.provide['$logger'] = { + log: (...args) => { + console.log(...args) + } +} +``` diff --git a/packages/shared/merge-options.js b/packages/shared/merge-options.js index f02efca3c..e09ef95d7 100644 --- a/packages/shared/merge-options.js +++ b/packages/shared/merge-options.js @@ -3,15 +3,19 @@ function getOptions (key, options, config) { if (options || (config[key] && Object.keys(config[key]).length > 0)) { - if (Array.isArray(options)) { + if (options instanceof Function) { + return options + } else if (Array.isArray(options)) { return [ ...options, ...Object.keys(config[key] || {})] - } else { + } else if (!(config[key] instanceof Function)) { return { ...config[key], ...options } + } else { + throw new Error(`Config can't be a Function.`) } } } @@ -24,7 +28,8 @@ export function mergeOptions ( ...options, stubs: getOptions('stubs', options.stubs, config), mocks: getOptions('mocks', options.mocks, config), - methods: getOptions('methods', options.methods, config) + methods: getOptions('methods', options.methods, config), + provide: getOptions('provide', options.provide, config) } } diff --git a/packages/test-utils/src/config.js b/packages/test-utils/src/config.js index 534f8fc6a..af5cb548c 100644 --- a/packages/test-utils/src/config.js +++ b/packages/test-utils/src/config.js @@ -7,5 +7,6 @@ export default { 'transition-group': TransitionGroupStub }, mocks: {}, - methods: {} + methods: {}, + provide: {} } diff --git a/test/specs/mounting-options/provide.spec.js b/test/specs/mounting-options/provide.spec.js index e357a8853..9cbecb250 100644 --- a/test/specs/mounting-options/provide.spec.js +++ b/test/specs/mounting-options/provide.spec.js @@ -1,3 +1,4 @@ +import { config } from '~vue/test-utils' import ComponentWithInject from '~resources/components/component-with-inject.vue' import { injectSupported } from '~resources/utils' import { @@ -6,6 +7,17 @@ import { } from '~resources/utils' describeWithMountingMethods('options.provide', (mountingMethod) => { + let configProvideSave + + beforeEach(() => { + configProvideSave = config.provide + config.provide = {} + }) + + afterEach(() => { + config.provide = configProvideSave + }) + itDoNotRunIf(!injectSupported(), 'provides objects which is injected by mounted component', () => { if (!injectSupported()) return @@ -44,4 +56,38 @@ describeWithMountingMethods('options.provide', (mountingMethod) => { expect(wrapper.vm.setInBeforeCreate).to.equal('created') }) + + itDoNotRunIf(!injectSupported(), 'injects the provide from the config', () => { + config.provide['fromMount'] = 'globalConfig' + + const wrapper = mountingMethod(ComponentWithInject) + const HTML = mountingMethod.name === 'renderToString' + ? wrapper + : wrapper.html() + + expect(HTML).to.contain('globalConfig') + }) + + itDoNotRunIf(!injectSupported(), 'prioritize mounting options over config', () => { + config.provide['fromMount'] = 'globalConfig' + + const wrapper = mountingMethod(ComponentWithInject, { + provide: { fromMount: '_' } + }) + const HTML = mountingMethod.name === 'renderToString' + ? wrapper + : wrapper.html() + + expect(HTML).to.contain('_') + }) + + itDoNotRunIf(!injectSupported(), 'config with function throws', () => { + config.provide = () => {} + + expect(() => { + mountingMethod(ComponentWithInject, { + provide: { fromMount: '_' } + }) + }).to.throw() + }) })