/
indexOf.spec.js
103 lines (94 loc) · 2.17 KB
/
indexOf.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import {indexOf} from './indexOf'
import {indexOf as indexOfRamda} from 'ramda'
import {compareCombinations} from './_internals/testUtils'
test('with NaN', () => {
expect(indexOf(NaN, [NaN])).toEqual(0)
})
test('will throw with bad input', () => {
expect(indexOfRamda([], true)).toEqual(-1)
expect(() => indexOf([], true)).toThrow()
})
test('without list of objects - no R.equals', () => {
expect(indexOf(3, [1, 2, 3, 4])).toEqual(2)
expect(indexOf(10)([1, 2, 3, 4])).toEqual(-1)
})
test('list of objects uses R.equals', () => {
const listOfObjects = [{a: 1}, {b: 2}, {c: 3}]
expect(indexOf({c: 4}, listOfObjects)).toBe(-1)
expect(indexOf({c: 3}, listOfObjects)).toBe(2)
})
test('list of arrays uses R.equals', () => {
const listOfLists = [[1], [2, 3], [2, 3, 4], [2, 3], [1], []]
expect(indexOf([], listOfLists)).toBe(5)
expect(indexOf([1], listOfLists)).toBe(0)
expect(indexOf([2, 3, 4], listOfLists)).toBe(2)
expect(indexOf([2, 3, 5], listOfLists)).toBe(-1)
})
test('with string as iterable', () => {
expect(() => indexOf('a', 'abc')).toThrowWithMessage(
Error,
`Cannot read property 'indexOf' of abc`
)
expect(indexOfRamda('a', 'abc')).toBe(0)
})
export const possibleTargets = [
x => x > 2,
/foo/,
'foo',
{a: 1},
true,
3,
null,
/bar/g,
NaN,
undefined,
4,
[],
[[]],
[[1], [2]],
{a: 1},
{a: 2},
Promise.resolve(1),
]
export const possibleIterables = [
[
1,
2,
new Boolean(true),
false,
true,
new String('foo'),
new Number(3),
null,
undefined,
],
[/foo/g, /bar/, /bar/g, NaN],
[1, 2, 3],
[1, [[], []]],
[{a: 3}, {a: 2}, {a: 1}],
{},
null,
undefined,
true,
'foo',
]
describe('brute force', () => {
compareCombinations({
fn: indexOf,
fnRamda: indexOfRamda,
firstInput: possibleTargets,
secondInput: possibleIterables,
callback: errorsCounters => {
expect(errorsCounters).toMatchInlineSnapshot(`
Object {
"ERRORS_MESSAGE_MISMATCH": 0,
"ERRORS_TYPE_MISMATCH": 34,
"RESULTS_MISMATCH": 0,
"SHOULD_NOT_THROW": 51,
"SHOULD_THROW": 0,
"TOTAL_TESTS": 170,
}
`)
},
})
})