@@ -35,6 +35,8 @@ const CSS_CLASS_DONE_BUTTON = 'igx-button--flat';
3535const CSS_CLASS_LABEL = 'igx-input-group__label' ;
3636const CSS_CLASS_OVERLAY_CONTENT = 'igx-overlay__content' ;
3737const CSS_CLASS_DATE_RANGE = 'igx-date-range-picker' ;
38+ const CSS_CLASS_CALENDAR_DATE = 'igx-calendar__date' ;
39+ const CSS_CLASS_INACTIVE_DATE = 'igx-calendar__date--inactive' ;
3840
3941describe ( 'IgxDateRangePicker' , ( ) => {
4042 describe ( 'Unit tests: ' , ( ) => {
@@ -298,16 +300,25 @@ describe('IgxDateRangePicker', () => {
298300 let calendar : DebugElement | Element ;
299301 let calendarDays : DebugElement [ ] | HTMLCollectionOf < Element > ;
300302
301- const selectDateRangeFromCalendar = ( startDateDay : number , dayRange : number ) => {
302- const startDateDayElIndex = startDateDay - 1 ;
303- const endDateDayElIndex = startDateDayElIndex + dayRange ;
303+ const selectDateRangeFromCalendar = ( sDate : Date , eDate : Date ) => {
304304 dateRange . open ( ) ;
305305 fixture . detectChanges ( ) ;
306- calendarDays = document . getElementsByClassName ( 'igx-calendar__date' ) ;
307- const activeDays = Array . from ( calendarDays ) . filter ( d => ! d . classList . contains ( 'igx-calendar__date--hidden' ) ) ;
308- UIInteractions . simulateClickAndSelectEvent ( activeDays [ startDateDayElIndex ] ) ;
309- if ( dayRange !== 0 ) {
310- UIInteractions . simulateClickAndSelectEvent ( activeDays [ endDateDayElIndex ] ) ;
306+ calendarDays = document . getElementsByClassName ( CSS_CLASS_CALENDAR_DATE ) ;
307+ const nodesArray = Array . from ( calendarDays ) ;
308+ const findNodeIndex : ( d : Date ) => number =
309+ ( d : Date ) => nodesArray
310+ . findIndex (
311+ n => n . attributes [ 'aria-label' ] . value === d . toDateString ( )
312+ && ! n . classList . contains ( CSS_CLASS_INACTIVE_DATE )
313+ ) ;
314+ const startIndex = findNodeIndex ( sDate ) ;
315+ const endIndex = findNodeIndex ( eDate ) ;
316+ if ( startIndex === - 1 ) {
317+ throw new Error ( 'Start date not found in calendar. Aborting.' ) ;
318+ }
319+ UIInteractions . simulateClickAndSelectEvent ( calendarDays [ startIndex ] ) ;
320+ if ( endIndex !== - 1 && endIndex !== startIndex ) { // do not click same date twice
321+ UIInteractions . simulateClickAndSelectEvent ( calendarDays [ endIndex ] ) ;
311322 }
312323 fixture . detectChanges ( ) ;
313324 dateRange . close ( ) ;
@@ -351,7 +362,7 @@ describe('IgxDateRangePicker', () => {
351362 expect ( singleInputElement . nativeElement . value ) . toEqual ( `${ inputStartDate } - ${ inputEndDate } ` ) ;
352363 } ;
353364
354- xdescribe ( 'Selection tests' , ( ) => {
365+ describe ( 'Selection tests' , ( ) => {
355366 it ( 'should assign range dates to the input when selecting a range from the calendar' , ( ) => {
356367 fixture . componentInstance . mode = PickerInteractionMode . DropDown ;
357368 fixture . componentInstance . dateRange . displayFormat = 'M/d/yyyy' ;
@@ -362,7 +373,7 @@ describe('IgxDateRangePicker', () => {
362373 startDate = new Date ( today . getFullYear ( ) , today . getMonth ( ) , 10 , 0 , 0 , 0 ) ;
363374 endDate = new Date ( startDate ) ;
364375 endDate . setDate ( endDate . getDate ( ) + dayRange ) ;
365- selectDateRangeFromCalendar ( startDate . getDate ( ) , dayRange ) ;
376+ selectDateRangeFromCalendar ( startDate , endDate ) ;
366377 verifyDateRangeInSingleInput ( ) ;
367378 } ) ;
368379
@@ -371,11 +382,10 @@ describe('IgxDateRangePicker', () => {
371382 fixture . componentInstance . dateRange . displayFormat = 'M/d/yyyy' ;
372383 fixture . detectChanges ( ) ;
373384
374- const dayRange = - 5 ;
375385 const today = new Date ( ) ;
376386 startDate = new Date ( today . getFullYear ( ) , today . getMonth ( ) , 5 , 0 , 0 , 0 ) ;
377387 endDate = new Date ( today . getFullYear ( ) , today . getMonth ( ) , 10 , 0 , 0 , 0 ) ;
378- selectDateRangeFromCalendar ( endDate . getDate ( ) , dayRange ) ;
388+ selectDateRangeFromCalendar ( endDate , startDate ) ;
379389 verifyDateRangeInSingleInput ( ) ;
380390 } ) ;
381391
@@ -384,11 +394,10 @@ describe('IgxDateRangePicker', () => {
384394 fixture . componentInstance . dateRange . displayFormat = 'M/d/yyyy' ;
385395 fixture . detectChanges ( ) ;
386396
387- const dayRange = 0 ;
388397 const today = new Date ( ) ;
389398 startDate = new Date ( today . getFullYear ( ) , today . getMonth ( ) , 10 , 0 , 0 , 0 ) ;
390399 endDate = startDate ;
391- selectDateRangeFromCalendar ( startDate . getDate ( ) , dayRange ) ;
400+ selectDateRangeFromCalendar ( startDate , endDate ) ;
392401 verifyDateRangeInSingleInput ( ) ;
393402 } ) ;
394403
@@ -398,9 +407,7 @@ describe('IgxDateRangePicker', () => {
398407 const today = new Date ( ) ;
399408 startDate = new Date ( today . getFullYear ( ) , today . getMonth ( ) , 1 , 0 , 0 , 0 ) ;
400409 endDate = new Date ( today . getFullYear ( ) , today . getMonth ( ) + 2 , 0 , 0 , 0 , 0 ) ;
401- const differenceMs = Math . abs ( startDate . getTime ( ) - endDate . getTime ( ) ) ;
402- const dayRange = Math . round ( differenceMs / ONE_DAY ) ;
403- selectDateRangeFromCalendar ( startDate . getDate ( ) , dayRange ) ;
410+ selectDateRangeFromCalendar ( startDate , endDate ) ;
404411 verifyDateRangeInSingleInput ( ) ;
405412 } ) ;
406413
@@ -767,7 +774,7 @@ describe('IgxDateRangePicker', () => {
767774 fixture = TestBed . createComponent ( DateRangeTwoInputsTestComponent ) ;
768775 fixture . detectChanges ( ) ;
769776 dateRange = fixture . componentInstance . dateRange ;
770- dateRange . value = { start : new Date ( '2/2/2020' ) , end : new Date ( '3/3/2020' ) } ;
777+ dateRange . value = { start : new Date ( '2/2/2020' ) , end : new Date ( '3/3/2020' ) } ;
771778 startInput = fixture . debugElement . query ( By . css ( 'input' ) ) ;
772779 endInput = fixture . debugElement . queryAll ( By . css ( 'input' ) ) [ 1 ] ;
773780 calendar = fixture . debugElement . query ( By . css ( CSS_CLASS_CALENDAR ) ) ;
@@ -782,7 +789,7 @@ describe('IgxDateRangePicker', () => {
782789 expect ( endInput . nativeElement . value ) . toEqual ( expectedEndDate ) ;
783790 } ;
784791
785- xdescribe ( 'Selection tests' , ( ) => {
792+ describe ( 'Selection tests' , ( ) => {
786793 it ( 'should assign range values correctly when selecting dates from the calendar' , ( ) => {
787794 fixture . componentInstance . mode = PickerInteractionMode . DropDown ;
788795 fixture . detectChanges ( ) ;
@@ -792,26 +799,25 @@ describe('IgxDateRangePicker', () => {
792799 startDate = new Date ( today . getFullYear ( ) , today . getMonth ( ) , 10 , 0 , 0 , 0 ) ;
793800 endDate = new Date ( startDate ) ;
794801 endDate . setDate ( endDate . getDate ( ) + dayRange ) ;
795- selectDateRangeFromCalendar ( startDate . getDate ( ) , dayRange ) ;
802+ selectDateRangeFromCalendar ( startDate , endDate ) ;
796803 verifyDateRange ( ) ;
797804
798805 dayRange = 13 ;
799806 startDate = new Date ( today . getFullYear ( ) , today . getMonth ( ) , 6 , 0 , 0 , 0 ) ;
800807 endDate = new Date ( startDate ) ;
801808 endDate . setDate ( endDate . getDate ( ) + dayRange ) ;
802- selectDateRangeFromCalendar ( startDate . getDate ( ) , dayRange ) ;
809+ selectDateRangeFromCalendar ( startDate , endDate ) ;
803810 verifyDateRange ( ) ;
804811 } ) ;
805812
806813 it ( 'should assign range values correctly when selecting dates in reversed order' , ( ) => {
807814 fixture . componentInstance . mode = PickerInteractionMode . DropDown ;
808815 fixture . detectChanges ( ) ;
809816
810- const dayRange = - 10 ;
811817 const today = new Date ( ) ;
812818 startDate = new Date ( today . getFullYear ( ) , today . getMonth ( ) , 10 , 0 , 0 , 0 ) ;
813819 endDate = new Date ( today . getFullYear ( ) , today . getMonth ( ) , 20 , 0 , 0 , 0 ) ;
814- selectDateRangeFromCalendar ( endDate . getDate ( ) , dayRange ) ;
820+ selectDateRangeFromCalendar ( endDate , startDate ) ;
815821 verifyDateRange ( ) ;
816822 } ) ;
817823
@@ -820,10 +826,10 @@ describe('IgxDateRangePicker', () => {
820826 fixture . detectChanges ( ) ;
821827
822828 const today = new Date ( ) ;
823- startDate = today ; // new Date(today.getFullYear(), today.getMonth(), 4 , 0, 0, 0);
824- endDate = today ; // startDate;
829+ startDate = new Date ( today . getFullYear ( ) , today . getMonth ( ) , today . getDate ( ) , 0 , 0 , 0 ) ;
830+ endDate = new Date ( today . getFullYear ( ) , today . getMonth ( ) , today . getDate ( ) , 0 , 0 , 0 ) ; // startDate;
825831
826- selectDateRangeFromCalendar ( today . getDate ( ) , 0 ) ;
832+ selectDateRangeFromCalendar ( startDate , endDate ) ;
827833 verifyDateRange ( ) ;
828834 } ) ;
829835
@@ -833,9 +839,7 @@ describe('IgxDateRangePicker', () => {
833839 const today = new Date ( ) ;
834840 startDate = new Date ( today . getFullYear ( ) , today . getMonth ( ) , 1 , 0 , 0 , 0 ) ;
835841 endDate = new Date ( today . getFullYear ( ) , today . getMonth ( ) + 2 , 0 , 0 , 0 , 0 ) ;
836- const differenceMs = Math . abs ( startDate . getTime ( ) - endDate . getTime ( ) ) ;
837- const dayRange = Math . round ( differenceMs / ONE_DAY ) ;
838- selectDateRangeFromCalendar ( startDate . getDate ( ) , dayRange ) ;
842+ selectDateRangeFromCalendar ( startDate , endDate ) ;
839843 verifyDateRange ( ) ;
840844 } ) ;
841845
0 commit comments