forked from urish/angular-moment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.js
546 lines (468 loc) · 21.8 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
/* License: MIT.
* Copyright (C) 2013, 2014, Uri Shaked.
*/
/* global describe, inject, module, beforeEach, afterEach, it, expect, spyOn */
'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 language of moment.js is set to english by default
moment.lang('en');
// Add a sample timezone for tests
moment.tz.add({
zones: {
'Pacific/Tahiti': ['-9:58:16 - LMT 1912_9 -9:58:16', '-10 - TAHT']
}
});
}));
afterEach(function () {
// Restore original configuration after each test
angular.copy(originalTimeAgoConfig, amTimeAgoConfig);
angular.copy(originalAngularMomentConfig, angularMomentConfig);
});
describe('am-time-ago directive', function () {
it('should change the text of the element to "a few seconds ago" when given unix timestamp', function () {
$rootScope.testDate = new Date().getTime() / 1000;
var element = angular.element('<span am-time-ago="testDate" am-preprocess="unix"></span>');
element = $compile(element)($rootScope);
$rootScope.$digest();
expect(element.text()).toBe('a few seconds 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 "one 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() < 45000) {
return;
}
clearInterval(waitsInterval);
$rootScope.$digest();
expect(element.text()).toBe('a minute ago');
done();
}, 50);
});
it('should change the text of the element to "a few seconds ago" when given unix timestamp with one time binding', function () {
$rootScope.testDate = new Date().getTime() / 1000;
var element = angular.element('<span am-time-ago="::testDate" am-preprocess="unix"></span>');
element = $compile(element)($rootScope);
$rootScope.$digest();
expect(element.text()).toBe('a few seconds ago');
});
it('should change the text of the element to "a few seconds ago" when given current time with one time binding', 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 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 change the text of the div to "2 hours ago" when given a date 2 hours ago with one time binding', 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 "one year ago" when given a date one year ago with one time binding', 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 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 not update the span text as time passes when using one time binding', 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() < 45000) {
return;
}
clearInterval(waitsInterval);
$rootScope.$digest();
expect(element.text()).toBe('a few seconds ago');
done();
}, 50);
});
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 language change via amMoment.changeLanguage() 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.changeLanguage('fr');
expect(element.text()).toBe('il y a quelques secondes');
});
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('am-format attribute', function () {
it('should support custom date format', function () {
var today = new Date();
$rootScope.testDate = today.getFullYear() + '#' + today.getDate() + '#' + today.getMonth();
var element = angular.element('<span am-time-ago="testDate" am-format="YYYY#DD#MM"></span>');
element = $compile(element)($rootScope);
$rootScope.$digest();
expect(element.text()).toBe('a month ago');
});
it('should support angular expressions in date format', function () {
var today = new Date();
$rootScope.testDate = today.getMonth() + '@' + today.getFullYear() + '@' + today.getDate();
var element = angular.element('<span am-time-ago="testDate" am-format="{{dateFormat}}"></span>');
element = $compile(element)($rootScope);
$rootScope.$digest();
$rootScope.dateFormat = 'MM@YYYY@DD';
$rootScope.$digest();
expect(element.text()).toBe('a month ago');
});
});
describe('format config property', function () {
it('should be used when no `am-format` attribute is found', function () {
angularMomentConfig.format = 'MM@YYYY@DD';
var today = new Date();
$rootScope.testDate = today.getMonth() + '@' + today.getFullYear() + '@' + today.getDate();
var element = angular.element('<span am-time-ago="testDate"></span>');
element = $compile(element)($rootScope);
$rootScope.$digest();
expect(element.text()).toBe('a month ago');
});
it('should be overridable by `am-format` attribute', function () {
angularMomentConfig.format = 'YYYY@MM@@DD';
var today = new Date();
$rootScope.testDate = today.getMonth() + '@' + today.getFullYear() + '@' + today.getDate();
var element = angular.element('<span am-format="MM@YYYY@DD" am-time-ago="testDate"></span>');
element = $compile(element)($rootScope);
$rootScope.$digest();
expect(element.text()).toBe('a month ago');
});
});
});
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 respect the configured timezone', function () {
angularMomentConfig.timezone = 'Pacific/Tahiti';
expect(amCalendar(Date.UTC(2012, 0, 22, 4, 46, 54))).toBe('01/21/2012');
});
it('should apply the "utc" preprocessor when the string "utc" is given in the second argument', function () {
expect(amCalendar(Date.UTC(2012, 0, 22, 0, 0, 0), 'utc')).toBe('01/22/2012');
expect(amCalendar(Date.UTC(2012, 0, 22, 23, 59, 59), 'utc')).toBe('01/22/2012');
});
it('should apply the "unix" preprocessor if angularMomentConfig.preprocess is set to "unix" and no preprocessor is given', function () {
angularMomentConfig.preprocess = 'unix';
expect(amCalendar(100000)).toBe('01/02/1970');
});
it('should ignore the default preprocessor if we explicity give it null in the second argument', function () {
angularMomentConfig.preprocess = 'unix';
expect(amCalendar(100000, null)).toBe('01/01/1970');
});
it('should gracefully handle the case where timezone is given but moment-timezone is not loaded', function () {
angularMomentConfig.timezone = 'Pacific/Tahiti';
var originalMomentTz = moment.fn.tz;
try {
delete moment.fn.tz;
expect(amCalendar(Date.UTC(2012, 0, 22, 4, 46, 54))).toBe('01/22/2012');
} finally {
moment.fn.tz = originalMomentTz;
}
});
it('should return an empty string for invalid input', function () {
expect(amCalendar('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('amMoment service', function () {
describe('#changeLanguage', function () {
it('should return the current language', function () {
expect(amMoment.changeLanguage()).toBe('en');
});
it('should broadcast an angularMoment:languageChange event on the root scope if a language is specified', function () {
var eventBroadcasted = false;
$rootScope.$on('amMoment:languageChange', function () {
eventBroadcasted = true;
});
amMoment.changeLanguage('fr');
expect(eventBroadcasted).toBe(true);
});
it('should not broadcast an angularMoment:languageChange event on the root scope if no language is specified', function () {
var eventBroadcasted = false;
$rootScope.$on('amMoment:languageChange', function () {
eventBroadcasted = true;
});
amMoment.changeLanguage();
expect(eventBroadcasted).toBe(false);
});
});
describe('#preprocessDate', function () {
it('should call a custom preprocessor that was registered on amMoment.preprocessors', function () {
var testDate = new Date(2013, 0, 22, 12, 46, 54);
var meeting = {
name: 'Budget plan',
date: testDate
};
amMoment.preprocessors.foobar = function (value) {
return moment(value.date);
};
expect(amMoment.preprocessDate(meeting, 'foobar').valueOf()).toEqual(testDate.getTime());
});
it('should issue a warning if an unsupported preprocessor is used and fall-back to default processing', inject(function ($log) {
var testDate = new Date(2014, 0, 22, 12, 46, 54);
spyOn($log, 'warn');
expect(amMoment.preprocessDate(testDate.getTime(), 'blabla').valueOf()).toEqual(testDate.getTime());
expect($log.warn).toHaveBeenCalledWith('angular-moment: Ignoring unsupported value for preprocess: blabla');
}));
});
});
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('');
});
it('should have an empty preprocess value by default', function () {
expect(angularMomentConfig.preprocess).toBe(null);
});
});
});