UNPKG

2.08 kBJavaScriptView Raw
1/* global it describe */
2const assert = require('assert');
3const { compose, pipe, curry } = require('../fp');
4
5const incrementBy = value => data => value + data;
6const foldBy = value => data => value * data;
7const rest = value => data => Promise.resolve(data - value);
8
9describe('FP', function() {
10 describe('#compose()', function() {
11 it('should return a composed function of right to left', function() {
12 let calc = compose(
13 foldBy(2),
14 incrementBy(1)
15 );
16 assert.strictEqual(calc(1), 4);
17 });
18 it('should return a composed function that works with promises', async function() {
19 let calc = compose(
20 foldBy(2),
21 rest(1),
22 incrementBy(2)
23 );
24 assert.strictEqual(await calc(1), 4);
25 });
26 });
27 describe('#pipe()', function() {
28 it('should return a composed function from left to right', function() {
29 let calc = pipe(
30 foldBy(2),
31 incrementBy(1)
32 );
33 assert.strictEqual(calc(1), 3);
34 });
35 it('should return a composed function that works with promises', async function() {
36 let calc = pipe(
37 foldBy(2),
38 rest(1),
39 incrementBy(2)
40 );
41 assert.strictEqual(await calc(1), 3);
42 });
43 });
44 describe('#curry()', function() {
45 it('should return work with all parameters and with closures', function() {
46 let calc = curry((a, b) => a + b);
47 assert.strictEqual(calc(1, 1), calc(1)(1));
48 });
49 it('should return with partial application should return a function', function() {
50 let calc = curry((a, b) => a + b);
51 assert.strictEqual(typeof calc(1), 'function');
52 });
53 it('should return an error if no function passed', function() {
54 assert.throws(() => {
55 curry();
56 }, /Error:[^[]+\[undefined\].+$/);
57 assert.throws(() => {
58 curry(33);
59 }, /Error:[^[]+\[number\].+$/);
60 assert.throws(() => {
61 curry('');
62 }, /Error:[^[]+\[string\].+$/);
63 assert.throws(() => {
64 curry(null);
65 }, /Error:[^[]+\[object\].+$/);
66 });
67 });
68});