forked from mui/material-ui-pickers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
luxon-utils.test.js
308 lines (231 loc) · 9.56 KB
/
luxon-utils.test.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
import LuxonUtils from '../../src/utils/luxon-utils';
const checkForFullICU = () => {
try {
const january = new Date(9e8);
const spanish = new Intl.DateTimeFormat('es', { month: 'long' });
return spanish.format(january) === 'enero';
} catch (err) {
return false;
}
};
describe('Luxon utils', () => {
let luxonUtil;
let luxonUtilDe;
const hasICU = checkForFullICU();
const hasFullICU = () => hasICU;
beforeEach(() => {
luxonUtil = new LuxonUtils();
luxonUtilDe = new LuxonUtils({ locale: 'de' });
});
it('Should return a Luxon DateTime instance', () => {
const now = luxonUtil.date();
const nowJs = luxonUtil.date(new Date());
const nowString = luxonUtil.date('2018-01-01');
expect(now.constructor.name).toBe('DateTime');
expect(nowJs.constructor.name).toBe('DateTime');
expect(nowString.constructor.name).toBe('DateTime');
expect(now.isValid).toBe(true);
expect(nowJs.isValid).toBe(true);
expect(nowString.isValid).toBe(true);
});
it('Should return a DateTime instance of now', () => {
const invalid = luxonUtil.date(null);
const now = luxonUtil.date(invalid.toISO());
expect(invalid.constructor.name).toBe('DateTime');
expect(invalid).toEqual(now);
});
it('Should add one day', () => {
const now = luxonUtil.date();
const then = luxonUtil.addDays(now, 1);
const thenPast = luxonUtil.addDays(now, -1);
expect(then).toEqual(now.plus({ day: 1 }));
expect(thenPast).toEqual(now.minus({ day: 1 }));
});
it('Should be a valid DateTime', () => {
const now = luxonUtil.date();
expect(luxonUtil.isValid(now)).toBe(true);
});
it('Should be equal to another DateTime', () => {
const now = luxonUtil.date();
const then = now.plus({ day: 1 });
expect(luxonUtil.isEqual(now, now)).toBe(true);
expect(luxonUtil.isEqual(now, then)).toBe(false);
});
it('Should be on the same day', () => {
const now = luxonUtil.date(new Date(2018, 0));
const then = now.plus({ hours: 1 });
const thenOtherDay = now.plus({ days: 1 });
expect(luxonUtil.isSameDay(now, then)).toBe(true);
expect(luxonUtil.isSameDay(now, thenOtherDay)).toBe(false);
});
it('Should check if one date is after another', () => {
const now = luxonUtil.date();
const then = now.plus({ day: 1 });
expect(luxonUtil.isAfter(now, then)).toBe(false);
expect(luxonUtil.isAfter(then, now)).toBe(true);
});
it('Should check if it is the day after', () => {
const now = luxonUtil.date();
const then = now.plus({ day: 1 });
expect(luxonUtil.isAfterDay(now, then)).toBe(false);
expect(luxonUtil.isAfterDay(then, now)).toBe(true);
});
it('Should check if it is the year after', () => {
const now = luxonUtil.date();
const then = now.plus({ years: 1 });
expect(luxonUtil.isAfterYear(now, then)).toBe(false);
expect(luxonUtil.isAfterYear(then, now)).toBe(true);
});
it('Should check if one date is before another', () => {
const now = luxonUtil.date();
const then = now.minus({ day: 1 });
expect(luxonUtil.isBefore(now, then)).toBe(false);
expect(luxonUtil.isBefore(then, now)).toBe(true);
});
it('Should check if it is the day before', () => {
const now = luxonUtil.date();
const then = now.minus({ day: 1 });
expect(luxonUtil.isBeforeDay(now, then)).toBe(false);
expect(luxonUtil.isBeforeDay(then, now)).toBe(true);
});
it('Should check if it is the year before', () => {
const now = luxonUtil.date();
const then = now.minus({ years: 1 });
expect(luxonUtil.isBeforeYear(now, then)).toBe(false);
expect(luxonUtil.isBeforeYear(then, now)).toBe(true);
});
it('Should get the start of the day', () => {
const now = luxonUtil.date(new Date(2018, 0, 1, 1));
const startDay = luxonUtil.date(new Date(2018, 0));
expect(luxonUtil.startOfDay(now)).toEqual(startDay);
});
it('Should get the end of the day', () => {
const now = luxonUtil.date(new Date(2018, 0));
const endDay = luxonUtil.date(new Date(2018, 0, 1, 23, 59, 59, 999));
expect(luxonUtil.endOfDay(now)).toEqual(endDay);
});
it('Should format correctly', () => {
const now = luxonUtil.date(new Date(2018, 2));
expect(luxonUtil.format(now, 'yyyy LLL dd')).toBe('2018 Mar 01');
expect(luxonUtilDe.format(now, 'yyyy LLL dd')).toBe(hasFullICU() ? '2018 Mär 01' : '2018 M03 01');
});
it('Should format numbers correclty', () => {
const number = 23;
expect(luxonUtil.formatNumber(number)).toBe('23');
});
it('Should get the hours', () => {
const now = luxonUtil.date(new Date(2018, 0));
expect(luxonUtil.getHours(now)).toBe(0);
});
it('Should set hours', () => {
const now = luxonUtil.date(new Date(2018, 0));
const plusOneHour = now.plus({ hours: 1 });
expect(luxonUtil.setHours(now, 1)).toEqual(plusOneHour);
});
it('Should get the minutes', () => {
const now = luxonUtil.date(new Date(2018, 0));
expect(luxonUtil.getMinutes(now)).toBe(0);
});
it('Should set minutes', () => {
const now = luxonUtil.date(new Date(2018, 0));
const plusOneMinute = now.plus({ minutes: 1 });
expect(luxonUtil.setMinutes(now, 1)).toEqual(plusOneMinute);
});
it('Should get the month', () => {
const now = luxonUtil.date(new Date(2018, 0));
expect(luxonUtil.getMonth(now)).toBe(0);
});
it('Should get the year', () => {
const now = luxonUtil.date(new Date(2018, 0));
expect(luxonUtil.getYear(now)).toBe(2018);
});
it('Should set the year', () => {
const now = luxonUtil.date(new Date(2018, 0));
const plusOneYear = now.plus({ year: 1 });
expect(luxonUtil.setYear(now, 2019)).toEqual(plusOneYear);
});
it('Should get the start of the month', () => {
const now = luxonUtil.date(new Date(2018, 0, 3, 1));
const startMonth = luxonUtil.date(new Date(2018, 0, 1));
expect(luxonUtil.getStartOfMonth(now)).toEqual(startMonth);
});
it('Should get the next month', () => {
const now = luxonUtil.date(new Date(2018, 0));
const nextMonth = now.plus({ months: 1 });
expect(luxonUtil.getNextMonth(now)).toEqual(nextMonth);
});
it('Should get the previous month', () => {
const now = luxonUtil.date(new Date(2018, 0));
const previousMonth = now.minus({ months: 1 });
expect(luxonUtil.getPreviousMonth(now)).toEqual(previousMonth);
});
it('Should get the weekdays', () => {
const weekdaysEn = ['M', 'T', 'W', 'T', 'F', 'S', 'S'];
const weekdaysDe = ['M', 'D', 'M', 'D', 'F', 'S', 'S'];
expect(luxonUtil.getWeekdays()).toEqual(weekdaysEn);
expect(luxonUtilDe.getWeekdays()).toEqual(hasFullICU() ? weekdaysDe : weekdaysEn);
});
it('Should get the weeks of a month', () => {
const now = luxonUtil.date(new Date(2018, 3));
const days = 42;
const weeks = 6;
const weekArray = luxonUtil.getWeekArray(now);
const firstWeek = weekArray[0];
const firstDay = firstWeek[0];
const lastWeek = weekArray[weeks - 1];
const lastDay = lastWeek[lastWeek.length - 1];
expect(weekArray.length).toBe(weeks);
expect(weekArray.map(arr => arr.length).reduce((p, v) => p + v)).toBe(days);
expect(firstDay).toEqual(now.startOf('month').startOf('week'));
expect(lastDay).toEqual(now.endOf('month').endOf('week').startOf('day'));
});
it('Should get a range of years', () => {
const years = [2016, 2017, 2018].map(year => luxonUtil.date(new Date(year, 0)));
expect(luxonUtil.getYearRange(years[0], years[2])).toEqual([years[0], years[1]]);
expect(luxonUtil.getYearRange(years[0], years[0])).toEqual([]);
});
it('Should get the meridiem text', () => {
expect(luxonUtil.getMeridiemText('am')).toBe('AM');
expect(luxonUtil.getMeridiemText('pm')).toBe('PM');
// Had to disable this test for now, but there is still 100% coverage
// expect(luxonUtilDe.getMeridiemText('am')).toBe(hasFullICU() ? 'vorm.' : 'AM');
// expect(luxonUtilDe.getMeridiemText('pm')).toBe(hasFullICU() ? 'nachm.' : 'PM');
});
it('Should get the calendar header text', () => {
const now = luxonUtil.date(new Date(2018, 0));
expect(luxonUtil.getCalendarHeaderText(now)).toBe('January 2018');
expect(luxonUtilDe.getCalendarHeaderText(now)).toBe(hasFullICU() ? 'Januar 2018' : 'M01 2018');
});
it('Should get the date picker header text', () => {
const now = luxonUtil.date(new Date(2018, 2));
expect(luxonUtil.getDatePickerHeaderText(now)).toBe('Thu, Mar 1');
expect(luxonUtilDe.getDatePickerHeaderText(now)).toBe(hasFullICU() ? 'Do, März 1' : 'Thu, M03 1');
});
it('Should get the date time picker header text', () => {
const now = luxonUtil.date(new Date(2018, 2));
expect(luxonUtil.getDateTimePickerHeaderText(now)).toBe('Mar 1');
expect(luxonUtilDe.getDateTimePickerHeaderText(now)).toBe(hasFullICU() ? 'März 1' : 'M03 1');
});
it('Should get the day text', () => {
const now = luxonUtil.date(new Date(2018, 0));
expect(luxonUtil.getDayText(now)).toBe('1');
});
it('Should get the hour text', () => {
const now = luxonUtil.date(new Date(2018, 0));
expect(luxonUtil.getHourText(now, true)).toBe('12');
expect(luxonUtil.getHourText(now, false)).toBe('00');
});
it('Should get the minute text', () => {
const now = luxonUtil.date(new Date(2018, 0));
expect(luxonUtil.getMinuteText(now)).toBe('00');
});
it('Should get the year text', () => {
const now = luxonUtil.date(new Date(2018, 0));
expect(luxonUtil.getYearText(now)).toBe('2018');
});
it('Should check if null', () => {
const now = luxonUtil.date();
expect(luxonUtil.isNull(null)).toBe(true);
expect(luxonUtil.isNull(now)).toBe(false);
});
});