Skip to content

Commit

Permalink
feat: add provide to the global config. (#555)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyzahner authored and eddyerburgh committed Apr 25, 2018
1 parent db5e07e commit 807d3c8
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 4 deletions.
19 changes: 19 additions & 0 deletions docs/en/api/config.md
Expand Up @@ -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)
}
}
```
11 changes: 8 additions & 3 deletions packages/shared/merge-options.js
Expand Up @@ -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.`)
}
}
}
Expand All @@ -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)
}
}

3 changes: 2 additions & 1 deletion packages/test-utils/src/config.js
Expand Up @@ -7,5 +7,6 @@ export default {
'transition-group': TransitionGroupStub
},
mocks: {},
methods: {}
methods: {},
provide: {}
}
46 changes: 46 additions & 0 deletions 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 {
Expand All @@ -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
Expand Down Expand Up @@ -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()
})
})

0 comments on commit 807d3c8

Please sign in to comment.