Skip to content

Commit

Permalink
wip: unit test fixes continued
Browse files Browse the repository at this point in the history
  • Loading branch information
jdalton committed Sep 21, 2023
1 parent bd518dd commit a79c5c4
Show file tree
Hide file tree
Showing 14 changed files with 66 additions and 77 deletions.
15 changes: 3 additions & 12 deletions test/ary.spec.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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);
});
});
41 changes: 21 additions & 20 deletions test/cond.spec.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
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', () => {
let args1;
let args2;
const expected = ['a', 'b', 'c'];

const cond = _.cond([
const resultFunc = cond([
[
function () {
args1 || (args1 = slice.call(arguments));
Expand All @@ -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];
Expand All @@ -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);
});
});
4 changes: 2 additions & 2 deletions test/memoize.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
20 changes: 9 additions & 11 deletions test/once.spec.js
Original file line number Diff line number Diff line change
@@ -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();
});
});
10 changes: 5 additions & 5 deletions test/random.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/remove.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('remove', () => {
return isEven(index);
});

assert.deepStrictEqual(argsList, [
expect(argsList).toEqual([
[1, 0, clone],
[2, 1, clone],
[3, 2, clone],
Expand Down
4 changes: 2 additions & 2 deletions test/shuffle.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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],
]);
Expand Down
2 changes: 1 addition & 1 deletion test/slice-and-toArray.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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\``, () => {
Expand Down
14 changes: 7 additions & 7 deletions test/slice.spec.js
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand All @@ -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`', () => {
Expand Down Expand Up @@ -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`', () => {
Expand All @@ -81,14 +81,14 @@ 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`', () => {
const array = [[1], [2, 3]];
const actual = lodashStable.map(array, slice);

expect(actual).toEqual(array);
assert.notStrictEqual(actual, array);
expect(actual).not.toBe(array);
});
});
5 changes: 2 additions & 3 deletions test/some.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions test/sortBy-methods.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`, () => {
Expand All @@ -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)`, () => {
Expand All @@ -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],
Expand Down
10 changes: 5 additions & 5 deletions test/sortBy.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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', () => {
Expand Down Expand Up @@ -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`', () => {
Expand All @@ -91,7 +91,7 @@ describe('sortBy', () => {
],
sortBy,
);
assert.deepStrictEqual(actual, [
expect(actual).toEqual([
[1, 2, 3],
[1, 2, 3],
]);
Expand Down
4 changes: 2 additions & 2 deletions test/startsWith-and-endsWith.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`', () => {
Expand Down

0 comments on commit a79c5c4

Please sign in to comment.