-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest.js
57 lines (48 loc) · 1.42 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
'use strict';
Array.prototype.find = undefined;
import test from 'ava';
require('./find.js');
const animals = [
{ name: 'Eagle' },
{ name: 'Dog' },
{ name: 'Monkey' },
{ name: 'Snake' },
{ name: 'Horse' },
{ name: 'Donkey' },
];
function searchHorse(animal) {
return animal && animal.name === 'Horse';
}
test('should find an element in an array', (t) => {
t.deepEqual(animals.find(searchHorse), { name: 'Horse' });
});
function searchElephant(animal) {
return animal && animal.name === 'Elephant';
}
test('should return undefined on not found', (t) => {
t.deepEqual(animals.find(searchElephant), undefined);
});
test('should return undefined with different prototypes from Array', (t) => {
t.deepEqual(animals.find.call(Number, searchElephant), undefined);
t.deepEqual(animals.find.call(String, searchElephant), undefined);
t.deepEqual(animals.find.call(Object, searchElephant), undefined);
t.deepEqual(animals.find.call(Function, searchElephant), undefined);
});
test('should throws with null as Prototype', (t) => {
const find = () => {
[].find.call(null)
};
t.throws(find, TypeError );
});
test('should throws when the param is not a function', (t) => {
const find = (param) => {
return () => {
[].find(param);
}
};
t.throws(find(1), TypeError);
t.throws(find(''), TypeError);
t.throws(find(), TypeError);
t.throws(find(null), TypeError);
t.throws(find({}), TypeError);
});