-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy patharray-functions.test.js
More file actions
63 lines (55 loc) · 1.66 KB
/
array-functions.test.js
File metadata and controls
63 lines (55 loc) · 1.66 KB
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
import {map,filter,find,findLast,head,sort} from "../services/array-functions";
const names = ["Jon","Bob","Ted","Barney","Lilly","Robin","Saul","Axe"];
const myNumbers = [4,3,55,22,99,1913,7,5,4,2,1];
function addHello(name){
return "Hello " + name;
}
function findThree(name){
return name.length === 3;
}
function findBarney(name){
return name === "Barney";
}
//head should find the first element in the array "Jon"
describe("head", () => {
it("should return the first element of an array 'Jon'", () => {
expect(head(names)).toEqual("Jon");
});
});
describe("map", () => {
it("should prepend Hello to each name", () => {
expect(map(names,addHello)).toEqual([
"Hello Jon",
"Hello Bob",
"Hello Ted",
"Hello Barney",
"Hello Lilly",
"Hello Robin",
"Hello Saul",
"Hello Axe"
]);
});
});
describe("sort", () => {
it("should return an array with numbers in order", () => {
expect(sort(myNumbers)).toEqual([
1,2,3,4,4,5,7,22,55,99,1913
]);
});
});
describe("filter", () => {
it("should return an array with names of length 3", () => {
expect(filter(names, findThree)).toEqual(["Jon","Bob","Ted","Axe"]);
});
});
//find should find one name of "Barney"
describe("find", () => {
it("find should find one name of 'Barney'", () => {
expect(find(names, findBarney)).toEqual("Barney");
});
});
//findLast should find the last name of "Axe"
//reverse should return an array with the elements in the opposite order
//["Axe","Saul","Robin","Lilly","Barney","Ted","Bob","Jon"]
//tail should return all elements in an array except the first one
//[Bob","Ted","Barney","Lilly","Robin","Saul","Axe"];