This repository was archived by the owner on May 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassignment.test.js
More file actions
202 lines (176 loc) · 5.56 KB
/
Copy pathassignment.test.js
File metadata and controls
202 lines (176 loc) · 5.56 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import {
Human,
fibonacci,
randomInt,
} from './helper';
import {
first,
second,
third,
fourth,
fifth,
sixth,
seventh,
eighth,
ninth,
tenth,
bind,
} from './assignment';
describe('assignments', () => {
test('first', () => {
expect(first).toBeInstanceOf(Function);
const data = [...new Array(10)]
.map(() => {
const list = [randomInt(100), randomInt(100)];
return [
list,
first(...list),
];
});
expect(data.every((list) => list[1] instanceof Function)).toBe(true);
expect(data.every((list) => {
const anotherList = [randomInt(100), randomInt(100)];
return list[1](...anotherList)
=== (anotherList[0] + anotherList[1]) - list[0][0] * list[0][1];
})).toBe(true);
});
test('second', () => {
expect(second).toBeInstanceOf(Function);
const list = second();
expect(list).toBeInstanceOf(Array);
expect(list.length).toBe(5);
expect(list.every((func) => func instanceof Function)).toBe(true);
[...new Array(10)].forEach(() => {
const arg = randomInt(100);
expect(list.every((func, ix) => func(arg) === arg + ix * 2)).toBe(true);
});
});
test('third', () => {
expect(third).toBeInstanceOf(Function);
[...new Array(10)].forEach(() => {
const prefix = 'Name';
const list = [randomInt(100), randomInt(100)];
const people = list.map((num) => new Human(`${prefix}${num}`));
people[0].sayName = function sayName() {
return this.name;
};
third(...people);
expect(people[1].sayName).toBeInstanceOf(Function);
expect(people[1].sayName()).toBe(`${prefix}${list[1]}`);
});
});
test('fourth', () => {
expect(fourth).toBeInstanceOf(Function);
[...new Array(10)].forEach(() => {
const prefix = 'Name';
const list = [randomInt(100), randomInt(100)];
const people = list.map((num) => new Human(`${prefix}${num}`));
people[0].sayName = function sayName() {
return this.name;
};
fourth(...people);
expect(people[1].sayName).toBeInstanceOf(Function);
expect(people[1].sayName()).toBe(`${prefix}${list[0]}`);
});
});
test('fifth', () => {
expect(fifth).toBeInstanceOf(Function);
const object = {};
const keyList = [...new Array(10)].map(() => String.fromCharCode('a'.charCodeAt(0) + randomInt(26)));
keyList.forEach((key, ix) => {
object[key] = ix;
});
expect(keyList.every((key) => Object.prototype.hasOwnProperty.call(object, key))).toBe(true);
const returnedObject = fifth(object);
expect(returnedObject).toBe(object);
// eslint-disable-next-line max-len
expect(keyList.every((key) => Object.prototype.hasOwnProperty.call(returnedObject, key.toUpperCase()) && object[key] === object[key.toUpperCase()])).toBe(true);
});
test('sixth', () => {
expect(sixth).toBeInstanceOf(Function);
const Square = sixth();
expect(Square).toBeInstanceOf(Function);
[...new Array(10)].forEach(() => {
const side = randomInt(100);
const square = new Square(side);
expect(square).toBeInstanceOf(Square);
expect(square.getPerimeter).toBeInstanceOf(Function);
expect(square.getSquare).toBeInstanceOf(Function);
expect(square.getPerimeter()).toEqual(side * 4);
expect(square.getSquare()).toEqual(side * side);
});
});
test('seventh', () => {
const testsCount = 20;
[...new Array(testsCount)].forEach(() => {
const n = randomInt(testsCount);
expect(seventh(n)).toEqual(fibonacci(n));
});
});
test('eighth', () => {
expect(eighth).toBeInstanceOf(Function);
expect(
[...new Array(10)]
.map(() => [...new Array(randomInt(10))].map(() => randomInt(100)))
.every((list) => eighth(...list) === list.reduce((sum, value) => sum + value, 0)),
).toBe(true);
});
test('ninth', () => {
expect(ninth).toBeInstanceOf(Function);
expect(
[...new Array(10)]
.map(() => [...new Array(randomInt(100))].map(() => randomInt(100)))
.every((list) => ninth(...list) === list.reduce((sum, value) => sum + value, 0)),
).toBe(true);
});
test('tenth', () => {
expect(tenth).toBeInstanceOf(Function);
const mockedTenth = jest.fn(tenth);
const mockedCallback = jest.fn();
[...new Array(1000)].forEach(() => mockedTenth(randomInt(1000), randomInt(15), mockedCallback));
expect(
mockedTenth.mock.calls
.every(([a, b], ix) => (
b === 0
? mockedCallback.mock.calls[ix][0] === 'ОШИБКА'
: mockedCallback.mock.calls[ix][0] == null && mockedCallback.mock.calls[ix][1] === a % b
)),
).toBe(true);
});
});
describe('bind', () => {
test('bind is a function', () => {
expect(bind).toBeInstanceOf(Function);
});
test('bind saves context', () => {
const ctx = {};
function foo() {
return this;
}
const bindedFoo = bind(foo, ctx);
expect(bindedFoo()).toBe(ctx);
});
test('double bind saves context', () => {
const ctx = {};
const ctx2 = {};
function foo() {
return this;
}
const bindedFoo = bind(bind(foo, ctx), ctx2);
expect(bindedFoo()).toBe(ctx);
});
test('a binded function consumes parameters', () => {
function foo(...arr) {
return arr;
}
const bindedFoo = bind(foo, {});
expect(bindedFoo(1, 2, 3)).toEqual([1, 2, 3]);
});
test('a binded function receives initialArgs', () => {
function foo(...arr) {
return arr;
}
const bindedFoo = bind(foo, {}, 1, 2, 3);
expect(bindedFoo(4, 5)).toEqual([1, 2, 3, 4, 5]);
});
});