-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
70 lines (62 loc) · 1.8 KB
/
test.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
'use strict';
import test from 'ava';
Array.prototype.findIndex = undefined;
require('./findIndex.js');
const planets = [
{ id: 1, name: 'Mercury' },
{ id: 2, name: 'Venus' },
{ id: 3, name: 'Earth' },
{ id: 4, name: 'Mars' },
{ id: 5, name: 'Jupiter' },
{ id: 6, name: 'Saturn' },
{ id: 7, name: 'Uranus' },
{ id: 8, name: 'Neptune' },
];
test('should find an element in an array', (t) => {
t.deepEqual(
planets.findIndex( (planet) => planet && planet.name === 'Venus'),
1
);
t.deepEqual(
planets.findIndex( (planet) => planet && planet.name === 'Earth'),
2
);
t.deepEqual(
planets.findIndex( (planet) => planet && planet.name === 'Jupiter'),
4
);
t.deepEqual(
planets.findIndex( (planet) => planet && planet.name === 'Neptune'),
7
);
});
function searchPluto(planet) {
return planet && planet.name === 'Pluto';
}
test('should return -1 on not found', (t) => {
t.deepEqual(planets.findIndex(searchPluto), -1);
});
test('should return -1 with different prototypes from Array', (t) => {
t.deepEqual(planets.findIndex.call(Number, searchPluto), -1);
t.deepEqual(planets.findIndex.call(String, searchPluto), -1);
t.deepEqual(planets.findIndex.call(Object, searchPluto), -1);
t.deepEqual(planets.findIndex.call(Function, searchPluto), -1);
});
test('should throws with null as Prototype', (t) => {
const searchIndex = () => {
[].findIndex.call(null)
};
t.throws(searchIndex, TypeError );
});
test('should throws when the param is not a function', (t) => {
const searchIndex = (param) => {
return () => {
[].findIndex(param);
}
};
t.throws(searchIndex(1), TypeError);
t.throws(searchIndex(''), TypeError);
t.throws(searchIndex(), TypeError);
t.throws(searchIndex(null), TypeError);
t.throws(searchIndex({}), TypeError);
});