Skip to content

Allow mocks to be set in config object #531

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

Merged
merged 9 commits into from
Apr 18, 2018
19 changes: 18 additions & 1 deletion packages/shared/merge-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,29 @@ function getStubs (optionStubs, config) {
}
}

function getMocks (optionMocks, config) {
Copy link
Member

@eddyerburgh eddyerburgh Apr 15, 2018

Choose a reason for hiding this comment

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

This is nearly the same as the getStubs function, can you split it into a generic getOptions function?

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure. Maybe we can use it to provide an interface to all options.

Copy link
Member Author

@lmiller1990 lmiller1990 Apr 15, 2018

Choose a reason for hiding this comment

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

Ok! done.

  • methods also works in the same way, now that we have a generic getOptions. Just need to add it to mergeOptions. Should we add that in this commit? It's only a few lines.
  • computed might need a bit more work, since it needs a getter and setter to be set up. It can't just be added in the same way as methods and mocks.
  • I did not try props and data yet.

Copy link
Member

Choose a reason for hiding this comment

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

Great work! Yes, please add methods :)

if (optionMocks ||
(config.mocks && Object.keys(config.mocks).length > 0)) {
if (Array.isArray(optionMocks)) {
return [
...optionMocks,
...Object.keys(config.mocks || {})]
} else {
return {
...config.mocks,
...optionMocks
}
}
}
}

export function mergeOptions (
options: Options,
config: Options
): Options {
return {
...options,
stubs: getStubs(options.stubs, config)
stubs: getStubs(options.stubs, config),
mocks: getMocks(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