Skip to content

Commit f5c1482

Browse files
authored
docs: updated example TodoApp to script setup (#2727)
1 parent 51672b9 commit f5c1482

File tree

3 files changed

+61
-59
lines changed

3 files changed

+61
-59
lines changed

examples/TodoApp.spec.js

Lines changed: 0 additions & 32 deletions
This file was deleted.

examples/TodoApp.spec.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* This is the example app used in the documentation.
3+
* If you want to run it, you will need to build the final bundle with
4+
* pnpm build. Then you can run this with `pnpm test examples`
5+
*/
6+
import { mount } from '../dist/vue-test-utils.cjs.js'
7+
import { expect, describe, it } from 'vitest'
8+
9+
import TodoApp from './TodoApp.vue'
10+
11+
describe('Todo App', () => {
12+
it('renders a todo', () => {
13+
const wrapper = mount(TodoApp)
14+
15+
expect(wrapper.findAll('[data-test="todo"]')).toHaveLength(1)
16+
expect(wrapper.find('[data-test="todo"]').text()).toBe('Learn Vue.js 3')
17+
})
18+
19+
it('creates a todo', async () => {
20+
const wrapper = mount(TodoApp)
21+
22+
await wrapper.find('[data-test="new-todo"]').setValue('New todo')
23+
await wrapper.find('[data-test="form"]').trigger('submit')
24+
expect(wrapper.findAll('[data-test="todo"]')).toHaveLength(2)
25+
})
26+
27+
it('completes a todo', async () => {
28+
const wrapper = mount(TodoApp)
29+
30+
expect(wrapper.find('[data-test="todo"]').classes()).not.toContain(
31+
'completed'
32+
)
33+
34+
await wrapper.find('[data-test="todo-checkbox"]').setChecked()
35+
36+
expect(wrapper.find('[data-test="todo"]').classes()).toContain('completed')
37+
})
38+
})

examples/TodoApp.vue

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
v-for="todo in todos"
55
:key="todo.id"
66
data-test="todo"
7-
:class="[todo.completed ? 'completed' : '']"
7+
:class="{ completed: todo.completed }"
88
>
99
{{ todo.text }}
1010
<input
@@ -20,33 +20,29 @@
2020
</div>
2121
</template>
2222

23-
<script lang="ts">
24-
import { defineComponent } from 'vue'
23+
<script setup lang="ts">
24+
import { ref } from 'vue'
2525
26-
export default defineComponent({
27-
name: 'TodoApp',
26+
interface TODO {
27+
id: number
28+
text: string
29+
completed: boolean
30+
}
2831
29-
data() {
30-
return {
31-
newTodo: '',
32-
todos: [
33-
{
34-
id: 1,
35-
text: 'Learn Vue.js 3',
36-
completed: false
37-
}
38-
]
39-
}
40-
},
41-
42-
methods: {
43-
createTodo() {
44-
this.todos.push({
45-
id: 2,
46-
text: this.newTodo,
47-
completed: false
48-
})
49-
}
32+
const newTodo = ref<string>('')
33+
const todos = ref<TODO[]>([
34+
{
35+
id: 1,
36+
text: 'Learn Vue.js 3',
37+
completed: false
5038
}
51-
})
39+
])
40+
41+
const createTodo = () => {
42+
todos.value.push({
43+
id: 2,
44+
text: newTodo.value,
45+
completed: false
46+
})
47+
}
5248
</script>

0 commit comments

Comments
 (0)