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
As usual, we start of by using `mount` to render the element. We are also asseting that only 1 todo is rendered - this makes it clear that we are adding an additional todo, as the final line of the test suggests.
105
+
As usual, we start of by using `mount` to render the element. We are also asseting that only 1 todo is rendered - this makes it clear that we are adding an additional todo, as the final line of the test suggests.
106
106
107
-
To update the `<input>`, we use `element` - this accesses the original DOM element wrapper, which is returned by `find`. `element` is useful to manipulate a DOM element in a manner that VTU does not provide any methods for.
107
+
To update the `<input>`, we use `setValue` - this allows us to set the input's value.
108
108
109
-
After updating the `<input>`, we use the `trigger` method to simulate the user submitting the form. Finally, we assert the number of todos has increased from 1 to 2.
109
+
After updating the `<input>`, we use the `trigger` method to simulate the user submitting the form. Finally, we assert the number of todos has increased from 1 to 2.
110
110
111
111
If we run this test, it will obviously fail. Let's update `TodoApp.vue` to have the `<form>` and `<input>` elements and make the test pass:
Received array: [{"element": <div data-test="todo">Learn Vue.js 3</div>}]
170
170
```
171
171
172
-
The number of todos has not increased. The problem is that Jest executes tests in a synchronous manner, ending the test as soon as the final function is called. Vue, however, updates the DOM asynchronously. We need to mark the test `async`, and call `await` on any methods that might cause the DOM to change. `trigger` is one such method - we can simply prepend `await`to the `trigger` call:
172
+
The number of todos has not increased. The problem is that Jest executes tests in a synchronous manner, ending the test as soon as the final function is called. Vue, however, updates the DOM asynchronously. We need to mark the test `async`, and call `await` on any methods that might cause the DOM to change. `trigger` is one such methods, and so is `setValue`- we can simply prepend `await`and the test should work as expected:
173
173
174
174
```js
175
175
import { mount } from'@vue/test-utils'
@@ -178,7 +178,7 @@ import TodoApp from './TodoApp.vue'
This test is similar to the previous two; we find an element, interact with in in same way (in this test we use `setChecked`, since we are interacting with a `<input type="checkbox">`. Again, since the DOM will be changing (the checkbox `checked` value will be updated) we need to prepand `await` to the interaction.
207
+
This test is similar to the previous two; we find an element and interact with it in same way (we use `setValue` again, since we are interacting with a `<input>`).
208
208
209
209
Lastly, we make an assertion. We will be applying a `completed` class to completed todos - we can then use this to add some styling to visually indicate the status of a todo.
210
210
@@ -213,17 +213,17 @@ We can get this test to pass by updating the `<template>` to include the `<input
213
213
```vue
214
214
<template>
215
215
<div>
216
-
<div
217
-
v-for="todo in todos"
218
-
:key="todo.id"
216
+
<div
217
+
v-for="todo in todos"
218
+
:key="todo.id"
219
219
data-test="todo"
220
-
:class="[ todo.completed ? 'completed' : '' ]"
220
+
:class="[ todo.completed ? 'completed' : '' ]"
221
221
>
222
222
{{ todo.text }}
223
-
<input
224
-
type="checkbox"
225
-
v-model="todo.completed"
226
-
data-test="todo-checkbox"
223
+
<input
224
+
type="checkbox"
225
+
v-model="todo.completed"
226
+
data-test="todo-checkbox"
227
227
/>
228
228
</div>
229
229
@@ -247,7 +247,7 @@ import TodoApp from './TodoApp.vue'
@@ -260,7 +260,7 @@ In the *arrange* phase, we are setting up the scenario for the test. A more comp
260
260
261
261
In the *act* phase, we act out the scenario, simulating how a user would interact with the component or application.
262
262
263
-
In the *assert* phase, we make assertions about how we expect the current state of the component to be.
263
+
In the *assert* phase, we make assertions about how we expect the current state of the component to be.
264
264
265
265
Almost all test will follow these three phases. You don't need to separate them with new lines like this guide does, but it is good to keep these three phases in mind as you write your tests.
0 commit comments