1- import { beforeEach , describe , expect , it } from 'vitest'
1+ import { beforeEach , describe , expect , it , beforeAll } from 'vitest'
22import { mount } from '@vue/test-utils'
33
44import ToDoList from './ToDoList.vue'
55import { createPinia , setActivePinia } from 'pinia'
6+ import type { ToDo } from '@/types'
7+ import { nextTick } from 'vue'
68
79describe ( 'ToDoList' , ( ) => {
810 beforeEach ( ( ) => {
@@ -14,8 +16,68 @@ describe('ToDoList', () => {
1416
1517 const label = wrapper . find ( 'label' )
1618
17- console . log ( label )
18-
1919 expect ( label . text ( ) ) . toMatchInlineSnapshot ( `"Show only pending"` )
2020 } )
2121} )
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