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

75 lines
2.3 KiB
JavaScript

import assert from 'assert';
import lodashStable from 'lodash';
import { _, LARGE_ARRAY_SIZE, slice } from './utils.js';
import sortBy from '../sortBy.js';
describe('uniqBy methods', function() {
lodashStable.each(['uniqBy', 'sortedUniqBy'], function(methodName) {
var func = _[methodName],
isSorted = methodName == 'sortedUniqBy',
objects = [{ 'a': 2 }, { 'a': 3 }, { 'a': 1 }, { 'a': 2 }, { 'a': 3 }, { 'a': 1 }];
if (isSorted) {
objects = sortBy(objects, 'a');
}
it('`_.' + methodName + '` should work with an `iteratee`', function() {
var expected = isSorted ? [{ 'a': 1 }, { 'a': 2 }, { 'a': 3 }] : objects.slice(0, 3);
var actual = func(objects, function(object) {
return object.a;
});
assert.deepStrictEqual(actual, expected);
});
it('should work with large arrays', function() {
var largeArray = lodashStable.times(LARGE_ARRAY_SIZE, function() {
return [1, 2];
});
var actual = func(largeArray, String);
assert.strictEqual(actual[0], largeArray[0]);
assert.deepStrictEqual(actual, [[1, 2]]);
});
it('`_.' + methodName + '` should provide correct `iteratee` arguments', function() {
var args;
func(objects, function() {
args || (args = slice.call(arguments));
});
assert.deepStrictEqual(args, [objects[0]]);
});
it('`_.' + methodName + '` should work with `_.property` shorthands', function() {
var expected = isSorted ? [{ 'a': 1 }, { 'a': 2 }, { 'a': 3 }] : objects.slice(0, 3),
actual = func(objects, 'a');
assert.deepStrictEqual(actual, expected);
var arrays = [[2], [3], [1], [2], [3], [1]];
if (isSorted) {
arrays = lodashStable.sortBy(arrays, 0);
}
expected = isSorted ? [[1], [2], [3]] : arrays.slice(0, 3);
actual = func(arrays, 0);
assert.deepStrictEqual(actual, expected);
});
lodashStable.each({
'an array': [0, 'a'],
'an object': { '0': 'a' },
'a number': 0,
'a string': '0'
},
function(iteratee, key) {
it('`_.' + methodName + '` should work with ' + key + ' for `iteratee`', function() {
var actual = func([['a'], ['a'], ['b']], iteratee);
assert.deepStrictEqual(actual, [['a'], ['b']]);
});
});
});
});