forked from JoinCODED/TASK-JS-Functions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.spec.js
112 lines (99 loc) · 3.26 KB
/
functions.spec.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
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
/**************************
*
* THIS IS A TESTING FILE
*
* DO NOT MODIFY THIS FILE
*
***************************/
import {
greet,
isOdd,
oddsSmallerThan,
squareOrDouble,
// ageFromBirthDate,
} from "./functions";
describe("greet(name)", () => {
test("logs 'Hello <name>'", () => {
const spy = jest.spyOn(console, "log");
const names = ["Asis", "Lailz", "Mishmish"];
names.forEach((name) => {
greet(name);
expect(spy).toHaveBeenCalledWith(`Hello ${name}`);
});
spy.mockRestore();
});
});
describe("isOdd(n)", () => {
test("returns true if the number is odd", () => {
const numbers = [1, 9, 3456087];
numbers.forEach((n) => expect(isOdd(n)).toBe(true));
});
test("returns false if the number is even", () => {
const numbers = [2, 10, 3456088];
numbers.forEach((n) => expect(isOdd(n)).toBe(false));
});
});
describe("oddsSmallerThan(n)", () => {
test("returns the correct number of odds", () => {
const numbers = [0, 1, 9, 10, 345, 448758328540529];
const results = [0, 0, 4, 5, 172, 224379164270264];
numbers.forEach((n, i) => expect(oddsSmallerThan(n)).toBe(results[i]));
});
});
describe("squareOrDouble(n)", () => {
test("squares odd numbers", () => {
const numbers = [1, 9, 99];
const results = [1, 81, 9801];
numbers.forEach((n, i) => expect(squareOrDouble(n)).toBe(results[i]));
});
test("doubles even numbers", () => {
const numbers = [2, 10, 100];
const results = [4, 20, 200];
numbers.forEach((n, i) => expect(squareOrDouble(n)).toBe(results[i]));
});
});
// describe("ageFromBirthDate(birthDate)", () => {
// const _MS_PER_YEAR = 1000 * 60 * 60 * 24 * 365;
// test("returns the correct age", () => {
// const today = new Date();
// let month = today.getMonth() - 2;
// month = month < 1 ? 12 : month;
// month = month < 10 ? `0${month}` : month;
// const birthDates = [`1987${month}05`, `1985${month}27`, `1998${month}05`];
// const dates = [`06/${month}/1987`, `08/${month}/1985`, `06/${month}/1998`];
// const results = dates.map(date => {
// const dob = new Date(date);
// return Math.floor((today - dob) / _MS_PER_YEAR);
// });
// birthDates.forEach((birthDate, i) =>
// expect(ageFromBirthDate(birthDate)).toBe(results[i])
// );
// });
// test("rounds down to the nearest year", () => {
// const today = new Date();
// let currentMonth = today.getMonth() + 2;
// let yearOffset = 0;
// currentMonth = currentMonth < 10 ? `0${currentMonth}` : currentMonth;
// if (currentMonth > 12) {
// currentMonth = 1;
// yearOffset = 1;
// }
// const birthDates = [
// `${1987 + yearOffset}${currentMonth}05`,
// `${1985 + yearOffset}${currentMonth}05`,
// `${1998 + yearOffset}${currentMonth}05`
// ];
// const dates = [
// `${currentMonth}/05/${1987 + yearOffset}`,
// `${currentMonth}/05/${1985 + yearOffset}`,
// `${currentMonth}/05/${1998 + yearOffset}`
// ];
// const results = dates.map(date => {
// const dob = new Date(date);
// return Math.floor((today - dob) / _MS_PER_YEAR);
// });
// birthDates.forEach((birthDate, i) =>
// expect(ageFromBirthDate(birthDate)).toBe(results[i])
// );
// });
// });