Skip to content

Commit 5bfef56

Browse files
38elementseddyerburgh
authored andcommitted
docs: update docs/ja (#422)
1 parent 1a0241b commit 5bfef56

File tree

9 files changed

+187
-13
lines changed

9 files changed

+187
-13
lines changed

docs/ja/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
* [contains](api/wrapper-array/contains.md)
5656
* [exists](api/wrapper/exists.md)
5757
* [destroy](api/wrapper-array/destroy.md)
58+
* [filter](api/wrapper-array/filter.md)
5859
* [is](api/wrapper-array/is.md)
5960
* [isEmpty](api/wrapper-array/isEmpty.md)
6061
* [isVueInstance](api/wrapper-array/isVueInstance.md)
@@ -67,6 +68,7 @@
6768
* [コンポーネント](api/components/README.md)
6869
* [TransitionStub](api/components/TransitionStub.md)
6970
* [TransitionGroupStub](api/components/TransitionGroupStub.md)
71+
* [RouterLinkStub](api/components/RouterLinkStub.md)
7072
* [セレクタ](api/selectors.md)
7173
* [createLocalVue](api/createLocalVue.md)
7274
* [config](api/config.md)

docs/ja/SUMMARY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
* [contains](api/wrapper-array/contains.md)
5454
* [exists](api/wrapper/exists.md)
5555
* [destroy](api/wrapper-array/destroy.md)
56+
* [filter](api/wrapper-array/filter.md)
5657
* [is](api/wrapper-array/is.md)
5758
* [isEmpty](api/wrapper-array/isEmpty.md)
5859
* [isVueInstance](api/wrapper-array/isVueInstance.md)
@@ -65,6 +66,7 @@
6566
* [コンポーネント](api/components/README.md)
6667
* [TransitionStub](api/components/TransitionStub.md)
6768
* [TransitionGroupStub](api/components/TransitionGroupStub.md)
69+
* [RouterLinkStub](api/components/RouterLinkStub.md)
6870
* [セレクタ](api/selectors.md)
6971
* [createLocalVue](api/createLocalVue.md)
7072
* [config](api/config.md)

docs/ja/api/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
* [contains](./wrapper-array/contains.md)
4242
* [exists](./wrapper/exists.md)
4343
* [destroy](./wrapper-array/destroy.md)
44+
* [filter](./wrapper-array/filter.md)
4445
* [is](./wrapper-array/is.md)
4546
* [isEmpty](./wrapper-array/isEmpty.md)
4647
* [isVueInstance](./wrapper-array/isVueInstance.md)
@@ -53,6 +54,7 @@
5354
* [コンポーネント](./components/README.md)
5455
* [TransitionStub](./components/TransitionStub.md)
5556
* [TransitionGroupStub](./components/TransitionGroupStub.md)
57+
* [RouterLinkStub](./components/RouterLinkStub.md)
5658
* [セレクタ](./selectors.md)
5759
* [createLocalVue](./createLocalVue.md)
5860
* [config](./config.md)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# RouterLinkStub
2+
3+
Vue Router の `router-link` コンポーネントをスタブするためのコンポーネントです。
4+
5+
レンダリングツリーにある router-link コンポーネントを見つけるためにこのコンポーネントを使用することができます。
6+
7+
- **使い方:**
8+
9+
スタブとしてマウンティングオプションにセットします。
10+
11+
```js
12+
import { mount, RouterLinkStub } from '@vue/test-utils'
13+
14+
const wrapper = mount(Component, {
15+
stubs: {
16+
RouterLink: RouterLinkStub
17+
}
18+
})
19+
expect(wrapper.find(RouterLinkStub).props().to).toBe('/some/path')
20+
```

docs/ja/api/wrapper-array/filter.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# filter(predicate)
2+
3+
`Wrapper` オブジェクトを判別する関数を使用して `WrapperArray` をフィルタリングします。
4+
5+
このメソッドの動作は [Array.prototype.filter](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) に似ています。
6+
7+
- **引数:**
8+
- `{function} predicate`
9+
10+
- **戻り値:** `{WrapperArray}`
11+
12+
predicate 関数が true を返す `Wrapper` インスタンスを含む新しい `WrapperArray` インスタンスを返します。
13+
14+
- **例:**
15+
16+
```js
17+
import { shallow } from '@vue/test-utils'
18+
import Foo from './Foo.vue'
19+
20+
const wrapper = shallow(Foo)
21+
const filteredDivArray = wrapper.findAll('div').filter(w => !w.hasClass('filtered'))
22+
```

docs/ja/api/wrapper/props.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,14 @@
99
- **例:**
1010

1111
```js
12+
import { mount } from '@vue/test-utils'
13+
import { expect } from 'chai'
1214
import Foo from './Foo.vue'
13-
import Bar from './Bar.vue'
1415

15-
const wrapper = mount(Component, {
16-
context: {
17-
props: { show: true },
18-
children: [Foo, Bar]
16+
const wrapper = mount(Foo, {
17+
propsData: {
18+
bar: 'baz'
1919
}
2020
})
21-
22-
expect(wrapper.is(Component)).toBe(true)
21+
expect(wrapper.props().bar).toBe('baz')
2322
```

docs/ja/guides/testing-SFCs-with-jest.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Jest は Facebook が開発したテストランナであり、ユニットテ
1111
まず Jest と `vue-test-utils` をインストールします:
1212

1313
```bash
14-
$ npm install --save-dev jest vue-test-utils
14+
$ npm install --save-dev jest @vue/test-utils
1515
```
1616

1717
次に、`package.json` にスクリプトを定義する必要があります。

docs/ja/guides/using-with-vue-router.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ shallow(Component, {
2020
})
2121
```
2222

23+
> Vue Router を localVue にインストールすると `$route` と `$router` が読み取り専用プロパティーとして localVue に追加されます。これは VueRouter をインストールした localVue を使用しているコンポーネントをマウントする時、 `mock` オプションで `$route` と `$router` を上書きすることができないことを意味します。
24+
2325
## `router-link` または `router-view` を使用するコンポーネントテスト
2426

2527
Vue Router をインストールする時、`router-link``router-view` コンポーネントが登録されます。これは、それらをアプリケーションにインポートする必要がなく、アプリケーションのどこでも使用することができます。

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

Lines changed: 130 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
このガイドでは、`vue-test-utils` でコンポーネントで Vuex をテストする方法について、見ていきます。
44

5-
## アクションのモック
5+
## コンポーネント内の Vuex のテスト
6+
7+
### アクションのモック
68

79
それではいくつかのコードを見ていきましょう。
810

@@ -107,7 +109,7 @@ describe('Actions.vue', () => {
107109

108110
素晴らしい!今、アクションをモック化できるので、ゲッタのモックについて見ていきましょう。
109111

110-
## ゲッタのモック
112+
### ゲッタのモック
111113

112114

113115
``` html
@@ -176,7 +178,7 @@ describe('Getters.vue', () => {
176178

177179
これは素晴らしいですが、もしゲッタが状態の正しい部分を返しているのを確認したい場合はどうしますか?
178180

179-
## モジュールによるモック
181+
### モジュールによるモック
180182

181183
[モジュール](https://vuex.vuejs.org/en/modules.html)はストアを管理しやすい塊に分けるために便利です。それらはゲッタもエクスポートします。テストではこれらを使用することができます。
182184

@@ -259,8 +261,131 @@ describe('Modules.vue', () => {
259261
})
260262
```
261263

262-
### リソース
263264

264-
- [このガイド向けの例](https://github.com/eddyerburgh/vue-test-utils-vuex-example)
265+
## Vuex ストアのテスト
266+
267+
Vuex ストアをテストする方法が2つあります。1つ目はゲッタとミューテーションとアクションを別々に単体テストする方法です。2つ目はストアを生成してそれをテストする方法です。
268+
269+
Vuex ストアをテストする方法を説明するためにシンプルなカウンターストアを用意します。このストアには `increment` ミューテーションと `counter` ゲッタがあります。
270+
271+
```js
272+
// mutations.js
273+
export default {
274+
increment (state) {
275+
state.count++
276+
}
277+
}
278+
```
279+
280+
```js
281+
// getters.js
282+
export default {
283+
evenOrOdd: state => state.count % 2 === 0 ? 'even' : 'odd'
284+
}
285+
```
286+
287+
### ゲッタとミューテーションとアクションを別々にテストする
288+
289+
ゲッタとミューテーションとアクションはすべて JavaScript の関数です。それらは `vue-test-utils` と Vuex を使用しなくてもテストすることができます。
290+
291+
ゲッタとミューテーションとアクションを別々にテストする利点は単体テストを詳細に記述することができることです。テストが失敗すると、コードの何が原因か正確に知ることができます。欠点は `commit` や `dispatch` のような Vuex の関数のモックが必要なことです。これは不正なモックが原因で単体テストはパスしてプロダクションは失敗する状況を作り出す可能性があります。
292+
293+
mutations.spec.js と getters.spec.js という名前のテストファイルを2つ作成します。
294+
295+
最初に increment ミューテーションをテストします。
296+
297+
```js
298+
// mutations.spec.js
299+
300+
import mutations from './mutations'
301+
302+
test('increment increments state.count by 1', () => {
303+
const state = {
304+
count: 0
305+
}
306+
mutations.increment(state)
307+
expect(state.count).toBe(1)
308+
})
309+
```
310+
311+
今度は `evenOrOdd` ゲッタを次の手順でテストします。 `state` モックを作成します。 `state` を引数としてゲッタ関数を実行します。そして、それが正しい値を返したか確認します。
312+
313+
```js
314+
// getters.spec.js
315+
316+
import getters from './getters'
317+
318+
test('evenOrOdd returns even if state.count is even', () => {
319+
const state = {
320+
count: 2
321+
}
322+
expect(getters.evenOrOdd(state)).toBe('even')
323+
})
324+
325+
test('evenOrOdd returns odd if state.count is even', () => {
326+
const state = {
327+
count: 1
328+
}
329+
expect(getters.evenOrOdd(state)).toBe('odd')
330+
})
331+
332+
```
333+
334+
### 実行可能なストアのテスト
335+
336+
Vuexストアをテストするもう1つの方法はストアの設定を使って実行可能なストアを生成することです。
337+
338+
実行可能なストアを生成してテストすることの利点は Vuex の関数をモックする必要がない事です。
339+
340+
欠点はテストが失敗した時、問題がある箇所を見つけることが難しいことです。
341+
342+
テストを書いてみましょう。ストアを生成する際は、 Vue のコンストラクタが汚染されることを避けるために `localVue` を使用します。このテストは store-config.js の export を使用してストアを生成します。
343+
344+
```js
345+
// store-config.js
346+
347+
import mutations from './mutations'
348+
import getters from './getters'
349+
350+
export default {
351+
state: {
352+
count: 0
353+
},
354+
mutations,
355+
getters
356+
}
357+
```
358+
359+
```js
360+
import { createLocalVue } from '@vue/test-utils'
361+
import Vuex from 'vuex'
362+
import storeConfig from './store-config'
363+
import { cloneDeep } from 'lodash'
364+
365+
test('increments count value when increment is commited', () => {
366+
const localVue = createLocalVue()
367+
localVue.use(Vuex)
368+
const store = new Vuex.Store(cloneDeep(storeConfig))
369+
expect(store.state.count).toBe(0)
370+
store.commit('increment')
371+
expect(store.state.count).toBe(1)
372+
})
373+
374+
test('updates evenOrOdd getter when increment is commited', () => {
375+
const localVue = createLocalVue()
376+
localVue.use(Vuex)
377+
const store = new Vuex.Store(cloneDeep(storeConfig))
378+
expect(store.getters.evenOrOdd).toBe('even')
379+
store.commit('increment')
380+
expect(store.getters.evenOrOdd).toBe('odd')
381+
})
382+
```
383+
384+
ストアをストアの設定から生成する前に `cloneDeep` を使用しています。こうする理由は Vuex はストアを生成するためにオプションオブジェクトを変更するからです。どのテストでも確実に汚染されていないストアを使うために `storeConfig` オブジェクトを複製する必要があります。
385+
386+
## リソース
387+
388+
- [コンポーネントをテストする例](https://github.com/eddyerburgh/vue-test-utils-vuex-example)
389+
- [ストアをテストする例](https://github.com/eddyerburgh/testing-vuex-store-example)
265390
- [localVue](../api/options.md#localvue)
266391
- [createLocalVue](../api/createLocalVue.md)

0 commit comments

Comments
 (0)