Skip to content

Commit 9ab23e9

Browse files
38elementseddyerburgh
authored andcommitted
docs: add docs/ja/guides/dom-events.md (vuejs#204)
1 parent 60e360a commit 9ab23e9

File tree

4 files changed

+201
-1
lines changed

4 files changed

+201
-1
lines changed

docs/ja/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* [ガイド](guides/README.md)
66
* [はじめる](guides/getting-started.md)
77
* [一般的なヒント](guides/common-tips.md)
8+
* [キー、マウス、その他の DOM イベントのテスト](guides/dom-events.md)
89
* [テストランナを選ぶ](guides/choosing-a-test-runner.md)
910
* [Jest による単一ファイルコンポーネントのテスト](guides/testing-SFCs-with-jest.md)
1011
* [Mocha + webpack による単一ファイルコンポーネントのテスト](guides/testing-SFCs-with-mocha-webpack.md)

docs/ja/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* [ガイド](guides/README.md)
44
* [はじめる](guides/getting-started.md)
55
* [一般的なヒント](guides/common-tips.md)
6+
* [キー、マウス、その他の DOM イベントのテスト](guides/dom-events.md)
67
* [テストランナを選ぶ](guides/choosing-a-test-runner.md)
78
* [Jest による単一ファイルコンポーネントのテスト](guides/testing-SFCs-with-jest.md)
89
* [Mocha + webpack による単一ファイルコンポーネントのテスト](guides/testing-SFCs-with-mocha-webpack.md)

docs/ja/guides/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
# ガイド
22

33
* [はじめる](./getting-started.md)
4-
* [よくある落とし穴](./common-tips.md)
4+
* [一般的なヒント](./common-tips.md)
5+
* [キー、マウス、その他の DOM イベントのテスト](./dom-events.md)
56
* [テストランナを選ぶ](./choosing-a-test-runner.md)
67
* [Jest による単一ファイルコンポーネントのテスト](./testing-SFCs-with-jest.md)
78
* [Mocha + webpack による単一ファイルコンポーネントのテスト](./testing-SFCs-with-mocha-webpack.md)
9+
* [Vue Router と一緒に使う](./using-with-vue-router.md)
810
* [Vuex と一緒に使う](./using-with-vuex.md)

docs/ja/guides/dom-events.md

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
# キー、マウス、その他の DOM イベントのテスト
2+
3+
## イベントをトリガする
4+
5+
`Wrapper``trigger` メソッドで DOM イベントをトリガすることができます。
6+
7+
```js
8+
const wrapper = mount(MyButton)
9+
10+
wrapper.trigger('click')
11+
```
12+
13+
`find` メソッドは `mount` メソッドと同じように `Wrapper` を返します。 `MyComponent` 内に `button` があると仮定すると、以下のコードは、 `button` をクリックします。
14+
15+
```js
16+
const wrapper = mount(MyComponent)
17+
18+
wrapper.find('button').trigger('click')
19+
```
20+
21+
## オプション
22+
23+
`trigger` メソッドはオプションで `options` オブジェクトを引数として取ります。`options` オブジェクトのプロパティはイベントオブジェクトのプロパティに追加されます。
24+
25+
`preventDefault: true``options` に渡すと、 `event.preventDefault()` を実行することができます。
26+
27+
```js
28+
const wrapper = mount(MyButton)
29+
30+
wrapper.trigger('click', { preventDefault: true })
31+
```
32+
33+
34+
## マウスクリックの例
35+
36+
**テスト対象のコンポーネント**
37+
38+
```html
39+
<template>
40+
<div>
41+
<button class="yes" @click="callYes">Yes</button>
42+
<button class="no" @click="callNo">No</button>
43+
</div>
44+
</template>
45+
<script>
46+
export default {
47+
name: 'YesNoComponent',
48+
props: {
49+
callMe: {
50+
type: Function
51+
}
52+
},
53+
methods: {
54+
callYes() {
55+
this.callMe('yes')
56+
},
57+
callNo() {
58+
this.callMe('no')
59+
}
60+
}
61+
}
62+
</script>
63+
64+
```
65+
66+
**テスト**
67+
68+
```js
69+
import YesNoComponent from '@/components/YesNoComponent'
70+
import { mount } from 'vue-test-utils'
71+
import sinon from 'sinon'
72+
73+
describe('Click event', () => {
74+
it('Click on yes button calls our method with argument "yes"', () => {
75+
const spy = sinon.spy()
76+
const wrapper = mount(YesNoComponent, {
77+
propsData: {
78+
callMe: spy
79+
}
80+
})
81+
wrapper.find('button.yes').trigger('click')
82+
83+
spy.should.have.been.calledWith('yes')
84+
})
85+
})
86+
```
87+
88+
## キーボードの例
89+
90+
**テスト対象のコンポーネント**
91+
92+
このコンポーネントはいくつかのキーを使用して `quantity` を増減することができます。
93+
94+
```html
95+
<template>
96+
<input type="text" @keydown.prevent="onKeydown" v-model="quantity" />
97+
</template>
98+
<script>
99+
const KEY_DOWN = 40
100+
const KEY_UP = 38
101+
const ESCAPE = 27
102+
const CHAR_A = 65
103+
104+
export default {
105+
data() {
106+
return {
107+
quantity: 0
108+
}
109+
},
110+
methods: {
111+
increment() {
112+
this.quantity += 1
113+
},
114+
decrement() {
115+
this.quantity -= 1
116+
},
117+
clear() {
118+
this.quantity = 0
119+
},
120+
onKeydown(e) {
121+
if (e.keyCode === ESCAPE) {
122+
this.clear()
123+
}
124+
if (e.keyCode === KEY_DOWN) {
125+
this.decrement()
126+
}
127+
if (e.keyCode === KEY_UP) {
128+
this.increment()
129+
}
130+
if (e.which === CHAR_A) {
131+
this.quantity = 13
132+
}
133+
}
134+
},
135+
watch: {
136+
quantity: function (newValue) {
137+
this.$emit('input', newValue)
138+
}
139+
}
140+
}
141+
</script>
142+
143+
```
144+
145+
**テスト**
146+
147+
```js
148+
import QuantityComponent from '@/components/QuantityComponent'
149+
import { mount } from 'vue-test-utils'
150+
151+
describe('Key event tests', () => {
152+
it('Quantity is zero by default', () => {
153+
const wrapper = mount(QuantityComponent)
154+
expect(wrapper.vm.quantity).to.equal(0)
155+
})
156+
157+
it('Cursor up sets quantity to 1', () => {
158+
const wrapper = mount(QuantityComponent)
159+
wrapper.trigger('keydown.up')
160+
expect(wrapper.vm.quantity).to.equal(1)
161+
})
162+
163+
it('Cursor down reduce quantity by 1', () => {
164+
const wrapper = mount(QuantityComponent)
165+
wrapper.vm.quantity = 5
166+
wrapper.trigger('keydown.down')
167+
expect(wrapper.vm.quantity).to.equal(4)
168+
})
169+
170+
it('Escape sets quantity to 0', () => {
171+
const wrapper = mount(QuantityComponent)
172+
wrapper.vm.quantity = 5
173+
wrapper.trigger('keydown.esc')
174+
expect(wrapper.vm.quantity).to.equal(0)
175+
})
176+
177+
it('Magic character "a" sets quantity to 13', () => {
178+
const wrapper = mount(QuantityComponent)
179+
wrapper.trigger('keydown', {
180+
which: 65
181+
})
182+
expect(wrapper.vm.quantity).to.equal(13)
183+
})
184+
})
185+
186+
```
187+
188+
**制限事項**
189+
190+
`.` の後のキー名( `keydown.up` の場合 `up` )は `keyCode` に変換されます。以下のキー名が変換されます。
191+
192+
* `enter`, `tab`, `delete`, `esc`, `space`, `up`, `down`, `left`, `right`
193+
194+
## 重要事項
195+
196+
`vue-test-utils` は同期的にイベントをトリガします。従って、 `Vue.nextTick()` を実行する必要はありません。

0 commit comments

Comments
 (0)