/
filter.spec.js
67 lines (59 loc) · 1.37 KB
/
filter.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import {T} from './T'
import {filter} from './filter'
import {filter as filterRamda} from 'ramda'
const sampleObject = {
a: 1,
b: 2,
c: 3,
d: 4,
}
test('happy', () => {
const isEven = n => n % 2 === 0
expect(filter(isEven, [1, 2, 3, 4])).toEqual([2, 4])
expect(
filter(isEven, {
a: 1,
b: 2,
d: 3,
})
).toEqual({b: 2})
})
test('predicate when input is object', () => {
const obj = {
a: 1,
b: 2,
}
const predicate = (val, prop, inputObject) => {
expect(inputObject).toEqual(obj)
expect(typeof prop).toEqual('string')
return val < 2
}
expect(filter(predicate, obj)).toEqual({a: 1})
})
test('with object', () => {
const isEven = n => n % 2 === 0
const result = filter(isEven, sampleObject)
const expectedResult = {
b: 2,
d: 4,
}
expect(result).toEqual(expectedResult)
})
test('bad inputs difference between Ramda and Rambda', () => {
expect(() => filter(T, null)).toThrowWithMessage(
Error,
`Incorrect iterable input`
)
expect(() => filter(T)(undefined)).toThrowWithMessage(
Error,
`Incorrect iterable input`
)
expect(() => filterRamda(T, null)).toThrowWithMessage(
TypeError,
`Cannot read properties of null (reading 'filter')`
)
expect(() => filterRamda(T, undefined)).toThrowWithMessage(
TypeError,
`Cannot read properties of undefined (reading 'filter')`
)
})