forked from urish/angular-moment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.js
193 lines (169 loc) · 7.5 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
/* License: MIT.
* Copyright (C) 2013, Uri Shaked.
*/
/* global describe, inject, module, beforeEach, it, expect, waitsFor, runs, spyOn */
'use strict';
describe('module angularMoment', function () {
var $rootScope, $compile, $timeout;
beforeEach(module('angularMoment'));
beforeEach(inject(function ($injector) {
$rootScope = $injector.get('$rootScope');
$compile = $injector.get('$compile');
$timeout = $injector.get('$timeout');
}));
describe('am-time-ago directive', function () {
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 () {
$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');
waitsFor(function () {
return (new Date().getTime() - $rootScope.testDate.getTime()) > 45000;
}, '$rootScope.date is more than 45 seconds old', 1500);
runs(function () {
$timeout.flush();
$rootScope.$digest();
expect(element.text()).toBe('a minute 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($timeout, 'cancel').andCallThrough();
$rootScope.$digest();
expect($timeout.cancel).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($timeout, 'cancel').andCallThrough();
scope.$destroy();
expect($timeout.cancel).toHaveBeenCalled();
});
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('amDateFormat filter', function () {
it('should support displaying format', function () {
var today = new Date();
$rootScope.testDate = today;
var element = angular.element('<span>{{testDate|amDateFormat:\'D.M.YYYY\'}}</span>');
element = $compile(element)($rootScope);
$rootScope.$digest();
expect(element.text()).toBe(today.getDate() + '.' + (today.getMonth() + 1) + '.' + today.getFullYear());
});
it('should gracefully handle undefined values', function () {
var element = angular.element('<span>{{testDate|amDateFormat:\'D.M.YYYY\'}}</span>');
element = $compile(element)($rootScope);
$rootScope.$digest();
expect(element.text()).toBe('');
});
it('should accept a numeric unix timestamp (milliseconds since the epoch) as input', function () {
$rootScope.testTimestamp = new Date(2012, 0, 22, 12, 46, 54).getTime();
var element = angular.element('<span>{{testTimestamp|amDateFormat:\'(HH,mm,ss);MM.DD.YYYY\'}}</span>');
element = $compile(element)($rootScope);
$rootScope.$digest();
expect(element.text()).toBe('(12,46,54);01.22.2012');
});
it('should gracefully handle string unix timestamp as input', function () {
$rootScope.testTimestamp = String(new Date(2012, 0, 22, 12, 46, 54).getTime());
var element = angular.element('<span>{{testTimestamp|amDateFormat:\'(HH,mm,ss);MM.DD.YYYY\'}}</span>');
element = $compile(element)($rootScope);
$rootScope.$digest();
expect(element.text()).toBe('(12,46,54);01.22.2012');
});
});
});