Files
lodash/test/reduce-methods.js
Benjamin Tan d5ef31929a Add initial test files from lodash v4. (#4172)
* 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.
2019-02-12 09:11:32 -08:00

69 lines
2.3 KiB
JavaScript

import assert from 'assert';
import lodashStable from 'lodash';
import { _, empties, noop, add } from './utils.js';
describe('reduce methods', function() {
lodashStable.each(['reduce', 'reduceRight'], function(methodName) {
var func = _[methodName],
array = [1, 2, 3],
isReduce = methodName == 'reduce';
it('`_.' + methodName + '` should reduce a collection to a single value', function() {
var actual = func(['a', 'b', 'c'], function(accumulator, value) {
return accumulator + value;
}, '');
assert.strictEqual(actual, isReduce ? 'abc' : 'cba');
});
it('`_.' + methodName + '` should support empty collections without an initial `accumulator` value', function() {
var actual = [],
expected = lodashStable.map(empties, noop);
lodashStable.each(empties, function(value) {
try {
actual.push(func(value, noop));
} catch (e) {}
});
assert.deepStrictEqual(actual, expected);
});
it('`_.' + methodName + '` should support empty collections with an initial `accumulator` value', function() {
var expected = lodashStable.map(empties, lodashStable.constant('x'));
var actual = lodashStable.map(empties, function(value) {
try {
return func(value, noop, 'x');
} catch (e) {}
});
assert.deepStrictEqual(actual, expected);
});
it('`_.' + methodName + '` should handle an initial `accumulator` value of `undefined`', function() {
var actual = func([], noop, undefined);
assert.strictEqual(actual, undefined);
});
it('`_.' + methodName + '` should return `undefined` for empty collections when no `accumulator` is given (test in IE > 9 and modern browsers)', function() {
var array = [],
object = { '0': 1, 'length': 0 };
if ('__proto__' in array) {
array.__proto__ = object;
assert.strictEqual(func(array, noop), undefined);
}
assert.strictEqual(func(object, noop), undefined);
});
it('`_.' + methodName + '` should return an unwrapped value when implicitly chaining', function() {
assert.strictEqual(_(array)[methodName](add), 6);
});
it('`_.' + methodName + '` should return a wrapped value when explicitly chaining', function() {
assert.ok(_(array).chain()[methodName](add) instanceof _);
});
});
});