|
| 1 | +const expect = require('chai').expect |
| 2 | +const { curry2, pipe } = require('../functional.es') |
| 3 | + |
| 4 | +describe('[ curry2 ]', function () { |
| 5 | + it('curry2 는 함수를 반환한다.', () => { |
| 6 | + const adder = curry2((a, b) => a + b) |
| 7 | + expect(adder).to.be.a('function') |
| 8 | + }) |
| 9 | + |
| 10 | + it('curry2 를 통해 반환된 함수는 인자가 1개이면 함수를, 1개보다 많으면 해당 함수를 실행한다.', () => { |
| 11 | + const adder = curry2((a, b) => a + b) |
| 12 | + const addWith5 = adder(5) |
| 13 | + |
| 14 | + expect(addWith5).to.be.a('function') |
| 15 | + expect(addWith5(3)).to.eql(8) |
| 16 | + expect(addWith5(5)).to.eql(10) |
| 17 | + }) |
| 18 | + |
| 19 | + it('curry2 에 함수가 아닌 인자를 입력하는 경우 TypeError 가 발생할 수 있다.', () => { |
| 20 | + const unknown = curry2('abc') |
| 21 | + |
| 22 | + expect(unknown).to.be.a('function') |
| 23 | + expect(unknown(1)).to.be.a('function') |
| 24 | + expect(() => unknown(1)(2)).to.throw(TypeError, 'Function.prototype.apply was called on abc, which is a string and not a function') |
| 25 | + unknown(1)(2) |
| 26 | + }) |
| 27 | +}) |
| 28 | + |
| 29 | +describe('[ pipe ]', function () { |
| 30 | + const add1 = a => a + 1 |
| 31 | + const add1P = a => new Promise(resolve => setTimeout(() => resolve(a + 1), 1000)) |
| 32 | + const mul2 = a => a * 2 |
| 33 | + |
| 34 | + it('pipe 는 인자로 받은 함수들을 순차적으로 실행하는 함수를 반환한다.', () => { |
| 35 | + const f = pipe(add1, mul2) |
| 36 | + |
| 37 | + expect(f).to.be.a('function') |
| 38 | + expect(f(1)).to.eql(4) |
| 39 | + expect(f(2)).to.eql(6) |
| 40 | + }) |
| 41 | + |
| 42 | + it('pipe 에 promise 를 반환하는 비동키 함수가 포함되는 경우 pipe 함수는 인자로 들어온 함수를 순차적으로 실행하지만 promise 를 반환한다.', async () => { |
| 43 | + this.timeout(2000) |
| 44 | + |
| 45 | + const fP = pipe(add1P, mul2) |
| 46 | + expect(fP).to.be.a('function') |
| 47 | + |
| 48 | + const p1 = fP(1) |
| 49 | + const p2 = fP(2) |
| 50 | + |
| 51 | + expect(p1).to.be.a('Promise') |
| 52 | + expect(p2).to.be.a('Promise') |
| 53 | + |
| 54 | + await p1.then(result => expect(result).to.eql(4)) |
| 55 | + await p2.then(result => expect(result).to.eql(6)) |
| 56 | + }) |
| 57 | + |
| 58 | + it('pipe 중간에 함수가 아닌 인자가 들어가는 경우 TypeError 가 발생할 수 있다.', () => { |
| 59 | + const f = pipe(add1, mul2, 3) |
| 60 | + |
| 61 | + expect(f).to.be.a('function') |
| 62 | + expect(() => f(1)).to.throw(TypeError, 'f is not a function') |
| 63 | + expect(() => f(2)).to.throw(TypeError, 'f is not a function') |
| 64 | + f(1) |
| 65 | + }) |
| 66 | +}) |
0 commit comments