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