Skip to content

Commit

Permalink
issue-#61 - Added tests for 'bind'/'' methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
elycruz committed Sep 2, 2022
1 parent f58e3d9 commit 37c7c2a
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion packages/fjl/tests/function/index_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {noop} from "../../src/function/noop";
import {toFunction} from "../../src/function/toFunction";
import {trampoline} from "../../src/function/trampoline";
import {until} from "../../src/function/until";
import {$bind, bind} from "../../src/function/bind";

describe('#compose', () => {
it('should be of type function.', () => {
Expand Down Expand Up @@ -124,7 +125,7 @@ describe('#curryN', () => {

describe('#curry', () => {

it('should be of type function.', () => {
it('should be of type function.', () => {
expect(curry).toBeInstanceOf(Function);
});

Expand Down Expand Up @@ -497,3 +498,29 @@ describe('#until', function () {
);
});
});

describe('#bind, #$bind', () => {
type Bind = typeof bind;
type ArgAfter = any; // Argument to pass to resulting function

const ternaryAdd = (a, b, c) => a + b + c,
plus = (a, b) => a + b;

(<[Parameters<Bind>, ArgAfter, ReturnType<Bind>][]>[
[[ternaryAdd, 'a', 'b'], 'c', c => 'ab' + c],
[[plus, 1], 1, b => b + 1],
[[x => x], 99, x => x],
])
.forEach(([args, argAfter, expected], i) => {
it(`Iter: ${i}; bind(${args.map((f, i2) => !i2 ? f.name : JSON.stringify(f)).join(', ')})` +
`(${JSON.stringify(argAfter)}) === ` +
`${JSON.stringify(expected(...args.slice(1).concat([argAfter])))} && (\`$bind\` variant)`, function () {
const rslt = bind(...args);
const rslt2 = $bind(args[0])(...args.slice(1)); // Should always return a function
expect(rslt).toBeInstanceOf(Function);
expect(rslt).toHaveLength(expected.length); // Assert arity
expect(rslt(argAfter)).toEqual(expected(argAfter)); // Assert result function 'call' result
expect(rslt2(argAfter)).toEqual(expected(argAfter));
});
});
});

0 comments on commit 37c7c2a

Please sign in to comment.