mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-01-31 15:27:50 +00:00
* Install test dependencies. * Add initial test files. These files were created using a simplistic AST manipulator using `recast` to preserve formatting. There's bound to be a huge chunk of errors, but this serves as a good start. QUnit was replaced with Mocha, with ES2015 imports running via `esm`. As far as possible, QUnit-specific syntax has been replaced with Mocha's `describe` and `it`, while the native Node.js `assert` module is used for assertions. Files in the `test` directory ending in `.test.js` will be treated as test files. * Add initial passing files to test run.
76 lines
2.6 KiB
JavaScript
76 lines
2.6 KiB
JavaScript
import assert from 'assert';
|
|
import lodashStable from 'lodash';
|
|
import sortBy from '../sortBy.js';
|
|
|
|
describe('sortBy', function() {
|
|
var objects = [
|
|
{ 'a': 'x', 'b': 3 },
|
|
{ 'a': 'y', 'b': 4 },
|
|
{ 'a': 'x', 'b': 1 },
|
|
{ 'a': 'y', 'b': 2 }
|
|
];
|
|
|
|
it('should sort in ascending order by `iteratee`', function() {
|
|
var actual = lodashStable.map(sortBy(objects, function(object) {
|
|
return object.b;
|
|
}), 'b');
|
|
|
|
assert.deepStrictEqual(actual, [1, 2, 3, 4]);
|
|
});
|
|
|
|
it('should use `_.identity` when `iteratee` is nullish', function() {
|
|
var array = [3, 2, 1],
|
|
values = [, null, undefined],
|
|
expected = lodashStable.map(values, lodashStable.constant([1, 2, 3]));
|
|
|
|
var actual = lodashStable.map(values, function(value, index) {
|
|
return index ? sortBy(array, value) : sortBy(array);
|
|
});
|
|
|
|
assert.deepStrictEqual(actual, expected);
|
|
});
|
|
|
|
it('should work with `_.property` shorthands', function() {
|
|
var actual = lodashStable.map(sortBy(objects.concat(undefined), 'b'), 'b');
|
|
assert.deepStrictEqual(actual, [1, 2, 3, 4, undefined]);
|
|
});
|
|
|
|
it('should work with an object for `collection`', function() {
|
|
var actual = sortBy({ 'a': 1, 'b': 2, 'c': 3 }, Math.sin);
|
|
assert.deepStrictEqual(actual, [3, 1, 2]);
|
|
});
|
|
|
|
it('should move `NaN`, nullish, and symbol values to the end', function() {
|
|
var symbol1 = Symbol ? Symbol('a') : null,
|
|
symbol2 = Symbol ? Symbol('b') : null,
|
|
array = [NaN, undefined, null, 4, symbol1, null, 1, symbol2, undefined, 3, NaN, 2],
|
|
expected = [1, 2, 3, 4, symbol1, symbol2, null, null, undefined, undefined, NaN, NaN];
|
|
|
|
assert.deepStrictEqual(sortBy(array), expected);
|
|
|
|
array = [NaN, undefined, symbol1, null, 'd', null, 'a', symbol2, undefined, 'c', NaN, 'b'];
|
|
expected = ['a', 'b', 'c', 'd', symbol1, symbol2, null, null, undefined, undefined, NaN, NaN];
|
|
|
|
assert.deepStrictEqual(sortBy(array), expected);
|
|
});
|
|
|
|
it('should treat number values for `collection` as empty', function() {
|
|
assert.deepStrictEqual(sortBy(1), []);
|
|
});
|
|
|
|
it('should coerce arrays returned from `iteratee`', function() {
|
|
var actual = sortBy(objects, function(object) {
|
|
var result = [object.a, object.b];
|
|
result.toString = function() { return String(this[0]); };
|
|
return result;
|
|
});
|
|
|
|
assert.deepStrictEqual(actual, [objects[0], objects[2], objects[1], objects[3]]);
|
|
});
|
|
|
|
it('should work as an iteratee for methods like `_.map`', function() {
|
|
var actual = lodashStable.map([[2, 1, 3], [3, 2, 1]], sortBy);
|
|
assert.deepStrictEqual(actual, [[1, 2, 3], [1, 2, 3]]);
|
|
});
|
|
});
|