Skip to content

Commit 6bc2cae

Browse files
committed
test(calendar): refactoring calendar tests #6453
1 parent 90639ca commit 6bc2cae

File tree

3 files changed

+565
-634
lines changed

3 files changed

+565
-634
lines changed
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
import { By } from '@angular/platform-browser';
2+
3+
export class HelperTestFunctions {
4+
public static DAYS_VIEW = 'igx-days-view';
5+
public static CALENDAR = 'igx-calendar';
6+
public static ICON_CSSCLASS = '.igx-icon';
7+
public static OVERLAY_CSSCLASS = '.igx-overlay';
8+
public static MODAL_OVERLAY_CSSCLASS = 'igx-overlay__wrapper--modal';
9+
10+
public static CALENDAR_CSSCLASS = '.igx-calendar';
11+
public static CALENDAR_HEADER_CSSCLASS = '.igx-calendar__header';
12+
public static CALENDAR_HEADER_YEAR_CSSCLASS = '.igx-calendar__header-year';
13+
public static CALENDAR_HEADER_DATE_CSSCLASS = '.igx-calendar__header-date';
14+
public static WEEKSTART_LABEL_CSSCLASS = '.igx-calendar__label';
15+
public static VERTICAL_CALENDAR_CSSCLASS = '.igx-calendar--vertical';
16+
public static DAY_CSSCLASS = '.igx-calendar__date';
17+
public static CURRENT_DATE_CSSCLASS = '.igx-calendar__date--current';
18+
public static INACTIVE_DAYS_CSSCLASS = '.igx-calendar__date--inactive';
19+
public static HIDDEN_DAYS_CSSCLASS = '.igx-calendar__date--hidden';
20+
public static SELECTED_DATE_CSSCLASS = '.igx-calendar__date--selected';
21+
public static RANGE_CSSCLASS = 'igx-calendar__date--range';
22+
public static CALENDAR_ROW_CSSCLASS = '.igx-calendar__body-row';
23+
public static CALENDAR_ROW_WRAP_CSSCLASS = '.igx-calendar__body-row--wrap';
24+
public static CALENDAR_COLUMN_CSSCLASS = '.igx-calendar__body-column';
25+
public static MONTH_CSSCLASS = '.igx-calendar__month';
26+
public static CURRENT_MONTH_CSSCLASS = '.igx-calendar__month--current';
27+
public static YEAR_CSSCLASS = '.igx-calendar__year';
28+
public static CURRENT_YEAR_CSSCLASS = '.igx-calendar__year--current';
29+
30+
public static CALENDAR_PREV_BUTTON_CSSCLASS = '.igx-calendar-picker__prev';
31+
public static CALENDAR_NEXT_BUTTON_CSSCLASS = '.igx-calendar-picker__next';
32+
public static CALENDAR_DATE_CSSCLASS = '.igx-calendar-picker__date';
33+
34+
public static CALENDAR_SUBHEADERS_SELECTOR =
35+
'div:not(' + HelperTestFunctions.CALENDAR_PREV_BUTTON_CSSCLASS + '):not(' + HelperTestFunctions.CALENDAR_NEXT_BUTTON_CSSCLASS + ')';
36+
37+
public static verifyMonthsViewNumber(fixture, monthsView: number, checkCurrentDate = false) {
38+
const el = fixture.nativeElement ? fixture.nativeElement : fixture;
39+
const daysView = el.querySelectorAll(HelperTestFunctions.DAYS_VIEW);
40+
expect(daysView).toBeDefined();
41+
expect(daysView.length).toBe(monthsView);
42+
const monthPickers = HelperTestFunctions.getCalendarSubHeader(el).querySelectorAll('div');
43+
expect(monthPickers.length).toBe(monthsView + 2); // plus the navigation arrows
44+
if (checkCurrentDate) {
45+
const currentDate = el.querySelector(HelperTestFunctions.CURRENT_DATE_CSSCLASS);
46+
expect(currentDate).not.toBeNull();
47+
}
48+
}
49+
50+
public static verifyCalendarHeader(fixture, selectedDate: Date) {
51+
const daysView = fixture.nativeElement.querySelector(HelperTestFunctions.CALENDAR_HEADER_CSSCLASS);
52+
expect(daysView).not.toBeNull();
53+
const year = fixture.nativeElement.querySelector(HelperTestFunctions.CALENDAR_HEADER_YEAR_CSSCLASS);
54+
expect(year).not.toBeNull();
55+
expect(Number(year.innerText)).toEqual(selectedDate.getFullYear());
56+
const date = fixture.nativeElement.querySelector(HelperTestFunctions.CALENDAR_HEADER_DATE_CSSCLASS);
57+
expect(date).not.toBeNull();
58+
const dateParts = selectedDate.toUTCString().split(' '); // (weekday, date month year)
59+
expect(date.children[0].innerText.trim()).toEqual(dateParts[0]);
60+
expect(date.children[1].innerText.trim()).toEqual(dateParts[2] + ' ' + Number(dateParts[1]));
61+
}
62+
63+
public static verifyNoRangeSelectionCreated(fixture, monthNumber: number) {
64+
expect(HelperTestFunctions.getMonthView(fixture, monthNumber).querySelector('.igx-calendar__date--range')).toBeNull();
65+
expect(HelperTestFunctions.getMonthView(fixture, monthNumber).querySelector('.igx-calendar__date--first')).toBeNull();
66+
expect(HelperTestFunctions.getMonthView(fixture, monthNumber).querySelector('.igx-calendar__date--last')).toBeNull();
67+
}
68+
69+
public static verifyCalendarSubHeader(fixture, monthNumber: number, viewDate: Date) {
70+
const monthPickers = HelperTestFunctions.getCalendarSubHeader(fixture).querySelectorAll('div');
71+
const dateParts = viewDate.toString().split(' '); // weekday month day year
72+
expect(monthPickers[monthNumber].children[0].innerHTML.trim()).toEqual(dateParts[1]);
73+
expect(monthPickers[monthNumber].children[1].innerHTML.trim()).toEqual(dateParts[3]);
74+
}
75+
76+
public static verifyCalendarSubHeaders(fixture, viewDates: Date[]) {
77+
const dom = fixture.nativeElement ? fixture.nativeElement : fixture;
78+
const monthPickers = HelperTestFunctions.getCalendarSubHeader(dom).querySelectorAll(this.CALENDAR_SUBHEADERS_SELECTOR);
79+
expect(monthPickers.length).toEqual(viewDates.length);
80+
for (let index = 0; index < viewDates.length; index++) {
81+
const dateParts = viewDates[index].toString().split(' '); // weekday month day year
82+
expect(monthPickers[index].children[0].innerHTML.trim()).toEqual(dateParts[1]);
83+
expect(monthPickers[index].children[1].innerHTML.trim()).toEqual(dateParts[3]);
84+
}
85+
}
86+
87+
public static getHiddenDays(fixture, monthNumber: number) {
88+
const monthView = HelperTestFunctions.getMonthView(fixture, monthNumber);
89+
return monthView.querySelectorAll(HelperTestFunctions.HIDDEN_DAYS_CSSCLASS);
90+
}
91+
92+
public static getInactiveDays(fixture, monthNumber: number) {
93+
const monthView = HelperTestFunctions.getMonthView(fixture, monthNumber);
94+
return monthView.querySelectorAll(HelperTestFunctions.INACTIVE_DAYS_CSSCLASS);
95+
}
96+
97+
public static getCalendarSubHeader(fixture): HTMLElement {
98+
const element = fixture.nativeElement ? fixture.nativeElement : fixture;
99+
return element.querySelector('div.igx-calendar-picker');
100+
}
101+
102+
public static getMonthView(fixture, monthsViewNumber: number) {
103+
const domEL = fixture.nativeElement ? fixture.nativeElement : fixture;
104+
return domEL.querySelectorAll('igx-days-view')[monthsViewNumber];
105+
}
106+
107+
public static getMonthViewDates(fixture, monthsViewNumber: number) {
108+
const month = HelperTestFunctions.getMonthView(fixture, monthsViewNumber);
109+
return month.querySelectorAll(HelperTestFunctions.DAY_CSSCLASS);
110+
}
111+
112+
public static getMonthViewInactiveDates(fixture, monthsViewNumber: number) {
113+
const month = HelperTestFunctions.getMonthView(fixture, monthsViewNumber);
114+
return month.querySelectorAll(HelperTestFunctions.INACTIVE_DAYS_CSSCLASS);
115+
}
116+
117+
public static getMonthViewSelectedDates(fixture, monthsViewNumber: number) {
118+
const month = HelperTestFunctions.getMonthView(fixture, monthsViewNumber);
119+
return month.querySelectorAll(HelperTestFunctions.SELECTED_DATE_CSSCLASS +
120+
`:not(${HelperTestFunctions.HIDDEN_DAYS_CSSCLASS})`);
121+
}
122+
123+
public static getMonthsFromMonthView(fixture) {
124+
return fixture.nativeElement.querySelector('igx-months-view')
125+
.querySelectorAll('.igx-calendar__month, .igx-calendar__month--current');
126+
}
127+
128+
public static getYearsFromYearView(fixture) {
129+
return fixture.nativeElement.querySelector('igx-years-view')
130+
.querySelectorAll('.igx-calendar__year, .igx-calendar__year--current');
131+
}
132+
133+
public static getCurrentYearsFromYearView(fixture) {
134+
return fixture.nativeElement.querySelector('igx-years-view')
135+
.querySelector('.igx-calendar__year--current');
136+
}
137+
138+
public static getNexArrowElement(fixture) {
139+
return fixture.debugElement.query(By.css(HelperTestFunctions.CALENDAR_NEXT_BUTTON_CSSCLASS)).nativeElement;
140+
}
141+
142+
public static getPreviousArrowElement(fixture) {
143+
return fixture.debugElement.query(By.css(HelperTestFunctions.CALENDAR_PREV_BUTTON_CSSCLASS)).nativeElement;
144+
}
145+
}

0 commit comments

Comments
 (0)