Skip to content

Commit b083488

Browse files
committed
added Jest tests
1 parent b8dccdd commit b083488

2 files changed

Lines changed: 67 additions & 2 deletions

File tree

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212

1313
function isProperFraction(numerator, denominator) {
1414
// TODO: Implement this function
15+
if (denominator === 0) {
16+
return false;
17+
}
18+
return Math.abs(numerator) < Math.abs(denominator);
1519
}
1620

1721
// The line below allows us to load the isProperFraction function into tests in other files.

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

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,67 @@ const isProperFraction = require("../implement/2-is-proper-fraction");
55
// TODO: Write tests in Jest syntax to cover all combinations of positives, negatives, zeros, and other categories.
66

77
// Special case: numerator is zero
8-
test(`should return false when denominator is zero`, () => {
9-
expect(isProperFraction(1, 0)).toEqual(false);
8+
// Tests using top-level `test()` calls
9+
10+
test("denominator 0 returns false", () => {
11+
expect(isProperFraction(1, 0)).toBe(false);
12+
});
13+
14+
test("1/2 is a proper fraction", () => {
15+
expect(isProperFraction(1, 2)).toBe(true);
16+
});
17+
18+
test("2/4 is a proper fraction (non-reduced)", () => {
19+
expect(isProperFraction(2, 4)).toBe(true);
20+
});
21+
22+
test("5/4 is not a proper fraction", () => {
23+
expect(isProperFraction(5, 4)).toBe(false);
1024
});
25+
26+
test("2/2 is not a proper fraction (equal numerator and denominator)", () => {
27+
expect(isProperFraction(2, 2)).toBe(false);
28+
});
29+
30+
test("0/5 is proper", () => {
31+
expect(isProperFraction(0, 5)).toBe(true);
32+
});
33+
34+
test("0/-5 is proper (negative denominator)", () => {
35+
expect(isProperFraction(0, -5)).toBe(true);
36+
});
37+
38+
test("negative numerator handled via absolute value", () => {
39+
expect(isProperFraction(-1, 2)).toBe(true);
40+
});
41+
42+
test("negative denominator handled via absolute value", () => {
43+
expect(isProperFraction(1, -2)).toBe(true);
44+
});
45+
46+
test("both negative handled via absolute value", () => {
47+
expect(isProperFraction(-3, -4)).toBe(true);
48+
});
49+
50+
test("-3/3 is not proper (equal abs values)", () => {
51+
expect(isProperFraction(-3, 3)).toBe(false);
52+
});
53+
54+
test("decimal numerator works (0.5/1)", () => {
55+
expect(isProperFraction(0.5, 1)).toBe(true);
56+
});
57+
58+
test("decimal where numerator > denominator (1/0.9999)", () => {
59+
expect(isProperFraction(1, 0.9999)).toBe(false);
60+
});
61+
62+
// Optional: uncomment if implementation is updated to reject non-finite values
63+
// test('NaN inputs return false', () => {
64+
// expect(isProperFraction(NaN, 1)).toBe(false);
65+
// expect(isProperFraction(1, NaN)).toBe(false);
66+
// });
67+
68+
// test('Infinity inputs return false', () => {
69+
// expect(isProperFraction(Infinity, 1)).toBe(false);
70+
// expect(isProperFraction(1, Infinity)).toBe(false);
71+
// });

0 commit comments

Comments
 (0)