Files
lodash/test/nth.spec.js
tison bd518dd906 test: partially fix broken tests (#5733)
* test: fix throttle.spec.js

Signed-off-by: tison <wander4096@gmail.com>

* test: fix pickBy.spec.js

Signed-off-by: tison <wander4096@gmail.com>

* test: fix isBuffer.spec.js

Signed-off-by: tison <wander4096@gmail.com>

* test: partially fix attempt.spec.js

Signed-off-by: tison <wander4096@gmail.com>

* test: partially fix dropRightWhile.spec.js

Signed-off-by: tison <wander4096@gmail.com>

* test: fix defer.spec.js and rest.spec.js

Signed-off-by: tison <wander4096@gmail.com>

* test: fix invoke.spec.js

Signed-off-by: tison <wander4096@gmail.com>

* test: fix isArray.spec.js

Signed-off-by: tison <wander4096@gmail.com>

* test: partially fix iteration-methods.spec.js

Signed-off-by: tison <wander4096@gmail.com>

* test: fix xor-methods.spec.js

Signed-off-by: tison <wander4096@gmail.com>

* test: fix property.spec.js

Signed-off-by: tison <wander4096@gmail.com>

* test: fix ary.spec.js

Signed-off-by: tison <wander4096@gmail.com>

* test: fix omit-methods.spec.js

Signed-off-by: tison <wander4096@gmail.com>

* test: fix debounce-and-throttle.spec.js

Signed-off-by: tison <wander4096@gmail.com>

* test: fix unzip-and-zip.spec.js

Signed-off-by: tison <wander4096@gmail.com>

* test: fix toPairs-methods.spec.js

Signed-off-by: tison <wander4096@gmail.com>

* test: fix exit-early.spec.js

Signed-off-by: tison <wander4096@gmail.com>

* test: temporarily comment out takeWhile and dropWhile tests

Signed-off-by: tison <wander4096@gmail.com>

* test: partially fix union*.spec.js

Signed-off-by: tison <wander4096@gmail.com>

* test: fix startsWith-and-endsWith.spec.js

Signed-off-by: tison <wander4096@gmail.com>

* test: fix isNil.spec.js

Signed-off-by: tison <wander4096@gmail.com>

* test: fix some of syntax errors

Signed-off-by: tison <wander4096@gmail.com>

---------

Signed-off-by: tison <wander4096@gmail.com>
2023-09-21 07:40:27 -07:00

59 lines
1.7 KiB
JavaScript

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