Skip to content

Commit 7a64436

Browse files
author
Pretty Taruvinga
committed
test: complete Jest tests for isProperFraction and ensure all cases pass
1 parent 9b97ed0 commit 7a64436

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

Sprint-3/1-implement-and-rewrite-tests/implement/2-is-proper-fraction.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,21 @@
1111
// execute the code to ensure all tests pass.
1212

1313
function isProperFraction(numerator, denominator) {
14-
if (denominator === 0) return false;
14+
if (denominator === 0) {
15+
return false; // A fraction with a zero denominator is undefined, so we return false.
16+
}
17+
18+
if (numerator === 0) {
19+
return true; // A fraction with a zero numerator is a proper fraction (0/denominator).
20+
}
21+
if (numerator < 0 && denominator > 0) {
22+
return false;
23+
// Negative numerator, positive denominator
24+
}
1525
return Math.abs(numerator) < Math.abs(denominator);
26+
27+
return numerator < denominator && numerator > 0 && denominator > 0;
1628
}
17-
//
1829
// The line below allows us to load the isProperFraction function into tests in other files.
1930
// This will be useful in the "rewrite tests with jest" step.
2031
module.exports = isProperFraction;

Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/2-is-proper-fraction.test.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,31 @@ const isProperFraction = require("../implement/2-is-proper-fraction");
44

55
// TODO: Write tests in Jest syntax to cover all combinations of positives, negatives, zeros, and other categories.
66

7-
// Special case: numerator is zero
7+
// Special case: denominator is zero
88
test(`should return false when denominator is zero`, () => {
99
expect(isProperFraction(1, 0)).toEqual(false);
1010
});
11+
12+
// Case: numerator is zero
13+
test(`should return true when numerator is zero and denominator is positive`, () => {
14+
expect(isProperFraction(0, 1)).toEqual(true);
15+
});
16+
test("should return true when numerator is smaller than denominator and both are positive", () => {
17+
expect(isProperFraction(1, 2)).toEqual(true);
18+
});
19+
20+
test("should return false when numerator is equal to denominator", () => {
21+
expect(isProperFraction(2, 2)).toEqual(false);
22+
});
23+
24+
test("should return false when numerator is greater than denominator", () => {
25+
expect(isProperFraction(3, 2)).toEqual(false);
26+
});
27+
28+
test("should return false when numerator is negative", () => {
29+
expect(isProperFraction(-1, 2)).toEqual(false);
30+
});
31+
32+
test("should return true when both numerator and denominator are negative and numerator is smaller than denominator", () => {
33+
expect(isProperFraction(-1, -2)).toEqual(true);
34+
});

0 commit comments

Comments
 (0)