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
120 lines (110 loc) · 3.82 KB
/
project-4.test.js
File metadata and controls
120 lines (110 loc) · 3.82 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
/* eslint-disable no-undef */
const exercises = require('../src/project-4');
describe('Project 4', () => {
describe('multiplyArguments()', () => {
it('should return the product of all the arguments', () => {
const product = exercises.multiplyArguments(5, 5);
const product2 = exercises.multiplyArguments();
const product3 = exercises.multiplyArguments(3, 3, 3, 3);
const product4 = exercises.multiplyArguments(1);
const product5 = exercises.multiplyArguments(10, 0, 10);
expect(product).toBe(25);
expect(product2).toBe(0);
expect(product3).toBe(81);
expect(product4).toBe(1);
expect(product5).toBe(0);
});
});
describe('invokeCallback(cb)', () => {
it('should invoke the callback that is passed in', () => {
const cb = jest.fn();
exercises.invokeCallback(cb);
expect(cb).toHaveBeenCalled();
});
});
describe('sumArray(cb)', () => {
it('should pass the sum of all array numbers to cb', (done) => {
exercises.sumArray([1, 2, 3, 4, 5], (sum) => {
expect(sum).toBe(15);
done();
});
});
});
describe('forEach(arr, cb)', () => {
it('should pass all array items one by one to cb', () => {
const nums = [];
exercises.forEach([1, 2, 3, 4, 5], (num) => {
nums.push(num);
});
expect(nums).toEqual([1, 2, 3, 4, 5]);
});
});
describe('map(arr, cb)', () => {
it('should return an array of all the processed array elements', () => {
const squares = exercises.map([1, 2, 3, 4, 5], (num) => {
return num * num;
});
expect(squares).toEqual([1, 4, 9, 16, 25]);
});
});
describe('getUserConstructor()', () => {
it('should return a user constructor that correctly builds user objects', () => {
const User = exercises.getUserConstructor();
const user = new User({ username: 'SunJieMing', name: 'Ben', email: '[email protected]', password: 'LS Rocks!' });
expect(user.username).toBe('SunJieMing');
expect(user.name).toBe('Ben');
expect(user.password).toBe('LS Rocks!');
});
});
describe('addPrototypeMethod(Constructor)', () => {
it('should add the method sayHi to the constructor', () => {
function Test() {
this.test = true;
}
exercises.addPrototypeMethod(Test);
const test = new Test();
expect(test.sayHi()).toBe('Hello World!');
});
});
describe('addReverseString(StringPrototype)', () => {
it('should add a reverse string method to the String prototype that returns a reversed version of the string', () => {
exercises.addReverseString();
const str = 'Hello';
expect(str.reverse()).toBe('olleH');
});
});
describe('nFactorial(n)', () => {
it('should return the factorial of n', () => {
expect(exercises.nFactorial(5)).toBe(120);
expect(exercises.nFactorial(15)).toBe(1307674368000);
});
});
describe('cacheFunction(cb)', () => {
it('should return the callback function', () => {
const cb = () => {};
expect(typeof exercises.cacheFunction(cb)).toEqual('function');
});
it('should return the callback functions result when the cached function is invoked', () => {
const cb = (x) => {
return x * 2;
};
const cachedFunction = exercises.cacheFunction(cb);
expect(cachedFunction(5)).toBe(10);
});
it('should cache function results', () => {
const cb = jest.fn();
const cachedFunction = exercises.cacheFunction(cb);
cachedFunction(true);
cachedFunction(true);
cachedFunction(true);
cachedFunction(true);
cachedFunction(true);
cachedFunction(10);
cachedFunction(10);
cachedFunction(10);
cachedFunction(10);
expect(cb).toHaveBeenCalledTimes(2);
});
});
});