|
| 1 | +# Testing Key, Mouse and other DOM events |
| 2 | + |
| 3 | +## Trigger events |
| 4 | + |
| 5 | +The `Wrapper` expose a `trigger` method. It can be used to trigger DOM events. |
| 6 | + |
| 7 | +```js |
| 8 | +const wrapper = mount(MyButton) |
| 9 | + |
| 10 | +wrapper.trigger('click') |
| 11 | +``` |
| 12 | + |
| 13 | +You should be aware, that find returns a wrapper as well. Assuming `MyComponent` contains a button, the following code clicks the button. |
| 14 | + |
| 15 | +```js |
| 16 | +const wrapper = mount(MyComponent) |
| 17 | + |
| 18 | +wrapper.find('button').trigger('click') |
| 19 | +``` |
| 20 | + |
| 21 | +## Options |
| 22 | + |
| 23 | +The trigger method takes an optional `options` object. The properties in the `options` object are added to the Event. |
| 24 | + |
| 25 | +You can run preventDefault on the event by passing `preventDefault: true` in `options`. |
| 26 | + |
| 27 | +```js |
| 28 | +const wrapper = mount(MyButton) |
| 29 | + |
| 30 | +wrapper.trigger('click', {preventDefault: true}) |
| 31 | +``` |
| 32 | + |
| 33 | + |
| 34 | +## Mouse Click Example |
| 35 | + |
| 36 | +**Component under test** |
| 37 | + |
| 38 | +```js |
| 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 | +**Test** |
| 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 | +## Keyboard Example |
| 89 | + |
| 90 | +**Component under test** |
| 91 | + |
| 92 | +This component allows to increment/decrement the quantity using various keys. |
| 93 | + |
| 94 | +```js |
| 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 | +**Test** |
| 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 | +**Limitations** |
| 189 | + |
| 190 | +A key name after the dot `keydown.up` is translated to a `keyCode`. This is supported for the following names: |
| 191 | + |
| 192 | +* enter, tab, delete, esc, space, up, down, left, right |
| 193 | + |
| 194 | +## Important |
| 195 | + |
| 196 | +vue-test-utils triggers event synchronously. Consequently, `vue.nextTick` is not required. |
0 commit comments