Skip to content

Commit 21bb888

Browse files
committed
docs: leverage setValue in crash course
1 parent 91c1510 commit 21bb888

File tree

1 file changed

+24
-24
lines changed

1 file changed

+24
-24
lines changed

src/guide/a-crash-course.md

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ If we run this test now, it fails with the following error message: `Cannot call
7070
```vue
7171
<template>
7272
<div>
73-
<div
74-
v-for="todo in todos"
75-
:key="todo.id"
73+
<div
74+
v-for="todo in todos"
75+
:key="todo.id"
7676
data-test="todo"
7777
>
7878
{{ todo.text }}
@@ -95,27 +95,27 @@ test('creates a todo', () => {
9595
const wrapper = mount(TodoApp)
9696
expect(wrapper.findAll('[data-test="todo"]')).toHaveLength(1)
9797

98-
wrapper.find('[data-test="new-todo"]').element.value = 'New todo'
98+
wrapper.find('[data-test="new-todo"]').setValue('New todo')
9999
wrapper.find('[data-test="form"]').trigger('submit')
100100

101101
expect(wrapper.findAll('[data-test="todo"]')).toHaveLength(2)
102102
})
103103
```
104104

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.
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.
106106

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.
108108

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.
110110

111111
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:
112112

113113
```vue
114114
<template>
115115
<div>
116-
<div
117-
v-for="todo in todos"
118-
:key="todo.id"
116+
<div
117+
v-for="todo in todos"
118+
:key="todo.id"
119119
data-test="todo"
120120
>
121121
{{ todo.text }}
@@ -169,7 +169,7 @@ expect(received).toHaveLength(expected)
169169
Received array: [{"element": <div data-test="todo">Learn Vue.js 3</div>}]
170170
```
171171

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:
173173

174174
```js
175175
import { mount } from '@vue/test-utils'
@@ -178,7 +178,7 @@ import TodoApp from './TodoApp.vue'
178178
test('creates a todo', async () => {
179179
const wrapper = mount(TodoApp)
180180

181-
wrapper.find('[data-test="new-todo"]').element.value = 'New todo'
181+
await wrapper.find('[data-test="new-todo"]').setValue('New todo')
182182
await wrapper.find('[data-test="form"]').trigger('submit')
183183

184184
expect(wrapper.findAll('[data-test="todo"]')).toHaveLength(2)
@@ -198,13 +198,13 @@ import TodoApp from './TodoApp.vue'
198198
test('completes a todo', async () => {
199199
const wrapper = mount(TodoApp)
200200

201-
await wrapper.find('[data-test="todo-checkbox"]').setChecked()
201+
await wrapper.find('[data-test="todo-checkbox"]').setValue(true)
202202

203203
expect(wrapper.find('[data-test="todo"]').classes()).toContain('completed')
204204
})
205205
```
206206

207-
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>`).
208208

209209
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.
210210

@@ -213,17 +213,17 @@ We can get this test to pass by updating the `<template>` to include the `<input
213213
```vue
214214
<template>
215215
<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"
219219
data-test="todo"
220-
:class="[ todo.completed ? 'completed' : '' ]"
220+
:class="[ todo.completed ? 'completed' : '' ]"
221221
>
222222
{{ 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"
227227
/>
228228
</div>
229229
@@ -247,7 +247,7 @@ import TodoApp from './TodoApp.vue'
247247
test('creates a todo', async () => {
248248
const wrapper = mount(TodoApp)
249249

250-
wrapper.find('[data-test="new-todo"]').element.value = 'New todo'
250+
await wrapper.find('[data-test="new-todo"]').setValue('New todo')
251251
await wrapper.find('[data-test="form"]').trigger('submit')
252252

253253
expect(wrapper.findAll('[data-test="todo"]')).toHaveLength(2)
@@ -260,7 +260,7 @@ In the *arrange* phase, we are setting up the scenario for the test. A more comp
260260

261261
In the *act* phase, we act out the scenario, simulating how a user would interact with the component or application.
262262

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.
264264

265265
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.
266266

0 commit comments

Comments
 (0)