forked from bloominstituteoftechnology/Basic-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject-4.test.js
More file actions
133 lines (126 loc) · 3.76 KB
/
project-4.test.js
File metadata and controls
133 lines (126 loc) · 3.76 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/* eslint-disable no-undef */
const {
getFirstItem,
getLength,
getLastItem,
sumNums,
multiplyNums,
contains,
removeDuplicates
} = require('../src/project-4');
describe('Project 4', () => {
describe('getFirstItem', () => {
it('should pass the first item from the collection to the cb', () => {
const collection = ['a', 'b', 'c', 'd'];
let firstItem;
getFirstItem(collection, (first) => {
firstItem = first;
});
expect(firstItem).toBe('a');
});
});
describe('getLength', () => {
it('should pass the length of the collection to the cb', () => {
const collection = [true, false, {}, []];
let collectionLength;
getLength(collection, (length) => {
collectionLength = length;
});
expect(collectionLength).toBe(4);
});
});
describe('getLastItem', () => {
it('should pass the last item from an array into the provided cb', () => {
const collection1 = [1, 2, 3];
const collection2 = ['a', 'b'];
const collection3 = [true, false, true, null];
const lastItems = [];
getLastItem(collection1, (lastItem) => {
lastItems.push(lastItem);
});
getLastItem(collection2, (lastItem) => {
lastItems.push(lastItem);
});
getLastItem(collection3, (lastItem) => {
lastItems.push(lastItem);
});
expect(lastItems).toEqual([3, 'b', null]);
});
});
describe('sumNums', () => {
it('should sum the numbers together and pass the sum to the cb', () => {
let sum;
sumNums(5, 10, (result) => {
sum = result;
});
expect(sum).toBe(15);
sumNums(-5, 5, (result) => {
sum = result;
});
expect(sum).toBe(0);
});
});
describe('multiplyNums', () => {
it('should multiply the numbers together and pass the product to the cb', () => {
let product;
multiplyNums(5, 10, (result) => {
product = result;
});
expect(product).toBe(50);
multiplyNums(-5, 5, (result) => {
product = result;
});
expect(product).toBe(-25);
});
});
describe('contains', () => {
it('should pass true to cb is the collection contains the specified item', () => {
const collection = ['a', 'b', 'c', 'd'];
let containsItem;
contains(collection, 'd', (result) => {
containsItem = result;
});
expect(containsItem).toBe(true);
});
it('should return false if the item is not contained in the array', () => {
const collection = ['a', 'b', 'c', 'd'];
let containsItem;
contains(collection, 55, (result) => {
containsItem = result;
});
expect(containsItem).toBe(false);
});
it('should work with array references', () => {
const nestedArray = [];
const collection = ['a', 'b', 'c', 'd', nestedArray];
let containsItem;
contains(collection, nestedArray, (result) => {
containsItem = result;
});
expect(containsItem).toBe(true);
contains(collection, [], (result) => {
containsItem = result;
});
expect(containsItem).toBe(false);
});
});
describe('removeDuplicates', () => {
it('should remove duplicates from an array', () => {
const arr = ['a', 'b', 'c', 'c'];
let duplicateFreeArray;
removeDuplicates(arr, (result) => {
duplicateFreeArray = result;
});
expect(duplicateFreeArray).toEqual(['a', 'b', 'c']);
});
it('should not mutate the original array', () => {
const arr = ['a', 'b', 'c', 'c'];
let duplicateFreeArray;
removeDuplicates(arr, (result) => {
duplicateFreeArray = result;
});
expect(Array.isArray(duplicateFreeArray)).toBe(true);
expect(duplicateFreeArray).not.toBe(arr);
});
});
});