functional/src/index.test.ts

54 lines
1.5 KiB
TypeScript
Raw Normal View History

2019-09-24 21:39:04 +00:00
import { always, cmp, identity, never, nop, partial } from "./index";
2019-09-16 18:20:42 +00:00
describe("nop", () => {
it("does nothing", () => {
expect(nop()).toBeUndefined();
});
});
describe("identity", () => {
it("returns the argument untouched", () => {
expect(identity(5)).toBe(5);
expect(identity(null)).toBe(null);
});
});
describe("partial", () => {
it("applies systematically fixed configuration", () => {
const func = (conf: { a: number, b: string, c: number }) => `${conf.a}${conf.b}${conf.c}`;
const pfunc = partial({ b: "test" }, func);
expect(pfunc({ a: 5, c: 8 })).toEqual("5test8");
});
it("pass through remaining arguments", () => {
const func = (conf: { a: number, b: string }, c: number) => `${conf.a}${conf.b}${c}`;
const pfunc = partial({ b: "test" }, func);
expect(pfunc({ a: 2 }, 3)).toEqual("2test3");
});
});
describe("cmp", () => {
it("simplifies array sorting", () => {
expect([8, 3, 5].sort(cmp())).toEqual([3, 5, 8]);
expect([8, 3, 5, 8].sort(cmp({ reverse: true }))).toEqual([8, 8, 5, 3]);
expect([-2, 8, -7].sort(cmp({ key: Math.abs }))).toEqual([-2, -7, 8]);
expect(["c", "a", "b"].sort(cmp())).toEqual(["a", "b", "c"]);
});
});
2019-09-24 21:39:04 +00:00
describe("always", () => {
it("returns true", () => {
expect(always()).toBe(true);
expect(always(true)).toBe(true);
expect(always(false)).toBe(true);
});
});
describe("never", () => {
it("returns false", () => {
expect(never()).toBe(false);
expect(never(true)).toBe(false);
expect(never(false)).toBe(false);
});
});