You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/guide/conditional-rendering.md
+22-8
Original file line number
Diff line number
Diff line change
@@ -30,34 +30,46 @@ In the `<Nav>` component, a link to the user's profile is shown. In addition, if
30
30
31
31
## Using `get()`
32
32
33
-
`wrapper` has a `get()` method for asserting an element exists. It uses [`querySelector`](https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector) syntax. We can assert the profile link is present using `get()`:
33
+
`wrapper` has a `get()` method that searches for an existing element. It uses [`querySelector`](https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector) syntax.
34
+
35
+
We can assert the profile link content by using `get()`:
34
36
35
37
```js
36
38
test('renders a profile link', () => {
37
39
constwrapper=mount(Nav)
38
-
wrapper.get('#profile')
40
+
41
+
// Here we are implicitly asserting that the
42
+
// element #profile exists.
43
+
constprofileLink=wrapper.get('#profile')
44
+
45
+
expect(profileLink.text()).toEqual('My Profile')
39
46
})
40
47
```
41
48
42
49
If `get()` does not return an element matching the selector, it will raise an error, and your test will fail.
43
50
44
51
## Using `find()` and `exists()`
45
52
46
-
`get()` works for asserting elements do exist, because it throws an error when it can't find an element, you can't use it to assert elements don't exist. For this, we can use `find()` and `exists()`. The next test asserts that if `admin` is `false` (which is it by default), the admin link is not present:
53
+
`get()` works for asserting elements do exist. However, as we mentioned, it throws an error when it can't find an element, so you can't use it to assert whether if elements exist.
54
+
55
+
To do so, we use `find()` and `exists()`. The next test asserts that if `admin` is `false` (which is it by default), the admin link is not present:
47
56
48
57
```js
49
58
test('does not render an admin link', () => {
50
59
constwrapper=mount(Nav)
51
-
constadminLink=wrapper.find('#admin')
52
-
expect(adminLink.exists()).toBe(false)
60
+
61
+
// Using `wrapper.get` would throw and make the test fail.
Notice we are calling `exists()` on the value returned from `.find()`? `find()`, like `mount()`, also returns a wrapper, similar to `mount()`. `mount()` has a few extra methods, because it's wrapping a Vue component, and `find()` only returns a regular DOM node, but many of the methods are shared between both. Some other methods include `classes()`, which gets the classes a DOM node has, and `trigger()` for simulating user interaction. You can find a list of methods supported [here](/api/#wrapper-methods).
57
67
58
68
## Using `data`
59
69
60
-
The final test is to assert that the admin link is rendered when `admin` is `true`. It's default by `false`, but we can override that using the second argument to `mount()`, the [`mounting options`](/api/#mount-options). For `data`, we use the aptly named `data` option:
70
+
The final test is to assert that the admin link is rendered when `admin` is `true`. It's `false` by default, but we can override that using the second argument to `mount()`, the [`mounting options`](/api/#mount-options).
Copy file name to clipboardExpand all lines: src/guide/event-handling.md
+89-49
Original file line number
Diff line number
Diff line change
@@ -4,14 +4,11 @@ Vue components interact with each other via props and by emitting events by call
4
4
5
5
## The Counter component
6
6
7
-
Here is a simple `<Counter>` component. It has a button that increments a `count` property when it is clicked. It also emits an `increment` event with the latest value of `count` by calling `this.$emit('increment', this.count)`:
7
+
Here is a simple `<Counter>` component. It features a button that, when clicked, increments an internal count variable and emits its value:
To fully test this component, we should verify that when the button is clicked, the `count` shown in the template is updated. We can also verify that an `increment` event with the latest `count` value is emitted. We will start with the latter.
26
+
To fully test this component, we should verify that an `increment` event with the latest `count` value is emitted.
30
27
31
-
## `VueWrapper.emitted()`
28
+
## Asserting the emitted events
32
29
33
-
`VueWrapper` has an `emitted()` method. It returns an object with all the events the component has emitted, and their arguments in an array. Let's see how it works:
30
+
To do so, we will rely on the `emitted()` method. It **returns an object with all the events the component has emitted**, and their arguments in an array. Let's see how it works:
34
31
35
32
```js
36
-
test('emits and event with count when clicked', () => {
> If you haven't seen `trigger()` before, don't worry. It's used to simulate user interaction. You can learn more in [Forms](/guide/forms).
43
+
> If you haven't seen `trigger()` before, don't worry. It's used to simulate user interaction. You can learn more in [Forms](/guide/forms).
47
44
48
-
The output of `emitted()` might be a little confusing at first. In this test, we are only interested in the `increment` event, so we can access that with `wrapper.emitted('increment')`. This would return `[ [ 1 ], [ 2 ], [ 3 ] ]`. Let's format it a bit more nicely to see what's going on:
45
+
The first thing to notice is that `emitted()` returns an object, where each key matches an emitted event. In this case, `increment`.
49
46
50
-
```js
51
-
// console.log(wrapper.emitted('increment'))
52
-
[
53
-
[ 1 ], // first time is it called, `count` is 1
54
-
[ 2 ], // second time is it called, `count` is 2
55
-
[ 3 ], // third time is it called, `count` is 3
56
-
]
47
+
This test should pass. We made sure we emitted an event with the appropriate name.
48
+
49
+
## Asserting the arguments of the event
50
+
51
+
This is good - but we can do better! We need to check that we emit the right arguments when `this.$emit('increment', this.count)` is called.
52
+
53
+
Our next step is to assert that the event contains the `count` value. We do so by passing an argument to `emitted()`.
54
+
55
+
```js {9}
56
+
test('emits an event with count when clicked', () => {
57
+
constwrapper=mount(Counter)
58
+
59
+
wrapper.find('button').trigger('click')
60
+
wrapper.find('button').trigger('click')
61
+
62
+
// `emitted()` accepts an argument. It returns an array with all the
63
+
// occurrences of `this.$emit('increment')`.
64
+
constincrementEvent=wrapper.emitted('increment')
65
+
66
+
// We have "clicked" twice, so the array of `increment` should
67
+
// have two values.
68
+
expect(incrementEvent).toHaveLength(2)
69
+
70
+
// Assert the result of the first click.
71
+
// Notice that the value is an array.
72
+
expect(incrementEvent[0]).toEqual([1])
73
+
74
+
// Then, the result of the second one.
75
+
expect(incrementEvent[1]).toEqual([2])
76
+
})
57
77
```
58
78
59
-
Each entry in the array represents one `increment` event that was emitted. Each entry in the array represents an argument to `this.$emit()`. For example, if the code was `this.$emit('increment, this.count, { status: 'success' })`, and the button was clicked twice, `emitted('increment')` would be:
79
+
Let's recap and break down the output of `emitted()`. Each of these keys contains the different values emitted during the test:
60
80
61
81
```js
62
-
[
63
-
[ // first `increment` event
64
-
1, // first argument
65
-
{ status:'success' } // second argument
66
-
],
67
-
[ // second `increment` event
68
-
2, // first argument
69
-
{ status:'success' } // second argument
70
-
]
82
+
// console.log(wrapper.emitted('increment'))
83
+
[
84
+
[ 1 ], // first time it is called, `count` is 1
85
+
[ 2 ], // second time it is called, `count` is 2
71
86
]
72
87
```
73
88
74
-
Each element in the array corresponds to an argument in `this.$emit`.
75
-
76
-
## Writing a Test
89
+
## Asserting complex events
77
90
78
-
Now we know that `emitted('eventName')` captures the events, we can write a simple test to assert an `increment` event is emitted:
79
-
80
-
```js
81
-
test('emits and event with count when clicked', () => {
82
-
constwrapper=mount(Counter)
91
+
Imagine that now our `<Counter>` component needs to emit an object with additional information. For instance, we need to tell any parent component listening to the `@increment` event if `count` is even or odd:
This is good - but we can do better. With the knowledge that `increment` will return an array, where each element represents an event and it's arguments, we can fully test the component by making assertions against the arguments passed when `this.$emit('increment')` is called:
114
+
As we did before, we need to trigger the `click` event on the `<button>`element. Then, we use `emitted('increment')` to make sure the right values are emitted.
91
115
92
116
```js
93
-
test('emits and event with count when clicked', () => {
117
+
test('emits an event with count when clicked', () => {
// Then, we can make sure each element of `wrapper.emitted('increment')`
128
+
// contains an array with the expected object.
129
+
expect(wrapper.emitted('increment')[0]).toEqual([
130
+
{
131
+
count:1,
132
+
isEven:false
133
+
}
134
+
])
135
+
136
+
expect(wrapper.emitted('increment')[1]).toEqual([
137
+
{
138
+
count:2,
139
+
isEven:true
140
+
}
141
+
])
104
142
})
105
143
```
106
144
145
+
Testing complex event payloads such as Objects is no different from testing simple values such as numbers or strings.
146
+
107
147
## Composition API
108
148
109
149
If you are using the Composition API, you will be calling `context.emit()` instead of `this.$emit()`. `emitted()` captures events from both, so you can test your component using the same techniques described here.
@@ -112,4 +152,4 @@ If you are using the Composition API, you will be calling `context.emit()` inste
112
152
113
153
- Use `emitted()` to access the events emitted from a Vue component.
114
154
-`emitted(eventName)` returns an array, where each element represents one event emitted.
115
-
- Arguments are stored in `emitted(eventName)[index]` in an array, in the same order they are emitted.
155
+
- Arguments are stored in `emitted(eventName)[index]` in an array in the same order they are emitted.
0 commit comments