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

70 lines
1.8 KiB
JavaScript

import assert from 'assert';
import lodashStable from 'lodash';
import { falsey, stubA, stubB, noop } from './utils.js';
import nth from '../nth.js';
describe('nth', function() {
var array = ['a', 'b', 'c', 'd'];
it('should get the nth element of `array`', function() {
var actual = lodashStable.map(array, function(value, index) {
return nth(array, index);
});
assert.deepStrictEqual(actual, array);
});
it('should work with a negative `n`', function() {
var actual = lodashStable.map(lodashStable.range(1, array.length + 1), function(n) {
return nth(array, -n);
});
assert.deepStrictEqual(actual, ['d', 'c', 'b', 'a']);
});
it('should coerce `n` to an integer', function() {
var values = falsey,
expected = lodashStable.map(values, stubA);
var actual = lodashStable.map(values, function(n) {
return n ? nth(array, n) : nth(array);
});
assert.deepStrictEqual(actual, expected);
values = ['1', 1.6];
expected = lodashStable.map(values, stubB);
actual = lodashStable.map(values, function(n) {
return nth(array, n);
});
assert.deepStrictEqual(actual, expected);
});
it('should return `undefined` for empty arrays', function() {
var values = [null, undefined, []],
expected = lodashStable.map(values, noop);
var actual = lodashStable.map(values, function(array) {
return nth(array, 1);
});
assert.deepStrictEqual(actual, expected);
});
it('should return `undefined` for non-indexes', function() {
var array = [1, 2],
values = [Infinity, array.length],
expected = lodashStable.map(values, noop);
array[-1] = 3;
var actual = lodashStable.map(values, function(n) {
return nth(array, n);
});
assert.deepStrictEqual(actual, expected);
});
});