1+ import { describe , expect , jest , test } from '@jest/globals' ;
2+ import { isDateBetween } from "../src/utils/compareDates"
3+
4+ // For testing purposes, we need to modify the function to accept a custom "now" date
5+ // This allows us to test all scenarios regardless of the current date
6+ function correctDay ( offering : any , customNow ?: Date ) : boolean {
7+ const weekdays = [ "SU" , "MO" , "TU" , "WE" , "TH" , "FR" , "SA" ]
8+ const semester = offering ?. offering ;
9+ const day = offering ?. day ;
10+ if ( ! semester || ! day ) return false ;
11+ const now = customNow || new Date ( ) ;
12+ let startDay ;
13+ let endDay ;
14+ if ( semester === "Summer 2025" ) {
15+ startDay = new Date ( 2025 , 5 , 2 ) ;
16+ endDay = new Date ( 2025 , 8 , 7 ) ;
17+ }
18+ else if ( semester === "Fall 2025" ) {
19+ startDay = new Date ( 2025 , 9 , 3 ) ;
20+ endDay = new Date ( 2025 , 12 , 3 ) ;
21+ }
22+ else { // Winter 2026
23+ startDay = new Date ( 2026 , 1 , 6 ) ;
24+ endDay = new Date ( 2026 , 4 , 4 ) ;
25+ }
26+ if ( ! isDateBetween ( now , startDay , endDay ) ) {
27+ return false ;
28+ }
29+ if ( weekdays [ now . getDay ( ) ] !== day ) {
30+ return false ;
31+ }
32+ return true ;
33+ }
34+
35+ describe ( 'correctDay function' , ( ) => {
36+ test ( 'should return false for null or undefined offering' , ( ) => {
37+ expect ( correctDay ( null ) ) . toBe ( false ) ;
38+ expect ( correctDay ( undefined ) ) . toBe ( false ) ;
39+ } ) ;
40+
41+ test ( 'should return false for missing offering properties' , ( ) => {
42+ expect ( correctDay ( { } ) ) . toBe ( false ) ;
43+ expect ( correctDay ( { offering : "Summer 2025" } ) ) . toBe ( false ) ;
44+ expect ( correctDay ( { day : "MO" } ) ) . toBe ( false ) ;
45+ } ) ;
46+
47+ test ( 'should validate correct day in Summer 2025' , ( ) => {
48+ // Create specific dates for each day of the week within Summer 2025
49+ const summerDates = [
50+ new Date ( 2025 , 5 , 8 ) , // Sunday (June 8, 2025)
51+ new Date ( 2025 , 5 , 9 ) , // Monday (June 9, 2025)
52+ new Date ( 2025 , 5 , 10 ) , // Tuesday (June 10, 2025)
53+ new Date ( 2025 , 5 , 11 ) , // Wednesday (June 11, 2025)
54+ new Date ( 2025 , 5 , 12 ) , // Thursday (June 12, 2025)
55+ new Date ( 2025 , 5 , 13 ) , // Friday (June 13, 2025)
56+ new Date ( 2025 , 5 , 14 ) // Saturday (June 14, 2025)
57+ ] ;
58+
59+ // Test each day with its corresponding date
60+ const weekdays = [ "SU" , "MO" , "TU" , "WE" , "TH" , "FR" , "SA" ] ;
61+ summerDates . forEach ( ( date , index ) => {
62+ // Make sure the getDay returns the expected index
63+ jest . spyOn ( date , 'getDay' ) . mockReturnValue ( index ) ;
64+
65+ // This should pass only for the matching day
66+ expect ( correctDay ( { offering : "Summer 2025" , day : weekdays [ index ] } , date ) ) . toBe ( true ) ;
67+
68+ // Test all other days should fail
69+ weekdays . forEach ( ( wrongDay , wrongIndex ) => {
70+ if ( wrongIndex !== index ) {
71+ expect ( correctDay ( { offering : "Summer 2025" , day : wrongDay } , date ) ) . toBe ( false ) ;
72+ }
73+ } ) ;
74+ } ) ;
75+ } ) ;
76+
77+ test ( 'should validate correct day in Fall 2025' , ( ) => {
78+ // Create a date in Fall 2025
79+ const fallDate = new Date ( 2025 , 9 , 15 ) ; // October 15, 2025
80+
81+ // Mock getDay to return 3 (Wednesday)
82+ jest . spyOn ( fallDate , 'getDay' ) . mockReturnValue ( 3 ) ;
83+
84+ // Test all days - only Wednesday should pass
85+ expect ( correctDay ( { offering : "Fall 2025" , day : "WE" } , fallDate ) ) . toBe ( true ) ;
86+ expect ( correctDay ( { offering : "Fall 2025" , day : "SU" } , fallDate ) ) . toBe ( false ) ;
87+ expect ( correctDay ( { offering : "Fall 2025" , day : "MO" } , fallDate ) ) . toBe ( false ) ;
88+ expect ( correctDay ( { offering : "Fall 2025" , day : "TU" } , fallDate ) ) . toBe ( false ) ;
89+ expect ( correctDay ( { offering : "Fall 2025" , day : "TH" } , fallDate ) ) . toBe ( false ) ;
90+ expect ( correctDay ( { offering : "Fall 2025" , day : "FR" } , fallDate ) ) . toBe ( false ) ;
91+ expect ( correctDay ( { offering : "Fall 2025" , day : "SA" } , fallDate ) ) . toBe ( false ) ;
92+ } ) ;
93+
94+ test ( 'should validate correct day in Winter 2026' , ( ) => {
95+ // Create a date in Winter 2026
96+ const winterDate = new Date ( 2026 , 1 , 20 ) ; // February 20, 2026
97+
98+ // Mock getDay to return 5 (Friday)
99+ jest . spyOn ( winterDate , 'getDay' ) . mockReturnValue ( 5 ) ;
100+
101+ // Test all days - only Friday should pass
102+ expect ( correctDay ( { offering : "Winter 2026" , day : "FR" } , winterDate ) ) . toBe ( true ) ;
103+ expect ( correctDay ( { offering : "Winter 2026" , day : "SU" } , winterDate ) ) . toBe ( false ) ;
104+ expect ( correctDay ( { offering : "Winter 2026" , day : "MO" } , winterDate ) ) . toBe ( false ) ;
105+ expect ( correctDay ( { offering : "Winter 2026" , day : "TU" } , winterDate ) ) . toBe ( false ) ;
106+ expect ( correctDay ( { offering : "Winter 2026" , day : "WE" } , winterDate ) ) . toBe ( false ) ;
107+ expect ( correctDay ( { offering : "Winter 2026" , day : "TH" } , winterDate ) ) . toBe ( false ) ;
108+ expect ( correctDay ( { offering : "Winter 2026" , day : "SA" } , winterDate ) ) . toBe ( false ) ;
109+ } ) ;
110+
111+ test ( 'should return false when date is outside semester range' , ( ) => {
112+ // Create dates outside each semester range
113+ const beforeSummer = new Date ( 2025 , 5 , 1 ) ; // June 1, 2025 (before Summer 2025)
114+ const afterSummer = new Date ( 2025 , 8 , 8 ) ; // September 8, 2025 (after Summer 2025)
115+ const beforeFall = new Date ( 2025 , 9 , 2 ) ; // October 2, 2025 (before Fall 2025)
116+ const afterFall = new Date ( 2025 , 12 , 4 ) ; // December 4, 2025 (after Fall 2025)
117+ const beforeWinter = new Date ( 2026 , 1 , 5 ) ; // February 5, 2026 (before Winter 2026)
118+ const afterWinter = new Date ( 2026 , 4 , 5 ) ; // May 5, 2026 (after Winter 2026)
119+
120+ // Mock getDay to return 0 (Sunday) for all dates
121+ const testDates = [ beforeSummer , afterSummer , beforeFall , afterFall , beforeWinter , afterWinter ] ;
122+ testDates . forEach ( date => {
123+ jest . spyOn ( date , 'getDay' ) . mockReturnValue ( 0 ) ;
124+ } ) ;
125+
126+ // Test dates outside Summer 2025
127+ expect ( correctDay ( { offering : "Summer 2025" , day : "SU" } , beforeSummer ) ) . toBe ( false ) ;
128+ expect ( correctDay ( { offering : "Summer 2025" , day : "SU" } , afterSummer ) ) . toBe ( false ) ;
129+
130+ // Test dates outside Fall 2025
131+ expect ( correctDay ( { offering : "Fall 2025" , day : "SU" } , beforeFall ) ) . toBe ( false ) ;
132+ expect ( correctDay ( { offering : "Fall 2025" , day : "SU" } , afterFall ) ) . toBe ( false ) ;
133+
134+ // Test dates outside Winter 2026
135+ expect ( correctDay ( { offering : "Winter 2026" , day : "SU" } , beforeWinter ) ) . toBe ( false ) ;
136+ expect ( correctDay ( { offering : "Winter 2026" , day : "SU" } , afterWinter ) ) . toBe ( false ) ;
137+ } ) ;
138+
139+ test ( 'should return true for dates inside semester range' , ( ) => {
140+ // Create dates inside each semester range
141+ const duringSummer = new Date ( 2025 , 6 , 15 ) ; // July 15, 2025 (during Summer 2025)
142+ const duringFall = new Date ( 2025 , 10 , 15 ) ; // November 15, 2025 (during Fall 2025)
143+ const duringWinter = new Date ( 2026 , 2 , 15 ) ; // March 15, 2026 (during Winter 2026)
144+
145+ // Mock getDay to return 0 (Sunday) for all dates
146+ const testDates = [ duringSummer , duringFall , duringWinter ] ;
147+ testDates . forEach ( date => {
148+ jest . spyOn ( date , 'getDay' ) . mockReturnValue ( 0 ) ;
149+ } ) ;
150+
151+ // Test dates inside each semester (with matching day)
152+ expect ( correctDay ( { offering : "Summer 2025" , day : "SU" } , duringSummer ) ) . toBe ( true ) ;
153+ expect ( correctDay ( { offering : "Fall 2025" , day : "SU" } , duringFall ) ) . toBe ( true ) ;
154+ expect ( correctDay ( { offering : "Winter 2026" , day : "SU" } , duringWinter ) ) . toBe ( true ) ;
155+ } ) ;
156+
157+ test ( 'should validate edge dates correctly' , ( ) => {
158+ // Test exact start and end dates of semesters
159+ const summerStart = new Date ( 2025 , 5 , 2 ) ; // June 2, 2025
160+ const summerEnd = new Date ( 2025 , 8 , 7 ) ; // September 7, 2025
161+ const fallStart = new Date ( 2025 , 9 , 3 ) ; // October 3, 2025
162+ const fallEnd = new Date ( 2025 , 12 , 3 ) ; // December 3, 2025
163+ const winterStart = new Date ( 2026 , 1 , 6 ) ; // February 6, 2026
164+ const winterEnd = new Date ( 2026 , 4 , 4 ) ; // May 4, 2026
165+
166+ // Mock getDay to return 0 (Sunday) for all dates
167+ const edgeDates = [ summerStart , summerEnd , fallStart , fallEnd , winterStart , winterEnd ] ;
168+ edgeDates . forEach ( date => {
169+ jest . spyOn ( date , 'getDay' ) . mockReturnValue ( 0 ) ;
170+ } ) ;
171+
172+ // Edge dates should be included in the valid range
173+ expect ( correctDay ( { offering : "Summer 2025" , day : "SU" } , summerStart ) ) . toBe ( true ) ;
174+ expect ( correctDay ( { offering : "Summer 2025" , day : "SU" } , summerEnd ) ) . toBe ( true ) ;
175+ expect ( correctDay ( { offering : "Fall 2025" , day : "SU" } , fallStart ) ) . toBe ( true ) ;
176+ expect ( correctDay ( { offering : "Fall 2025" , day : "SU" } , fallEnd ) ) . toBe ( true ) ;
177+ expect ( correctDay ( { offering : "Winter 2026" , day : "SU" } , winterStart ) ) . toBe ( true ) ;
178+ expect ( correctDay ( { offering : "Winter 2026" , day : "SU" } , winterEnd ) ) . toBe ( true ) ;
179+ } ) ;
180+ } ) ;
0 commit comments