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.
65 lines
1.9 KiB
JavaScript
65 lines
1.9 KiB
JavaScript
import assert from 'assert';
|
|
import lodashStable from 'lodash';
|
|
import { _ } from './utils.js';
|
|
|
|
describe('extremum methods', function() {
|
|
lodashStable.each(['max', 'maxBy', 'min', 'minBy'], function(methodName) {
|
|
var func = _[methodName],
|
|
isMax = /^max/.test(methodName);
|
|
|
|
it('`_.' + methodName + '` should work with Date objects', function() {
|
|
var curr = new Date,
|
|
past = new Date(0);
|
|
|
|
assert.strictEqual(func([curr, past]), isMax ? curr : past);
|
|
});
|
|
|
|
it('`_.' + methodName + '` should work with extremely large arrays', function() {
|
|
var array = lodashStable.range(0, 5e5);
|
|
assert.strictEqual(func(array), isMax ? 499999 : 0);
|
|
});
|
|
|
|
it('`_.' + methodName + '` should work when chaining on an array with only one value', function() {
|
|
var actual = _([40])[methodName]();
|
|
assert.strictEqual(actual, 40);
|
|
});
|
|
});
|
|
|
|
lodashStable.each(['maxBy', 'minBy'], function(methodName) {
|
|
var array = [1, 2, 3],
|
|
func = _[methodName],
|
|
isMax = methodName == 'maxBy';
|
|
|
|
it('`_.' + methodName + '` should work with an `iteratee`', function() {
|
|
var actual = func(array, function(n) {
|
|
return -n;
|
|
});
|
|
|
|
assert.strictEqual(actual, isMax ? 1 : 3);
|
|
});
|
|
|
|
it('should work with `_.property` shorthands', function() {
|
|
var objects = [{ 'a': 2 }, { 'a': 3 }, { 'a': 1 }],
|
|
actual = func(objects, 'a');
|
|
|
|
assert.deepStrictEqual(actual, objects[isMax ? 1 : 2]);
|
|
|
|
var arrays = [[2], [3], [1]];
|
|
actual = func(arrays, 0);
|
|
|
|
assert.deepStrictEqual(actual, arrays[isMax ? 1 : 2]);
|
|
});
|
|
|
|
it('`_.' + methodName + '` should work when `iteratee` returns +/-Infinity', function() {
|
|
var value = isMax ? -Infinity : Infinity,
|
|
object = { 'a': value };
|
|
|
|
var actual = func([object, { 'a': value }], function(object) {
|
|
return object.a;
|
|
});
|
|
|
|
assert.strictEqual(actual, object);
|
|
});
|
|
});
|
|
});
|