forked from eko3alpha/hollydates
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
495 lines (367 loc) · 11.2 KB
/
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
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
495
var h = HollyDates();
var module = QUnit.module;
var test = QUnit.test;
module('getMonthAWeekFromDate', function () {
test('check month of following week', function (assert) {
assert.equal(
h._.getMonthAWeekFromDate(new Date('12/31/2020')),
h.JAN,
'a week after Dec 31 2020 is Jan'
);
assert.equal(
h._.getMonthAWeekFromDate(new Date('12/24/2020')),
h.DEC,
'a week after Dec 24 2020 is still Dec'
);
});
});
module('isDay', function () {
test('sunday in month', function (assert) {
assert.true(
h._.isDay(new Date('7/1/2018'), h.FIRST, h.SUN, h.JUL),
'7/1/2018 first instance on first day of the month'
);
assert.true(
h._.isDay(new Date('1/7/2018'), h.FIRST, h.SUN, h.JAN),
'1/7/2018 first instance on second week'
);
assert.true(
h._.isDay(new Date('12/31/2017'), h.LAST, h.SUN, h.DEC),
'12/31/2017 last instance on last day of the month'
);
});
test('friday in month', function (assert) {
assert.true(
h._.isDay(new Date('12/1/2017'), h.FIRST, h.FRI, h.DEC),
'12/1/2017 first instance falls on first of the month'
);
assert.true(
h._.isDay(new Date('9/7/2018'), h.FIRST, h.FRI, h.SEP),
'9/7/2018 first instance on second week'
);
assert.true(
h._.isDay(new Date('11/30/2018'), h.LAST, h.FRI, h.NOV),
'11/30/2018 last instance on last day of the month'
);
});
});
module('getRegisteredOccurrence', function () {
test('Martin Luther King, Jr. Day', function (assert) {
var HOLIDAY = 'Martin Luther King, Jr. Day';
assert.equal(
h._.getRegisteredOccurrence(new Date('1/18/2021')),
HOLIDAY,
'1/18/2021'
);
assert.equal(
h._.getRegisteredOccurrence(new Date('1/15/2103')),
HOLIDAY,
'1/15/2103'
);
assert.equal(
h._.getRegisteredOccurrence(new Date('1/20/1986')),
HOLIDAY,
'1/20/1986'
);
});
test('Thanksgiving Day', function (assert) {
var HOLIDAY = 'Thanksgiving Day';
assert.equal(
h._.getRegisteredOccurrence(new Date('11/26/2015')),
HOLIDAY,
'11/26/2015'
);
assert.equal(
h._.getRegisteredOccurrence(new Date('11/30/2023')),
HOLIDAY,
'11/30/2023'
);
assert.equal(
h._.getRegisteredOccurrence(new Date('11/27/2025')),
HOLIDAY,
'11/27/2025'
);
});
});
module('getRegisteredDate', function () {
test('custom dates', function (assert) {
var HOLIDAY = 'Christmas';
assert.equal(
h._.getRegisteredDate(new Date('12/25/2021')),
HOLIDAY,
'1/25/2021 is Christmas'
);
var HOLIDAY = "New Year's Day";
assert.equal(
h._.getRegisteredDate(new Date('1/1/2021')),
HOLIDAY,
"1/1/2021 is New Year's Day"
);
});
});
module('isHoliday', function () {
test('closed for holidays', function (assert) {
assert.true(
h.isHoliday(new Date('9/7/2020')),
'9/7/2020 Labor Day'
);
assert.true(
h.isHoliday(new Date('1/16/2023')),
'1/16/2023 MLK'
);
});
test('closed for dates', function (assert) {
assert.true(
h.isHoliday(new Date('1/1/2020')),
"1/1/2020 New Year's Day"
);
assert.true(
h.isHoliday(new Date('12/25/2023')),
'12/25/2023 Christmas'
);
});
});
module('isDate', function () {
test('8/16/1979', function (assert) {
assert.true(
h._.isDate(new Date('8/16/1979'), h.AUG, 16),
'8/16/1979'
);
});
test('8/17/1979', function (assert) {
assert.false(
h._.isDate(new Date('8/17/1979'), h.AUG, 16),
'wrong day 8/17/1979 is not 8/16'
);
assert.false(
h._.isDate(new Date('7/16/1979'), h.AUG, 16),
'wrong month 7/16/1979 is not 8/16'
);
});
});
module('addByOccurrence', function () {
test('Last Day of School', function (assert) {
h.addByOccurrence('Last Day of School', h.LAST, h.FRI, h.JUN);
assert.equal(
h._.getRegisteredOccurrence(new Date('6/26/2020')),
'Last Day of School'
);
});
});
module('addByDate', function () {
test('My Birthday', function (assert) {
h.addByDate('My Birthday', h.AUG, 16);
assert.equal(
h._.getRegisteredDate(new Date('8/16/2020')),
'My Birthday'
);
});
});
module('reset', function () {
test('reset', function (assert) {
var h = HollyDates();
h.reset();
assert.deepEqual(
h._.getRegisteredOccurrences(), {},
'reset should clear all holidays'
);
assert.deepEqual(
h._.getCustomDates(), {},
'reset should clear all custom dates'
);
});
});
module('isWeekend', function () {
test('check weekends', function (assert) {
assert.true(
h.isWeekend(new Date('9/5/2020')),
'9/5/2020 falls on a saturday'
);
assert.true(
h.isWeekend(new Date('9/6/2020')),
'9/6/2020 falls on a sunday'
);
});
test('check weekdays', function (assert) {
assert.false(
h.isWeekend(new Date('9/4/2020')),
'9/5/2020 falls on a friday'
);
assert.false(
h.isWeekend(new Date('9/7/2020')),
'9/6/2020 falls on a monday'
);
});
});
module('getCallBacks', function () {
test('add callbacks', function (assert) {
var h = HollyDates();
h.reset();
h.addByOccurrence('Last Day of School', h.LAST, h.FRI, h.JUN)
.onMatch(function (holiday) {});
assert.equal(
typeof h._.getCallBacks()['Last Day of School'],
'function',
'callback should be a function'
);
assert.equal(
typeof h._.getCallBacks()['no callback registered'],
'undefined',
'no callback for holiday should return undefined'
);
});
});
module('getHoliday', function () {
test('returns registered holiday', function (assert) {
var h = HollyDates();
h.reset();
h.addByOccurrence('Last Day of School', h.LAST, h.FRI, h.JUN);
assert.equal(
h.getHoliday(new Date('6/26/2020')),
'Last Day of School',
'date should return "Last Day of School'
);
assert.equal(
h.getHoliday(new Date('6/27/2020')),
null,
'date should return "Last Day of School'
);
});
test('returns registered date', function (assert) {
var h = HollyDates();
h.reset();
h.addByDate('Last Day of School', h.JUN, 1);
assert.equal(
h.getHoliday(new Date('6/1/2020')),
'Last Day of School',
'date should return "Last Day of School'
);
assert.equal(
h.getHoliday(new Date('6/2/2020')),
null,
'date should return "Last Day of School'
);
});
});
module('onMatch', function () {
test('test trigger() return value', function (assert) {
var h = HollyDates();
h.reset();
var test;
h.addByOccurrence('My Fav Holiday', h.LAST, h.FRI, h.JUN)
.onMatch(function (holiday) {
test = 'Occurrence';
});
assert.equal(
h.trigger(new Date('6/26/2020')),
true,
'registered occurrence should be triggered and return true'
);
h.addByDate('My 2nd Fav Holiday', h.JUL, 4)
.onMatch(function (holiday) {
test = 'Date';
});
assert.equal(
h.trigger(new Date('7/4/2020')),
true,
'registered date should be triggered and return true'
);
assert.equal(
h.trigger(new Date('1/1/2020')),
false,
'should return false, no registered holidays available'
);
});
test('test callback execution', function (assert) {
var h = HollyDates();
h.reset();
var test;
h.addByOccurrence('My Fav Holiday', h.LAST, h.FRI, h.JUN)
.onMatch(function (holiday) {
test = 'Occurrence';
});
h.trigger(new Date('6/26/2020'));
assert.equal(
test,
'Occurrence',
'registered occurrence should be triggered'
);
h.addByDate('My 2nd Fav Holiday', h.JUL, 4)
.onMatch(function (holiday) {
test = 'Date';
});
h.trigger(new Date('7/4/2020'));
assert.equal(
test,
'Date',
'registered date should be triggered'
);
});
test('test callback parameters for custom occurrences', function (assert) {
var h = HollyDates();
h.reset();
var testHoliday;
var testDate;
h.addByOccurrence('My Fav Holiday', h.LAST, h.FRI, h.JUN)
.onMatch(function (holiday, date) {
testHoliday = holiday;
testDate = date;
});
h.trigger(new Date('6/26/2020'));
assert.equal(
testHoliday,
'My Fav Holiday',
'occurrence holiday parameter passed'
);
assert.deepEqual(
testDate,
new Date('6/26/2020'),
'occurrence date parameter passed'
);
});
test('test callback parameters for custom dates', function (assert) {
var h = HollyDates();
h.reset();
var testHoliday;
var testDate;
h.addByDate('My 2nd Fav Holiday', h.JUL, 4)
.onMatch(function (holiday, date) {
testHoliday = holiday;
testDate = date;
});
h.trigger(new Date('7/4/2020'));
assert.equal(
testHoliday,
'My 2nd Fav Holiday',
'custom date holiday parameter passed'
);
assert.deepEqual(
testDate,
new Date('7/4/2020'),
'custom date date parameter passed'
);
});
});
module('default todays date if no date is passed', function () {
test('getHoliday', function (assert) {
var h = HollyDates();
h.reset();
h.addByDate('today', new Date().getMonth() + 1, new Date().getDate())
.onMatch(function (holiday, date) {
// do something
});
assert.true(
h.isHoliday(),
'today is holiday'
);
assert.equal(
h.getHoliday(),
'today',
'should return "today"'
);
assert.true(
h.trigger(),
'call back should have fired for today'
);
});
});