Skip to content

Guide: dom-events.md #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 6, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/en/guides/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

* [Commencer](./getting-started.md)
* [Astuces](./common-tips.md)
* [Mouse, Key and other DOM Events (EN)](guides/dom-events.md)
* [Mouse, Key and other DOM Events (EN)](./dom-events.md)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tu peux retirer le EN :) Et traduire la ligne :)

* [Choisir un lanceur de tests](./choosing-a-test-runner.md)
* [Utiliser avec Jest](./using-with-jest.md)
* [Tester des composants monofichiers avec Jest](./testing-SFCs-with-jest.md)
Expand Down
117 changes: 59 additions & 58 deletions docs/en/guides/dom-events.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
# Testing Key, Mouse and other DOM events
# Tester le clavier, la souris et les autres événements DOM
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On utilise la graphie post 1990. Donc

évèments


## Trigger events
## Déclencheur d'événements

The `Wrapper` expose a `trigger` method. It can be used to trigger DOM events.

Le `Wrapper` expose une méthode `trigger` method. Elle peut être utilisée pour déclencher des événements du DOM.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

method.


```js
const wrapper = mount(MyButton)

wrapper.trigger('click')
```

You should be aware, that find returns a wrapper as well. Assuming `MyComponent` contains a button, the following code clicks the button.
Vous devez être au courant que la méthode find retourne un wrapper aussi. Partant du principe que `MyComponent` contient un bouton, le code suivant clique sur le bouton.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

`find`

