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

Allow mocks to be set in config object #531

Merged
merged 9 commits into from
Apr 18, 2018
19 changes: 19 additions & 0 deletions docs/en/api/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,22 @@ import VueTestUtils from '@vue/test-utils'

VueTestUtils.config.stubs['my-component'] = '<div />'
```

### `mocks`

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, for sure. Will do. Thanks.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Edit: actually, I don't think we need to. This doesn't add a new API, just build on the existing config page.

- type: `Object`
- default: `{}`

Like `stubs`, the values passed to `config.mocks` are used by default. Any values passed to the mounting options `mocks` object will take priority over the ones declared in `config.mocks`.

Example:

```js
import VueTestUtils from '@vue/test-utils'

VueTestUtils.config.mocks['$store'] = {
state: {
id: 1
}
}
```
20 changes: 11 additions & 9 deletions packages/shared/merge-options.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
// @flow

function getStubs (optionStubs, config) {
if (optionStubs ||
(config.stubs && Object.keys(config.stubs).length > 0)) {
if (Array.isArray(optionStubs)) {
function getOptions (key, options, config) {
if (options ||
(config[key] && Object.keys(config[key]).length > 0)) {
if (Array.isArray(options)) {
return [
...optionStubs,
...Object.keys(config.stubs || {})]
...options,
...Object.keys(config[key] || {})]
} else {
return {
...config.stubs,
...optionStubs
...config[key],
...options
}
}
}
Expand All @@ -22,6 +22,8 @@ export function mergeOptions (
): Options {
return {
...options,
stubs: getStubs(options.stubs, config)
stubs: getOptions('stubs', options.stubs, config),
mocks: getOptions('mocks', options.mocks, 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 @@ -5,5 +5,6 @@ export default {
stubs: {
transition: TransitionStub,
'transition-group': TransitionGroupStub
}
},
mocks: {}
}
26 changes: 25 additions & 1 deletion test/specs/config.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import {
mount,
config,
TransitionStub,
TransitionGroupStub
TransitionGroupStub,
createLocalVue
} from '~vue/test-utils'

describe('config', () => {
Expand Down Expand Up @@ -33,6 +34,29 @@ describe('config', () => {
expect(wrapper.contains(TransitionGroupStub)).to.equal(true)
})

it('mocks a global variable', () => {
const localVue = createLocalVue()
const t = 'real value'
localVue.prototype.$t = t

const testComponent = {
template: `
<div>{{ $t }}</div>
`
}

config.mocks['$t'] = 'mock value'

const wrapper = mount(testComponent, {
localVue, t
})

expect(wrapper.vm.$t).to.equal('mock value')
expect(wrapper.text()).to.equal('mock value')

localVue.prototype.$t = undefined
})

it('doesn\'t stub transition when config.stubs.transition is set to false', () => {
const testComponent = {
template: `
Expand Down