Skip to content

Commit 2a1efed

Browse files
committed
Add unit test for _.sortBy to ensure arrays returned from callbacks are coerced.
1 parent 1a65774 commit 2a1efed

File tree

1 file changed

+19
-19
lines changed

1 file changed

+19
-19
lines changed

test/test.js

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6546,21 +6546,18 @@
65466546

65476547
(function() {
65486548
var objects = [
6549-
{ 'num': 991 },
6550-
{ 'num': 212 },
6551-
{ 'num': 11 },
6552-
{ 'num': 16 },
6553-
{ 'num': 74 },
6554-
{ 'num': 0 },
6555-
{ 'num': 1515 }
6549+
{ 'a': 'x', 'b': 3 },
6550+
{ 'a': 'y', 'b': 4 },
6551+
{ 'a': 'x', 'b': 1 },
6552+
{ 'a': 'y', 'b': 2 }
65566553
];
65576554

65586555
test('should sort in ascending order', 1, function() {
65596556
var actual = _.pluck(_.sortBy(objects, function(object) {
6560-
return object.num;
6561-
}), 'num');
6557+
return object.b;
6558+
}), 'b');
65626559

6563-
deepEqual(actual, [0, 11, 16, 74, 212, 991, 1515]);
6560+
deepEqual(actual, [1, 2, 3, 4]);
65646561
});
65656562

65666563
test('should perform a stable sort (test in IE > 8, Opera, and V8)', 1, function() {
@@ -6617,20 +6614,23 @@
66176614
});
66186615

66196616
test('should work with an array for `callback`', 1, function() {
6620-
var objects = [
6621-
{ 'a': 'x', 'b': 3 },
6622-
{ 'a': 'y', 'b': 4 },
6623-
{ 'a': 'x', 'b': 1 },
6624-
{ 'a': 'y', 'b': 2 }
6625-
];
6626-
66276617
var actual = _.sortBy(objects, ['a', 'b']);
66286618
deepEqual(actual, [objects[2], objects[0], objects[3], objects[1]]);
66296619
});
66306620

6621+
test('should coerce arrays returned from a `callback`', 1, function() {
6622+
var actual = _.sortBy(objects, function(object) {
6623+
var result = [object.a, object.b];
6624+
result.toString = function() { return String(this[0]); };
6625+
return result;
6626+
});
6627+
6628+
deepEqual(actual, [objects[0], objects[2], objects[1], objects[3]]);
6629+
});
6630+
66316631
test('should work with a string for `callback`', 1, function() {
6632-
var actual = _.pluck(_.sortBy(objects, 'num'), 'num');
6633-
deepEqual(actual, [0, 11, 16, 74, 212, 991, 1515]);
6632+
var actual = _.pluck(_.sortBy(objects, 'b'), 'b');
6633+
deepEqual(actual, [1, 2, 3, 4]);
66346634
});
66356635

66366636
test('should work with an object for `collection`', 1, function() {

0 commit comments

Comments
 (0)