Skip to content

Commit 9960f7c

Browse files
lmiller1990eddyerburgh
authored andcommittedApr 18, 2018
feat: allow mocks and methods to be set in config object (#531)
1 parent f3d4086 commit 9960f7c

File tree

8 files changed

+156
-13
lines changed

8 files changed

+156
-13
lines changed
 

Diff for: ‎docs/.eslintrc

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
},
1515
"rules": {
1616
"no-unused-vars": 0,
17-
"no-undef": 0
17+
"no-undef": 0,
18+
"no-labels": 0
1819
}
1920
}

Diff for: ‎docs/en/api/config.md

+36
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,39 @@ import VueTestUtils from '@vue/test-utils'
2424

2525
VueTestUtils.config.stubs['my-component'] = '<div />'
2626
```
27+
28+
### `mocks`
29+
30+
- type: `Object`
31+
- default: `{}`
32+
33+
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`.
34+
35+
Example:
36+
37+
```js
38+
import VueTestUtils from '@vue/test-utils'
39+
40+
VueTestUtils.config.mocks['$store'] = {
41+
state: {
42+
id: 1
43+
}
44+
}
45+
```
46+
47+
### `methods`
48+
49+
- type: `Object`
50+
- default: `{}`
51+
52+
You can configure default methods using the `config` object. This can be useful for plugins that inject methods to components, like [VeeValidate](https://vee-validate.logaretm.com/). You can override methods set in `config` by passing `methods` in the mounting options.
53+
54+
Example:
55+
56+
```js
57+
import VueTestUtils from '@vue/test-utils'
58+
59+
VueTestUtils.config.methods['errors'] = () => {
60+
any: () => false
61+
}
62+
```

Diff for: ‎flow/options.flow.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
declare type Options = { // eslint-disable-line no-undef
22
attachToDocument?: boolean,
33
mocks?: Object,
4+
methods?: Object,
45
slots?: Object,
56
scopedSlots?: Object,
67
localVue?: Component,

Diff for: ‎packages/shared/merge-options.js

+12-9
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
// @flow
22

3-
function getStubs (optionStubs, config) {
4-
if (optionStubs ||
5-
(config.stubs && Object.keys(config.stubs).length > 0)) {
6-
if (Array.isArray(optionStubs)) {
3+
function getOptions (key, options, config) {
4+
if (options ||
5+
(config[key] && Object.keys(config[key]).length > 0)) {
6+
if (Array.isArray(options)) {
77
return [
8-
...optionStubs,
9-
...Object.keys(config.stubs || {})]
8+
...options,
9+
...Object.keys(config[key] || {})]
1010
} else {
1111
return {
12-
...config.stubs,
13-
...optionStubs
12+
...config[key],
13+
...options
1414
}
1515
}
1616
}
@@ -22,6 +22,9 @@ export function mergeOptions (
2222
): Options {
2323
return {
2424
...options,
25-
stubs: getStubs(options.stubs, config)
25+
stubs: getOptions('stubs', options.stubs, config),
26+
mocks: getOptions('mocks', options.mocks, config),
27+
methods: getOptions('methods', options.methods, config)
2628
}
2729
}
30+

Diff for: ‎packages/test-utils/src/config.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,7 @@ export default {
55
stubs: {
66
transition: TransitionStub,
77
'transition-group': TransitionGroupStub
8-
}
8+
},
9+
mocks: {},
10+
methods: {}
911
}

Diff for: ‎test/specs/config.spec.js

+40-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import {
22
mount,
33
config,
44
TransitionStub,
5-
TransitionGroupStub
5+
TransitionGroupStub,
6+
createLocalVue
67
} from '~vue/test-utils'
78

89
describe('config', () => {
@@ -33,6 +34,44 @@ describe('config', () => {
3334
expect(wrapper.contains(TransitionGroupStub)).to.equal(true)
3435
})
3536

37+
it('mocks a global variable', () => {
38+
const localVue = createLocalVue()
39+
const t = 'real value'
40+
localVue.prototype.$t = t
41+
42+
const testComponent = {
43+
template: `
44+
<div>{{ $t }}</div>
45+
`
46+
}
47+
48+
config.mocks['$t'] = 'mock value'
49+
50+
const wrapper = mount(testComponent, {
51+
localVue, t
52+
})
53+
54+
expect(wrapper.vm.$t).to.equal('mock value')
55+
expect(wrapper.text()).to.equal('mock value')
56+
57+
localVue.prototype.$t = undefined
58+
})
59+
60+
it('overrides a method', () => {
61+
const testComponent = {
62+
template: `
63+
<div>{{ val() }}</div>
64+
`
65+
}
66+
67+
config.methods['val'] = () => 'method'
68+
69+
const wrapper = mount(testComponent)
70+
71+
expect(wrapper.vm.val()).to.equal('method')
72+
expect(wrapper.text()).to.equal('method')
73+
})
74+
3675
it('doesn\'t stub transition when config.stubs.transition is set to false', () => {
3776
const testComponent = {
3877
template: `

Diff for: ‎test/specs/mounting-options/methods.spec.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { config } from '~vue/test-utils'
2+
import {
3+
describeWithMountingMethods
4+
} from '~resources/utils'
5+
6+
describeWithMountingMethods('options.methods', (mountingMethod) => {
7+
it('prioritize mounting options over config', () => {
8+
config.methods['val'] = () => 'methodFromConfig'
9+
10+
const TestComponent = {
11+
template: `
12+
<div>{{ val() }}</div>
13+
`
14+
}
15+
16+
const wrapper = mountingMethod(TestComponent, {
17+
methods: {
18+
val () {
19+
return 'methodFromOptions'
20+
}
21+
}
22+
})
23+
const HTML = mountingMethod.name === 'renderToString'
24+
? wrapper
25+
: wrapper.html()
26+
console.log(wrapper)
27+
expect(HTML).to.contain('methodFromOptions')
28+
})
29+
})

Diff for: ‎test/specs/mounting-options/mocks.spec.js

+33-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createLocalVue } from '~vue/test-utils'
1+
import { createLocalVue, config } from '~vue/test-utils'
22
import Component from '~resources/components/component.vue'
33
import ComponentWithVuex from '~resources/components/component-with-vuex.vue'
44
import {
@@ -7,6 +7,17 @@ import {
77
} from '~resources/utils'
88

99
describeWithMountingMethods('options.mocks', (mountingMethod) => {
10+
let configMocksSave
11+
12+
beforeEach(() => {
13+
configMocksSave = config.mocks
14+
config.mocks = {}
15+
})
16+
17+
afterEach(() => {
18+
config.mocks = configMocksSave
19+
})
20+
1021
it('adds variables to vm when passed', () => {
1122
const TestComponent = {
1223
template: `
@@ -127,6 +138,27 @@ describeWithMountingMethods('options.mocks', (mountingMethod) => {
127138
error.restore()
128139
})
129140

141+
it('prioritize mounting options over config', () => {
142+
config.mocks['$global'] = 'globallyMockedValue'
143+
144+
const TestComponent = {
145+
template: `
146+
<div>{{ $global }}</div>
147+
`
148+
}
149+
150+
const wrapper = mountingMethod(TestComponent, {
151+
mocks: {
152+
'$global': 'locallyMockedValue'
153+
}
154+
})
155+
const HTML = mountingMethod.name === 'renderToString'
156+
? wrapper
157+
: wrapper.html()
158+
console.log(wrapper)
159+
expect(HTML).to.contain('locallyMockedValue')
160+
})
161+
130162
it('logs that a property cannot be overwritten if there are problems writing', () => {
131163
const error = sinon.stub(console, 'error')
132164
const localVue = createLocalVue()

0 commit comments

Comments
 (0)
Please sign in to comment.