@@ -2,19 +2,27 @@ import { renderHook } from '@testing-library/react';
2
2
import { describe , expect , it } from 'vitest' ;
3
3
import useCalendar from '../useCalendar' ;
4
4
5
- describe ( 'useCalendar' , ( ) => {
6
- const february = 1 ;
7
- const march = 2 ;
8
- const november = 10 ;
9
- const april = 3 ;
10
- const july = 6 ;
11
- const august = 7 ;
12
- const june = 5 ;
13
- const december = 11 ;
5
+ const february = 1 ;
6
+ const march = 2 ;
7
+ const april = 3 ;
8
+ const june = 5 ;
9
+ const july = 6 ;
10
+ const august = 7 ;
11
+ const november = 10 ;
12
+ const december = 11 ;
13
+
14
+ const displayedMonthStartDate = new Date ( 2024 , july , 1 ) ;
15
+ const selectedSlot = { start : new Date ( 2024 , july , 10 ) , end : new Date ( 2024 , july , 20 ) } ;
16
+ const selectableSlot = { start : new Date ( 2024 , july , 5 ) , end : new Date ( 2024 , july , 25 ) } ;
17
+
18
+ const { result } = renderHook ( ( ) =>
19
+ useCalendar ( { displayedMonthStartDate, selectedSlot, selectableSlot, onDayClick : ( ) => { } } )
20
+ ) ;
14
21
22
+ describe ( 'useCalendar' , ( ) => {
15
23
describe ( 'day calculation' , ( ) => {
16
24
it ( 'should include days from the previous month when the first day of the displayed month is not a Monday' , ( ) => {
17
- const displayedMonthStartDate = new Date ( 2024 , march , 1 ) ;
25
+ const displayedMonthStartDate = new Date ( 2024 , march , 1 ) ; //friday
18
26
const { result } = renderHook ( ( ) =>
19
27
useCalendar ( { displayedMonthStartDate, onDayClick : ( ) => { } } )
20
28
) ;
@@ -23,7 +31,8 @@ describe('useCalendar', () => {
23
31
} ) ;
24
32
25
33
it ( 'should include days from the next month when the last day of the displayed month is not a Sunday' , ( ) => {
26
- const displayedMonthStartDate = new Date ( 2024 , november , 1 ) ;
34
+ //the last day of november 2024 is a saturday
35
+ const displayedMonthStartDate = new Date ( 2024 , november , 1 ) ; //friday
27
36
const { result } = renderHook ( ( ) =>
28
37
useCalendar ( { displayedMonthStartDate, onDayClick : ( ) => { } } )
29
38
) ;
@@ -32,7 +41,7 @@ describe('useCalendar', () => {
32
41
} ) ;
33
42
34
43
it ( 'should not include days from the previous month when the displayed month starts on a Monday' , ( ) => {
35
- const displayedMonthStartDate = new Date ( 2024 , july , 1 ) ;
44
+ const displayedMonthStartDate = new Date ( 2024 , july , 1 ) ; //monday
36
45
const { result } = renderHook ( ( ) =>
37
46
useCalendar ( { displayedMonthStartDate, onDayClick : ( ) => { } } )
38
47
) ;
@@ -41,7 +50,7 @@ describe('useCalendar', () => {
41
50
} ) ;
42
51
43
52
it ( 'should not include days from the next month when the displayed month ends on a Sunday' , ( ) => {
44
- const displayedMonthStartDate = new Date ( 2024 , june , 1 ) ;
53
+ const displayedMonthStartDate = new Date ( 2024 , june , 1 ) ; //saturday
45
54
const { result } = renderHook ( ( ) =>
46
55
useCalendar ( { displayedMonthStartDate, onDayClick : ( ) => { } } )
47
56
) ;
@@ -52,25 +61,12 @@ describe('useCalendar', () => {
52
61
53
62
describe ( 'day wrapper classnames' , ( ) => {
54
63
it ( 'should have "day-wrapper" classname for all days' , ( ) => {
55
- const displayedMonthStartDate = new Date ( 2024 , july , 1 ) ;
56
- const selectedSlot = { start : new Date ( 2024 , july , 10 ) , end : new Date ( 2024 , july , 20 ) } ;
57
- const selectableSlot = { start : new Date ( 2024 , july , 5 ) , end : new Date ( 2024 , july , 25 ) } ;
58
- const { result } = renderHook ( ( ) =>
59
- useCalendar ( { displayedMonthStartDate, selectedSlot, selectableSlot, onDayClick : ( ) => { } } )
60
- ) ;
61
64
expect ( result . current . buildDayWrapperClassName ( new Date ( 2024 , july , 15 ) ) ) . toContain (
62
65
'day-wrapper'
63
66
) ;
64
67
} ) ;
65
68
66
69
describe ( 'selectable slot classname' , ( ) => {
67
- const displayedMonthStartDate = new Date ( 2024 , july , 1 ) ;
68
- const selectedSlot = { start : new Date ( 2024 , july , 10 ) , end : new Date ( 2024 , july , 20 ) } ;
69
- const selectableSlot = { start : new Date ( 2024 , july , 5 ) , end : new Date ( 2024 , july , 25 ) } ;
70
- const { result } = renderHook ( ( ) =>
71
- useCalendar ( { displayedMonthStartDate, selectedSlot, selectableSlot, onDayClick : ( ) => { } } )
72
- ) ;
73
-
74
70
it ( 'should have "outside-selectable-slot" classname for days outside the selectable slot' , ( ) => {
75
71
expect ( result . current . buildDayWrapperClassName ( new Date ( 2024 , april , 1 ) ) ) . toContain (
76
72
'outside-selectable-slot'
@@ -85,13 +81,6 @@ describe('useCalendar', () => {
85
81
} ) ;
86
82
87
83
describe ( 'selected slot classname' , ( ) => {
88
- const displayedMonthStartDate = new Date ( 2024 , july , 1 ) ;
89
- const selectedSlot = { start : new Date ( 2024 , july , 10 ) , end : new Date ( 2024 , july , 20 ) } ;
90
- const selectableSlot = { start : new Date ( 2024 , july , 5 ) , end : new Date ( 2024 , july , 25 ) } ;
91
- const { result } = renderHook ( ( ) =>
92
- useCalendar ( { displayedMonthStartDate, selectedSlot, selectableSlot, onDayClick : ( ) => { } } )
93
- ) ;
94
-
95
84
it ( 'should have "start" classname for the start day of the selected slot' , ( ) => {
96
85
expect ( result . current . buildDayWrapperClassName ( selectedSlot . start ) ) . toContain ( 'start' ) ;
97
86
} ) ;
@@ -105,22 +94,23 @@ describe('useCalendar', () => {
105
94
'within-selected-slot'
106
95
) ;
107
96
} ) ;
97
+
98
+ it ( 'should not have "within-selected-slot" classname for days within the selected slot' , ( ) => {
99
+ expect ( result . current . buildDayWrapperClassName ( new Date ( 2024 , july , 21 ) ) ) . not . toContain (
100
+ 'within-selected-slot'
101
+ ) ;
102
+ } ) ;
108
103
} ) ;
109
104
110
105
describe ( 'other classnames' , ( ) => {
111
- const displayedMonthStartDate = new Date ( 2024 , july , 1 ) ;
112
- const selectedSlot = { start : new Date ( 2024 , july , 10 ) , end : null } ;
113
- const selectableSlot = { start : new Date ( 2024 , july , 5 ) , end : new Date ( 2024 , july , 25 ) } ;
114
- const { result } = renderHook ( ( ) =>
115
- useCalendar ( { displayedMonthStartDate, selectedSlot, selectableSlot, onDayClick : ( ) => { } } )
116
- ) ;
117
-
118
106
it ( 'should have "past" classname for days before today' , ( ) => {
119
107
expect ( result . current . buildDayWrapperClassName ( new Date ( 1900 , june , 30 ) ) ) . toContain ( 'past' ) ;
120
108
} ) ;
121
109
122
110
it ( 'should not assign past if the day is after selected start' , ( ) => {
123
- expect ( result . current . buildDayWrapperClassName ( new Date ( 2024 , august , 1 ) ) ) . not . toContain ( 'past' ) ;
111
+ expect ( result . current . buildDayWrapperClassName ( new Date ( 2024 , august , 1 ) ) ) . not . toContain (
112
+ 'past'
113
+ ) ;
124
114
} ) ;
125
115
126
116
it ( 'should have "current-month" classname for days in the displayed month' , ( ) => {
@@ -132,13 +122,6 @@ describe('useCalendar', () => {
132
122
} ) ;
133
123
134
124
describe ( 'isDateSelectable' , ( ) => {
135
- const displayedMonthStartDate = new Date ( 2024 , july , 1 ) ;
136
- const selectedSlot = { start : new Date ( 2024 , july , 10 ) , end : null } ;
137
- const selectableSlot = { start : new Date ( 2024 , july , 5 ) , end : new Date ( 2024 , july , 25 ) } ;
138
- const { result } = renderHook ( ( ) =>
139
- useCalendar ( { displayedMonthStartDate, selectedSlot, selectableSlot, onDayClick : ( ) => { } } )
140
- ) ;
141
-
142
125
it ( 'should return false for days outside the selectable slot' , ( ) => {
143
126
expect ( result . current . isDateSelectable ( new Date ( 2024 , april , 1 ) ) ) . toBe ( false ) ;
144
127
} ) ;
@@ -159,9 +142,6 @@ describe('useCalendar', () => {
159
142
describe ( 'isToday' , ( ) => {
160
143
const today = new Date ( ) ;
161
144
today . setHours ( 0 , 0 , 0 , 0 ) ;
162
- const displayedMonthStartDate = new Date ( 2024 , july , 1 ) ;
163
- const selectedSlot = { start : new Date ( 2024 , july , 10 ) , end : null } ;
164
- const selectableSlot = { start : new Date ( 2024 , july , 5 ) , end : new Date ( 2024 , july , 25 ) } ;
165
145
const { result } = renderHook ( ( ) =>
166
146
useCalendar ( { displayedMonthStartDate, selectedSlot, selectableSlot, onDayClick : ( ) => { } } )
167
147
) ;
0 commit comments