wip: unit test fixes continued

This commit is contained in:
jdalton
2023-09-21 07:45:49 -07:00
parent bd518dd906
commit a79c5c434c
14 changed files with 66 additions and 77 deletions

View File

@@ -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);
});
});