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

73 lines
2.4 KiB
JavaScript

import assert from 'assert';
import lodashStable from 'lodash';
import { _, identity, falsey, stubArray } from './utils.js';
describe('flatMap methods', function() {
lodashStable.each(['flatMap', 'flatMapDeep', 'flatMapDepth'], function(methodName) {
var func = _[methodName],
array = [1, 2, 3, 4];
function duplicate(n) {
return [n, n];
}
it('`_.' + methodName + '` should map values in `array` to a new flattened array', function() {
var actual = func(array, duplicate),
expected = lodashStable.flatten(lodashStable.map(array, duplicate));
assert.deepStrictEqual(actual, expected);
});
it('`_.' + methodName + '` should work with `_.property` shorthands', function() {
var objects = [{ 'a': [1, 2] }, { 'a': [3, 4] }];
assert.deepStrictEqual(func(objects, 'a'), array);
});
it('`_.' + methodName + '` should iterate over own string keyed properties of objects', function() {
function Foo() {
this.a = [1, 2];
}
Foo.prototype.b = [3, 4];
var actual = func(new Foo, identity);
assert.deepStrictEqual(actual, [1, 2]);
});
it('`_.' + methodName + '` should use `_.identity` when `iteratee` is nullish', function() {
var array = [[1, 2], [3, 4]],
object = { 'a': [1, 2], 'b': [3, 4] },
values = [, null, undefined],
expected = lodashStable.map(values, lodashStable.constant([1, 2, 3, 4]));
lodashStable.each([array, object], function(collection) {
var actual = lodashStable.map(values, function(value, index) {
return index ? func(collection, value) : func(collection);
});
assert.deepStrictEqual(actual, expected);
});
});
it('`_.' + methodName + '` should accept a falsey `collection`', function() {
var expected = lodashStable.map(falsey, stubArray);
var actual = lodashStable.map(falsey, function(collection, index) {
try {
return index ? func(collection) : func();
} catch (e) {}
});
assert.deepStrictEqual(actual, expected);
});
it('`_.' + methodName + '` should treat number values for `collection` as empty', function() {
assert.deepStrictEqual(func(1), []);
});
it('`_.' + methodName + '` should work with objects with non-number length properties', function() {
var object = { 'length': [1, 2] };
assert.deepStrictEqual(func(object, identity), [1, 2]);
});
});
});