mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-05 01:17:50 +00:00
wip: code formatting nits continued
This commit is contained in:
94
test/slice.spec.js
Normal file
94
test/slice.spec.js
Normal file
@@ -0,0 +1,94 @@
|
||||
import lodashStable from 'lodash';
|
||||
import { falsey, LARGE_ARRAY_SIZE } from './utils';
|
||||
import slice from '../src/slice';
|
||||
|
||||
describe('slice', () => {
|
||||
const array = [1, 2, 3];
|
||||
|
||||
it('should use a default `start` of `0` and a default `end` of `length`', () => {
|
||||
const actual = slice(array);
|
||||
expect(actual).toEqual(array);
|
||||
assert.notStrictEqual(actual, array);
|
||||
});
|
||||
|
||||
it('should work with a positive `start`', () => {
|
||||
expect(slice(array, 1), [2).toEqual(3]);
|
||||
expect(slice(array, 1, 3), [2).toEqual(3]);
|
||||
});
|
||||
|
||||
it('should work with a `start` >= `length`', () => {
|
||||
lodashStable.each([3, 4, 2 ** 32, Infinity], (start) => {
|
||||
expect(slice(array, start)).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
it('should treat falsey `start` values as `0`', () => {
|
||||
const expected = lodashStable.map(falsey, lodashStable.constant(array));
|
||||
|
||||
const actual = lodashStable.map(falsey, (start) => slice(array, start));
|
||||
|
||||
expect(actual).toEqual(expected);
|
||||
});
|
||||
|
||||
it('should work with a negative `start`', () => {
|
||||
expect(slice(array, -1)).toEqual([3]);
|
||||
});
|
||||
|
||||
it('should work with a negative `start` <= negative `length`', () => {
|
||||
lodashStable.each([-3, -4, -Infinity], (start) => {
|
||||
expect(slice(array, start)).toEqual(array);
|
||||
});
|
||||
});
|
||||
|
||||
it('should work with `start` >= `end`', () => {
|
||||
lodashStable.each([2, 3], (start) => {
|
||||
expect(slice(array, start, 2)).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
it('should work with a positive `end`', () => {
|
||||
expect(slice(array, 0, 1)).toEqual([1]);
|
||||
});
|
||||
|
||||
it('should work with a `end` >= `length`', () => {
|
||||
lodashStable.each([3, 4, 2 ** 32, Infinity], (end) => {
|
||||
expect(slice(array, 0, end)).toEqual(array);
|
||||
});
|
||||
});
|
||||
|
||||
it('should treat falsey `end` values, except `undefined`, as `0`', () => {
|
||||
const expected = lodashStable.map(falsey, (value) => (value === undefined ? array : []));
|
||||
|
||||
const actual = lodashStable.map(falsey, (end, index) =>
|
||||
index ? slice(array, 0, end) : slice(array, 0),
|
||||
);
|
||||
|
||||
expect(actual).toEqual(expected);
|
||||
});
|
||||
|
||||
it('should work with a negative `end`', () => {
|
||||
expect(slice(array, 0, -1), [1).toEqual(2]);
|
||||
});
|
||||
|
||||
it('should work with a negative `end` <= negative `length`', () => {
|
||||
lodashStable.each([-3, -4, -Infinity], (end) => {
|
||||
expect(slice(array, 0, end)).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
it('should coerce `start` and `end` to integers', () => {
|
||||
const positions = [[0.1, 1.6], ['0', 1], [0, '1'], ['1'], [NaN, 1], [1, NaN]];
|
||||
|
||||
const actual = lodashStable.map(positions, (pos) => slice.apply(_, [array].concat(pos)));
|
||||
|
||||
expect(actual, [[1], [1], [1], [2, 3], [1]).toEqual([]]);
|
||||
});
|
||||
|
||||
it('should work as an iteratee for methods like `_.map`', () => {
|
||||
const array = [[1], [2, 3]];
|
||||
const actual = lodashStable.map(array, slice);
|
||||
|
||||
expect(actual).toEqual(array);
|
||||
assert.notStrictEqual(actual, array);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user