Files
lodash/test/reduce.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

58 lines
1.4 KiB
JavaScript

import assert from 'assert';
import { slice } from './utils.js';
import reduce from '../reduce.js';
import head from '../head.js';
import keys from '../keys.js';
describe('reduce', function() {
var array = [1, 2, 3];
it('should use the first element of a collection as the default `accumulator`', function() {
assert.strictEqual(reduce(array), 1);
});
it('should provide correct `iteratee` arguments when iterating an array', function() {
var args;
reduce(array, function() {
args || (args = slice.call(arguments));
}, 0);
assert.deepStrictEqual(args, [0, 1, 0, array]);
args = undefined;
reduce(array, function() {
args || (args = slice.call(arguments));
});
assert.deepStrictEqual(args, [1, 2, 1, array]);
});
it('should provide correct `iteratee` arguments when iterating an object', function() {
var args,
object = { 'a': 1, 'b': 2 },
firstKey = head(keys(object));
var expected = firstKey == 'a'
? [0, 1, 'a', object]
: [0, 2, 'b', object];
reduce(object, function() {
args || (args = slice.call(arguments));
}, 0);
assert.deepStrictEqual(args, expected);
args = undefined;
expected = firstKey == 'a'
? [1, 2, 'b', object]
: [2, 1, 'a', object];
reduce(object, function() {
args || (args = slice.call(arguments));
});
assert.deepStrictEqual(args, expected);
});
});