forked from react-dates/react-dates
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetCalendarMonthWeeks_spec.js
160 lines (134 loc) · 6.12 KB
/
getCalendarMonthWeeks_spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import moment from 'moment';
import { expect } from 'chai';
import isSameDay from '../../src/utils/isSameDay';
import getCalendarMonthWeeks from '../../src/utils/getCalendarMonthWeeks';
const today = moment();
const weeks = getCalendarMonthWeeks(today);
const weeksWithOutsideDays = getCalendarMonthWeeks(today, true);
describe('getCalendarMonthWeeks', () => {
it('returns an array of arrays', () => {
expect(weeks).to.be.instanceof(Array);
weeks.forEach((week) => {
expect(week).to.be.instanceof(Array);
});
});
it('today is included', () => {
let isIncluded = false;
weeks.forEach((week) => {
week.forEach((day) => {
if (day && day.isSame(today, 'day')) isIncluded = true;
});
});
expect(isIncluded).to.equal(true);
});
it('all days have a time of 12PM', () => {
weeks.forEach((week) => {
week.forEach((day) => {
if (day) {
expect(day.hours()).to.equal(12);
}
});
});
});
describe('padding when enableOutsideDays is false', () => {
let weeksWithPadding;
before(() => {
// using specific month Feb 2017 to manually compare with calendar
weeksWithPadding = getCalendarMonthWeeks(moment('2017-02-01'), false);
});
it('null pads leading days', () => {
const firstWeek = weeksWithPadding[0];
expect(firstWeek[0]).to.equal(null); // Sun Jan 29
expect(firstWeek[1]).to.equal(null); // Mon Jan 30
expect(firstWeek[2]).to.equal(null); // Tue Jan 31
expect(firstWeek[3]).to.not.equal(null); // Wed Feb 1
});
it('null pads trailing days', () => {
const lastWeek = weeksWithPadding[weeksWithPadding.length - 1];
expect(lastWeek[2]).to.not.equal(null); // Tue Feb 28
expect(lastWeek[3]).to.equal(null); // Wed Mar 1
expect(lastWeek[4]).to.equal(null); // Thu Mar 2
expect(lastWeek[5]).to.equal(null); // Fri Mar 3
expect(lastWeek[6]).to.equal(null); // Sat Mar 4
});
});
describe('Daylight Savings Time issues', () => {
it('last of February does not equal first of March', () => {
const february = getCalendarMonthWeeks(today.clone().month(1));
const lastWeekOfFebruary = february[february.length - 1].filter(Boolean);
const lastOfFebruary = lastWeekOfFebruary[lastWeekOfFebruary.length - 1];
const march = getCalendarMonthWeeks(today.clone().month(2));
const firstOfMarch = march[0].filter(Boolean)[0];
expect(isSameDay(lastOfFebruary, firstOfMarch)).to.equal(false);
});
it('last of March does not equal first of April', () => {
const march = getCalendarMonthWeeks(today.clone().month(2));
const lastWeekOfMarch = march[march.length - 1].filter(Boolean);
const lastOfMarch = lastWeekOfMarch[lastWeekOfMarch.length - 1];
const april = getCalendarMonthWeeks(today.clone().month(3));
const firstOfApril = april[0].filter(Boolean)[0];
expect(isSameDay(lastOfMarch, firstOfApril)).to.equal(false);
});
});
describe('enableOutsideDays arg is false', () => {
it('first non-null element is first of the month', () => {
const firstOfMonth = today.clone().startOf('month');
const firstNonNullDay = weeks[0].filter(day => day)[0];
expect(firstOfMonth.isSame(firstNonNullDay, 'day')).to.equal(true);
});
it('last non-null element is last of the month', () => {
const lastOfMonth = today.clone().endOf('month');
const lastWeek = weeks[weeks.length - 1].filter(day => day);
const lastNonNullDay = lastWeek[lastWeek.length - 1];
expect(lastOfMonth.isSame(lastNonNullDay, 'day')).to.equal(true);
});
it('number of non-null elements is equal to number of days in month', () => {
const daysInCalendarMonthWeeks = weeks.reduce((a, b) => a + b.filter(day => day).length, 0);
expect(daysInCalendarMonthWeeks).to.equal(today.daysInMonth());
});
});
describe('enableOutsideDays arg is true', () => {
it('contains first of the month', () => {
const firstOfMonth = today.clone().startOf('month');
const containsFirstOfMonth =
weeksWithOutsideDays[0].filter(day => firstOfMonth.isSame(day, 'day')).length > 0;
expect(containsFirstOfMonth).to.equal(true);
});
it('last week contains last of the month', () => {
const lastOfMonth = today.clone().endOf('month');
const containsLastOfMonth = weeks[weeksWithOutsideDays.length - 1]
.filter(day => lastOfMonth.isSame(day, 'day')).length > 0;
expect(containsLastOfMonth).to.equal(true);
});
it('last week contains last of the month if next month begins on Sunday', () => {
const december2016 = moment('2016-12-01');
const lastOfMonth = december2016.clone().endOf('month');
const weeksInDecember = getCalendarMonthWeeks(december2016);
const containsLastOfMonth = weeksInDecember[weeksInDecember.length - 1]
.filter(day => lastOfMonth.isSame(day, 'day')).length > 0;
expect(containsLastOfMonth).to.equal(true);
});
it('last week contains last of the month if next month begins on Monday', () => {
moment.locale('es');
const april2017 = moment('2017-04-01');
const lastOfMonth = april2017.clone().endOf('month');
const weeksInApril = getCalendarMonthWeeks(april2017);
const containsLastOfMonth = weeksInApril[weeksInApril.length - 1]
.filter(day => lastOfMonth.isSame(day, 'day')).length > 0;
expect(containsLastOfMonth).to.equal(true);
});
it('last week contains last of the month if next month begins on Saturday', () => {
const september2016 = moment('2016-09-01');
const lastOfMonth = september2016.clone().endOf('month');
const weeksInSeptember = getCalendarMonthWeeks(september2016);
const containsLastOfMonth = weeksInSeptember[weeksInSeptember.length - 1]
.filter(day => lastOfMonth.isSame(day, 'day')).length > 0;
expect(containsLastOfMonth).to.equal(true);
});
it('each week has 7 non-null elements', () => {
const hasNoNullElements = weeksWithOutsideDays
.reduce((w1, w2) => w1 && w2.reduce((d1, d2) => d1 && !!d2, true), true);
expect(hasNoNullElements).to.equal(true);
});
});
});