Skip to content

Commit 9236573

Browse files
committed
sync 1.0.0 beta-2 docs
1 parent d31f10e commit 9236573

File tree

3 files changed

+119
-44
lines changed

3 files changed

+119
-44
lines changed

docs/ja/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* [テストランナを選ぶ](guides/choosing-a-test-runner.md)
77
* [Jest による単一ファイルコンポーネントのテスト](guides/testing-SFCs-with-jest.md)
88
* [Mocha + webpack による単一ファイルコンポーネントのテスト](guides/testing-SFCs-with-mocha-webpack.md)
9+
* [Using with vue-router](guides/using-with-vuex.md)
910
* [Vuex と一緒に使う](guides/using-with-vuex.md)
1011
* [API](api/README.md)
1112
* [mount](api/mount.md)
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Using with vue-router
2+
3+
## Installing vue-router in tests
4+
5+
You should never install vue-router on the Vue base constructor in tests. Installing vue-router adds `$route` and `$router` as read-only properties on Vue prototype.
6+
7+
To avoid this, we can create a localVue, and install vue-router on that.
8+
9+
```js
10+
import VueRouter from 'vue-router'
11+
const localVue = createLocalVue()
12+
13+
localVue.use(VueRouter)
14+
15+
shallow(Component, {
16+
localVue
17+
})
18+
```
19+
20+
## Testing components that use router-link or router-view
21+
22+
When you install vue-router, the router-link and router-view components are registered. This means we can use them anywhere in our application without needing to import them.
23+
24+
When we run tests, we need to make these vue-router components available to the component we're mounting. There are two methods to do this.
25+
26+
### Using stubs
27+
28+
```js
29+
shallow(Component, {
30+
stubs: ['router-link', 'router-view']
31+
})
32+
```
33+
34+
### Installing vue-router with localVue
35+
36+
```js
37+
import VueRouter from 'vue-router'
38+
const localVue = createLocalVue()
39+
40+
localVue.use(VueRouter)
41+
42+
shallow(Component, {
43+
localVue
44+
})
45+
```
46+
47+
## Mocking $route and $router
48+
49+
Sometimes you want to test that a component does something with parameters from the `$route` and `$router` objects. To do that, you can pass custom mocks to the Vue instance.
50+
51+
```js
52+
const $route = {
53+
path: '/some/path'
54+
}
55+
56+
const wrapper = shallow(Component, {
57+
mocks: {
58+
$route
59+
}
60+
})
61+
62+
wrapper.vm.$router // /some/path
63+
```
64+
65+
## Common gotchas
66+
67+
Installing vue-router adds `$route` and `$router` as read-only properties on Vue prototype.
68+
69+
This means any future tests that try to mock $route or `$router` will fail.
70+
71+
To avoid this, never install vue-router when you're running tests.

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

Lines changed: 47 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
# Vuex と一緒に使用する
1+
# Using with Vuex
2+
3+
In this guide, we'll see how to test Vuex in components with vue-test-utils.
24

35
## Mocking Actions
46

@@ -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 })
70-
const input = wrapper.find('input')[0]
71+
const wrapper = shallow(Actions, { store, localVue })
72+
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 })
78-
const input = wrapper.find('input')[0]
79+
const wrapper = shallow(Actions, { store, localVue })
80+
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 })
86-
wrapper.find('button')[0].trigger('click')
87-
expect(actions.actionClick.calledOnce).toBe(true)
87+
const wrapper = shallow(Actions, { store, localVue })
88+
wrapper.find('button').trigger('click')
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 p = wrapper.find('p')[0]
162+
const wrapper = shallow(Actions, { store, localVue })
163+
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 p = wrapper.find('p')[1]
168+
const wrapper = shallow(Actions, { store, localVue })
169+
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 })
248-
const button = wrapper.find('button')[0]
246+
const wrapper = shallow(Modules, { store, localVue })
247+
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 })
255-
const p = wrapper.find('p')[0]
253+
const wrapper = shallow(Modules, { store, localVue })
254+
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)