mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-01-29 06:27:49 +00:00
wip: unit test fixes continued
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import lodashStable, { ary, curry, rearg } from 'lodash';
|
||||
import { slice, _ } from './utils';
|
||||
import lodashStable from 'lodash';
|
||||
import { slice } from './utils';
|
||||
import ary from '../src/ary';
|
||||
|
||||
describe('ary', () => {
|
||||
function fn(a, b, c) {
|
||||
@@ -72,14 +73,4 @@ describe('ary', () => {
|
||||
|
||||
expect(actual).toEqual(['a', 'b', 'c']);
|
||||
});
|
||||
|
||||
it('should work when combined with other methods that use metadata', () => {
|
||||
const array = ['a', 'b', 'c'];
|
||||
let includes = curry(rearg(ary(_.includes, 2), 1, 0), 2);
|
||||
|
||||
expect(includes('b')(array, 2)).toBe(true);
|
||||
|
||||
includes = _(_.includes).ary(2).rearg(1, 0).curry(2).value();
|
||||
expect(includes('b')(array, 2)).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,17 +1,18 @@
|
||||
import lodashStable from 'lodash';
|
||||
import { _, stubA, stubB, stubC, slice, stubFalse, stubTrue } from './utils';
|
||||
import cond from '../src/cond';
|
||||
import { stubA, stubB, stubC, slice, stubFalse, stubTrue } from './utils';
|
||||
|
||||
describe('cond', () => {
|
||||
it('should create a conditional function', () => {
|
||||
const cond = _.cond([
|
||||
const resultFunc = cond([
|
||||
[lodashStable.matches({ a: 1 }), stubA],
|
||||
[lodashStable.matchesProperty('b', 1), stubB],
|
||||
[lodashStable.property('c'), stubC],
|
||||
]);
|
||||
|
||||
expect(cond({ a: 1, b: 2, c: 3 })).toBe('a');
|
||||
expect(cond({ a: 0, b: 1, c: 2 })).toBe('b');
|
||||
expect(cond({ a: -1, b: 0, c: 1 })).toBe('c');
|
||||
expect(resultFunc({ a: 1, b: 2, c: 3 })).toBe('a');
|
||||
expect(resultFunc({ a: 0, b: 1, c: 2 })).toBe('b');
|
||||
expect(resultFunc({ a: -1, b: 0, c: 1 })).toBe('c');
|
||||
});
|
||||
|
||||
it('should provide arguments to functions', () => {
|
||||
@@ -19,7 +20,7 @@ describe('cond', () => {
|
||||
let args2;
|
||||
const expected = ['a', 'b', 'c'];
|
||||
|
||||
const cond = _.cond([
|
||||
const resultFunc = cond([
|
||||
[
|
||||
function () {
|
||||
args1 || (args1 = slice.call(arguments));
|
||||
@@ -31,39 +32,39 @@ describe('cond', () => {
|
||||
],
|
||||
]);
|
||||
|
||||
cond('a', 'b', 'c');
|
||||
resultFunc('a', 'b', 'c');
|
||||
|
||||
expect(args1).toEqual(expected);
|
||||
expect(args2).toEqual(expected);
|
||||
});
|
||||
|
||||
it('should work with predicate shorthands', () => {
|
||||
const cond = _.cond([
|
||||
const resultFunc = cond([
|
||||
[{ a: 1 }, stubA],
|
||||
[['b', 1], stubB],
|
||||
['c', stubC],
|
||||
]);
|
||||
|
||||
expect(cond({ a: 1, b: 2, c: 3 })).toBe('a');
|
||||
expect(cond({ a: 0, b: 1, c: 2 })).toBe('b');
|
||||
expect(cond({ a: -1, b: 0, c: 1 })).toBe('c');
|
||||
expect(resultFunc({ a: 1, b: 2, c: 3 })).toBe('a');
|
||||
expect(resultFunc({ a: 0, b: 1, c: 2 })).toBe('b');
|
||||
expect(resultFunc({ a: -1, b: 0, c: 1 })).toBe('c');
|
||||
});
|
||||
|
||||
it('should return `undefined` when no condition is met', () => {
|
||||
const cond = _.cond([[stubFalse, stubA]]);
|
||||
expect(cond({ a: 1 })).toBe(undefined);
|
||||
const resultFunc = cond([[stubFalse, stubA]]);
|
||||
expect(resultFunc({ a: 1 })).toBe(undefined);
|
||||
});
|
||||
|
||||
it('should throw a TypeError if `pairs` is not composed of functions', () => {
|
||||
it('should throw a TypeError if `pairs` is not resultFunc of functions', () => {
|
||||
lodashStable.each([false, true], (value) => {
|
||||
assert.throws(() => {
|
||||
_.cond([[stubTrue, value]])();
|
||||
}, TypeError);
|
||||
expect(() => {
|
||||
cond([[stubTrue, value]])();
|
||||
}).toThrowError(TypeError);
|
||||
});
|
||||
});
|
||||
|
||||
it('should use `this` binding of function for `pairs`', () => {
|
||||
const cond = _.cond([
|
||||
const resultFunc = cond([
|
||||
[
|
||||
function (a) {
|
||||
return this[a];
|
||||
@@ -74,7 +75,7 @@ describe('cond', () => {
|
||||
],
|
||||
]);
|
||||
|
||||
const object = { cond: cond, a: 1, b: 2 };
|
||||
expect(object.cond('a', 'b')).toBe(2);
|
||||
const object = { resultFunc, a: 1, b: 2 };
|
||||
expect(object.resultFunc('a', 'b')).toBe(2);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -74,9 +74,9 @@ describe('memoize', () => {
|
||||
});
|
||||
|
||||
it('should throw a TypeError if `resolve` is truthy and not a function', () => {
|
||||
assert.throws(() => {
|
||||
expect(() => {
|
||||
memoize(noop, true);
|
||||
}, TypeError);
|
||||
}).toThrowError(TypeError);
|
||||
});
|
||||
|
||||
it('should not error if `resolver` is nullish', () => {
|
||||
|
||||
@@ -1,35 +1,33 @@
|
||||
import { _ } from './utils';
|
||||
import once from '../once';
|
||||
|
||||
describe('once', () => {
|
||||
it('should invoke `func` once', () => {
|
||||
let count = 0;
|
||||
const once = _.once(() => ++count);
|
||||
const resultFunc = once(() => ++count);
|
||||
|
||||
once();
|
||||
expect(once()).toBe(1);
|
||||
expect(resultFunc()).toBe(1);
|
||||
expect(count).toBe(1);
|
||||
});
|
||||
|
||||
it('should ignore recursive calls', () => {
|
||||
let count = 0;
|
||||
|
||||
var once = _.once(() => {
|
||||
once();
|
||||
var resultFunc = once(() => {
|
||||
resultFunc();
|
||||
return ++count;
|
||||
});
|
||||
|
||||
expect(once()).toBe(1);
|
||||
expect(resultFunc()).toBe(1);
|
||||
expect(count).toBe(1);
|
||||
});
|
||||
|
||||
it('should not throw more than once', () => {
|
||||
const once = _.once(() => {
|
||||
const resultFunc = once(() => {
|
||||
throw new Error();
|
||||
});
|
||||
|
||||
assert.throws(once);
|
||||
|
||||
once();
|
||||
expect(true);
|
||||
expect(resultFunc).toThrow();
|
||||
expect(resultFunc).not.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -8,14 +8,14 @@ describe('random', () => {
|
||||
it('should return `0` or `1` when no arguments are given', () => {
|
||||
const actual = lodashStable.uniq(lodashStable.map(array, () => random())).sort();
|
||||
|
||||
expect(actual, [0).toEqual(1]);
|
||||
expect(actual).toEqual([0, 1]);
|
||||
});
|
||||
|
||||
it('should support a `min` and `max`', () => {
|
||||
const min = 5;
|
||||
const max = 10;
|
||||
|
||||
assert.ok(
|
||||
expect(
|
||||
lodashStable.some(array, () => {
|
||||
const result = random(min, max);
|
||||
return result >= min && result <= max;
|
||||
@@ -27,7 +27,7 @@ describe('random', () => {
|
||||
const min = 0;
|
||||
const max = 5;
|
||||
|
||||
assert.ok(
|
||||
expect(
|
||||
lodashStable.some(array, () => {
|
||||
const result = random(max);
|
||||
return result >= min && result <= max;
|
||||
@@ -49,7 +49,7 @@ describe('random', () => {
|
||||
const min = 2 ** 31;
|
||||
const max = 2 ** 62;
|
||||
|
||||
assert.ok(
|
||||
expect(
|
||||
lodashStable.every(array, () => {
|
||||
const result = random(min, max);
|
||||
return result >= min && result <= max;
|
||||
@@ -62,7 +62,7 @@ describe('random', () => {
|
||||
it('should coerce arguments to finite numbers', () => {
|
||||
const actual = [random(NaN, NaN), random('1', '1'), random(Infinity, Infinity)];
|
||||
|
||||
expect(actual, [0, 1).toEqual(MAX_INTEGER]);
|
||||
expect(actual).toEqual([0, 1, MAX_INTEGER]);
|
||||
});
|
||||
|
||||
it('should support floats', () => {
|
||||
|
||||
@@ -22,7 +22,7 @@ describe('remove', () => {
|
||||
return isEven(index);
|
||||
});
|
||||
|
||||
assert.deepStrictEqual(argsList, [
|
||||
expect(argsList).toEqual([
|
||||
[1, 0, clone],
|
||||
[2, 1, clone],
|
||||
[3, 2, clone],
|
||||
|
||||
@@ -6,7 +6,7 @@ describe('shuffle', () => {
|
||||
const object = { a: 1, b: 2, c: 3 };
|
||||
|
||||
it('should return a new array', () => {
|
||||
assert.notStrictEqual(shuffle(array), array);
|
||||
expect(shuffle(array)).not.toBe(array);
|
||||
});
|
||||
|
||||
it('should contain the same elements after a collection is shuffled', () => {
|
||||
@@ -17,7 +17,7 @@ describe('shuffle', () => {
|
||||
it('should shuffle small collections', () => {
|
||||
const actual = lodashStable.times(1000, () => shuffle([1, 2]));
|
||||
|
||||
assert.deepStrictEqual(lodashStable.sortBy(lodashStable.uniqBy(actual, String), '0'), [
|
||||
expect(lodashStable.sortBy(lodashStable.uniqBy(actual, String), '0')).toEqual([
|
||||
[1, 2],
|
||||
[2, 1],
|
||||
]);
|
||||
|
||||
@@ -26,7 +26,7 @@ describe('slice and toArray', () => {
|
||||
it(`\`_.${methodName}\` should return a shallow clone of arrays`, () => {
|
||||
const actual = func(array);
|
||||
expect(actual).toEqual(array);
|
||||
assert.notStrictEqual(actual, array);
|
||||
expect(actual).not.toBe(array);
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should work with a node list for \`collection\``, () => {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import lodashStable from 'lodash';
|
||||
import { falsey, LARGE_ARRAY_SIZE } from './utils';
|
||||
import { falsey } from './utils';
|
||||
import slice from '../src/slice';
|
||||
|
||||
describe('slice', () => {
|
||||
@@ -8,12 +8,12 @@ describe('slice', () => {
|
||||
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);
|
||||
expect(actual).not.toBe(array);
|
||||
});
|
||||
|
||||
it('should work with a positive `start`', () => {
|
||||
expect(slice(array, 1), [2).toEqual(3]);
|
||||
expect(slice(array, 1, 3), [2).toEqual(3]);
|
||||
expect(slice(array, 1)).toEqual( [2, 3]);
|
||||
expect(slice(array, 1, 3)).toEqual([2, 3]);
|
||||
});
|
||||
|
||||
it('should work with a `start` >= `length`', () => {
|
||||
@@ -67,7 +67,7 @@ describe('slice', () => {
|
||||
});
|
||||
|
||||
it('should work with a negative `end`', () => {
|
||||
expect(slice(array, 0, -1), [1).toEqual(2]);
|
||||
expect(slice(array, 0, -1)).toEqual([1, 2]);
|
||||
});
|
||||
|
||||
it('should work with a negative `end` <= negative `length`', () => {
|
||||
@@ -81,7 +81,7 @@ describe('slice', () => {
|
||||
|
||||
const actual = lodashStable.map(positions, (pos) => slice.apply(_, [array].concat(pos)));
|
||||
|
||||
expect(actual, [[1], [1], [1], [2, 3], [1]).toEqual([]]);
|
||||
expect(actual).toEqual([[1], [1], [1], [2, 3], [1], []]);
|
||||
});
|
||||
|
||||
it('should work as an iteratee for methods like `_.map`', () => {
|
||||
@@ -89,6 +89,6 @@ describe('slice', () => {
|
||||
const actual = lodashStable.map(array, slice);
|
||||
|
||||
expect(actual).toEqual(array);
|
||||
assert.notStrictEqual(actual, array);
|
||||
expect(actual).not.toBe(array);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -23,12 +23,11 @@ describe('some', () => {
|
||||
it('should return `true` as soon as `predicate` returns truthy', () => {
|
||||
let count = 0;
|
||||
|
||||
assert.strictEqual(
|
||||
expect(
|
||||
some([null, true, null], (value) => {
|
||||
count++;
|
||||
return value;
|
||||
}),
|
||||
true,
|
||||
})
|
||||
);
|
||||
|
||||
expect(count).toBe(2);
|
||||
|
||||
@@ -45,7 +45,7 @@ describe('sortBy methods', () => {
|
||||
|
||||
it(`\`_.${methodName}\` should sort multiple properties in ascending order`, () => {
|
||||
const actual = func(objects, ['a', 'b']);
|
||||
expect(actual, [objects[2], objects[0], objects[3]).toEqual(objects[1]]);
|
||||
expect(actual).toEqual([objects[2], objects[0], objects[3], objects[1]]);
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should support iteratees`, () => {
|
||||
@@ -55,7 +55,7 @@ describe('sortBy methods', () => {
|
||||
return object.b;
|
||||
},
|
||||
]);
|
||||
expect(actual, [objects[2], objects[0], objects[3]).toEqual(objects[1]]);
|
||||
expect(actual).toEqual([objects[2], objects[0], objects[3], objects[1]]);
|
||||
});
|
||||
|
||||
it(`\`_.${methodName}\` should perform a stable sort (test in IE > 8 and V8)`, () => {
|
||||
@@ -70,7 +70,7 @@ describe('sortBy methods', () => {
|
||||
var actual = func(objects.concat(null, undefined), ['a', 'b']);
|
||||
} catch (e) {}
|
||||
|
||||
assert.deepStrictEqual(actual, [
|
||||
expect(actual).toEqual([
|
||||
objects[2],
|
||||
objects[0],
|
||||
objects[3],
|
||||
|
||||
@@ -15,7 +15,7 @@ describe('sortBy', () => {
|
||||
'b',
|
||||
);
|
||||
|
||||
expect(actual, [1, 2, 3).toEqual(4]);
|
||||
expect(actual).toEqual([1, 2, 3, 4]);
|
||||
});
|
||||
|
||||
it('should use `_.identity` when `iteratee` is nullish', () => {
|
||||
@@ -32,12 +32,12 @@ describe('sortBy', () => {
|
||||
|
||||
it('should work with `_.property` shorthands', () => {
|
||||
const actual = lodashStable.map(sortBy(objects.concat(undefined), 'b'), 'b');
|
||||
expect(actual, [1, 2, 3, 4).toEqual(undefined]);
|
||||
expect(actual).toEqual([1, 2, 3, 4, undefined]);
|
||||
});
|
||||
|
||||
it('should work with an object for `collection`', () => {
|
||||
const actual = sortBy({ a: 1, b: 2, c: 3 }, Math.sin);
|
||||
expect(actual, [3, 1).toEqual(2]);
|
||||
expect(actual).toEqual([3, 1, 2]);
|
||||
});
|
||||
|
||||
it('should move `NaN`, nullish, and symbol values to the end', () => {
|
||||
@@ -80,7 +80,7 @@ describe('sortBy', () => {
|
||||
return result;
|
||||
});
|
||||
|
||||
expect(actual, [objects[0], objects[2], objects[1]).toEqual(objects[3]]);
|
||||
expect(actual).toEqual([objects[0], objects[2], objects[1], objects[3]]);
|
||||
});
|
||||
|
||||
it('should work as an iteratee for methods like `_.map`', () => {
|
||||
@@ -91,7 +91,7 @@ describe('sortBy', () => {
|
||||
],
|
||||
sortBy,
|
||||
);
|
||||
assert.deepStrictEqual(actual, [
|
||||
expect(actual).toEqual([
|
||||
[1, 2, 3],
|
||||
[1, 2, 3],
|
||||
]);
|
||||
|
||||
@@ -24,8 +24,8 @@ describe('startsWith and endsWith', () => {
|
||||
|
||||
expect(func(string, 'b', Object(position))).toBe(true);
|
||||
expect(
|
||||
func(string, 'b', { toString: lodashStable.constant(String(position)) }),
|
||||
).toBeTruthy();
|
||||
func(string, 'b', { toString: lodashStable.constant(String(position)) })
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it('should return `true` when `target` is an empty string regardless of `position`', () => {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import lodashStable from 'lodash';
|
||||
import { falsey, stubArray, LARGE_ARRAY_SIZE } from './utils';
|
||||
import { falsey, stubArray } from './utils';
|
||||
import tail from '../src/tail';
|
||||
|
||||
describe('tail', () => {
|
||||
@@ -18,7 +18,7 @@ describe('tail', () => {
|
||||
});
|
||||
|
||||
it('should exclude the first element', () => {
|
||||
expect(tail(array), [2).toEqual(3]);
|
||||
expect(tail(array)).toEqual([2, 3]);
|
||||
});
|
||||
|
||||
it('should return an empty when querying empty arrays', () => {
|
||||
@@ -33,7 +33,7 @@ describe('tail', () => {
|
||||
];
|
||||
const actual = lodashStable.map(array, tail);
|
||||
|
||||
assert.deepStrictEqual(actual, [
|
||||
expect(actual).toEqual([
|
||||
[2, 3],
|
||||
[5, 6],
|
||||
[8, 9],
|
||||
|
||||
Reference in New Issue
Block a user