import assert from 'assert'
import { isFunction } from '../../is'
import pipe from '../../fp/pipeTwo'
import pipeAll from '../../fp/pipe'
describe('pipe', () => {
it('is in correct order', () => {
function x(val) {
return `${val}x`
}
function y(val) {
return `${val}y`
}
function z(val) {
return `${val}z`
}
expect(pipeAll(x, y, z)('w')).toBe('wxyz')
})
it('is a variadic function', () => {
expect(isFunction(pipe)).toBe(true)
expect(isFunction(pipeAll)).toBe(true)
// is a smaller version just 2 args
// expect(pipe.length).toBe(0)
})
it('passes context to functions', () => {
function x(val) {
return this.x * val
}
function y(val) {
return this.y * val
}
function z(val) {
return this.z * val
}
const context = {
a: pipe(
pipe(
x,
y
),
z
),
x: 4,
y: 2,
z: 1,
}
expect(context.a(5)).toBe(40)
})
it.skip('throws if given no arguments', () => {
assert.throws(() => {
pipe()
}, err => err.constructor === Error && err.message === 'pipe requires at least one argument')
})
it('can be applied to one argument, (with 2 fns)', () => {
const f = (a, b, c) => [a, b, c]
const g = pipe(
f,
x => x
)
// expect(g.length).toEqual(3)
expect(g(1, 2, 3)).toEqual([1, 2, 3])
})
})