@@ -12,6 +12,7 @@ import {
1212 generateReservableMap ,
1313 isRangeReservable ,
1414 getDayIntervals ,
15+ isStartTimeValid ,
1516} from "../reservable" ;
1617import {
1718 type IsReservableFieldsFragment ,
@@ -237,7 +238,67 @@ describe("generateReservableMap", () => {
237238 } ) ;
238239} ) ;
239240
241+ describe ( "isStartTimeValid" , ( ) => {
242+ function mockReservableTimes ( ) : ReservableMap {
243+ const map : ReservableMap = new Map ( ) ;
244+ for ( let i = 0 ; i < 30 ; i ++ ) {
245+ const date = addDays ( startOfToday ( ) , i ) ;
246+ const key = format ( date , "yyyy-MM-dd" ) ;
247+ // TODO need to have holes in this
248+ const value = [ { start : startOfDay ( date ) , end : endOfDay ( date ) } ] ;
249+ map . set ( key , value ) ;
250+ }
251+ return map ;
252+ }
253+ // TODO fuzzy this
254+ test ( "YES for 15 min intervals" , ( ) => {
255+ const date = startOfDay ( addDays ( new Date ( ) , 1 ) ) ;
256+ const start = addHours ( date , 9 ) ;
257+ const interval = ReservationStartInterval . Interval_15Mins ;
258+ const reservableTimes = mockReservableTimes ( ) ;
259+ expect ( isStartTimeValid ( start , reservableTimes , interval ) ) . toBe ( true ) ;
260+ } ) ;
261+ // TODO fuzzy
262+ test ( "YES for 30 min intervals" , ( ) => {
263+ const date = startOfDay ( addDays ( new Date ( ) , 1 ) ) ;
264+ const start = addHours ( date , 9 ) ;
265+ const interval = ReservationStartInterval . Interval_30Mins ;
266+ const reservableTimes = mockReservableTimes ( ) ;
267+ expect ( isStartTimeValid ( start , reservableTimes , interval ) ) . toBe ( true ) ;
268+ } ) ;
269+ test ( "YES for 60 min intervals" , ( ) => {
270+ const date = startOfDay ( addDays ( new Date ( ) , 1 ) ) ;
271+ const start = addHours ( date , 9 ) ;
272+ const interval = ReservationStartInterval . Interval_60Mins ;
273+ const reservableTimes = mockReservableTimes ( ) ;
274+ expect ( isStartTimeValid ( start , reservableTimes , interval ) ) . toBe ( true ) ;
275+ } ) ;
276+ test ( "NO for 60 min intervals" , ( ) => {
277+ const date = startOfDay ( addDays ( new Date ( ) , 1 ) ) ;
278+ const start = addHours ( date , 9.5 ) ;
279+ const interval = ReservationStartInterval . Interval_60Mins ;
280+ const reservableTimes = mockReservableTimes ( ) ;
281+ expect ( isStartTimeValid ( start , reservableTimes , interval ) ) . toBe ( false ) ;
282+ } ) ;
283+ test ( "YES for 120 min intervals" , ( ) => {
284+ const date = startOfDay ( addDays ( new Date ( ) , 1 ) ) ;
285+ const start = addHours ( date , 10 ) ;
286+ const interval = ReservationStartInterval . Interval_120Mins ;
287+ const reservableTimes = mockReservableTimes ( ) ;
288+ expect ( isStartTimeValid ( start , reservableTimes , interval ) ) . toBe ( true ) ;
289+ } ) ;
290+ test ( "NO for 120 min intervals" , ( ) => {
291+ const date = startOfDay ( addDays ( new Date ( ) , 1 ) ) ;
292+ const start = addHours ( date , 9 ) ;
293+ const interval = ReservationStartInterval . Interval_120Mins ;
294+ const reservableTimes = mockReservableTimes ( ) ;
295+ expect ( isStartTimeValid ( start , reservableTimes , interval ) ) . toBe ( false ) ;
296+ } ) ;
297+ } ) ;
298+
240299describe ( "isRangeReservable" , ( ) => {
300+ // TODO mock time
301+
241302 // one month of reservable times
242303 function mockReservableTimes ( ) : ReservableMap {
243304 const map : ReservableMap = new Map ( ) ;
@@ -282,25 +343,29 @@ describe("isRangeReservable", () => {
282343 }
283344
284345 test ( "YES for the base case" , ( ) => {
346+ const start = addHours ( startOfDay ( addDays ( new Date ( ) , 1 ) ) , 10 ) ;
285347 const input = createInput ( {
286- start : addHours ( new Date ( ) , 1 ) ,
287- end : addHours ( new Date ( ) , 2 ) ,
348+ start,
349+ end : addHours ( start , 2 ) ,
288350 } ) ;
289351 expect ( isRangeReservable ( input ) ) . toBe ( true ) ;
290352 } ) ;
291353
292354 test ( "NO for starting in the past" , ( ) => {
355+ const now = new Date ( ) ;
356+ const start = addHours ( startOfDay ( now ) , now . getHours ( ) - 1 ) ;
293357 const input = createInput ( {
294- start : addHours ( new Date ( ) , - 1 ) ,
295- end : addHours ( new Date ( ) , 2 ) ,
358+ start,
359+ end : addHours ( start , 2 ) ,
296360 } ) ;
297361 expect ( isRangeReservable ( input ) ) . toBe ( false ) ;
298362 } ) ;
299363
300364 test ( "NO if end < start" , ( ) => {
365+ const start = addHours ( startOfDay ( Date . now ( ) ) , 10 ) ;
301366 const input = createInput ( {
302- start : addHours ( new Date ( ) , 2 ) ,
303- end : addHours ( new Date ( ) , 1 ) ,
367+ start,
368+ end : addHours ( start , - 1 ) ,
304369 } ) ;
305370 expect ( isRangeReservable ( input ) ) . toBe ( false ) ;
306371 } ) ;
@@ -327,18 +392,28 @@ describe("isRangeReservable", () => {
327392 test ( "YES if the range is exactly an interval" , ( ) => {
328393 const date = startOfDay ( addDays ( new Date ( ) , 1 ) ) ;
329394 const input = createInput ( {
330- start : addHours ( date , 9 ) ,
331- end : addHours ( date , 11 ) ,
395+ start : addHours ( date , 10 ) ,
396+ end : addHours ( date , 12 ) ,
332397 interval : ReservationStartInterval . Interval_120Mins ,
333398 } ) ;
334399 expect ( isRangeReservable ( input ) ) . toBe ( true ) ;
335400 } ) ;
336401
402+ test ( "NO if the range start time doesn't match interval" , ( ) => {
403+ const date = startOfDay ( addDays ( new Date ( ) , 1 ) ) ;
404+ const input = createInput ( {
405+ start : addHours ( date , 11 ) ,
406+ end : addHours ( date , 13 ) ,
407+ interval : ReservationStartInterval . Interval_120Mins ,
408+ } ) ;
409+ expect ( isRangeReservable ( input ) ) . toBe ( false ) ;
410+ } ) ;
411+
337412 test ( "NO if the range is not divisible with interval" , ( ) => {
338413 const date = startOfDay ( addDays ( new Date ( ) , 1 ) ) ;
339414 const input = createInput ( {
340- start : addHours ( date , 9 ) ,
341- end : addHours ( date , 12 ) ,
415+ start : addHours ( date , 10 ) ,
416+ end : addHours ( date , 13 ) ,
342417 interval : ReservationStartInterval . Interval_120Mins ,
343418 } ) ;
344419 expect ( isRangeReservable ( input ) ) . toBe ( false ) ;
0 commit comments