-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy patharrowFunctions.test.js
64 lines (50 loc) · 1.63 KB
/
arrowFunctions.test.js
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
const { faker } = require("@faker-js/faker");
const { greet, sum, square, squares } = require("./arrowFunctions"); // Replace with the correct file name
function isArrowFunction(fn) {
return /([a-zA-Z]\w*|\([a-zA-Z]\w*(,\s*[a-zA-Z]\w*)*\)) =>/.test(
fn.toString()
);
}
describe("greet", () => {
it("should return 'Hello <name>' when name is provided", () => {
const name = faker.person.firstName();
expect(greet(name)).toBe(`Hello ${name}`);
});
it("should be an arrow function", () => {
expect(isArrowFunction(greet)).toBe(true);
});
});
describe("sum", () => {
it("should return the correct sum", () => {
const numbers = Array(10)
.fill(0)
.map(() => [faker.number.int(), faker.number.int()]);
numbers.forEach(([a, b]) => expect(sum(a, b)).toBe(a + b));
});
it("should be an arrow function", () => {
expect(isArrowFunction(sum)).toBe(true);
});
});
describe("square", () => {
it("should return the square of a number", () => {
const numbers = Array(10)
.fill(0)
.map(() => faker.number.int({ max: 100 }));
numbers.forEach((n) => expect(square(n)).toBe(n * n));
});
it("should be an arrow function", () => {
expect(isArrowFunction(square)).toBe(true);
});
});
describe("🌶️🌶️ squares", () => {
it("should return an array of squares", () => {
const numbers = Array(10)
.fill(0)
.map(() => faker.number.int({ max: 100 }));
const results = squares(numbers);
results.forEach((n, i) => expect(n).toBe(numbers[i] ** 2));
});
it("should be an arrow function", () => {
expect(isArrowFunction(squares)).toBe(true);
});
});