Skip to content

Commit 8766dbe

Browse files
committed
docs: use localVue in using-with-vuex
1 parent a2aaff7 commit 8766dbe

File tree

1 file changed

+39
-36
lines changed

1 file changed

+39
-36
lines changed

docs/en/guides/using-with-vuex.md

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Using with Vuex
22

3+
In this guide, we'll see how to test Vuex in components with vue-test-utils.
4+
35
## Mocking Actions
46

57
Let’s look at some code.
@@ -35,29 +37,29 @@ export default{
3537

3638
For the purposes of this test, we don’t care what the actions do, or what the store looks like. We just need to know that these actions are being fired when they should, and that they are fired with the expected value.
3739

38-
To test this, we need to pass a mock store to Vue when we mount our component.
40+
To test this, we need to pass a mock store to Vue when we shallow our component.
41+
42+
Instead of passing the store to the base Vue constructor, we can pass it to a - [localVue](../api/options.md#localvue). A localVue is a scoped Vue constructor that we can make changes to without affecting the global Vue constructor.
3943

4044
Let’s see what this looks like:
4145

4246
``` js
43-
import Vue from 'vue'
44-
import { mount } from 'vue-test-utils'
45-
import sinon from 'sinon'
46-
import { expect } from 'chai'
47+
import { shallow, createLocalVue } from 'vue-test-utils'
4748
import Vuex from 'vuex'
48-
import 'babel-polyfill'
4949
import Actions from '../../../src/components/Actions'
5050

51-
Vue.use(Vuex)
51+
const localVue = createLocalVue()
52+
53+
localVue.use(Vuex)
5254

5355
describe('Actions.vue', () => {
5456
let actions
5557
let store
5658

5759
beforeEach(() => {
5860
actions = {
59-
actionClick: sinon.stub(),
60-
actionInput: sinon.stub()
61+
actionClick: jest.fn(),
62+
actionInput: jest.fn()
6163
}
6264
store = new Vuex.Store({
6365
state: {},
@@ -66,25 +68,25 @@ describe('Actions.vue', () => {
6668
})
6769

6870
it('calls store action actionInput when input value is input and an input even is fired', () => {
69-
const wrapper = mount(Actions, { store })
71+
const wrapper = shallow(Actions, { store, localVue })
7072
const input = wrapper.find('input')
7173
input.element.value = 'input'
7274
input.trigger('input')
73-
expect(actions.actionInput.calledOnce).toBe(true)
75+
expect(actions.actionInput).toHaveBeenCalled()
7476
})
7577

7678
it('does not call store action actionInput when input value is not input and an input even is fired', () => {
77-
const wrapper = mount(Actions, { store })
79+
const wrapper = shallow(Actions, { store, localVue })
7880
const input = wrapper.find('input')
7981
input.element.value = 'not input'
8082
input.trigger('input')
81-
expect(actions.actionInput.calledOnce).toBe(false)
83+
expect(actions.actionInput).not.toHaveBeenCalled()
8284
})
8385

8486
it('calls store action actionClick when button is clicked', () => {
85-
const wrapper = mount(Actions, { store })
87+
const wrapper = shallow(Actions, { store, localVue })
8688
wrapper.find('button').trigger('click')
87-
expect(actions.actionClick.calledOnce).toBe(true)
89+
expect(actions.actionClick).toHaveBeenCalled()
8890
})
8991
})
9092
```
@@ -93,13 +95,13 @@ What’s happening here? First we tell Vue to use Vuex with the Vue.use method.
9395

9496
We then make a mock store by calling new Vuex.store with our mock values. We only pass it the actions, since that’s all we care about.
9597

96-
The actions are [sinon stubs](http://sinonjs.org/). The stubs give us methods to assert whether the actions were called or not.
98+
The actions are [jest mock functions](https://facebook.github.io/jest/docs/en/mock-functions.html). These mock functions give us methods to assert whether the actions were called or not.
9799

98100
We can then assert in our tests that the action stub was called when expected.
99101

100102
Now the way we define the store might look a bit foreign to you.
101103

102-
We’re using beforeEach to ensure we have a clean store before each test. beforeEach is a mocha hook that’s called before each test. In our test, we are reassigning the store variables value. If we didn’t do this, the sinon stubs would need to be automatically reset. It also lets us change the state in our tests, without it affecting later tests.
104+
We’re using beforeEach to ensure we have a clean store before each test. beforeEach is a mocha hook that’s called before each test. In our test, we are reassigning the store variables value. If we didn’t do this, the mock functions would need to be automatically reset. It also lets us change the state in our tests, without it affecting later tests.
103105

104106
The most important thing to note in this test is that **we create a mock Vuex store and then pass it to vue-test-utils**.
105107

@@ -133,14 +135,13 @@ This is a fairly simple component. It renders the result of the getters clicks a
133135
Let’s see the test:
134136

135137
``` js
136-
import 'babel-polyfill'
137-
import Vue from 'vue'
138-
import { mount } from 'vue-test-utils'
139-
import { expect } from 'chai'
138+
import { shallow, createLocalVue } from 'vue-test-utils'
140139
import Vuex from 'vuex'
141140
import Actions from '../../../src/components/Getters'
142141

143-
Vue.use(Vuex)
142+
const localVue = createLocalVue()
143+
144+
localVue.use(Vuex)
144145

145146
describe('Getters.vue', () => {
146147
let getters
@@ -158,19 +159,19 @@ describe('Getters.vue', () => {
158159
})
159160

160161
it('Renders state.inputValue in first p tag', () => {
161-
const wrapper = mount(Actions, { store })
162+
const wrapper = shallow(Actions, { store, localVue })
162163
const p = wrapper.find('p')
163164
expect(p.text()).toBe(getters.inputValue())
164165
})
165166

166167
it('Renders state.clicks in second p tag', () => {
167-
const wrapper = mount(Actions, { store })
168+
const wrapper = shallow(Actions, { store, localVue })
168169
const p = wrapper.findAll('p').at(1)
169170
expect(p.text()).toBe(getters.clicks().toString())
170171
})
171172
})
172173
```
173-
This test is similar to our actions test. We create a mock store before each test, pass it as an option when we call mount, and assert that the value returned by our mock getters is being rendered.
174+
This test is similar to our actions test. We create a mock store before each test, pass it as an option when we call shallow, and assert that the value returned by our mock getters is being rendered.
174175

175176
This is great, but what if we want to check our getters are returning the correct part of our state?
176177

@@ -209,16 +210,14 @@ Simple component that includes one action and one getter.
209210
And the test:
210211

211212
``` js
212-
import Vue from 'vue'
213-
import { mount } from 'vue-test-utils'
214-
import sinon from 'sinon'
215-
import { expect } from 'chai'
213+
import { shallow, createLocalVue } from 'vue-test-utils'
216214
import Vuex from 'vuex'
217-
import 'babel-polyfill'
218215
import Modules from '../../../src/components/Modules'
219216
import module from '../../../src/store/module'
220217

221-
Vue.use(Vuex)
218+
const localVue = createLocalVue()
219+
220+
localVue.use(Vuex)
222221

223222
describe('Modules.vue', () => {
224223
let actions
@@ -233,7 +232,7 @@ describe('Modules.vue', () => {
233232
}
234233

235234
actions = {
236-
moduleActionClick: sinon.stub()
235+
moduleActionClick: jest.fn()
237236
}
238237

239238
store = new Vuex.Store({
@@ -244,18 +243,22 @@ describe('Modules.vue', () => {
244243
})
245244

246245
it('calls store action moduleActionClick when button is clicked', () => {
247-
const wrapper = mount(Modules, { store })
246+
const wrapper = shallow(Modules, { store, localVue })
248247
const button = wrapper.find('button')
249248
button.trigger('click')
250-
expect(actions.moduleActionClick.calledOnce).toBe(true)
249+
expect(actions.moduleActionClick).toHaveBeenCalled()
251250
})
252251

253252
it('Renders state.inputValue in first p tag', () => {
254-
const wrapper = mount(Modules, { store })
253+
const wrapper = shallow(Modules, { store, localVue })
255254
const p = wrapper.find('p')
256255
expect(p.text()).toBe(state.module.clicks.toString())
257256
})
258257
})
259258
```
260259

261-
To have a look at what the module file looks like, [check out the repo](https://github.com/eddyerburgh/mock-vuex-in-vue-unit-tests-tutorial).
260+
### Resources
261+
262+
- [Example project for this guide](https://github.com/eddyerburgh/vue-test-utils-vuex-example)
263+
- [localVue](../api/options.md#localvue)
264+
- [createLocalVue](../api/createLocalVue.md)

0 commit comments

Comments
 (0)