forked from angular/angular.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlimitToSpec.js
92 lines (75 loc) · 3.15 KB
/
limitToSpec.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
'use strict';
describe('Filter: limitTo', function() {
var items;
var str;
var number;
var limitTo;
beforeEach(inject(function($filter) {
items = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'];
str = "tuvwxyz";
number = 100.045;
limitTo = $filter('limitTo');
}));
it('should return the first X items when X is positive', function() {
expect(limitTo(items, 3)).toEqual(['a', 'b', 'c']);
expect(limitTo(items, '3')).toEqual(['a', 'b', 'c']);
expect(limitTo(str, 3)).toEqual("tuv");
expect(limitTo(str, '3')).toEqual("tuv");
expect(limitTo(number, 3)).toEqual("100");
expect(limitTo(number, '3')).toEqual("100");
});
it('should return the last X items when X is negative', function() {
expect(limitTo(items, -3)).toEqual(['f', 'g', 'h']);
expect(limitTo(items, '-3')).toEqual(['f', 'g', 'h']);
expect(limitTo(str, -3)).toEqual("xyz");
expect(limitTo(str, '-3')).toEqual("xyz");
expect(limitTo(number, -3)).toEqual("045");
expect(limitTo(number, '-3')).toEqual("045");
});
it('should return an empty array when X cannot be parsed', function() {
expect(limitTo(items, 'bogus')).toEqual([]);
expect(limitTo(items, 'null')).toEqual([]);
expect(limitTo(items, 'undefined')).toEqual([]);
expect(limitTo(items, null)).toEqual([]);
expect(limitTo(items, undefined)).toEqual([]);
});
it('should return an empty string when X cannot be parsed', function() {
expect(limitTo(str, 'bogus')).toEqual("");
expect(limitTo(str, 'null')).toEqual("");
expect(limitTo(str, 'undefined')).toEqual("");
expect(limitTo(str, null)).toEqual("");
expect(limitTo(str, undefined)).toEqual("");
});
it('should return input if not String or Array or Number', function() {
expect(limitTo(null, 1)).toEqual(null);
expect(limitTo(undefined, 1)).toEqual(undefined);
expect(limitTo({}, 1)).toEqual({});
});
it('should return a copy of input array if X is exceeds array length', function () {
expect(limitTo(items, 9)).toEqual(items);
expect(limitTo(items, '9')).toEqual(items);
expect(limitTo(items, -9)).toEqual(items);
expect(limitTo(items, '-9')).toEqual(items);
expect(limitTo(items, 9)).not.toBe(items);
});
it('should return the entire string if X exceeds input length', function() {
expect(limitTo(str, 9)).toEqual(str);
expect(limitTo(str, '9')).toEqual(str);
expect(limitTo(str, -9)).toEqual(str);
expect(limitTo(str, '-9')).toEqual(str);
expect(limitTo(number, 9)).toEqual(number.toString());
expect(limitTo(number, '-9')).toEqual(number.toString());
});
it('should return entire input array when limited by Infinity', function() {
expect(limitTo(items, Infinity)).toEqual(items);
expect(limitTo(items, 'Infinity')).toEqual(items);
expect(limitTo(items, -Infinity)).toEqual(items);
expect(limitTo(items, '-Infinity')).toEqual(items);
});
it('should return the entire string when limited by Infinity', function() {
expect(limitTo(str, Infinity)).toEqual(str);
expect(limitTo(str, 'Infinity')).toEqual(str);
expect(limitTo(str, -Infinity)).toEqual(str);
expect(limitTo(str, '-Infinity')).toEqual(str);
});
});