Skip to content

Commit eda008b

Browse files
committed
test(orderBy): add test cases for ordering array-like objects
Closes angular#10060
1 parent 409bcb3 commit eda008b

File tree

1 file changed

+202
-89
lines changed

1 file changed

+202
-89
lines changed

test/ng/filter/orderBySpec.js

+202-89
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,214 @@
11
'use strict';
22

33
describe('Filter: orderBy', function() {
4-
var orderBy;
4+
var orderBy, orderByFilter;
55
beforeEach(inject(function($filter) {
6-
orderBy = $filter('orderBy');
6+
orderBy = orderByFilter = $filter('orderBy');
77
}));
88

9-
it('should return sorted array if predicate is not provided', function() {
10-
expect(orderBy([2, 1, 3])).toEqual([1, 2, 3]);
119

12-
expect(orderBy([2, 1, 3], '')).toEqual([1, 2, 3]);
13-
expect(orderBy([2, 1, 3], [])).toEqual([1, 2, 3]);
14-
expect(orderBy([2, 1, 3], [''])).toEqual([1, 2, 3]);
15-
16-
expect(orderBy([2, 1, 3], '+')).toEqual([1, 2, 3]);
17-
expect(orderBy([2, 1, 3], ['+'])).toEqual([1, 2, 3]);
18-
19-
expect(orderBy([2, 1, 3], '-')).toEqual([3, 2, 1]);
20-
expect(orderBy([2, 1, 3], ['-'])).toEqual([3, 2, 1]);
21-
});
22-
23-
it('shouldSortArrayInReverse', function() {
24-
expect(orderBy([{a:15}, {a:2}], 'a', true)).toEqualData([{a:15}, {a:2}]);
25-
expect(orderBy([{a:15}, {a:2}], 'a', "T")).toEqualData([{a:15}, {a:2}]);
26-
expect(orderBy([{a:15}, {a:2}], 'a', "reverse")).toEqualData([{a:15}, {a:2}]);
27-
});
28-
29-
it('should sort inherited from array', function(){
30-
function BaseCollection(){}
31-
BaseCollection.prototype = Array.prototype;
32-
var child = new BaseCollection();
33-
child.push({a:2});
34-
child.push({a:15});
35-
36-
expect(orderBy(child, 'a', true)).toEqualData([{a:15}, {a:2}]);
37-
});
38-
39-
it('should sort array by predicate', function() {
40-
expect(orderBy([{a:15, b:1}, {a:2, b:1}], ['a', 'b'])).toEqualData([{a:2, b:1}, {a:15, b:1}]);
41-
expect(orderBy([{a:15, b:1}, {a:2, b:1}], ['b', 'a'])).toEqualData([{a:2, b:1}, {a:15, b:1}]);
42-
expect(orderBy([{a:15, b:1}, {a:2, b:1}], ['+b', '-a'])).toEqualData([{a:15, b:1}, {a:2, b:1}]);
43-
});
44-
45-
46-
it('should sort array by date predicate', function() {
47-
// same dates
48-
expect(orderBy([
49-
{ a:new Date('01/01/2014'), b:1 },
50-
{ a:new Date('01/01/2014'), b:3 },
51-
{ a:new Date('01/01/2014'), b:4 },
52-
{ a:new Date('01/01/2014'), b:2 }
53-
],
54-
['a', 'b']))
55-
.toEqualData([
56-
{ a:new Date('01/01/2014'), b:1 },
57-
{ a:new Date('01/01/2014'), b:2 },
58-
{ a:new Date('01/01/2014'), b:3 },
59-
{ a:new Date('01/01/2014'), b:4 }
60-
]);
61-
62-
// one different date
63-
expect(orderBy([
64-
{ a:new Date('01/01/2014'), b:1 },
65-
{ a:new Date('01/01/2014'), b:3 },
66-
{ a:new Date('01/01/2013'), b:4 },
67-
{ a:new Date('01/01/2014'), b:2 }
68-
],
69-
['a', 'b']))
70-
.toEqualData([
71-
{ a:new Date('01/01/2013'), b:4 },
72-
{ a:new Date('01/01/2014'), b:1 },
73-
{ a:new Date('01/01/2014'), b:2 },
74-
{ a:new Date('01/01/2014'), b:3 }
75-
]);
10+
describe('(Arrays)', function() {
11+
it('should return sorted array if predicate is not provided', function() {
12+
expect(orderBy([2, 1, 3])).toEqual([1, 2, 3]);
13+
14+
expect(orderBy([2, 1, 3], '')).toEqual([1, 2, 3]);
15+
expect(orderBy([2, 1, 3], [])).toEqual([1, 2, 3]);
16+
expect(orderBy([2, 1, 3], [''])).toEqual([1, 2, 3]);
17+
18+
expect(orderBy([2, 1, 3], '+')).toEqual([1, 2, 3]);
19+
expect(orderBy([2, 1, 3], ['+'])).toEqual([1, 2, 3]);
20+
21+
expect(orderBy([2, 1, 3], '-')).toEqual([3, 2, 1]);
22+
expect(orderBy([2, 1, 3], ['-'])).toEqual([3, 2, 1]);
23+
});
24+
25+
26+
it('shouldSortArrayInReverse', function() {
27+
expect(orderBy([{a:15}, {a:2}], 'a', true)).toEqualData([{a:15}, {a:2}]);
28+
expect(orderBy([{a:15}, {a:2}], 'a', "T")).toEqualData([{a:15}, {a:2}]);
29+
expect(orderBy([{a:15}, {a:2}], 'a', "reverse")).toEqualData([{a:15}, {a:2}]);
30+
});
31+
32+
33+
it('should sort inherited from array', function() {
34+
function BaseCollection() {}
35+
BaseCollection.prototype = Array.prototype;
36+
var child = new BaseCollection();
37+
child.push({a:2});
38+
child.push({a:15});
39+
40+
expect(Array.isArray(child)).toBe(false);
41+
expect(child instanceof Array).toBe(true);
42+
43+
expect(orderBy(child, 'a', true)).toEqualData([{a:15}, {a:2}]);
44+
});
45+
46+
47+
it('should sort array by predicate', function() {
48+
expect(orderBy([{a:15, b:1}, {a:2, b:1}], ['a', 'b'])).toEqualData([{a:2, b:1}, {a:15, b:1}]);
49+
expect(orderBy([{a:15, b:1}, {a:2, b:1}], ['b', 'a'])).toEqualData([{a:2, b:1}, {a:15, b:1}]);
50+
expect(orderBy([{a:15, b:1}, {a:2, b:1}], ['+b', '-a'])).toEqualData([{a:15, b:1}, {a:2, b:1}]);
51+
});
52+
53+
54+
it('should sort array by date predicate', function() {
55+
// same dates
56+
expect(orderBy([
57+
{ a:new Date('01/01/2014'), b:1 },
58+
{ a:new Date('01/01/2014'), b:3 },
59+
{ a:new Date('01/01/2014'), b:4 },
60+
{ a:new Date('01/01/2014'), b:2 }],
61+
['a', 'b']))
62+
.toEqualData([
63+
{ a:new Date('01/01/2014'), b:1 },
64+
{ a:new Date('01/01/2014'), b:2 },
65+
{ a:new Date('01/01/2014'), b:3 },
66+
{ a:new Date('01/01/2014'), b:4 }]);
67+
68+
// one different date
69+
expect(orderBy([
70+
{ a:new Date('01/01/2014'), b:1 },
71+
{ a:new Date('01/01/2014'), b:3 },
72+
{ a:new Date('01/01/2013'), b:4 },
73+
{ a:new Date('01/01/2014'), b:2 }],
74+
['a', 'b']))
75+
.toEqualData([
76+
{ a:new Date('01/01/2013'), b:4 },
77+
{ a:new Date('01/01/2014'), b:1 },
78+
{ a:new Date('01/01/2014'), b:2 },
79+
{ a:new Date('01/01/2014'), b:3 }]);
80+
});
81+
82+
83+
it('should use function', function() {
84+
expect(
85+
orderBy(
86+
[{a:15, b:1},{a:2, b:1}],
87+
function(value) { return value.a; })).
88+
toEqual([{a:2, b:1},{a:15, b:1}]);
89+
});
90+
91+
92+
it('should support string predicates with names containing non-identifier characters', function() {
93+
/*jshint -W008 */
94+
expect(orderBy([{"Tip %": .25}, {"Tip %": .15}, {"Tip %": .40}], '"Tip %"'))
95+
.toEqualData([{"Tip %": .15}, {"Tip %": .25}, {"Tip %": .40}]);
96+
expect(orderBy([{"원": 76000}, {"원": 31000}, {"원": 156000}], '"원"'))
97+
.toEqualData([{"원": 31000}, {"원": 76000}, {"원": 156000}]);
98+
});
99+
100+
101+
it('should throw if quoted string predicate is quoted incorrectly', function() {
102+
/*jshint -W008 */
103+
expect(function() {
104+
return orderBy([{"Tip %": .15}, {"Tip %": .25}, {"Tip %": .40}], '"Tip %\'');
105+
}).toThrow();
106+
});
76107
});
77108

78109

79-
it('should use function', function() {
80-
expect(
81-
orderBy(
82-
[{a:15, b:1},{a:2, b:1}],
83-
function(value) { return value.a; })).
84-
toEqual([{a:2, b:1},{a:15, b:1}]);
85-
});
86-
87-
it('should support string predicates with names containing non-identifier characters', function() {
88-
/* jshint -W008 */
89-
expect(orderBy([{"Tip %": .25}, {"Tip %": .15}, {"Tip %": .40}], '"Tip %"'))
90-
.toEqualData([{"Tip %": .15}, {"Tip %": .25}, {"Tip %": .40}]);
91-
expect(orderBy([{"원": 76000}, {"원": 31000}, {"원": 156000}], '"원"'))
92-
.toEqualData([{"원": 31000}, {"원": 76000}, {"원": 156000}]);
93-
});
94-
95-
it('should throw if quoted string predicate is quoted incorrectly', function() {
96-
/* jshint -W008 */
97-
expect(function() {
98-
return orderBy([{"Tip %": .15}, {"Tip %": .25}, {"Tip %": .40}], '"Tip %\'');
99-
}).toThrow();
110+
describe('(Array-Like Objects)', function() {
111+
function arrayLike(args) {
112+
var result = {};
113+
var i;
114+
for (i = 0; i < args.length; ++i) {
115+
result[i] = args[i];
116+
}
117+
result.length = i;
118+
return result;
119+
}
120+
121+
122+
beforeEach(inject(function($filter) {
123+
orderBy = function(collection) {
124+
var args = Array.prototype.slice.call(arguments, 0);
125+
args[0] = arrayLike(args[0]);
126+
return orderByFilter.apply(null, args);
127+
};
128+
}));
129+
130+
131+
it('should return sorted array if predicate is not provided', function() {
132+
expect(orderBy([2, 1, 3])).toEqual([1, 2, 3]);
133+
134+
expect(orderBy([2, 1, 3], '')).toEqual([1, 2, 3]);
135+
expect(orderBy([2, 1, 3], [])).toEqual([1, 2, 3]);
136+
expect(orderBy([2, 1, 3], [''])).toEqual([1, 2, 3]);
137+
138+
expect(orderBy([2, 1, 3], '+')).toEqual([1, 2, 3]);
139+
expect(orderBy([2, 1, 3], ['+'])).toEqual([1, 2, 3]);
140+
141+
expect(orderBy([2, 1, 3], '-')).toEqual([3, 2, 1]);
142+
expect(orderBy([2, 1, 3], ['-'])).toEqual([3, 2, 1]);
143+
});
144+
145+
146+
it('shouldSortArrayInReverse', function() {
147+
expect(orderBy([{a:15}, {a:2}], 'a', true)).toEqualData([{a:15}, {a:2}]);
148+
expect(orderBy([{a:15}, {a:2}], 'a', "T")).toEqualData([{a:15}, {a:2}]);
149+
expect(orderBy([{a:15}, {a:2}], 'a', "reverse")).toEqualData([{a:15}, {a:2}]);
150+
});
151+
152+
153+
it('should sort array by predicate', function() {
154+
expect(orderBy([{a:15, b:1}, {a:2, b:1}], ['a', 'b'])).toEqualData([{a:2, b:1}, {a:15, b:1}]);
155+
expect(orderBy([{a:15, b:1}, {a:2, b:1}], ['b', 'a'])).toEqualData([{a:2, b:1}, {a:15, b:1}]);
156+
expect(orderBy([{a:15, b:1}, {a:2, b:1}], ['+b', '-a'])).toEqualData([{a:15, b:1}, {a:2, b:1}]);
157+
});
158+
159+
160+
it('should sort array by date predicate', function() {
161+
// same dates
162+
expect(orderBy([
163+
{ a:new Date('01/01/2014'), b:1 },
164+
{ a:new Date('01/01/2014'), b:3 },
165+
{ a:new Date('01/01/2014'), b:4 },
166+
{ a:new Date('01/01/2014'), b:2 }],
167+
['a', 'b']))
168+
.toEqualData([
169+
{ a:new Date('01/01/2014'), b:1 },
170+
{ a:new Date('01/01/2014'), b:2 },
171+
{ a:new Date('01/01/2014'), b:3 },
172+
{ a:new Date('01/01/2014'), b:4 }]);
173+
174+
// one different date
175+
expect(orderBy([
176+
{ a:new Date('01/01/2014'), b:1 },
177+
{ a:new Date('01/01/2014'), b:3 },
178+
{ a:new Date('01/01/2013'), b:4 },
179+
{ a:new Date('01/01/2014'), b:2 }],
180+
['a', 'b']))
181+
.toEqualData([
182+
{ a:new Date('01/01/2013'), b:4 },
183+
{ a:new Date('01/01/2014'), b:1 },
184+
{ a:new Date('01/01/2014'), b:2 },
185+
{ a:new Date('01/01/2014'), b:3 }]);
186+
});
187+
188+
189+
it('should use function', function() {
190+
expect(
191+
orderBy(
192+
[{a:15, b:1},{a:2, b:1}],
193+
function(value) { return value.a; })).
194+
toEqual([{a:2, b:1},{a:15, b:1}]);
195+
});
196+
197+
198+
it('should support string predicates with names containing non-identifier characters', function() {
199+
/*jshint -W008 */
200+
expect(orderBy([{"Tip %": .25}, {"Tip %": .15}, {"Tip %": .40}], '"Tip %"'))
201+
.toEqualData([{"Tip %": .15}, {"Tip %": .25}, {"Tip %": .40}]);
202+
expect(orderBy([{"원": 76000}, {"원": 31000}, {"원": 156000}], '"원"'))
203+
.toEqualData([{"원": 31000}, {"원": 76000}, {"원": 156000}]);
204+
});
205+
206+
207+
it('should throw if quoted string predicate is quoted incorrectly', function() {
208+
/*jshint -W008 */
209+
expect(function() {
210+
return orderBy([{"Tip %": .15}, {"Tip %": .25}, {"Tip %": .40}], '"Tip %\'');
211+
}).toThrow();
212+
});
100213
});
101214
});

0 commit comments

Comments
 (0)