-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathBMI.test.js
71 lines (63 loc) · 1.92 KB
/
BMI.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
65
66
67
68
69
70
71
const { faker } = require("@faker-js/faker");
const { calculateBMI, getBMIState, getBMIStateByAge } = require("./BMI");
describe("calculateBMI", () => {
it("calculates the correct BMI", () => {
const people = Array(10)
.fill(0)
.map(() => ({
weight: faker.number.float({ min: 50, max: 100 }),
height: faker.number.int({ min: 120, max: 200 }),
}));
for (const { weight, height } of people) {
expect(calculateBMI(weight, height).toFixed(3)).toBe(
((10000 * weight) / height ** 2).toFixed(3)
);
}
});
});
describe("getBMIState", () => {
it("gets the correct BMI state", () => {
const BMIs = Array(100)
.fill(0)
.map(() => faker.number.float({ min: 16, max: 32 }));
for (const BMI of BMIs) {
expect(getBMIState(BMI)).toBe(
BMI < 18.5
? "underweight"
: BMI < 24.9
? "healthy"
: BMI < 29.9
? "overweight"
: "obese"
);
}
});
});
describe("getBMIStateByAge", () => {
it("calculates the correct BMI based on age", () => {
const people = Array(100)
.fill(0)
.map(() => ({
BMI: faker.number.float({ min: 16, max: 32 }),
age: faker.number.int({ min: 19, max: 100 }),
}));
const ageRanges = [
{ minAge: 19, maxAge: 24, min: 19, max: 24 },
{ minAge: 25, maxAge: 34, min: 20, max: 25 },
{ minAge: 35, maxAge: 44, min: 21, max: 26 },
{ minAge: 45, maxAge: 54, min: 22, max: 27 },
{ minAge: 55, maxAge: 64, min: 23, max: 28 },
{ minAge: 65, maxAge: Infinity, min: 24, max: 29 },
];
for (const { BMI, age } of people) {
const healthyRange = ageRanges.find(
({ minAge, maxAge }) => age >= minAge && age <= maxAge
);
expect(getBMIStateByAge(BMI, age)).toBe(
BMI < healthyRange.min || BMI > healthyRange.max
? "not healthy"
: "healthy"
);
}
});
});