forked from angular/angular.js
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathFiltersSpec.js
307 lines (248 loc) · 10 KB
/
FiltersSpec.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
'use strict';
describe('filter', function() {
var filter = angular.filter;
it('should called the filter when evaluating expression', function() {
var scope = createScope();
filter.fakeFilter = function() {};
spyOn(filter, 'fakeFilter');
scope.$eval('10|fakeFilter');
expect(filter.fakeFilter).toHaveBeenCalledWith(10);
delete filter['fakeFilter'];
});
it('should call filter on scope context', function() {
var scope = createScope();
scope.name = 'misko';
filter.fakeFilter = function() {
expect(this.name).toEqual('misko');
};
spyOn(filter, 'fakeFilter').andCallThrough();
scope.$eval('10|fakeFilter');
expect(filter.fakeFilter).toHaveBeenCalled();
delete filter['fakeFilter'];
});
describe('formatNumber', function() {
var pattern;
beforeEach(function() {
pattern = { minInt: 1,
minFrac: 0,
maxFrac: 3,
posPre: '',
posSuf: '',
negPre: '-',
negSuf: '',
gSize: 3,
lgSize: 3 };
});
it('should format according to different patterns', function() {
pattern.gSize = 2;
var num = formatNumber(1234567.89, pattern, ',', '.');
expect(num).toBe('12,34,567.89');
num = formatNumber(1234.56, pattern, ',', '.');
expect(num).toBe('1,234.56');
pattern.negPre = '(';
pattern.negSuf = '-)';
num = formatNumber(-1234, pattern, ',', '.');
expect(num).toBe('(1,234-)');
pattern.posPre = '+';
pattern.posSuf = '+';
num = formatNumber(1234, pattern, ',', '.');
expect(num).toBe('+1,234+');
pattern.posPre = pattern.posSuf = '';
pattern.minFrac = 2;
num = formatNumber(1, pattern, ',', '.');
expect(num).toBe('1.00');
pattern.maxFrac = 4;
num = formatNumber(1.11119, pattern, ',', '.');
expect(num).toBe('1.1112');
});
it('should format according different seperators', function() {
var num = formatNumber(1234567.1, pattern, '.', ',', 2);
expect(num).toBe('1.234.567,10');
});
it('should format with or without fractionSize', function() {
var num = formatNumber(123.1, pattern, ',', '.', 3);
expect(num).toBe('123.100');
num = formatNumber(123.12, pattern, ',', '.');
expect(num).toBe('123.12');
var num = formatNumber(123.1116, pattern, ',', '.');
expect(num).toBe('123.112');
});
});
describe('currency', function() {
var currency, html, context;
beforeEach(function() {
html = jqLite('<span></span>');
context = createScope();
context.$element = html;
currency = bind(context, filter.currency);
});
afterEach(function() {
dealoc(context);
});
it('should do basic currency filtering', function() {
expect(currency(0)).toEqual('$0.00');
expect(html.hasClass('ng-format-negative')).toBeFalsy();
expect(currency(-999)).toEqual('($999.00)');
expect(html.hasClass('ng-format-negative')).toBeTruthy();
expect(currency(1234.5678, "USD$")).toEqual('USD$1,234.57');
expect(html.hasClass('ng-format-negative')).toBeFalsy();
});
it('should return empty string for non-numbers', function() {
expect(currency()).toBe('');
expect(html.hasClass('ng-format-negative')).toBeFalsy();
expect(currency('abc')).toBe('');
expect(html.hasClass('ng-format-negative')).toBeFalsy();
});
});
describe('number', function() {
var context, number;
beforeEach(function() {
context = createScope();
number = bind(context, filter.number);
});
it('should do basic filter', function() {
expect(number(0, 0)).toEqual('0');
expect(number(-999)).toEqual('-999');
expect(number(123)).toEqual('123');
expect(number(1234567)).toEqual('1,234,567');
expect(number(1234)).toEqual('1,234');
expect(number(1234.5678)).toEqual('1,234.568');
expect(number(Number.NaN)).toEqual('');
expect(number("1234.5678")).toEqual('1,234.568');
expect(number(1/0)).toEqual("");
expect(number(1, 2)).toEqual("1.00");
expect(number(.1, 2)).toEqual("0.10");
expect(number(.01, 2)).toEqual("0.01");
expect(number(.001, 3)).toEqual("0.001");
expect(number(.0001, 3)).toEqual("0.000");
expect(number(9, 2)).toEqual("9.00");
expect(number(.9, 2)).toEqual("0.90");
expect(number(.99, 2)).toEqual("0.99");
expect(number(.999, 3)).toEqual("0.999");
expect(number(.9999, 3)).toEqual("1.000");
expect(number(1234.567, 0)).toEqual("1,235");
expect(number(1234.567, 1)).toEqual("1,234.6");
expect(number(1234.567, 2)).toEqual("1,234.57");
});
it('should filter exponential numbers', function() {
expect(number(1e50, 0)).toEqual('1e+50');
expect(number(-2e50, 2)).toEqual('-2e+50');
});
});
describe('json', function () {
it('should do basic filter', function() {
expect(filter.json.call({$element:jqLite('<div></div>')}, {a:"b"})).toEqual(toJson({a:"b"}, true));
});
});
describe('lowercase', function() {
it('should do basic filter', function() {
expect(filter.lowercase('AbC')).toEqual('abc');
expect(filter.lowercase(null)).toBeNull();
});
});
describe('uppercase', function() {
it('should do basic filter', function() {
expect(filter.uppercase('AbC')).toEqual('ABC');
expect(filter.uppercase(null)).toBeNull();
});
});
describe('html', function() {
it('should do basic filter', function() {
var html = filter.html("a<b>c</b>d");
expect(html instanceof HTML).toBeTruthy();
expect(html.html).toEqual("a<b>c</b>d");
});
});
describe('linky', function() {
var linky = filter.linky;
it('should do basic filter', function() {
expect(linky("http://ab/ (http://a/) <http://a/> http://1.2/v:~-123. c").html).
toEqual('<a href="http://ab/">http://ab/</a> ' +
'(<a href="http://a/">http://a/</a>) ' +
'<<a href="http://a/">http://a/</a>> ' +
'<a href="http://1.2/v:~-123">http://1.2/v:~-123</a>. c');
expect(linky(undefined)).not.toBeDefined();
});
it('should handle mailto:', function() {
expect(linky("mailto:[email protected]").html).
toEqual('<a href="mailto:[email protected]">[email protected]</a>');
expect(linky("[email protected]").html).
toEqual('<a href="mailto:[email protected]">[email protected]</a>');
expect(linky("send email to [email protected], but").html).
toEqual('send email to <a href="mailto:[email protected]">[email protected]</a>, but');
});
});
describe('date', function() {
var morning = new TzDate(+5, '2010-09-03T12:05:08.000Z'); //7am
var noon = new TzDate(+5, '2010-09-03T17:05:08.000Z'); //12pm
var midnight = new TzDate(+5, '2010-09-03T05:05:08.000Z'); //12am
var earlyDate = new TzDate(+5, '0001-09-03T05:05:08.000Z');
var context, date;
beforeEach(function() {
context = createScope();
date = bind(context, filter.date);
});
it('should ignore falsy inputs', function() {
expect(date(null)).toBeNull();
expect(date('')).toEqual('');
});
it('should do basic filter', function() {
expect(date(noon)).toEqual(date(noon, 'mediumDate'));
expect(date(noon, '')).toEqual(date(noon, 'mediumDate'));
});
it('should accept number or number string representing milliseconds as input', function() {
expect(date(noon.getTime())).toEqual(date(noon.getTime(), 'mediumDate'));
expect(date(noon.getTime() + "")).toEqual(date(noon.getTime() + "", 'mediumDate'));
});
it('should accept various format strings', function() {
expect(date(morning, "yy-MM-dd HH:mm:ss")).
toEqual('10-09-03 07:05:08');
expect(date(midnight, "yyyy-M-d h=H:m:saZ")).
toEqual('2010-9-3 12=0:5:8AM0500');
expect(date(midnight, "yyyy-MM-dd hh=HH:mm:ssaZ")).
toEqual('2010-09-03 12=00:05:08AM0500');
expect(date(noon, "yyyy-MM-dd hh=HH:mm:ssaZ")).
toEqual('2010-09-03 12=12:05:08PM0500');
expect(date(noon, "EEE, MMM d, yyyy")).
toEqual('Fri, Sep 3, 2010');
expect(date(noon, "EEEE, MMMM dd, yyyy")).
toEqual('Friday, September 03, 2010');
expect(date(earlyDate, "MMMM dd, y")).
toEqual('September 03, 1');
});
it('should treat single quoted strings as string literals', function() {
expect(date(midnight, "yyyy'de' 'a'x'dd' 'adZ' h=H:m:saZ")).
toEqual('2010de axdd adZ 12=0:5:8AM0500');
});
it('should treat a sequence of two single quotes as a literal single quote', function() {
expect(date(midnight, "yyyy'de' 'a''dd' 'adZ' h=H:m:saZ")).
toEqual("2010de a'dd adZ 12=0:5:8AM0500");
});
it('should accept default formats', function() {
expect(date(noon, "medium")).
toEqual('Sep 3, 2010 12:05:08 PM');
expect(date(noon, "short")).
toEqual('9/3/10 12:05 PM');
expect(date(noon, "fullDate")).
toEqual('Friday, September 3, 2010');
expect(date(noon, "longDate")).
toEqual('September 3, 2010');
expect(date(noon, "mediumDate")).
toEqual('Sep 3, 2010');
expect(date(noon, "shortDate")).
toEqual('9/3/10');
expect(date(noon, "mediumTime")).
toEqual('12:05:08 PM');
expect(date(noon, "shortTime")).
toEqual('12:05 PM');
});
it('should be able to parse ISO 8601 dates/times using', function() {
var isoString = '2010-09-03T05:05:08.872Z';
expect(date(isoString)).
toEqual(date(isoString, 'mediumDate'));
});
it('should parse format ending with non-replaced string', function() {
expect(date(morning, 'yy/xxx')).toEqual('10/xxx');
});
});
});