-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheadless-datepicker.spec.ts
494 lines (408 loc) · 16.6 KB
/
headless-datepicker.spec.ts
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
import { expect, assert } from 'chai'
import { HeadlessDatepicker } from './headless-datepicker'
import * as moment from 'moment'
describe('Headless datepicker', () => {
let sut;
beforeEach(() => {
sut = new HeadlessDatepicker.Calendar()
})
describe('Initialization', () => {
it('should be initialized', () => {
expect(sut).not.to.be.null
})
it('should have default options set', () => {
expect(sut.options.locale).to.equal('en')
expect(sut.options.disabledDates).to.be.an('array')
expect(sut.options.extras).to.be.an('array')
})
it('should override default options by setting options in contructor', () => {
const dateFormat = 'DD/MM/YYYY'
const minimumDate = new Date(2017, 1, 10)
const maximumDate = new Date(2017, 1, 20)
const disabledDates = [new Date(2017, 1, 14), new Date(2017, 1, 15)]
sut = new HeadlessDatepicker.Calendar({
minimumDate: minimumDate,
maximumDate: maximumDate,
disabledDates: disabledDates,
zeroBasedMonth: true,
calendarMode: 'exact'
})
expect(sut.options.minimumDate).to.deep.equal(minimumDate)
expect(sut.options.maximumDate).to.deep.equal(maximumDate)
expect(sut.options.disabledDates).to.deep.equal(disabledDates)
expect(sut.options.zeroBasedMonth).to.be.true
expect(sut.options.calendarMode).to.equal('exact')
})
it('should support multiple objects', () => {
const date1 = new Date(2017, 3, 1)
const date2 = new Date(2017, 4, 1)
sut = new HeadlessDatepicker.Calendar({ minimumDate: date1 })
const sut2 = new HeadlessDatepicker.Calendar({ minimumDate: date2 })
expect(sut.options.minimumDate).to.deep.equal(date1)
expect(sut2.options.minimumDate).to.deep.equal(date2)
})
it('should set zeroBasedMonth to true when not supplied', () => {
expect(sut.options.zeroBasedMonth).to.be.false
})
it('should fill-mode to be default', () => {
expect(sut.options.calendarMode).to.equal('fill')
})
})
describe('Getting dates for a time span', () => {
let startDate: Date;
let endDate: Date;
beforeEach(() => {
startDate = new Date(2017, 2, 1)
endDate = new Date(2017, 3, 30)
})
it('should have correct start and end date', () => {
const dates = sut.getRange(startDate, endDate)
expect(dates).to.be.an('array')
expect(dates.length).to.equal(61)
expect(dates[0].moment.toDate()).to.deep.equal(startDate)
expect(dates[60].moment.toDate()).to.deep.equal(endDate)
})
it('should have todays date selected', () => {
let today: moment.Moment = moment()
startDate = moment().clone().subtract(10, 'days').toDate()
endDate = moment().clone().add(10, 'days').toDate()
const dates = sut.getRange(startDate, endDate)
const todayRange = dates.filter(d => d.isToday)
expect(todayRange.length).to.equal(1)
expect(todayRange[0].moment.isSame(today, 'day')).to.be.true
})
describe('Selected dates', () => {
it('should have selected dates marked', () => {
const testDates = [new Date(2017, 2, 2), new Date(2017, 2, 31), new Date(2017, 3, 29)]
sut.selectedDates = testDates
const dates = sut.getRange(startDate, endDate)
expect(dates[1].isSelected).to.be.true
expect(dates[30].isSelected).to.be.true
expect(dates[59].isSelected).to.be.true
expect(dates.filter((d) => d.isSelected == false).length).to.equal(58)
})
})
describe('Minimum date', () => {
const minimumDate = new Date(2017, 2, 10)
let datepickerResult;
beforeEach(() => {
sut = new HeadlessDatepicker.Calendar({
minimumDate: minimumDate
})
datepickerResult = sut.getRange(startDate, endDate)
})
it('should have minimum date and above marked as active', () => {
datepickerResult.slice(9).forEach((item) => {
assert.isFalse(item.isBelowMinimumDate, `Expected isBelowMinimumDate to be false for date ${item.date}`)
assert.isTrue(item.isActive, `Expected active to be true for date ${item.date}`)
})
})
it('should have dates below minimum marked as inactive', () => {
datepickerResult.slice(0, 9).forEach((item) => {
assert.isTrue(item.isBelowMinimumDate, `Expected isBelowMinimumDate to be true for date ${item.date}`)
assert.isFalse(item.isActive, `Expected active to be false for date ${item.date}`)
})
})
})
describe('Maximum date', () => {
const maximumDate = new Date(2017, 2, 10)
let datepickerResult;
beforeEach(() => {
sut = new HeadlessDatepicker.Calendar({
maximumDate: maximumDate
})
datepickerResult = sut.getRange(startDate, endDate)
})
it('should have maximum date and dates below marked as active', () => {
datepickerResult.slice(0, 10).forEach((item) => {
assert.isFalse(item.isAboveMaximumDate, `Expected isAboveMaximumDate to be false for date ${item.date}`)
assert.isTrue(item.isActive, `Expected active to be true for date ${item.date}`)
})
})
it('should have dates above maximum marked marked as inactive', () => {
datepickerResult.slice(10).forEach((item) => {
assert.isTrue(item.isAboveMaximumDate, `Expected isAboveMaximumDate to be true for date ${item.date}`)
assert.isFalse(item.isActive, `Expected active to be false for date ${item.date}`)
})
})
})
it('should have disabled dates marked as inactive', () => {
const disabledDates = [new Date(2017, 2, 5), new Date(2017, 2, 25), new Date(2017, 3, 15)]
sut = new HeadlessDatepicker.Calendar({
disabledDates: disabledDates
})
const dates = sut.getRange(startDate, endDate)
expect(dates[4].moment.toDate()).to.deep.equal(disabledDates[0])
expect(dates[24].moment.toDate()).to.deep.equal(disabledDates[1])
expect(dates[45].moment.toDate()).to.deep.equal(disabledDates[2])
dates.forEach((item, i) => {
if (i == 4 || i == 24 || i == 45) {
expect(item.isDisabled).to.true
expect(item.isActive).to.false
return
}
assert.isFalse(item.isDisabled, `Expected isDisabled to be false for date ${item.date}. Index (${i})`)
assert.isTrue(item.isActive, `Expected isActive to be true for date ${item.date}. Index (${i})`)
})
})
it('should return the additional information that was provided for specific dates', () => {
const data1 = { holiday: 'Easter' }
const data2 = 'The coolest thing'
const data3 = [1, 2, 3]
sut = new HeadlessDatepicker.Calendar({
extras: [
{ date: new Date(2017, 2, 10), data: data1 },
{ date: new Date(2017, 2, 20), data: data2 },
{ date: new Date(2017, 2, 30), data: data3 }
]
})
const dates = sut.getRange(startDate, endDate)
expect(dates[8].extras).to.be.null
expect(dates[9].extras).to.deep.equal(data1)
expect(dates[19].extras).to.equal(data2)
expect(dates[29].extras).to.deep.equal(data3)
})
})
describe('Getting dates in grid by months', () => {
let yearMonthPair: HeadlessDatepicker.YearMonthPair
let calendar;
beforeEach(() => {
yearMonthPair = {
year: 2017,
month: 4
}
})
describe('"exact"-mode', function () {
beforeEach(() => {
sut = new HeadlessDatepicker.Calendar({ calendarMode: 'exact' })
calendar = sut.getMonth(yearMonthPair)
})
it('should return the calendar as an object with dates divided into weeks', () => {
expect(calendar.weeks.length).to.equal(6)
})
it('should return correct week day names in correct order', () => {
expect(calendar.weekDayName.full).to.deep.equal(['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'])
expect(calendar.weekDayName.short).to.deep.equal(['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'])
expect(calendar.weekDayName.min).to.deep.equal(['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'])
})
it('should respect zero based months', () => {
sut = new HeadlessDatepicker.Calendar({ zeroBasedMonth: true, calendarMode: 'exact' })
calendar = sut.getMonth(yearMonthPair)
expect(calendar.number).to.equal(4)
expect(calendar.weeks[0].dates[0].moment.format('MMMM')).to.equal('May')
expect(calendar.monthName.full).to.equal('May')
expect(calendar.monthName.short).to.equal('May')
})
it('should respect one based months', () => {
expect(calendar.number).to.equal(4)
expect(calendar.weeks[0].dates[0].moment.format('MMMM')).to.equal('April')
expect(calendar.monthName.full).to.equal('April')
expect(calendar.monthName.short).to.equal('Apr')
})
it('should return correct number of days for each week', function () {
expect(calendar.weeks[0].dates.length).to.equal(1)
expect(calendar.weeks[1].dates.length).to.equal(7)
expect(calendar.weeks[2].dates.length).to.equal(7)
expect(calendar.weeks[3].dates.length).to.equal(7)
expect(calendar.weeks[4].dates.length).to.equal(7)
expect(calendar.weeks[5].dates.length).to.equal(1)
})
it('should return correct week of year', () => {
expect(calendar.weeks[0].weekOfYear).to.equal(13)
expect(calendar.weeks[4].weekOfYear).to.equal(17)
})
it('should return correct week of month', () => {
expect(calendar.weeks[0].weekOfMonth).to.equal(1)
expect(calendar.weeks[4].weekOfMonth).to.equal(4)
})
})
describe('"fill"-mode', function () {
beforeEach(() => {
sut = new HeadlessDatepicker.Calendar({ calendarMode: 'fill' })
calendar = sut.getMonth(yearMonthPair)
})
it('should return 7 days in all weeks', () => {
calendar.weeks.forEach(week => {
assert.isTrue(week.dates.length == 7, `Expected week to have 7 days: ${week}`)
})
})
it('should not show adjacent months', () => {
const firstWeekDays = calendar.weeks[0].dates.slice(0, 6)
const lastWeekDays = calendar.weeks[5].dates.slice(1)
expect(firstWeekDays).to.deep.equal([null, null, null, null, null, null])
expect(lastWeekDays).to.deep.equal([null, null, null, null, null, null])
})
})
describe('"adjacent"-mode', function () {
beforeEach(() => {
sut = new HeadlessDatepicker.Calendar({ calendarMode: 'adjacent' })
calendar = sut.getMonth(yearMonthPair)
})
it('should show previous adjacent month', () => {
const days = calendar.weeks[0].dates.slice(0, 6).map(week => week.moment.format('YYYY-MM-DD'))
expect(calendar.weeks[0].dates[6].moment.format('YYYY-MM-DD')).to.equal('2017-04-01')
expect(days).to.deep.equal(['2017-03-26', '2017-03-27', '2017-03-28', '2017-03-29', '2017-03-30', '2017-03-31'])
})
it('should show next adjacent month', () => {
const days = calendar.weeks[5].dates.slice(1).map(week => week.moment.format('YYYY-MM-DD'))
expect(calendar.weeks[5].dates[0].moment.format('YYYY-MM-DD')).to.equal('2017-04-30')
expect(days).to.deep.equal(['2017-05-01', '2017-05-02', '2017-05-03', '2017-05-04', '2017-05-05', '2017-05-06'])
})
})
describe('"fixed"-mode', function () {
beforeEach(() => {
sut = new HeadlessDatepicker.Calendar({ calendarMode: 'fixed' })
calendar = sut.getMonth(yearMonthPair)
})
it('should show 6 weeks for month with 5 weeks', () => {
yearMonthPair.month = 3
calendar = sut.getMonth(yearMonthPair)
expect(calendar.weeks.slice(-1)[0].dates.map(item => item.moment.format('YYYY-MM-DD'))).to.deep.equal(['2017-04-02', '2017-04-03', '2017-04-04', '2017-04-05', '2017-04-06', '2017-04-07', '2017-04-08'])
})
it('should show 6 weeks for month with 6 weeks', () => {
expect(calendar.weeks.slice(-1)[0].dates.map(item => item.moment.format('YYYY-MM-DD'))).to.deep.equal(['2017-04-30', '2017-05-01', '2017-05-02', '2017-05-03', '2017-05-04', '2017-05-05', '2017-05-06'])
})
})
})
describe('Multiple grids', () => {
let months;
let calendars;
beforeEach(() => {
months = [{ year: 2017, month: 1 }, { year: 2017, month: 2 }, { year: 2017, month: 3 }, { year: 2018, month: 3 }]
calendars = sut.getMonths(months)
})
it('should support multiple months', () => {
expect(calendars.length).to.equal(4)
})
it('should return the correct number of days for all month. Including leap year', () => {
months = [
{ year: 2016, month: 2 },
{ year: 2017, month: 1 },
{ year: 2017, month: 2 },
{ year: 2017, month: 3 },
{ year: 2017, month: 4 },
{ year: 2017, month: 5 },
{ year: 2017, month: 6 },
{ year: 2017, month: 7 },
{ year: 2017, month: 8 },
{ year: 2017, month: 9 },
{ year: 2017, month: 10 },
{ year: 2017, month: 11 },
{ year: 2017, month: 12 }
]
sut = new HeadlessDatepicker.Calendar({ calendarMode: 'exact', zeroBasedMonth: false })
calendars = sut.getMonths(months)
expect(calendars[0].weeks.reduce(reduceFn).length).to.equal(29)
expect(calendars[1].weeks.reduce(reduceFn).length).to.equal(31)
expect(calendars[2].weeks.reduce(reduceFn).length).to.equal(28)
expect(calendars[3].weeks.reduce(reduceFn).length).to.equal(31)
expect(calendars[4].weeks.reduce(reduceFn).length).to.equal(30)
expect(calendars[5].weeks.reduce(reduceFn).length).to.equal(31)
expect(calendars[6].weeks.reduce(reduceFn).length).to.equal(30)
expect(calendars[7].weeks.reduce(reduceFn).length).to.equal(31)
expect(calendars[8].weeks.reduce(reduceFn).length).to.equal(31)
expect(calendars[9].weeks.reduce(reduceFn).length).to.equal(30)
expect(calendars[10].weeks.reduce(reduceFn).length).to.equal(31)
expect(calendars[11].weeks.reduce(reduceFn).length).to.equal(30)
expect(calendars[12].weeks.reduce(reduceFn).length).to.equal(31)
})
})
describe('Localization', () => {
it('should be able to set locale for moment', () => {
let dates;
dates = sut.getRange(new Date(), new Date())
expect(dates[0].moment.fromNow()).to.equal('a few seconds ago')
sut = new HeadlessDatepicker.Calendar({ locale: 'da', localeSettings: da })
dates = sut.getRange(new Date(), new Date())
expect(dates[0].moment.fromNow()).to.equal('få sekunder siden')
})
it('should respect first day in week, based on locale', () => {
let dates;
let calendar;
let pair = { year: 2017, month: 3 }
dates = sut.getRange(new Date(2017, 3, 1), new Date(2017, 3, 3))
calendar = sut.getMonth(pair)
expect(dates[0].moment.weekday()).to.equal(6)
expect(dates[1].moment.weekday()).to.equal(0)
expect(dates[2].moment.weekday()).to.equal(1)
expect(calendar.weekDayName.min).to.deep.equal(['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'])
sut = new HeadlessDatepicker.Calendar({ locale: 'da', localeSettings: da })
dates = sut.getRange(new Date(2017, 3, 1), new Date(2017, 3, 3))
calendar = sut.getMonth(pair)
expect(dates[0].moment.weekday()).to.equal(5)
expect(dates[1].moment.weekday()).to.equal(6)
expect(dates[2].moment.weekday()).to.equal(0)
expect(calendar.weekDayName.min).to.deep.equal(['ma', 'ti', 'on', 'to', 'fr', 'lø', 'sø'])
})
})
describe('Bug fixes', () => {
it('should return calendar for December 2018', () => {
sut = new HeadlessDatepicker.Calendar({ calendarMode: 'exact' })
const calendar = sut.getMonth({ year: 2018, month: 12 })
expect(calendar).not.to.be.null
expect(calendar.number).to.equal(12)
expect(calendar.weeks[0].weekOfYear).to.equal(48)
expect(calendar.weeks[0].weekOfMonth).to.equal(1)
expect(calendar.weeks.reduce(reduceFn).length).to.equal(31)
})
it('should return calendar for january in any locale when first day of year belongs to the last week in the previous year', () => {
sut = new HeadlessDatepicker.Calendar(
{
locale: 'de',
calendarMode: 'exact',
zeroBasedMonth: true,
})
const calendar = sut.getMonth({ year: 2017, month: 0 })
expect(calendar).not.to.be.null
expect(calendar.number).to.equal(0)
expect(calendar.weeks[0].weekOfYear).to.equal(52)
expect(calendar.weeks[0].weekOfMonth).to.equal(1)
expect(calendar.weeks.reduce(reduceFn).length).to.equal(31)
})
})
})
const reduceFn = (a, b) => a.dates? a.dates.concat(b.dates) : a.concat(b.dates)
const da = {
months: "januar_februar_marts_april_maj_juni_juli_august_september_oktober_november_december".split("_"),
monthsShort: "jan_feb_mar_apr_maj_jun_jul_aug_sep_okt_nov_dec".split("_"),
weekdays: "søndag_mandag_tirsdag_onsdag_torsdag_fredag_lørdag".split("_"),
weekdaysShort: "søn_man_tir_ons_tor_fre_lør".split("_"),
weekdaysMin: "sø_ma_ti_on_to_fr_lø".split("_"),
longDateFormat: {
LT: "HH:mm",
LTS: "HH:mm:ss",
L: "DD/MM/YYYY",
LL: "D. MMMM YYYY",
LLL: "D. MMMM YYYY HH:mm",
LLLL: "dddd [d.] D. MMMM YYYY [kl.] HH:mm"
},
calendar: {
sameDay: "[i dag kl.] LT",
nextDay: "[i morgen kl.] LT",
nextWeek: "på dddd [kl.] LT",
lastDay: "[i går kl.] LT",
lastWeek: "[i] dddd[s kl.] LT",
sameElse: "L"
},
relativeTime: {
future: "om %s",
past: "%s siden",
s: "få sekunder",
m: "et minut",
mm: "%d minutter",
h: "en time",
hh: "%d timer",
d: "en dag",
dd: "%d dage",
M: "en måned",
MM: "%d måneder",
y: "et år",
yy: "%d år"
},
dayOfMonthOrdinalParse: /\d{1,2}\./,
ordinal: "%d.",
week: {
dow: 1,
doy: 4
}
}