1
- import { beforeEach , describe , expect , it } from 'vitest'
1
+ import { beforeEach , describe , expect , it , beforeAll } from 'vitest'
2
2
import { mount } from '@vue/test-utils'
3
3
4
4
import ToDoList from './ToDoList.vue'
5
5
import { createPinia , setActivePinia } from 'pinia'
6
+ import type { ToDo } from '@/types'
7
+ import { nextTick } from 'vue'
6
8
7
9
describe ( 'ToDoList' , ( ) => {
8
10
beforeEach ( ( ) => {
@@ -14,8 +16,68 @@ describe('ToDoList', () => {
14
16
15
17
const label = wrapper . find ( 'label' )
16
18
17
- console . log ( label )
18
-
19
19
expect ( label . text ( ) ) . toMatchInlineSnapshot ( `"Show only pending"` )
20
20
} )
21
21
} )
22
+
23
+ describe ( 'ToDoList (with localStorage)' , ( ) => {
24
+ beforeAll ( ( ) => {
25
+ setActivePinia ( createPinia ( ) )
26
+ } )
27
+
28
+ beforeEach ( ( ) => {
29
+ localStorage . setItem (
30
+ 'todos' ,
31
+ JSON . stringify ( < ToDo [ ] > [
32
+ { timestamp : 1 , text : 'First todo' , completed : false } ,
33
+ { timestamp : 2 , text : 'Second todo' , completed : true } ,
34
+ ] ) ,
35
+ )
36
+ } )
37
+
38
+ it ( 'should render a list with the expected number of items' , async ( ) => {
39
+ const wrapper = mount ( ToDoList )
40
+ // Wait for the next tick to allow the component to update
41
+ // after the localStorage data is loaded inside the `onMounted` hook
42
+ await nextTick ( )
43
+
44
+ const list = wrapper . find ( 'ul' )
45
+ const listItems = list . findAll ( 'li' )
46
+
47
+ expect ( listItems ) . toHaveLength ( 2 )
48
+ } )
49
+
50
+ it ( 'should render a list and some items should be checked' , async ( ) => {
51
+ const wrapper = mount ( ToDoList )
52
+ // Wait for the next tick to allow the component to update
53
+ // after the localStorage data is loaded inside the `onMounted` hook
54
+ await nextTick ( )
55
+
56
+ const list = wrapper . find ( 'ul' )
57
+ const listItems = list . findAll ( 'li' )
58
+
59
+ const checkedItems = listItems . filter ( ( item ) => item . find ( 'input' ) . element . checked )
60
+
61
+ expect ( checkedItems ) . toHaveLength ( 1 )
62
+ } )
63
+
64
+ it ( 'should render a list and the items can be checked' , async ( ) => {
65
+ const wrapper = mount ( ToDoList )
66
+ // Wait for the next tick to allow the component to update
67
+ // after the localStorage data is loaded inside the `onMounted` hook
68
+ await nextTick ( )
69
+
70
+ const list = wrapper . find ( 'ul' )
71
+ const listItems = list . findAll ( 'li' )
72
+
73
+ const unCheckedItems = listItems . filter ( ( item ) => ! item . find ( 'input' ) . element . checked )
74
+
75
+ expect ( unCheckedItems ) . toHaveLength ( 1 )
76
+
77
+ await unCheckedItems [ 0 ] . find ( 'input' ) . trigger ( 'click' )
78
+
79
+ const checkedItems = listItems . filter ( ( item ) => item . find ( 'input' ) . element . checked )
80
+
81
+ expect ( checkedItems ) . toHaveLength ( 2 )
82
+ } )
83
+ } )
0 commit comments