UNPKG

22.9 kBJavaScriptView Raw
1var datetime = require('../');
2var assert = require('assert');
3var time = '2015-01-01 00:00:00.000';
4var past = '2014-11-02 00:00:00.000';
5var future = '2015-03-02 00:00:00.000';
6var dayOffset = 60;
7var hourOffset = 25;
8var hourPast = '2014-12-30 23:00:00.000';
9var hourFuture = '2015-01-02 01:00:00.000';
10
11describe('Tests node-datetime', function () {
12
13 it('Can create datetime object with no argument passed', function () {
14 var d = datetime.create();
15 assert(d);
16 });
17
18 it('Can return timestamp in milliseconds', function () {
19 var then = new Date(time).getTime();
20 var d = datetime.create(time);
21 assert.equal(then, d.getTime());
22 });
23
24 it('Can return timestamp in seconds', function () {
25 var then = Math.floor(new Date(time).getTime() / 1000);
26 var d = datetime.create(time);
27 assert.equal(then, d.epoch());
28 });
29
30 it('Can return now in milliseconds', function (done) {
31 var then = new Date(time).getTime();
32 var d = datetime.create(time);
33 setTimeout(function () {
34 var now = d.now();
35 assert.notEqual(then, now);
36 done();
37 }, 1000);
38 });
39
40 it('Can format Y-m-d H:M:S.N', function () {
41 var d = datetime.create(time);
42 var f = d.format('Y-m-d H:M:S.N');
43 assert.equal(f, time);
44 });
45
46 it('Can format Y-m-d I:M:S.N', function () {
47 var str = '2015-05-01 04:30:00.666';
48 var d = datetime.create(str);
49 var f = d.format('Y-m-d I:M:S.N');
50 assert.equal(str, f);
51 });
52
53 it('Can return w/ I:M:S p', function () {
54 datetime.setPeriod([ '午前', '午後' ]);
55 var str = '2017-09-05 14:21:00';
56 var d = datetime.create(str);
57 var f = d.format('I:M:S p');
58 assert.equal('02:21:00 午後', f);
59 });
60
61 it('Can return y/m/d', function () {
62 var d = datetime.create(time);
63 var f = d.format('y/m/d');
64 assert.equal(f, '15/01/01');
65 });
66
67 it('Can return name of week', function () {
68 var d = datetime.create(time);
69 var f = d.format('w W');
70 assert.equal('Thu Thursday', f);
71 });
72
73 it('Can return a short name of a month', function () {
74 var d = datetime.create(time);
75 var m = d.format('n');
76 assert.equal('Jan', m);
77 });
78
79 it('Can return a full name of a month', function () {
80 var d = datetime.create(time);
81 var m = d.format('f');
82 assert.equal('January', m);
83 });
84
85 it('Can offset ' + dayOffset + ' days in the past', function () {
86 var d = datetime.create(time);
87 d.offsetInDays(-1 * dayOffset);
88 assert.equal(past, d.format('Y-m-d H:M:S.N'));
89 });
90
91 it('Can offset ' + dayOffset + ' days in the future', function () {
92 var d = datetime.create(time);
93 d.offsetInDays(dayOffset);
94 assert.equal(future, d.format('Y-m-d H:M:S.N'));
95 });
96
97 it('Can offset ' + hourOffset + ' hours in the past', function () {
98 var d = datetime.create(time);
99 d.offsetInHours(-1 * hourOffset);
100 assert.equal(d.format('Y-m-d H:M:S.N'), hourPast);
101 });
102
103 it('Can offset ' + hourOffset + ' hours in the future', function () {
104 var d = datetime.create(time);
105 d.offsetInHours(hourOffset);
106 assert.equal(d.format('Y-m-d H:M:S.N'), hourFuture);
107 });
108
109 it('Can get instances of DateTime object between 2015-04-12 and 2015-05-12', function () {
110 var start = datetime.create('2015-04-12');
111 var end = datetime.create('2015-05-12');
112 var format = 'Y-m-d H:M:S.N';
113 var list = start.getDatesInRange(end);
114 for (var i = 0, len = list.length; i < len; i++) {
115 var day = list[i];
116 var check = datetime.create(start.getTime());
117 check.offsetInDays(i);
118 assert.equal(day.format(format), check.format(format));
119 }
120 });
121
122 it('can return a list of date time per hour between 2015-04-30 20:40:32.332 and 2015-05-01 17:05:10.223', function () {
123 var start = datetime.create('2015-04-30 20:40:32.332');
124 var end = datetime.create('2015-05-01 17:05:10.223');
125 var format = 'Y/m/d H:M:S.N';
126 var list = start.getHoursInRange(end);
127 for (var i = 0, len = list.length; i < len; i++) {
128 var hour = list[i];
129 var check = datetime.create(start.getTime());
130 check.offsetInHours(i);
131 assert.equal(hour.format(format), check.format(format));
132 }
133 });
134
135 it('Can use default format', function () {
136 var dateStr = '2015-04-30 11:59:59.999';
137 var dt = datetime.create(dateStr, 'Y-m-d H:M:S.N');
138 assert.equal(dateStr, dt.format());
139 });
140
141 it('Can create an increment type timed data', function () {
142 var conf = {
143 init: 10,
144 max: 10,
145 min: 0,
146 interval: 10,
147 step: 1,
148 type: 'inc'
149 };
150 var timedData = datetime.createTimedNumber(conf);
151 var value = timedData.getValue();
152
153 assert.equal(value, 10);
154 });
155
156 it('Can not create an increment type timed data w/ invalid init', function () {
157 var conf = {
158 init: '10',
159 max: 10,
160 min: 0,
161 interval: 10,
162 step: 1,
163 type: 'inc'
164 };
165 try {
166 datetime.createTimedNumber(conf);
167 } catch (e) {
168 assert(e);
169 }
170 });
171
172 it('Can not create an increment type timed data w/ invalid max', function () {
173 var conf = {
174 init: 10,
175 max: 0,
176 min: 0,
177 interval: 10,
178 step: 1,
179 type: 'inc'
180 };
181 try {
182 datetime.createTimedNumber(conf);
183 } catch (e) {
184 assert(e);
185 }
186 });
187
188 it('Can not create an increment type timed data w/ invalid min', function () {
189 var conf = {
190 init: 10,
191 max: 10,
192 min: -1,
193 interval: 10,
194 step: 1,
195 type: 'inc'
196 };
197 try {
198 datetime.createTimedNumber(conf);
199 } catch (e) {
200 assert(e);
201 }
202 });
203
204 it('Can not create an increment type timed data w/ invalid interval', function () {
205 var conf = {
206 init: 10,
207 max: 10,
208 min: 0,
209 interval: [1, 2, 3],
210 step: 1,
211 type: 'inc'
212 };
213 try {
214 datetime.createTimedNumber(conf);
215 } catch (e) {
216 assert(e);
217 }
218 });
219
220 it('Can not create an increment type timed data w/ invalid step', function () {
221 var conf = {
222 init: 10,
223 max: 10,
224 min: 0,
225 interval: 10,
226 step: 100,
227 type: 'inc'
228 };
229 try {
230 datetime.createTimedNumber(conf);
231 } catch (e) {
232 assert(e);
233 }
234 });
235
236 it('Can not create an increment type timed data w/ invalid type', function () {
237 var conf = {
238 init: 10,
239 max: 10,
240 min: 0,
241 interval: 10,
242 step: 1,
243 type: 'foo'
244 };
245 try {
246 datetime.createTimedNumber(conf);
247 } catch (e) {
248 assert(e);
249 }
250 });
251
252 it('Can create an increment type timed data and decrement', function () {
253 var conf = {
254 init: 10,
255 max: 10,
256 min: 0,
257 interval: 10,
258 step: 1,
259 type: 'inc'
260 };
261 var timedData = datetime.createTimedNumber(conf);
262 var value = timedData.getValue();
263
264 assert.equal(value, 10);
265
266 var success = timedData.dec(5);
267
268 assert.equal(success, true);
269 assert.equal(timedData.getValue(), 5);
270 });
271
272 it('Can create an increment type timed data and cannot decrement beyond min', function () {
273 var conf = {
274 init: 10,
275 max: 10,
276 min: 0,
277 interval: 10,
278 step: 1,
279 type: 'inc'
280 };
281 var timedData = datetime.createTimedNumber(conf);
282 var value = timedData.getValue();
283
284 assert.equal(value, 10);
285
286 var success = timedData.dec(100);
287
288 assert.equal(success, false);
289 assert.equal(timedData.getValue(), 10);
290 });
291
292 it('Can create an increment type timed data and decrement and recover by 1 after 10 milliseconds', function (done) {
293 var conf = {
294 init: 10,
295 max: 10,
296 min: 0,
297 interval: 10,
298 step: 1,
299 type: 'inc'
300 };
301 var timedData = datetime.createTimedNumber(conf);
302 var value = timedData.getValue();
303
304 assert.equal(value, 10);
305
306 var success = timedData.dec(5);
307
308 assert.equal(success, true);
309 assert.equal(timedData.getValue(), 5);
310
311 setTimeout(function () {
312 assert.equal(timedData.getValue(), 6);
313 done();
314 }, 10);
315 });
316
317 it('Can create an increment type timed data and decrement and recover by 5 after 50 milliseconds', function (done) {
318 var conf = {
319 init: 10,
320 max: 10,
321 min: 0,
322 interval: 10,
323 step: 1,
324 type: 'inc'
325 };
326 var timedData = datetime.createTimedNumber(conf);
327 var value = timedData.getValue();
328
329 assert.equal(value, 10);
330
331 var success = timedData.dec(5);
332
333 assert.equal(success, true);
334 assert.equal(timedData.getValue(), 5);
335
336 setTimeout(function () {
337 assert.equal(timedData.getValue(), 10);
338 done();
339 }, 50);
340 });
341
342 it('Can create an increment type timed data and decrement and cannot recover beyond max after 60 milliseconds', function (done) {
343 var conf = {
344 init: 10,
345 max: 10,
346 min: 0,
347 interval: 10,
348 step: 1,
349 type: 'inc'
350 };
351 var timedData = datetime.createTimedNumber(conf);
352 var value = timedData.getValue();
353
354 assert.equal(value, 10);
355
356 var success = timedData.dec(5);
357
358 assert.equal(success, true);
359 assert.equal(timedData.getValue(), 5);
360
361 setTimeout(function () {
362 assert.equal(timedData.getValue(), 10);
363 done();
364 }, 60);
365 });
366
367 it('Can create an decrement type timed data', function () {
368 var conf = {
369 init: 10,
370 max: 10,
371 min: 0,
372 interval: 10,
373 step: 1,
374 type: 'dec'
375 };
376 var timedData = datetime.createTimedNumber(conf);
377 var value = timedData.getValue();
378
379 assert.equal(value, 10);
380 });
381
382 it('Can not create an decrement type timed data w/ invalid init', function () {
383 var conf = {
384 init: '10',
385 max: 10,
386 min: 0,
387 interval: 10,
388 step: 1,
389 type: 'dec'
390 };
391 try {
392 datetime.createTimedNumber(conf);
393 } catch (e) {
394 assert(e);
395 }
396 });
397
398 it('Can not create an deccrement type timed data w/ invalid max', function () {
399 var conf = {
400 init: 10,
401 max: 0,
402 min: 0,
403 interval: 10,
404 step: 1,
405 type: 'dec'
406 };
407 try {
408 datetime.createTimedNumber(conf);
409 } catch (e) {
410 assert(e);
411 }
412 });
413
414 it('Can not create an increment type timed data w/ invalid min', function () {
415 var conf = {
416 init: 10,
417 max: 10,
418 min: -1,
419 interval: 10,
420 step: 1,
421 type: 'dec'
422 };
423 try {
424 datetime.createTimedNumber(conf);
425 } catch (e) {
426 assert(e);
427 }
428 });
429
430 it('Can not create an decrement type timed data w/ invalid interval', function () {
431 var conf = {
432 init: 10,
433 max: 10,
434 min: 0,
435 interval: [1, 2, 3],
436 step: 1,
437 type: 'dec'
438 };
439 try {
440 datetime.createTimedNumber(conf);
441 } catch (e) {
442 assert(e);
443 }
444 });
445
446 it('Can not create an decrement type timed data w/ invalid step', function () {
447 var conf = {
448 init: 10,
449 max: 10,
450 min: 0,
451 interval: 10,
452 step: 100,
453 type: 'dec'
454 };
455 try {
456 datetime.createTimedNumber(conf);
457 } catch (e) {
458 assert(e);
459 }
460 });
461
462 it('Can not create an decrement type timed data w/ invalid type', function () {
463 var conf = {
464 init: 10,
465 max: 10,
466 min: 0,
467 interval: 10,
468 step: 1,
469 type: 'foo'
470 };
471 try {
472 datetime.createTimedNumber(conf);
473 } catch (e) {
474 assert(e);
475 }
476 });
477
478 it('Can create an decrement type timed data and increment', function () {
479 var conf = {
480 init: 9,
481 max: 10,
482 min: 0,
483 interval: 10,
484 step: 1,
485 type: 'dec'
486 };
487 var timedData = datetime.createTimedNumber(conf);
488 var value = timedData.getValue();
489
490 assert.equal(value, 9);
491
492 var success = timedData.inc(1);
493
494 assert.equal(success, true);
495 assert.equal(timedData.getValue(), 10);
496 });
497
498 it('Can create an decrement type timed data and cannot increment beyond max', function () {
499 var conf = {
500 init: 9,
501 max: 10,
502 min: 0,
503 interval: 10,
504 step: 1,
505 type: 'dec'
506 };
507 var timedData = datetime.createTimedNumber(conf);
508 var value = timedData.getValue();
509
510 assert.equal(value, 9);
511
512 var success = timedData.inc(100);
513
514 assert.equal(success, false);
515 assert.equal(timedData.getValue(), 9);
516 });
517
518 it('Can create an decrement type timed data by 1 after 10 milliseconds', function (done) {
519 var conf = {
520 init: 10,
521 max: 10,
522 min: 0,
523 interval: 10,
524 step: 1,
525 type: 'dec'
526 };
527 var timedData = datetime.createTimedNumber(conf);
528 var value = timedData.getValue();
529
530 assert.equal(value, 10);
531
532 setTimeout(function () {
533 assert.equal(timedData.getValue(), 9);
534 done();
535 }, 10);
536 });
537
538 it('Can create an decrement type timed data and decrement and derecements by 5 after 50 milliseconds', function (done) {
539 var conf = {
540 init: 10,
541 max: 10,
542 min: 0,
543 interval: 10,
544 step: 1,
545 type: 'dec'
546 };
547 var timedData = datetime.createTimedNumber(conf);
548 var value = timedData.getValue();
549
550 assert.equal(value, 10);
551
552 var success = timedData.dec(5);
553
554 assert.equal(success, true);
555 assert.equal(timedData.getValue(), 5);
556
557 setTimeout(function () {
558 assert.equal(timedData.getValue(), 0);
559 done();
560 }, 50);
561 });
562
563 it('Can create an decrement type timed data and decrement and cannot decrement beyond min after 60 milliseconds', function (done) {
564 var conf = {
565 init: 10,
566 max: 10,
567 min: 0,
568 interval: 10,
569 step: 1,
570 type: 'dec'
571 };
572 var timedData = datetime.createTimedNumber(conf);
573 var value = timedData.getValue();
574
575 assert.equal(value, 10);
576
577 var success = timedData.dec(5);
578
579 assert.equal(success, true);
580 assert.equal(timedData.getValue(), 5);
581
582 setTimeout(function () {
583 assert.equal(timedData.getValue(), 0);
584 done();
585 }, 60);
586 });
587
588 it('Can reconstruct timed number with exactly the same sate', function () {
589 var conf = {
590 init: 10,
591 max: 10,
592 min: 0,
593 interval: 1000,
594 step: 1,
595 type: 'inc'
596 };
597 var tn1 = datetime.createTimedNumber(conf);
598 tn1.dec(1);
599 var tn1Source = tn1.toObject();
600 var tn2 = datetime.createTimedNumber(tn1Source);
601 var tn2Source = tn2.toObject();
602 for (var i in tn2Source) {
603 assert.equal(tn1Source[i], tn2Source[i]);
604 }
605 });
606
607 it('Can use "global" offset in days and "global format"', function () {
608 datetime.setOffsetInDays(dayOffset);
609 datetime.setDefaultFormat('Y-m-d H:M:S.N');
610 var dt1 = datetime.create(time);
611 var dt2 = datetime.create(time);
612 assert.equal(dt1.format(), dt2.format());
613 assert.equal(dt1.format(), future);
614 assert.equal(dt2.format(), future);
615 });
616
617 it('Can create a new instance of TimedState object', function () {
618 var conf = {
619 states: [
620 'a',
621 'b',
622 'c',
623 'd'
624 ],
625 init: 0,
626 interval: 100,
627 loop: false
628 };
629 var ts = datetime.createTimedState(conf);
630 assert(ts);
631 });
632
633 it('Moves forward by 2 states after 200 milliseconds w/ itnerval 100 miliseconds configuration', function (done) {
634 var conf = {
635 states: [
636 'a',
637 'b',
638 'c',
639 'd'
640 ],
641 init: 0,
642 interval: 100,
643 loop: false
644 };
645 var ts = datetime.createTimedState(conf);
646 assert(ts);
647 setTimeout(function () {
648 var state = ts.getState();
649 assert.equal('c', state);
650 done();
651 }, 200);
652 });
653
654 it('Stays at the last state after 500 milliseconds w/ itnerval 100 miliseconds configuration', function (done) {
655 var conf = {
656 states: [
657 'a',
658 'b',
659 'c',
660 'd'
661 ],
662 init: 0,
663 interval: 100,
664 loop: false
665 };
666 var ts = datetime.createTimedState(conf);
667 assert(ts);
668 setTimeout(function () {
669 var state = ts.getState();
670 assert.equal('d', state);
671 done();
672 }, 500);
673 });
674
675 it('Loops back to 1st state after 400 milliseconds w/ itnerval 100 miliseconds configuration', function (done) {
676 var conf = {
677 states: [
678 'a',
679 'b',
680 'c',
681 'd'
682 ],
683 init: 0,
684 interval: 100,
685 loop: true
686 };
687 var ts = datetime.createTimedState(conf);
688 assert(ts);
689 setTimeout(function () {
690 var state = ts.getState();
691 assert.equal('a', state);
692 done();
693 }, 400);
694 });
695
696 it('Loops to 2nd state after 500 milliseconds w/ itnerval 100 miliseconds configuration', function (done) {
697 var conf = {
698 states: [
699 'a',
700 'b',
701 'c',
702 'd'
703 ],
704 init: 0,
705 interval: 100,
706 loop: true
707 };
708 var ts = datetime.createTimedState(conf);
709 assert(ts);
710 setTimeout(function () {
711 var state = ts.getState();
712 assert.equal('b', state);
713 done();
714 }, 500);
715 });
716
717 it('Can move the state forward with .forward()', function () {
718 var conf = {
719 states: [
720 'a',
721 'b',
722 'c',
723 'd'
724 ],
725 init: 0,
726 interval: 10000,
727 loop: false
728 };
729 var ts = datetime.createTimedState(conf);
730 assert(ts);
731 ts.forward();
732 assert.equal('b', ts.getState());
733 });
734
735 it('Can move the state backward with .backward()', function () {
736 var conf = {
737 states: [
738 'a',
739 'b',
740 'c',
741 'd'
742 ],
743 init: 1,
744 interval: 10000,
745 loop: false
746 };
747 var ts = datetime.createTimedState(conf);
748 assert(ts);
749 ts.backward();
750 assert.equal('a', ts.getState());
751 });
752
753 it('Can reconstruct the same state w/ .toObject()', function () {
754 var conf = {
755 states: [
756 'a',
757 'b',
758 'c',
759 'd'
760 ],
761 init: 0,
762 interval: 10000,
763 loop: false
764 };
765 var ts1 = datetime.createTimedState(conf);
766 ts1.forward();
767 var obj1 = ts1.toObject();
768 var ts2 = datetime.createTimedState(obj1);
769 var obj2 = ts2.toObject();
770 assert.equal(JSON.stringify(obj1), JSON.stringify(obj2));
771 });
772});