-
Notifications
You must be signed in to change notification settings - Fork 87
/
converge.spec.js
51 lines (45 loc) · 1.28 KB
/
converge.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import {add} from './add'
import {converge} from './converge'
import {multiply} from './multiply'
const f1 = converge(multiply, [a => a + 1, a => a + 10])
const f2 = converge(multiply, [a => a + 1, (a, b) => a + b + 10])
const f3 = converge(multiply, [a => a + 1, (a, b, c) => a + b + c + 10])
test('happy', () => {
expect(f2(6, 7)).toEqual(161)
})
test('passes the results of applying the arguments individually', () => {
const result = converge(multiply)([add(1), add(3)])(2)
expect(result).toEqual(15)
})
test('returns a function with the length of the longest argument', () => {
expect(f1.length).toEqual(1)
expect(f2.length).toEqual(2)
expect(f3.length).toEqual(3)
})
test('passes context to its functions', () => {
const a = function (x) {
return this.f1(x)
}
const b = function (x) {
return this.f2(x)
}
const c = function (x, y) {
return this.f3(x, y)
}
const d = converge(c, [a, b])
const context = {
f1: add(1),
f2: add(2),
f3: add,
}
expect(a.call(context, 1)).toEqual(2)
expect(b.call(context, 1)).toEqual(3)
expect(d.call(context, 1)).toEqual(5)
})
test('works with empty functions list', () => {
const fn = converge(function () {
return arguments.length
}, [])
expect(fn.length).toEqual(0)
expect(fn()).toEqual(0)
})