Skip to content
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

feat: Adds provide to the global config. #555

Merged
merged 1 commit into from
Apr 25, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 19 additions & 0 deletions docs/en/api/config.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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()
})
})