-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy patharray-functions.test.js
More file actions
83 lines (72 loc) · 2.31 KB
/
array-functions.test.js
File metadata and controls
83 lines (72 loc) · 2.31 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import {head,map,sort,filter,find,findLast,reverse,tail} 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
]);
});
});
//filter should return an array with names of length 3
//["Jon","Bob","Ted","Axe"]
describe("filter", () => {
it("filter 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"
describe("findLast", () => {
it("findLast should find the last name of Axe", () => {
expect(findLast(names)).toEqual("Axe");
});
});
//reverse should return an array with the elements in the opposite order
//["Axe","Saul","Robin","Lilly","Barney","Ted","Bob","Jon"]
describe("reverse", () => {
it("reverse should return an array with the elements in the opposite order", () => {
expect(reverse(names)).toEqual(["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"];
describe("tail", () => {
it("tail should return all elements in an array except the first one", () => {
expect(tail(names)).toEqual(["Bob","Ted","Barney","Lilly","Robin","Saul","Axe"]);
});
});