mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-08 02:17:48 +00:00
Add unit test for _.sortBy to ensure arrays returned from callbacks are coerced.
This commit is contained in:
38
test/test.js
38
test/test.js
@@ -6546,21 +6546,18 @@
|
|||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
var objects = [
|
var objects = [
|
||||||
{ 'num': 991 },
|
{ 'a': 'x', 'b': 3 },
|
||||||
{ 'num': 212 },
|
{ 'a': 'y', 'b': 4 },
|
||||||
{ 'num': 11 },
|
{ 'a': 'x', 'b': 1 },
|
||||||
{ 'num': 16 },
|
{ 'a': 'y', 'b': 2 }
|
||||||
{ 'num': 74 },
|
|
||||||
{ 'num': 0 },
|
|
||||||
{ 'num': 1515 }
|
|
||||||
];
|
];
|
||||||
|
|
||||||
test('should sort in ascending order', 1, function() {
|
test('should sort in ascending order', 1, function() {
|
||||||
var actual = _.pluck(_.sortBy(objects, function(object) {
|
var actual = _.pluck(_.sortBy(objects, function(object) {
|
||||||
return object.num;
|
return object.b;
|
||||||
}), 'num');
|
}), 'b');
|
||||||
|
|
||||||
deepEqual(actual, [0, 11, 16, 74, 212, 991, 1515]);
|
deepEqual(actual, [1, 2, 3, 4]);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should perform a stable sort (test in IE > 8, Opera, and V8)', 1, function() {
|
test('should perform a stable sort (test in IE > 8, Opera, and V8)', 1, function() {
|
||||||
@@ -6617,20 +6614,23 @@
|
|||||||
});
|
});
|
||||||
|
|
||||||
test('should work with an array for `callback`', 1, function() {
|
test('should work with an array for `callback`', 1, function() {
|
||||||
var objects = [
|
|
||||||
{ 'a': 'x', 'b': 3 },
|
|
||||||
{ 'a': 'y', 'b': 4 },
|
|
||||||
{ 'a': 'x', 'b': 1 },
|
|
||||||
{ 'a': 'y', 'b': 2 }
|
|
||||||
];
|
|
||||||
|
|
||||||
var actual = _.sortBy(objects, ['a', 'b']);
|
var actual = _.sortBy(objects, ['a', 'b']);
|
||||||
deepEqual(actual, [objects[2], objects[0], objects[3], objects[1]]);
|
deepEqual(actual, [objects[2], objects[0], objects[3], objects[1]]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('should coerce arrays returned from a `callback`', 1, function() {
|
||||||
|
var actual = _.sortBy(objects, function(object) {
|
||||||
|
var result = [object.a, object.b];
|
||||||
|
result.toString = function() { return String(this[0]); };
|
||||||
|
return result;
|
||||||
|
});
|
||||||
|
|
||||||
|
deepEqual(actual, [objects[0], objects[2], objects[1], objects[3]]);
|
||||||
|
});
|
||||||
|
|
||||||
test('should work with a string for `callback`', 1, function() {
|
test('should work with a string for `callback`', 1, function() {
|
||||||
var actual = _.pluck(_.sortBy(objects, 'num'), 'num');
|
var actual = _.pluck(_.sortBy(objects, 'b'), 'b');
|
||||||
deepEqual(actual, [0, 11, 16, 74, 212, 991, 1515]);
|
deepEqual(actual, [1, 2, 3, 4]);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should work with an object for `collection`', 1, function() {
|
test('should work with an object for `collection`', 1, function() {
|
||||||
|
|||||||
Reference in New Issue
Block a user