(Tu peux PR l'original)


```js
const wrapper = mount(MyComponent)
Expand All @@ -20,9 +21,9 @@ wrapper.find('button').trigger('click')

## Options

The trigger method takes an optional `options` object. The properties in the `options` object are added to the Event.
La méthode trigger prend en paramètre optionnel l'objet `options`. Les propriétés de l'objet `options` sont ajoutées à l'événement Event.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

La méthode `trigger`

sont ajoutées à l'évènement Event.

ou

sont ajoutées à l'évènement.


You can run preventDefault on the event by passing `preventDefault: true` in `options`.
Vous pouvez utiliser preventDefault sur l'événement en passant `preventDefault: true` dans le paramètre `options`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

`preventDefault`


```js
const wrapper = mount(MyButton)
Expand All @@ -31,31 +32,31 @@ wrapper.trigger('click', {preventDefault: true})
```


## Mouse Click Example
## Exemple de clic de souris

**Component under test**
**Componsant à tester**
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Composant


```js
<template>
<div>
<button class="yes" @click="callYes">Yes</button>
<button class="no" @click="callNo">No</button>
<button class="oui" @click="callOui">Oui</button>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On a pris le parti de ne traduire que les chaines de caractère et les commentaire ce qui donne

<button class="yes" @click="callYes">Oui</button>

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Du coup tu traduirais le this.callMe('oui') ou tu le laisserais en this.callMe('yes')?
Je prends le parti dans un premier temps de remettre yes.

<button class="non" @click="callNon">Non</button>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<button class="no" @click="callNo">Non</button>

</div>
</template>
<script>
export default {
name: 'YesNoComponent',
name: 'OuiNonComponent',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

name: 'YesNoComponent',

props: {
callMe: {
type: Function
}
},
methods: {
callYes() {
this.callMe('yes')
callOui() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

callYes() {

this.callMe('oui')
},
callNo() {
this.callMe('no')
callNon() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

callNo() {

this.callMe('non')
}
}
}
Expand All @@ -66,34 +67,34 @@ export default {
**Test**

```js
import YesNoComponent from '@/components/YesNoComponent'
import OuiNonComponent from '@/components/OuiNonComponent'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import YesNoComponent from '@/components/YesNoComponent'

import { mount } from 'vue-test-utils'
import sinon from 'sinon'

describe('Click event', () => {
it('Click on yes button calls our method with argument "yes"', () => {
describe('Evenement click', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

describe('Évènement `click`', () => {

it('Cliquer sur le bouton oui appelle notre méthode avec l argument "oui"', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'Cliquer sur le bouton oui appelle notre méthode avec l\'argument "oui"'

const spy = sinon.spy()
const wrapper = mount(YesNoComponent, {
const wrapper = mount(OuiNonComponent, {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const wrapper = mount(YesNoComponent, {

propsData: {
callMe: spy
}
})
wrapper.find('button.yes').trigger('click')
wrapper.find('button.oui').trigger('click')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

button.yes


spy.should.have.been.calledWith('yes')
spy.should.have.been.calledWith('oui')
})
})
```

## Keyboard Example
## Example de test clavier

**Component under test**
**Composant à tester**

This component allows to increment/decrement the quantity using various keys.
Ce composant permet d'incrémenter/décrémenter la quantité en utilisant différentes touches.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

incrémenter / décrémenter (avec espace entre la barre)


```js
<template>
<input type="text" @keydown.prevent="onKeydown" v-model="quantity" />
<input type="text" @keydown.prevent="onKeydown" v-model="quantite" />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

garder quantity

</template>
<script>
const KEY_DOWN = 40
Expand All @@ -104,36 +105,36 @@ const CHAR_A = 65
export default {
data() {
return {
quantity: 0
quantite: 0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

quantity

}
},
methods: {
increment() {
this.quantity += 1
incrementer() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

increment()

this.quantite += 1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this.quantity

},
decrement() {
this.quantity -= 1
decrementer() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remettre le FR en EN

this.quantite -= 1
},
clear() {
this.quantity = 0
nettoyer() {
this.quantite = 0
},
onKeydown(e) {
if (e.keyCode === ESCAPE) {
this.clear()
this.nettoyer()
}
if (e.keyCode === KEY_DOWN) {
this.decrement()
this.decrementer()
}
if (e.keyCode === KEY_UP) {
this.increment()
this.incrementer()
}
if (e.which === CHAR_A) {
this.quantity = 13
this.quantite = 13
}
}
},
watch: {
quantity: function (newValue) {
quantite: function (newValue) {
this.$emit('input', newValue)
}
}
Expand All @@ -145,52 +146,52 @@ export default {
**Test**

```js
import QuantityComponent from '@/components/QuantityComponent'
import QuantiteComponent from '@/components/QuantiteComponent'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import QuantityComponent from '@/components/QuantityComponent'

import { mount } from 'vue-test-utils'

describe('Key event tests', () => {
it('Quantity is zero by default', () => {
const wrapper = mount(QuantityComponent)
expect(wrapper.vm.quantity).to.equal(0)
describe('Tests événement clavier', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

évènement

it('La quantité est zéro par défaut', () => {
const wrapper = mount(QuantiteComponent)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remettre en EN et le reste du code (garder en FR ce qui est dans it(

expect(wrapper.vm.quantite).to.equal(0)
})

it('Cursor up sets quantity to 1', () => {
const wrapper = mount(QuantityComponent)
it('La flèche du haut positionne la quantité à 1', () => {
const wrapper = mount(QuantiteComponent)
wrapper.trigger('keydown.up')
expect(wrapper.vm.quantity).to.equal(1)
expect(wrapper.vm.quantite).to.equal(1)
})

it('Cursor down reduce quantity by 1', () => {
const wrapper = mount(QuantityComponent)
wrapper.vm.quantity = 5
it('La flèche du bas réduit la quantité de 1', () => {
const wrapper = mount(QuantiteComponent)
wrapper.vm.quantite = 5
wrapper.trigger('keydown.down')
expect(wrapper.vm.quantity).to.equal(4)
expect(wrapper.vm.quantite).to.equal(4)
})

it('Escape sets quantity to 0', () => {
const wrapper = mount(QuantityComponent)
wrapper.vm.quantity = 5
it('La touche Echap positionne la quantité à 0', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

La touche Échap

const wrapper = mount(QuantiteComponent)
wrapper.vm.quantite = 5
wrapper.trigger('keydown.esc')
expect(wrapper.vm.quantity).to.equal(0)
expect(wrapper.vm.quantite).to.equal(0)
})

it('Magic character "a" sets quantity to 13', () => {
const wrapper = mount(QuantityComponent)
it('Le caractère magique "a" positionne la quantité à 13', () => {
const wrapper = mount(QuantiteComponent)
wrapper.trigger('keydown', {
which: 65
})
expect(wrapper.vm.quantity).to.equal(13)
expect(wrapper.vm.quantite).to.equal(13)
})
})

```

**Limitations**

A key name after the dot `keydown.up` is translated to a `keyCode`. This is supported for the following names:
Un nom de touche après le point `keydown.up` est traduit vers un `keyCode`. Cela est supporté pour les noms suivant:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

les noms suivant : (espace avant les :)


* enter, tab, delete, esc, space, up, down, left, right

## Important

vue-test-utils triggers event synchronously. Consequently, `vue.nextTick` is not required.
vue-test-utils déclenche les événements de façon synchrone. Par conséquent, `vue.nextTick` n'est pas requis.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

évènements