This repository has been archived by the owner on Nov 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 397
/
tests.js
912 lines (757 loc) · 33.9 KB
/
tests.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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
/* License: MIT.
* Copyright (C) 2013, 2014, 2015, Uri Shaked.
*/
/* global describe, inject, module, beforeEach, afterEach, it, expect, spyOn, jasmine */
'use strict';
describe('module angularMoment', function () {
var $rootScope, $compile, $window, $filter, moment, amTimeAgoConfig, originalTimeAgoConfig, angularMomentConfig,
originalAngularMomentConfig, amMoment;
beforeEach(module('angularMoment'));
beforeEach(inject(function ($injector) {
$rootScope = $injector.get('$rootScope');
$compile = $injector.get('$compile');
$window = $injector.get('$window');
$filter = $injector.get('$filter');
moment = $injector.get('moment');
amMoment = $injector.get('amMoment');
amTimeAgoConfig = $injector.get('amTimeAgoConfig');
angularMomentConfig = $injector.get('angularMomentConfig');
originalTimeAgoConfig = angular.copy(amTimeAgoConfig);
originalAngularMomentConfig = angular.copy(angularMomentConfig);
// Ensure the locale of moment.js is set to en by default
(moment.locale || moment.lang)('en');
// Add a sample timezones for tests
moment.tz.add('UTC|UTC|0|0|');
moment.tz.add('Europe/Zurich|CET CEST|-10 -20|01010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010|-19Lc0 11A0 1o00 11A0 1xG10 1cM0 1cM0 1cM0 1cM0 1cM0 1cM0 1fA0 1cM0 1cM0 1cM0 1cM0 1cM0 1cM0 1cM0 1cM0 1cM0 1cM0 1cM0 1fA0 1cM0 1cM0 1cM0 1cM0 1cM0 1cM0 1cM0 1cM0 1cM0 1cM0 1fA0 1o00 11A0 1o00 11A0 1o00 11A0 1qM0 WM0 1qM0 WM0 1qM0 11A0 1o00 11A0 1o00 11A0 1qM0 WM0 1qM0 WM0 1qM0 WM0 1qM0 11A0 1o00 11A0 1o00 11A0 1qM0 WM0 1qM0 WM0 1qM0 11A0 1o00 11A0 1o00 11A0 1o00 11A0 1qM0 WM0 1qM0 WM0 1qM0 11A0 1o00 11A0 1o00 11A0 1qM0 WM0 1qM0 WM0 1qM0 11A0 1o00 11A0 1o00 11A0 1o00 11A0 1qM0 WM0 1qM0 WM0 1qM0 11A0 1o00 11A0 1o00 11A0 1qM0 WM0 1qM0 WM0 1qM0 WM0 1qM0 11A0 1o00 11A0 1o00');
moment.tz.add('Pacific/Tahiti|LMT TAHT|9W.g a0|01|-2joe1.I');
moment.tz.setDefault(null);
}));
afterEach(function () {
// Restore original configuration after each test
angular.copy(originalTimeAgoConfig, amTimeAgoConfig);
angular.copy(originalAngularMomentConfig, angularMomentConfig);
});
describe('am-time-ago directive', function () {
it('should be change output format after time through attributes amFullDateThreshold and amFullDateThresholdUnit', function () {
$rootScope.testFormat = 'HH:mm';
$rootScope.testDate = new Date(new Date().getTime() - 5 * 60 * 1000);
// after 5 min
var element = angular.element('<span am-time-ago="testDate" am-full-date-threshold="5" am-full-date-threshold-unit="minute" am-full-date-format="{{ testFormat }}"></span>');
element = $compile(element)($rootScope);
$rootScope.$digest();
expect(element.text()).toBe(moment($rootScope.testDate).format($rootScope.testFormat));
});
it('should not change output format after time through attributes amFullDateThreshold and amFullDateThresholdUnit', function () {
$rootScope.testFormat = 'HH:mm';
$rootScope.testDate = new Date(new Date().getTime() - 4 * 60 * 1000);
// after 4 min
var element = angular.element('<span am-time-ago="testDate" am-full-date-threshold="5" am-full-date-threshold-unit="minute" am-full-date-format="{{ testFormat }}"></span>');
element = $compile(element)($rootScope);
$rootScope.$digest();
expect(element.text()).toBe('4 minutes ago');
});
it('should change the text of the element to "a few seconds ago" when given current time', function () {
$rootScope.testDate = new Date();
var element = angular.element('<span am-time-ago="testDate"></span>');
element = $compile(element)($rootScope);
$rootScope.$digest();
expect(element.text()).toBe('a few seconds ago');
});
it('should change the text of the div to "3 minutes ago" when given a date 3 minutes ago', function () {
$rootScope.testDate = new Date(new Date().getTime() - 3 * 60 * 1000);
var element = angular.element('<div am-time-ago="testDate"></div>');
element = $compile(element)($rootScope);
$rootScope.$digest();
expect(element.text()).toBe('3 minutes ago');
});
it('should change the text of the div to "2 hours ago" when given a date 2 hours ago', function () {
$rootScope.testDate = new Date(new Date().getTime() - 2 * 60 * 60 * 1000);
var element = angular.element('<div am-time-ago="testDate"></div>');
element = $compile(element)($rootScope);
$rootScope.$digest();
expect(element.text()).toBe('2 hours ago');
});
it('should change the text of the div to "a year ago" when given a date one year ago', function () {
var today = new Date();
$rootScope.testDate = new Date(today.getFullYear() - 1, today.getMonth(), today.getDate());
var element = angular.element('<div am-time-ago="testDate"></div>');
element = $compile(element)($rootScope);
$rootScope.$digest();
expect(element.text()).toBe('a year ago');
});
it('should parse correctly numeric dates as milliseconds since the epoch', function () {
$rootScope.testDate = new Date().getTime();
var element = angular.element('<div am-time-ago="testDate"></div>');
element = $compile(element)($rootScope);
$rootScope.$digest();
expect(element.text()).toBe('a few seconds ago');
});
it('should update the value if date changes on scope', function () {
var today = new Date();
$rootScope.testDate = new Date(today.getFullYear() - 1, today.getMonth(), today.getDate()).getTime();
var element = angular.element('<div am-time-ago="testDate"></div>');
element = $compile(element)($rootScope);
$rootScope.$digest();
expect(element.text()).toBe('a year ago');
$rootScope.testDate = new Date();
$rootScope.$digest();
expect(element.text()).toBe('a few seconds ago');
});
it('should update the span text as time passes', function (done) {
$rootScope.testDate = new Date(new Date().getTime() - 44000);
var element = angular.element('<div am-time-ago="testDate"></div>');
element = $compile(element)($rootScope);
$rootScope.$digest();
expect(element.text()).toBe('a few seconds ago');
var waitsInterval = setInterval(function () {
// Wait until $rootScope.date is more than 45 seconds old
if (new Date().getTime() - $rootScope.testDate.getTime() < 45200) {
return;
}
clearInterval(waitsInterval);
$rootScope.$digest();
expect(element.text()).toBe('a minute ago');
done();
}, 50);
});
it('should schedule the update timer to one hour ahead for date in the far future (#73)', function () {
$rootScope.testDate = new Date(new Date().getTime() + 86400000);
spyOn($window, 'setTimeout');
var element = angular.element('<div am-time-ago="testDate"></div>');
element = $compile(element)($rootScope);
$rootScope.$digest();
expect($window.setTimeout).toHaveBeenCalledWith(jasmine.any(Function), 3600000);
});
describe('bindonce', function () {
it('should change the text of the div to "3 minutes ago" when given a date 3 minutes ago with one time binding', function () {
$rootScope.testDate = new Date(new Date().getTime() - 3 * 60 * 1000);
var element = angular.element('<div am-time-ago="::testDate"></div>');
element = $compile(element)($rootScope);
$rootScope.$digest();
expect(element.text()).toBe('3 minutes ago');
});
it('should parse correctly numeric dates as milliseconds since the epoch with one time binding', function () {
$rootScope.testDate = new Date().getTime();
var element = angular.element('<div am-time-ago="::testDate"></div>');
element = $compile(element)($rootScope);
$rootScope.$digest();
expect(element.text()).toBe('a few seconds ago');
});
it('should not update the value if date changes on scope when using one time binding', function () {
var today = new Date();
$rootScope.testDate = new Date(today.getFullYear() - 1, today.getMonth(), today.getDate()).getTime();
var element = angular.element('<div am-time-ago="::testDate"></div>');
element = $compile(element)($rootScope);
$rootScope.$digest();
expect(element.text()).toBe('a year ago');
$rootScope.testDate = new Date();
$rootScope.$digest();
expect(element.text()).toBe('a year ago');
});
});
it('should handle undefined data', function () {
$rootScope.testDate = null;
var element = angular.element('<div am-time-ago="testDate"></div>');
element = $compile(element)($rootScope);
var digest = function () {
$rootScope.$digest();
};
expect(digest).not.toThrow();
});
it('should remove the element text and cancel the timer when an empty string is given (#15)', function () {
$rootScope.testDate = new Date().getTime();
var element = angular.element('<div am-time-ago="testDate"></div>');
element = $compile(element)($rootScope);
$rootScope.$digest();
expect(element.text()).toBe('a few seconds ago');
$rootScope.testDate = '';
spyOn($window, 'clearTimeout').and.callThrough();
$rootScope.$digest();
expect($window.clearTimeout).toHaveBeenCalled();
expect(element.text()).toBe('');
});
it('should not change the contents of the element until a date is given', function () {
$rootScope.testDate = null;
var element = angular.element('<div am-time-ago="testDate">Initial text</div>');
element = $compile(element)($rootScope);
$rootScope.$digest();
expect(element.text()).toBe('Initial text');
$rootScope.testDate = new Date().getTime();
$rootScope.$digest();
expect(element.text()).toBe('a few seconds ago');
});
it('should cancel the timer when the scope is destroyed', function () {
var scope = $rootScope.$new();
$rootScope.testDate = new Date();
var element = angular.element('<span am-time-ago="testDate"></span>');
element = $compile(element)(scope);
$rootScope.$digest();
spyOn($window, 'clearTimeout').and.callThrough();
scope.$destroy();
expect($window.clearTimeout).toHaveBeenCalled();
});
it('should generate a time string without suffix when configured to do so', function () {
amTimeAgoConfig.withoutSuffix = true;
$rootScope.testDate = new Date();
var element = angular.element('<span am-time-ago="testDate"></span>');
element = $compile(element)($rootScope);
$rootScope.$digest();
expect(element.text()).toBe('a few seconds');
});
it('should generate update the text following a locale change via amMoment.changeLocale() method', function () {
$rootScope.testDate = new Date();
var element = angular.element('<span am-time-ago="testDate"></span>');
element = $compile(element)($rootScope);
$rootScope.$digest();
expect(element.text()).toBe('a few seconds ago');
amMoment.changeLocale('fr');
expect(element.text()).toBe('il y a quelques secondes');
});
it('should update the `datetime` attr if applied to a TIME element', function () {
$rootScope.testDate = Date.UTC(2012, 8, 20, 15, 20, 12);
var element = angular.element('<time am-time-ago="testDate"></span>');
element = $compile(element)($rootScope);
$rootScope.$digest();
expect(element.attr('datetime')).toBe('2012-09-20T15:20:12.000Z');
});
describe('setting the element title', function () {
it('should not set the title attribute of the element to the date by default', function () {
$rootScope.testDate = new Date().getTime() / 1000;
var element = angular.element('<span am-time-ago="testDate"></span>');
element = $compile(element)($rootScope);
$rootScope.$digest();
expect(element.attr('title')).toBeUndefined();
});
it('should not change the title attribute of the element if the element already has a title', function () {
amTimeAgoConfig.titleFormat = 'MMMM Do YYYY, h:mm:ss a';
$rootScope.testDate = new Date().getTime() / 1000;
var element = angular.element('<span am-time-ago="testDate" title="test"></span>');
element = $compile(element)($rootScope);
$rootScope.$digest();
expect(element.attr('title')).toBe('test');
});
it('should set the title attribute of the element to the formatted date as per the config', function () {
amTimeAgoConfig.titleFormat = 'MMMM Do YYYY, h:mm:ss a';
$rootScope.testDate = new Date().getTime() / 1000;
var element = angular.element('<span am-time-ago="testDate"></span>');
element = $compile(element)($rootScope);
$rootScope.$digest();
var testDateWithCustomFormatting = moment($rootScope.testDate).format(amTimeAgoConfig.titleFormat);
expect(element.attr('title')).toBe(testDateWithCustomFormatting);
});
it('should update the title attribute of the element if the binding model changed', function () {
amTimeAgoConfig.titleFormat = 'MMMM Do YYYY, h:mm:ss a';
$rootScope.testDate = Date.UTC(2015, 11, 10, 18, 44, 12);
var element = angular.element('<span am-time-ago="testDate"></span>');
element = $compile(element)($rootScope);
$rootScope.$digest();
$rootScope.testDate = Date.UTC(2015, 11, 10, 19, 43, 34);
$rootScope.$digest();
var testDate = moment($rootScope.testDate).format(amTimeAgoConfig.titleFormat);
expect(element.attr('title')).toBe(testDate);
});
describe('full date support', function () {
it('should display relative time if the date is recent', function () {
amTimeAgoConfig.fullDateThreshold = 7;
$rootScope.testDate = new Date(new Date().getTime() - 2 * 24 * 60 * 60 * 1000);
var element = angular.element('<span am-time-ago="testDate"></span>');
element = $compile(element)($rootScope);
$rootScope.$digest();
expect(element.text()).toBe('2 days ago');
});
it('should display full time if the date is past the threshold', function () {
amTimeAgoConfig.fullDateThreshold = 7;
$rootScope.testDate = new Date(2012, 5, 5);
var element = angular.element('<span am-time-ago="testDate"></span>');
element = $compile(element)($rootScope);
$rootScope.$digest();
expect(element.text()).toMatch(/^2012-06-05T00:00:00[\+\-]\d\d:\d\d$/);
});
it('should display full time using the given format', function () {
amTimeAgoConfig.fullDateThreshold = 7;
amTimeAgoConfig.fullDateFormat = 'YYYY,DD,MM';
$rootScope.testDate = new Date(2010, 1, 8);
var element = angular.element('<span am-time-ago="testDate"></span>');
element = $compile(element)($rootScope);
$rootScope.$digest();
expect(element.text()).toBe('2010,08,02');
});
it('should support changing the full date threshold through attribute', function () {
$rootScope.threshold = 7;
$rootScope.testDate = new Date(new Date().getTime() - 12 * 24 * 60 * 60 * 1000);
var element = angular.element('<span am-time-ago="testDate" am-full-date-threshold="{{threshold}}"></span>');
element = $compile(element)($rootScope);
$rootScope.$digest();
expect(element.text()).toBe(moment($rootScope.testDate).format());
$rootScope.threshold = 20;
$rootScope.$digest();
expect(element.text()).toBe('12 days ago');
});
it('should support setting the full date format through attribute', function () {
amTimeAgoConfig.fullDateThreshold = 7;
$rootScope.testDate = new Date(2013, 11, 15);
var element = angular.element('<span am-time-ago="testDate" am-full-date-format="YYYY-MM-DD"></span>');
element = $compile(element)($rootScope);
$rootScope.$digest();
expect(element.text()).toBe('2013-12-15');
});
});
describe('am-from attribute', function () {
it('should make the calculations from the am-from given', function () {
$rootScope.from = new Date(2015, 6, 11);
$rootScope.testDate = new Date(2015, 6, 12);
var element = angular.element('<span am-time-ago="testDate" am-from="from"></span>');
element = $compile(element)($rootScope);
$rootScope.$digest();
expect(element.text()).toBe('in a day');
});
});
});
describe('am-without-suffix attribute', function () {
it('should generate a time string without suffix when true', function () {
$rootScope.testDate = new Date();
var element = angular.element('<span am-time-ago="testDate" am-without-suffix="true"></span>');
element = $compile(element)($rootScope);
$rootScope.$digest();
expect(element.text()).toBe('a few seconds');
});
it('should generate a time string with suffix when false', function () {
amTimeAgoConfig.withoutSuffix = true;
$rootScope.testDate = new Date();
var element = angular.element('<span am-time-ago="testDate" am-without-suffix="false"></span>');
element = $compile(element)($rootScope);
$rootScope.$digest();
expect(element.text()).toBe('a few seconds ago');
});
it('should support expressions', function () {
$rootScope.testDate = new Date();
$rootScope.withSuffix = false;
var element = angular.element('<span am-time-ago="testDate" am-without-suffix="!withSuffix"></span>');
element = $compile(element)($rootScope);
$rootScope.$digest();
expect(element.text()).toBe('a few seconds');
$rootScope.withSuffix = true;
$rootScope.$digest();
expect(element.text()).toBe('a few seconds ago');
});
it('should ignore non-boolean values', function () {
$rootScope.testDate = new Date();
$rootScope.withoutSuffix = 'string';
var element = angular.element('<span am-time-ago="testDate" am-without-suffix="withoutSuffix"></span>');
element = $compile(element)($rootScope);
$rootScope.$digest();
expect(element.text()).toBe('a few seconds ago');
});
});
describe('serverTime configuration', function () {
it('should calculate time ago in respect to the configured server time', function () {
amTimeAgoConfig.serverTime = Date.UTC(2014, 5, 12, 5, 22, 11);
$rootScope.testDate = Date.UTC(2014, 5, 12, 9, 22, 11);
var element = angular.element('<span am-time-ago="testDate"></span>');
element = $compile(element)($rootScope);
$rootScope.$digest();
expect(element.text()).toBe('in 4 hours');
});
});
});
describe('amFromUnix filter', function () {
var amFromUnix;
beforeEach(function () {
amFromUnix = $filter('amFromUnix');
});
it('should convert a unix-timestamp into a moment object with the correct time value', function () {
var result = amFromUnix(1422095523);
expect(moment.isMoment(result)).toBe(true);
expect(result.valueOf()).toBe(1422095523000);
});
});
describe('amUtc filter', function () {
var amUtc;
beforeEach(function () {
amUtc = $filter('amUtc');
});
it('should create a UTC moment from the given date', function () {
var result = amUtc(new Date());
expect(moment.isMoment(result)).toBe(true);
expect(result.utcOffset()).toBe(0);
});
});
describe('amLocal filter', function () {
var amLocal;
beforeEach(function () {
amLocal = $filter('amLocal');
});
it('should convert the given moment object into local object', function () {
var localOffset = moment().utcOffset();
var utcDate = moment.utc();
var result = amLocal(utcDate);
expect(moment.isMoment(result)).toBe(true);
expect(result.utcOffset()).toBe(localOffset);
});
it('should return null if a non moment object was given', function () {
expect(amLocal(null)).toBe(null);
expect(amLocal('test')).toBe(null);
expect(amLocal(new Date())).toBe(null);
});
});
describe('amParse filter', function () {
var amParse;
beforeEach(function () {
amParse = $filter('amParse');
});
it('should parse the given date string according to the given format, and return a moment object', function () {
var result = amParse('2015#09#13', 'YYYY#MM#DD');
expect(moment.isMoment(result)).toBe(true);
expect(result.year()).toBe(2015);
expect(result.month()).toBe(8);
expect(result.date()).toBe(13);
});
});
describe('amTimezone filter', function () {
var amTimezone;
beforeEach(function () {
amTimezone = $filter('amTimezone');
});
it('should apply the given timezone', function () {
var timestamp = Date.UTC(2012, 0, 22, 12, 46, 54);
var result = amTimezone(timestamp, 'Pacific/Tahiti');
expect(result.utcOffset()).toBe(-600);
expect(result.format('YYYY-MM-DD HH:mm:ss')).toBe('2012-01-22 02:46:54');
});
it('should parse timezones containing Z correctly (issue #168)', function () {
var timestamp = Date.UTC(2015, 8, 3, 23, 55, 55);
var result = amTimezone(timestamp, 'Europe/Zurich');
expect(result.utcOffset()).toBe(120);
expect(result.format('YYYY-MM-DD HH:mm:ss')).toBe('2015-09-04 01:55:55');
});
it('should gracefully handle the case where moment-timezone is not loaded', function () {
var timestamp = Date.UTC(2012, 0, 22, 12, 46, 54);
var originalMomentTz = moment.fn.tz;
try {
delete moment.fn.tz;
var result = amTimezone(timestamp, 'Pacific/Tahiti');
expect(moment.isMoment(result)).toBe(true);
// We the timezone will not be correct since moment.fn.tz isn't present. The user will be warned
// about this.
} finally {
moment.fn.tz = originalMomentTz;
moment.fn.tz = originalMomentTz;
}
});
it('should not cause infinite digest loop when combined with am-time-ago', function () {
$rootScope.testDate = new Date();
var element = angular.element('<div am-time-ago="testDate|amTimezone:\'UTC\'"></div>');
element = $compile(element)($rootScope);
$rootScope.$digest();
});
});
describe('amUtcOffset filter', function () {
var amUtcOffset;
beforeEach(function () {
amUtcOffset = $filter('amUtcOffset');
});
it('should accept a numeric UTC offset and apply it to the given date / moment', function () {
var timestamp = Date.UTC(2012, 0, 22, 12, 46, 54);
var result = amUtcOffset(timestamp, -450);
expect(result.utcOffset()).toBe(-450);
expect(result.format('YYYY-MM-DD HH:mm:ss')).toBe('2012-01-22 05:16:54');
});
it('should accept a string UTC offset and apply it to the given date / moment', function () {
var timestamp = Date.UTC(2012, 0, 22, 12, 46, 54);
var result = amUtcOffset(timestamp, '-08:00');
expect(result.utcOffset()).toBe(-480);
expect(result.format('YYYY-MM-DD HH:mm:ss')).toBe('2012-01-22 04:46:54');
});
});
describe('amCalendar filter', function () {
var amCalendar;
beforeEach(function () {
amCalendar = $filter('amCalendar');
});
it('should convert today date to calendar form', function () {
var today = new Date();
var testDate = new Date(today.getFullYear(), today.getMonth(), today.getDate(), 13, 33, 33);
expect(amCalendar(testDate)).toBe('Today at 1:33 PM');
});
it('should convert date in long past to calendar form', function () {
expect(amCalendar(new Date(2012, 2, 25, 13, 14, 15))).toBe('03/25/2012');
});
it('should gracefully handle undefined values', function () {
expect(amCalendar()).toBe('');
});
it('should accept a numeric unix timestamp (milliseconds since the epoch) as input', function () {
expect(amCalendar(new Date(2012, 0, 22, 4, 46, 54).getTime())).toBe('01/22/2012');
});
it('should return an empty string for invalid input', function () {
expect(amCalendar('blah blah')).toBe('');
});
it('should take advanced arguments referenceTime and formats', function () {
expect(amCalendar(new Date(2012, 0, 22, 4, 46, 54), new Date(2012, 0, 23, 4, 46, 54), {lastDay: '[Yesterday at] H:m'})).toBe('Yesterday at 4:46');
});
});
describe('amDifference filter', function () {
var amDifference;
beforeEach(function () {
amDifference = $filter('amDifference');
});
it('should take the difference of two dates in milliseconds', function () {
var today = new Date(2012, 0, 22, 0, 0, 0);
var testDate = new Date(today.getFullYear(), today.getMonth(), today.getDate(), 13, 33, 33);
expect(amDifference(testDate, today)).toBe(48813000);
});
it('should support passing "years", "months", "days", etc as a units parameter', function () {
var test = new Date(2012, 0, 22, 4, 46, 54);
var testDate1 = new Date(2013, 0, 22, 4, 46, 54);
expect(amDifference(testDate1, test, 'years')).toBe(1);
var testDate2 = new Date(2012, 1, 22, 4, 46, 54);
expect(amDifference(testDate2, test, 'months')).toBe(1);
var testDate3 = new Date(2012, 0, 23, 4, 46, 54);
expect(amDifference(testDate3, test, 'days')).toBe(1);
});
it('should allow rounding to be disabled via parameter', function () {
var test = new Date(2012, 0, 22, 4, 46, 54);
var testDate1 = new Date(test.getFullYear() + 1, test.getMonth() + 6, test.getDate());
expect(amDifference(testDate1, test, 'years')).toBe(1);
expect(amDifference(testDate1, test, 'years', true)).toBeCloseTo(1.5);
});
it('dates from the future should return negative values', function () {
var today = new Date(2012, 0, 22, 4, 46, 54);
var testDate = new Date(2013, 0, 22, 4, 46, 54);
expect(String(amDifference(today, testDate))).toContain('-');
});
it('should gracefully handle undefined values', function () {
expect(amDifference()).toBe('');
});
it('should accept a numeric unix timestamp (milliseconds since the epoch) as input', function () {
expect(amDifference(new Date(2012, 0, 22, 4, 46, 55).getTime(), new Date(2012, 0, 22, 4, 46, 54).getTime())).toBe(1000);
});
it('should return an empty string for invalid input', function () {
expect(amDifference('blah blah')).toBe('');
});
});
describe('amDateFormat filter', function () {
var amDateFormat;
beforeEach(function () {
amDateFormat = $filter('amDateFormat');
});
it('should support displaying format', function () {
var today = new Date();
var expectedResult = today.getDate() + '.' + (today.getMonth() + 1) + '.' + today.getFullYear();
expect(amDateFormat(today, 'D.M.YYYY')).toBe(expectedResult);
});
it('should gracefully handle undefined values', function () {
expect(amDateFormat(undefined, 'D.M.YYYY')).toBe('');
});
it('should accept a numeric unix timestamp (milliseconds since the epoch) as input', function () {
var timestamp = new Date(2012, 0, 22, 12, 46, 54).getTime();
expect(amDateFormat(timestamp, '(HH,mm,ss);MM.DD.YYYY')).toBe('(12,46,54);01.22.2012');
});
it('should gracefully handle string unix timestamp as input', function () {
var strTimestamp = String(new Date(2012, 0, 22, 12, 46, 54).getTime());
expect(amDateFormat(strTimestamp, '(HH,mm,ss);MM.DD.YYYY')).toBe('(12,46,54);01.22.2012');
});
it('should respect the configured timezone', function () {
angularMomentConfig.timezone = 'Pacific/Tahiti';
var timestamp = Date.UTC(2012, 0, 22, 12, 46, 54);
expect(amDateFormat(timestamp, '(HH,mm,ss);MM.DD.YYYY')).toBe('(02,46,54);01.22.2012');
});
it('should return an empty string for invalid input', function () {
expect(amDateFormat('blah blah', '(HH,mm,ss);MM.DD.YYYY')).toBe('');
});
});
describe('amDurationFormat filter', function () {
var amDurationFormat;
beforeEach(function () {
amDurationFormat = $filter('amDurationFormat');
});
it('should support return the given duration as text', function () {
expect(amDurationFormat(1000, 'milliseconds')).toBe('a few seconds');
});
it('should support return a day given 24 hours', function () {
expect(amDurationFormat(24, 'hours')).toBe('a day');
});
it('should add prefix the result with the word "in" if the third parameter (suffix) is true', function () {
expect(amDurationFormat(1, 'minutes', true)).toBe('in a minute');
});
it('should add suffix the result with the word "ago" if the duration is negative and the third parameter is true', function () {
expect(amDurationFormat(-1, 'minutes', true)).toBe('a minute ago');
});
it('should gracefully handle undefined values for duration', function () {
expect(amDurationFormat(undefined, 'minutes')).toBe('');
});
});
describe('amTimeAgo filter', function () {
var amTimeAgo;
beforeEach(function () {
amTimeAgo = $filter('amTimeAgo');
});
it('should support return the time ago as text', function () {
var date = new Date();
expect(amTimeAgo(date)).toBe('a few seconds ago');
});
it('should remove suffix from the result if the second parameter (suffix) is true', function () {
var date = new Date();
expect(amTimeAgo(date, true)).toBe('a few seconds');
});
it('should support starting date as third parameter', function () {
var date = new Date(2015, 7, 14),
from = new Date(2015, 7, 15);
expect(amTimeAgo(date, null, from)).toBe('a day ago');
expect(amTimeAgo(date, true, from)).toBe('a day');
});
it('should gracefully handle undefined values', function () {
expect(amTimeAgo()).toBe('');
});
it('should gracefully handle invalid input', function () {
expect(amTimeAgo('noDate')).toBe('');
});
});
describe('amSubtract filter', function () {
var amSubtract;
beforeEach(function () {
amSubtract = $filter('amSubtract');
});
it('should subtract 1 hour from date', function () {
var date = new Date(2000, 1, 1, 0, 0, 0);
expect(amSubtract(date, 1, 'hours').toString()).toMatch(/^Mon Jan 31 2000 23:00:00/);
});
it('should subtract 1 minute from date', function () {
var date = new Date(2000, 1, 1, 0, 0, 0);
expect(amSubtract(date, 1, 'minutes').toString()).toMatch(/^Mon Jan 31 2000 23:59:00/);
});
});
describe('amAdd filter', function () {
var amAdd;
beforeEach(function () {
amAdd = $filter('amAdd');
});
it('should add 1 hour to date', function () {
var date = new Date(2000, 1, 1, 0, 0, 0);
expect(amAdd(date, 1, 'hours').toString()).toMatch(/^Tue Feb 01 2000 01:00:00/);
});
it('should add 1 minute to date', function () {
var date = new Date(2000, 1, 1, 0, 0, 0);
expect(amAdd(date, 1, 'minutes').toString()).toMatch(/^Tue Feb 01 2000 00:01:00/);
});
});
describe('amStartOf filter', function () {
var amStartOf;
beforeEach(function () {
amStartOf = $filter('amStartOf');
});
it('should set date to start of the hour', function () {
var date = new Date(2000, 1, 0, 12, 12, 12);
expect(amStartOf(date, 'hour').toString()).toMatch(/^Mon Jan 31 2000 12:00:00/);
});
it('should set date to start of the day', function () {
var date = new Date(2000, 1, 0, 12, 12, 12);
expect(amStartOf(date, 'day').toString()).toMatch(/^Mon Jan 31 2000 00:00:00/);
});
it('should set date to start of the week', function () {
var date = new Date(2000, 1, 10, 12, 12, 12);
expect(amStartOf(date, 'week').toString()).toMatch(/^Sun Feb 06 2000 00:00:00/);
});
it('should set date to start of the year', function () {
var date = new Date(2000, 6, 6, 12, 12, 12);
expect(amStartOf(date, 'year').toString()).toMatch(/^Sat Jan 01 2000 00:00:00/);
});
});
describe('amEndOf filter', function () {
var amEndOf;
beforeEach(function () {
amEndOf = $filter('amEndOf');
});
it('should set date to end of the hour', function () {
var date = new Date(2000, 0, 1, 12, 12, 12);
expect(amEndOf(date, 'hour').toString()).toMatch(/^Sat Jan 01 2000 12:59:59/);
});
it('should set date to end of the day', function () {
var date = new Date(2000, 0, 1, 12, 12, 12);
expect(amEndOf(date, 'day').toString()).toMatch(/^Sat Jan 01 2000 23:59:59/);
});
it('should set date to end of the week', function () {
var date = new Date(2000, 0, 10, 12, 12, 12);
expect(amEndOf(date, 'week').toString()).toMatch(/^Sat Jan 15 2000 23:59:59/);
});
it('should set date to end of the year', function () {
var date = new Date(2000, 6, 6, 12, 12, 12);
expect(amEndOf(date, 'year').toString()).toMatch(/^Sun Dec 31 2000 23:59:59/);
});
});
describe('amMoment service', function () {
describe('#changeLocale', function () {
it('should convert today\'s date to custom calendar format', function () {
var today = new Date();
amMoment.changeLocale('en', {calendar: {sameDay: '[This Day]'}});
var amCalendar = $filter('amCalendar');
var testDate = new Date(today.getFullYear(), today.getMonth(), today.getDate(), 13, 33, 33);
expect(amCalendar(testDate)).toBe('This Day');
});
it('should return the current locale', function () {
expect(amMoment.changeLocale()).toBe('en');
});
it('should broadcast an angularMoment:localeChanged event on the root scope if a locale is specified', function () {
var eventBroadcasted = false;
$rootScope.$on('amMoment:localeChanged', function () {
eventBroadcasted = true;
});
amMoment.changeLocale('fr');
expect(eventBroadcasted).toBe(true);
});
it('should not broadcast an angularMoment:localeChanged event on the root scope if no locale is specified', function () {
var eventBroadcasted = false;
$rootScope.$on('amMoment:localeChanged', function () {
eventBroadcasted = true;
});
amMoment.changeLocale();
expect(eventBroadcasted).toBe(false);
});
});
describe('#changeTimezone', function () {
it('Should update the current timezone', function () {
amMoment.changeTimezone('UTC');
expect(amMoment.preprocessDate(moment()).utcOffset()).toBe(0);
amMoment.changeTimezone('Pacific/Tahiti');
expect(amMoment.preprocessDate(moment()).utcOffset()).toBe(-600);
});
it('should broadcast an angularMoment:timezoneChanged event on the root scope with the new timezone value', function () {
var eventBroadcasted = false;
$rootScope.$on('amMoment:timezoneChanged', function () {
eventBroadcasted = true;
});
amMoment.changeTimezone('UTC');
expect(eventBroadcasted).toBe(true);
});
});
describe('#preprocessDate', function () {
it('should call a custom preprocessor set by `angularMomentConfig.preprocess`', function () {
var testDate = new Date(2013, 0, 22, 12, 46, 54);
var meeting = {
name: 'Budget plan',
date: testDate
};
angularMomentConfig.preprocess = function (value) {
return moment(value.date);
};
expect(amMoment.preprocessDate(meeting, 'foobar').valueOf()).toEqual(testDate.getTime());
});
it('should respect the default timezone', function () {
angularMomentConfig.timezone = 'Pacific/Tahiti';
expect(amMoment.preprocessDate(new Date()).utcOffset()).toBe(-600);
});
});
});
describe('amTimeAgoConfig constant', function () {
it('should generate time with suffix by default', function () {
expect(amTimeAgoConfig.withoutSuffix).toBe(false);
});
});
describe('angularMomentConfig constant', function () {
it('should have an empty timezone value by default', function () {
expect(angularMomentConfig.timezone).toBe(null);
});
it('should have an empty preprocess value by default', function () {
expect(angularMomentConfig.preprocess).toBe(null);
});
});
});