|
1 | 1 | 'use strict';
|
2 | 2 |
|
3 | 3 | describe('Filter: orderBy', function() {
|
4 |
| - var orderBy; |
| 4 | + var orderBy, orderByFilter; |
5 | 5 | beforeEach(inject(function($filter) {
|
6 |
| - orderBy = $filter('orderBy'); |
| 6 | + orderBy = orderByFilter = $filter('orderBy'); |
7 | 7 | }));
|
8 | 8 |
|
9 |
| - it('should return sorted array if predicate is not provided', function() { |
10 |
| - expect(orderBy([2, 1, 3])).toEqual([1, 2, 3]); |
11 |
| - |
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(Array.isArray(child)).toBe(false); |
37 |
| - expect(child instanceof Array).toBe(true); |
38 |
| - |
39 |
| - expect(orderBy(child, 'a', true)).toEqualData([{a:15}, {a:2}]); |
| 9 | + describe('(Arrays)', function() { |
| 10 | + it('should return sorted array if predicate is not provided', function() { |
| 11 | + expect(orderBy([2, 1, 3])).toEqual([1, 2, 3]); |
| 12 | + |
| 13 | + expect(orderBy([2, 1, 3], '')).toEqual([1, 2, 3]); |
| 14 | + expect(orderBy([2, 1, 3], [])).toEqual([1, 2, 3]); |
| 15 | + expect(orderBy([2, 1, 3], [''])).toEqual([1, 2, 3]); |
| 16 | + |
| 17 | + expect(orderBy([2, 1, 3], '+')).toEqual([1, 2, 3]); |
| 18 | + expect(orderBy([2, 1, 3], ['+'])).toEqual([1, 2, 3]); |
| 19 | + |
| 20 | + expect(orderBy([2, 1, 3], '-')).toEqual([3, 2, 1]); |
| 21 | + expect(orderBy([2, 1, 3], ['-'])).toEqual([3, 2, 1]); |
| 22 | + }); |
| 23 | + |
| 24 | + |
| 25 | + it('shouldSortArrayInReverse', function() { |
| 26 | + expect(orderBy([{a:15}, {a:2}], 'a', true)).toEqualData([{a:15}, {a:2}]); |
| 27 | + expect(orderBy([{a:15}, {a:2}], 'a', "T")).toEqualData([{a:15}, {a:2}]); |
| 28 | + expect(orderBy([{a:15}, {a:2}], 'a', "reverse")).toEqualData([{a:15}, {a:2}]); |
| 29 | + }); |
| 30 | + |
| 31 | + |
| 32 | + it('should sort inherited from array', function() { |
| 33 | + function BaseCollection() {} |
| 34 | + BaseCollection.prototype = Array.prototype; |
| 35 | + var child = new BaseCollection(); |
| 36 | + child.push({a:2}); |
| 37 | + child.push({a:15}); |
| 38 | + |
| 39 | + expect(Array.isArray(child)).toBe(false); |
| 40 | + expect(child instanceof Array).toBe(true); |
| 41 | + |
| 42 | + expect(orderBy(child, 'a', true)).toEqualData([{a:15}, {a:2}]); |
| 43 | + }); |
| 44 | + |
| 45 | + |
| 46 | + it('should sort array by predicate', function() { |
| 47 | + expect(orderBy([{a:15, b:1}, {a:2, b:1}], ['a', 'b'])).toEqualData([{a:2, b:1}, {a:15, b:1}]); |
| 48 | + expect(orderBy([{a:15, b:1}, {a:2, b:1}], ['b', 'a'])).toEqualData([{a:2, b:1}, {a:15, b:1}]); |
| 49 | + expect(orderBy([{a:15, b:1}, {a:2, b:1}], ['+b', '-a'])).toEqualData([{a:15, b:1}, {a:2, b:1}]); |
| 50 | + }); |
| 51 | + |
| 52 | + |
| 53 | + it('should sort array by date predicate', function() { |
| 54 | + // same dates |
| 55 | + expect(orderBy([ |
| 56 | + { a:new Date('01/01/2014'), b:1 }, |
| 57 | + { a:new Date('01/01/2014'), b:3 }, |
| 58 | + { a:new Date('01/01/2014'), b:4 }, |
| 59 | + { a:new Date('01/01/2014'), b:2 }], |
| 60 | + ['a', 'b'])) |
| 61 | + .toEqualData([ |
| 62 | + { a:new Date('01/01/2014'), b:1 }, |
| 63 | + { a:new Date('01/01/2014'), b:2 }, |
| 64 | + { a:new Date('01/01/2014'), b:3 }, |
| 65 | + { a:new Date('01/01/2014'), b:4 }]); |
| 66 | + |
| 67 | + // one different date |
| 68 | + expect(orderBy([ |
| 69 | + { a:new Date('01/01/2014'), b:1 }, |
| 70 | + { a:new Date('01/01/2014'), b:3 }, |
| 71 | + { a:new Date('01/01/2013'), b:4 }, |
| 72 | + { a:new Date('01/01/2014'), b:2 }], |
| 73 | + ['a', 'b'])) |
| 74 | + .toEqualData([ |
| 75 | + { a:new Date('01/01/2013'), b:4 }, |
| 76 | + { a:new Date('01/01/2014'), b:1 }, |
| 77 | + { a:new Date('01/01/2014'), b:2 }, |
| 78 | + { a:new Date('01/01/2014'), b:3 }]); |
| 79 | + }); |
| 80 | + |
| 81 | + |
| 82 | + it('should use function', function() { |
| 83 | + expect( |
| 84 | + orderBy( |
| 85 | + [{a:15, b:1},{a:2, b:1}], |
| 86 | + function(value) { return value.a; })). |
| 87 | + toEqual([{a:2, b:1},{a:15, b:1}]); |
| 88 | + }); |
| 89 | + |
| 90 | + |
| 91 | + it('should support string predicates with names containing non-identifier characters', function() { |
| 92 | + /*jshint -W008 */ |
| 93 | + expect(orderBy([{"Tip %": .25}, {"Tip %": .15}, {"Tip %": .40}], '"Tip %"')) |
| 94 | + .toEqualData([{"Tip %": .15}, {"Tip %": .25}, {"Tip %": .40}]); |
| 95 | + expect(orderBy([{"원": 76000}, {"원": 31000}, {"원": 156000}], '"원"')) |
| 96 | + .toEqualData([{"원": 31000}, {"원": 76000}, {"원": 156000}]); |
| 97 | + }); |
| 98 | + |
| 99 | + |
| 100 | + it('should throw if quoted string predicate is quoted incorrectly', function() { |
| 101 | + /*jshint -W008 */ |
| 102 | + expect(function() { |
| 103 | + return orderBy([{"Tip %": .15}, {"Tip %": .25}, {"Tip %": .40}], '"Tip %\''); |
| 104 | + }).toThrow(); |
| 105 | + }); |
40 | 106 | });
|
41 | 107 |
|
42 |
| - it('should sort array by predicate', function() { |
43 |
| - expect(orderBy([{a:15, b:1}, {a:2, b:1}], ['a', 'b'])).toEqualData([{a:2, b:1}, {a:15, b:1}]); |
44 |
| - expect(orderBy([{a:15, b:1}, {a:2, b:1}], ['b', 'a'])).toEqualData([{a:2, b:1}, {a:15, b:1}]); |
45 |
| - expect(orderBy([{a:15, b:1}, {a:2, b:1}], ['+b', '-a'])).toEqualData([{a:15, b:1}, {a:2, b:1}]); |
46 |
| - }); |
47 |
| - |
48 |
| - |
49 |
| - it('should sort array by date predicate', function() { |
50 |
| - // same dates |
51 |
| - expect(orderBy([ |
52 |
| - { a:new Date('01/01/2014'), b:1 }, |
53 |
| - { a:new Date('01/01/2014'), b:3 }, |
54 |
| - { a:new Date('01/01/2014'), b:4 }, |
55 |
| - { a:new Date('01/01/2014'), b:2 }], |
56 |
| - ['a', 'b'])) |
57 |
| - .toEqualData([ |
58 |
| - { a:new Date('01/01/2014'), b:1 }, |
59 |
| - { a:new Date('01/01/2014'), b:2 }, |
60 |
| - { a:new Date('01/01/2014'), b:3 }, |
61 |
| - { a:new Date('01/01/2014'), b:4 }]); |
62 |
| - |
63 |
| - // one different date |
64 |
| - expect(orderBy([ |
65 |
| - { a:new Date('01/01/2014'), b:1 }, |
66 |
| - { a:new Date('01/01/2014'), b:3 }, |
67 |
| - { a:new Date('01/01/2013'), b:4 }, |
68 |
| - { a:new Date('01/01/2014'), b:2 }], |
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 |
| - }); |
76 |
| - |
77 |
| - |
78 |
| - it('should use function', function() { |
79 |
| - expect( |
80 |
| - orderBy( |
81 |
| - [{a:15, b:1},{a:2, b:1}], |
82 |
| - function(value) { return value.a; })). |
83 |
| - toEqual([{a:2, b:1},{a:15, b:1}]); |
84 |
| - }); |
85 |
| - |
86 |
| - it('should support string predicates with names containing non-identifier characters', function() { |
87 |
| - /*jshint -W008 */ |
88 |
| - expect(orderBy([{"Tip %": .25}, {"Tip %": .15}, {"Tip %": .40}], '"Tip %"')) |
89 |
| - .toEqualData([{"Tip %": .15}, {"Tip %": .25}, {"Tip %": .40}]); |
90 |
| - expect(orderBy([{"원": 76000}, {"원": 31000}, {"원": 156000}], '"원"')) |
91 |
| - .toEqualData([{"원": 31000}, {"원": 76000}, {"원": 156000}]); |
92 |
| - }); |
93 | 108 |
|
94 |
| - it('should throw if quoted string predicate is quoted incorrectly', function() { |
95 |
| - /*jshint -W008 */ |
96 |
| - expect(function() { |
97 |
| - return orderBy([{"Tip %": .15}, {"Tip %": .25}, {"Tip %": .40}], '"Tip %\''); |
98 |
| - }).toThrow(); |
| 109 | + describe('(Array-Like Objects)', function() { |
| 110 | + function arrayLike(args) { |
| 111 | + var result = {}; |
| 112 | + var i; |
| 113 | + for (i = 0; i < args.length; ++i) { |
| 114 | + result[i] = args[i]; |
| 115 | + } |
| 116 | + result.length = i; |
| 117 | + return result; |
| 118 | + } |
| 119 | + |
| 120 | + |
| 121 | + beforeEach(inject(function($filter) { |
| 122 | + orderBy = function(collection) { |
| 123 | + var args = Array.prototype.slice.call(arguments, 0); |
| 124 | + args[0] = arrayLike(args[0]); |
| 125 | + return orderByFilter.apply(null, args); |
| 126 | + } |
| 127 | + })); |
| 128 | + |
| 129 | + |
| 130 | + it('should return sorted array if predicate is not provided', function() { |
| 131 | + expect(orderBy([2, 1, 3])).toEqual([1, 2, 3]); |
| 132 | + |
| 133 | + expect(orderBy([2, 1, 3], '')).toEqual([1, 2, 3]); |
| 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([1, 2, 3]); |
| 138 | + expect(orderBy([2, 1, 3], ['+'])).toEqual([1, 2, 3]); |
| 139 | + |
| 140 | + expect(orderBy([2, 1, 3], '-')).toEqual([3, 2, 1]); |
| 141 | + expect(orderBy([2, 1, 3], ['-'])).toEqual([3, 2, 1]); |
| 142 | + }); |
| 143 | + |
| 144 | + |
| 145 | + it('shouldSortArrayInReverse', function() { |
| 146 | + expect(orderBy([{a:15}, {a:2}], 'a', true)).toEqualData([{a:15}, {a:2}]); |
| 147 | + expect(orderBy([{a:15}, {a:2}], 'a', "T")).toEqualData([{a:15}, {a:2}]); |
| 148 | + expect(orderBy([{a:15}, {a:2}], 'a', "reverse")).toEqualData([{a:15}, {a:2}]); |
| 149 | + }); |
| 150 | + |
| 151 | + |
| 152 | + it('should sort array by predicate', function() { |
| 153 | + expect(orderBy([{a:15, b:1}, {a:2, b:1}], ['a', 'b'])).toEqualData([{a:2, b:1}, {a:15, b:1}]); |
| 154 | + expect(orderBy([{a:15, b:1}, {a:2, b:1}], ['b', 'a'])).toEqualData([{a:2, b:1}, {a:15, b:1}]); |
| 155 | + expect(orderBy([{a:15, b:1}, {a:2, b:1}], ['+b', '-a'])).toEqualData([{a:15, b:1}, {a:2, b:1}]); |
| 156 | + }); |
| 157 | + |
| 158 | + |
| 159 | + it('should sort array by date predicate', function() { |
| 160 | + // same dates |
| 161 | + expect(orderBy([ |
| 162 | + { a:new Date('01/01/2014'), b:1 }, |
| 163 | + { a:new Date('01/01/2014'), b:3 }, |
| 164 | + { a:new Date('01/01/2014'), b:4 }, |
| 165 | + { a:new Date('01/01/2014'), b:2 }], |
| 166 | + ['a', 'b'])) |
| 167 | + .toEqualData([ |
| 168 | + { a:new Date('01/01/2014'), b:1 }, |
| 169 | + { a:new Date('01/01/2014'), b:2 }, |
| 170 | + { a:new Date('01/01/2014'), b:3 }, |
| 171 | + { a:new Date('01/01/2014'), b:4 }]); |
| 172 | + |
| 173 | + // one different date |
| 174 | + expect(orderBy([ |
| 175 | + { a:new Date('01/01/2014'), b:1 }, |
| 176 | + { a:new Date('01/01/2014'), b:3 }, |
| 177 | + { a:new Date('01/01/2013'), b:4 }, |
| 178 | + { a:new Date('01/01/2014'), b:2 }], |
| 179 | + ['a', 'b'])) |
| 180 | + .toEqualData([ |
| 181 | + { a:new Date('01/01/2013'), b:4 }, |
| 182 | + { a:new Date('01/01/2014'), b:1 }, |
| 183 | + { a:new Date('01/01/2014'), b:2 }, |
| 184 | + { a:new Date('01/01/2014'), b:3 }]); |
| 185 | + }); |
| 186 | + |
| 187 | + |
| 188 | + it('should use function', function() { |
| 189 | + expect( |
| 190 | + orderBy( |
| 191 | + [{a:15, b:1},{a:2, b:1}], |
| 192 | + function(value) { return value.a; })). |
| 193 | + toEqual([{a:2, b:1},{a:15, b:1}]); |
| 194 | + }); |
| 195 | + |
| 196 | + |
| 197 | + it('should support string predicates with names containing non-identifier characters', function() { |
| 198 | + /*jshint -W008 */ |
| 199 | + expect(orderBy([{"Tip %": .25}, {"Tip %": .15}, {"Tip %": .40}], '"Tip %"')) |
| 200 | + .toEqualData([{"Tip %": .15}, {"Tip %": .25}, {"Tip %": .40}]); |
| 201 | + expect(orderBy([{"원": 76000}, {"원": 31000}, {"원": 156000}], '"원"')) |
| 202 | + .toEqualData([{"원": 31000}, {"원": 76000}, {"원": 156000}]); |
| 203 | + }); |
| 204 | + |
| 205 | + |
| 206 | + it('should throw if quoted string predicate is quoted incorrectly', function() { |
| 207 | + /*jshint -W008 */ |
| 208 | + expect(function() { |
| 209 | + return orderBy([{"Tip %": .15}, {"Tip %": .25}, {"Tip %": .40}], '"Tip %\''); |
| 210 | + }).toThrow(); |
| 211 | + }); |
99 | 212 | });
|
100 | 213 | });
|
0 commit comments