Skip to content

Commit b1f2917

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

File tree

1 file changed

+198
-89
lines changed

1 file changed

+198
-89
lines changed

test/ng/filter/orderBySpec.js

+198-89
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,210 @@
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+
expect(orderBy(child, 'a', true)).toEqualData([{a:15}, {a:2}]);
40+
});
41+
42+
43+
it('should sort array by predicate', function() {
44+
expect(orderBy([{a:15, b:1}, {a:2, b:1}], ['a', 'b'])).toEqualData([{a:2, b:1}, {a:15, b:1}]);
45+
expect(orderBy([{a:15, b:1}, {a:2, b:1}], ['b', 'a'])).toEqualData([{a:2, b:1}, {a:15, b:1}]);
46+
expect(orderBy([{a:15, b:1}, {a:2, b:1}], ['+b', '-a'])).toEqualData([{a:15, b:1}, {a:2, b:1}]);
47+
});
48+
49+
50+
it('should sort array by date predicate', function() {
51+
// same dates
52+
expect(orderBy([
53+
{ a:new Date('01/01/2014'), b:1 },
54+
{ a:new Date('01/01/2014'), b:3 },
55+
{ a:new Date('01/01/2014'), b:4 },
56+
{ a:new Date('01/01/2014'), b:2 }],
57+
['a', 'b']))
58+
.toEqualData([
59+
{ a:new Date('01/01/2014'), b:1 },
60+
{ a:new Date('01/01/2014'), b:2 },
61+
{ a:new Date('01/01/2014'), b:3 },
62+
{ a:new Date('01/01/2014'), b:4 }]);
63+
64+
// one different date
65+
expect(orderBy([
66+
{ a:new Date('01/01/2014'), b:1 },
67+
{ a:new Date('01/01/2014'), b:3 },
68+
{ a:new Date('01/01/2013'), b:4 },
69+
{ a:new Date('01/01/2014'), b:2 }],
70+
['a', 'b']))
71+
.toEqualData([
72+
{ a:new Date('01/01/2013'), b:4 },
73+
{ a:new Date('01/01/2014'), b:1 },
74+
{ a:new Date('01/01/2014'), b:2 },
75+
{ a:new Date('01/01/2014'), b:3 }]);
76+
});
77+
78+
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+
88+
it('should support string predicates with names containing non-identifier characters', function() {
89+
/*jshint -W008 */
90+
expect(orderBy([{"Tip %": .25}, {"Tip %": .15}, {"Tip %": .40}], '"Tip %"'))
91+
.toEqualData([{"Tip %": .15}, {"Tip %": .25}, {"Tip %": .40}]);
92+
expect(orderBy([{"원": 76000}, {"원": 31000}, {"원": 156000}], '"원"'))
93+
.toEqualData([{"원": 31000}, {"원": 76000}, {"원": 156000}]);
94+
});
95+
96+
97+
it('should throw if quoted string predicate is quoted incorrectly', function() {
98+
/*jshint -W008 */
99+
expect(function() {
100+
return orderBy([{"Tip %": .15}, {"Tip %": .25}, {"Tip %": .40}], '"Tip %\'');
101+
}).toThrow();
102+
});
76103
});
77104

78105

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

0 commit comments

Comments
 (0)