Skip to content

Commit df94ea4

Browse files
written jest test scripts for all the three functions
1 parent 0ef19ec commit df94ea4

5 files changed

Lines changed: 189 additions & 5 deletions

File tree

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

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,16 @@
1111
// execute the code to ensure all tests pass.
1212

1313
function isProperFraction(numerator, denominator) {
14-
// TODO: Implement this function
14+
if (numerator === 0) {
15+
return false;
16+
}
17+
if (denominator === 0) {
18+
return false;
19+
}
20+
if (Number.isNaN(numerator) || Number.isNaN(denominator)) {
21+
return false;
22+
}
23+
return Math.abs(numerator) < Math.abs(denominator);
1524
}
1625

1726
// The line below allows us to load the isProperFraction function into tests in other files.
@@ -25,6 +34,25 @@ function assertEquals(actualOutput, targetOutput) {
2534
`Expected ${actualOutput} to equal ${targetOutput}`
2635
);
2736
}
37+
// Proper fractions
38+
assertEquals(isProperFraction(1, 2), true);
39+
assertEquals(isProperFraction(3, 4), true);
40+
assertEquals(isProperFraction(-1, 5), true);
41+
assertEquals(isProperFraction(1, -5), true);
42+
43+
// Improper fractions
44+
assertEquals(isProperFraction(5, 3), false);
45+
assertEquals(isProperFraction(10, 10), false);
46+
assertEquals(isProperFraction(-7, 3), false);
47+
48+
// Zero cases
49+
assertEquals(isProperFraction(0, 5), false);
50+
assertEquals(isProperFraction(3, 0), false);
51+
52+
// NaN cases
53+
assertEquals(isProperFraction(NaN, 5), false);
54+
assertEquals(isProperFraction(3, NaN), false);
55+
assertEquals(isProperFraction(NaN, NaN), false);
2856

2957
// TODO: Write tests to cover all cases.
3058
// What combinations of numerators and denominators should you test?

Sprint-3/1-implement-and-rewrite-tests/implement/3-get-card-value.js

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,34 @@
2222
// execute the code to ensure all tests pass.
2323

2424
function getCardValue(card) {
25-
// TODO: Implement this function
25+
const ranks = [
26+
"A",
27+
"2",
28+
"3",
29+
"4",
30+
"5",
31+
"6",
32+
"7",
33+
"8",
34+
"9",
35+
"10",
36+
"J",
37+
"Q",
38+
"K",
39+
];
40+
const suits = ["♠", "♥", "♦", "♣"];
41+
42+
let rank = card.slice(0, -1); // extracting the rank from the input
43+
let suit = card.slice(-1); // extracting the suit from the input
44+
if (!suits.includes(suit)) {
45+
throw new Error("Invalid suit");
46+
}
47+
if (!ranks.includes(rank)) {
48+
throw new Error("Invalid rank");
49+
}
50+
if (rank === "A") return 11;
51+
if (["J", "Q", "K"].includes(rank)) return 10;
52+
return Number(rank);
2653
}
2754

2855
// The line below allows us to load the getCardValue function into tests in other files.
@@ -39,7 +66,19 @@ function assertEquals(actualOutput, targetOutput) {
3966

4067
// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
4168
// Examples:
42-
assertEquals(getCardValue("9♠"), 9);
69+
assertEquals(getCardValue("J♥"), 10);
70+
// Number cards
71+
assertEquals(getCardValue("2♠"), 2);
72+
assertEquals(getCardValue("9♥"), 9);
73+
assertEquals(getCardValue("10♦"), 10);
74+
75+
// Face cards
76+
assertEquals(getCardValue("J♣"), 10);
77+
assertEquals(getCardValue("Q♦"), 10);
78+
assertEquals(getCardValue("K♥"), 10);
79+
80+
// Ace
81+
assertEquals(getCardValue("A♠"), 11);
4382

4483
// Handling invalid cards
4584
try {
@@ -52,3 +91,30 @@ try {
5291
}
5392

5493
// What other invalid card cases can you think of?
94+
try {
95+
getCardValue("");
96+
console.error("Error NOT thrown for empty string");
97+
} catch (e) {
98+
console.log("Error thrown for empty string 🎉");
99+
}
100+
101+
try {
102+
getCardValue("1♠");
103+
console.error("Error NOT thrown for invalid rank");
104+
} catch (e) {
105+
console.log("Error thrown for invalid rank 🎉");
106+
}
107+
108+
try {
109+
getCardValue("A");
110+
console.error("Error NOT thrown for missing suit");
111+
} catch (e) {
112+
console.log("Error thrown for missing suit 🎉");
113+
}
114+
115+
try {
116+
getCardValue("A?");
117+
console.error("Error NOT thrown for invalid suit");
118+
} catch (e) {
119+
console.log("Error thrown for invalid suit 🎉");
120+
}

Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/1-get-angle-type.test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,33 @@ test(`should return "Acute angle" when (0 < angle < 90)`, () => {
1414
});
1515

1616
// Case 2: Right angle
17+
test(`should return "Right angle" when angle === 90`, () => {
18+
expect(getAngleType(90)).toEqual("Right angle");
19+
});
20+
1721
// Case 3: Obtuse angles
22+
test(`should return "Obtuse angle" when (90 < angle < 180)`, () => {
23+
expect(getAngleType(91)).toEqual("Obtuse angle");
24+
expect(getAngleType(120)).toEqual("Obtuse angle");
25+
expect(getAngleType(179)).toEqual("Obtuse angle");
26+
});
27+
1828
// Case 4: Straight angle
29+
test(`should return "Straight angle" when angle === 180`, () => {
30+
expect(getAngleType(180)).toEqual("Straight angle");
31+
});
32+
1933
// Case 5: Reflex angles
34+
test(`should return "Reflex angle" when (180 < angle < 360)`, () => {
35+
expect(getAngleType(181)).toEqual("Reflex angle");
36+
expect(getAngleType(250)).toEqual("Reflex angle");
37+
expect(getAngleType(359)).toEqual("Reflex angle");
38+
});
39+
2040
// Case 6: Invalid angles
41+
test(`should return "Invalid angle" for angles outside 0–360`, () => {
42+
expect(getAngleType(0)).toEqual("Invalid angle"); // boundary
43+
expect(getAngleType(360)).toEqual("Invalid angle"); // boundary
44+
expect(getAngleType(-10)).toEqual("Invalid angle");
45+
expect(getAngleType(400)).toEqual("Invalid angle");
46+
});

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

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,40 @@ 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+
//Special case: numerator is zero
13+
test(`should return false when numerator is zero`, () => {
14+
expect(isProperFraction(0, 1)).toEqual(false);
15+
});
16+
// Proper functions with absolute numerator < absolute denominator
17+
test(`should return true for proper positive fractions`, () => {
18+
expect(isProperFraction(1, 2)).toEqual(true);
19+
expect(isProperFraction(3, 4)).toEqual(true);
20+
});
21+
// Proper functions with negative numbers
22+
test(`should return true for proper negative fractions`, () => {
23+
expect(isProperFraction(-1, 2)).toEqual(true);
24+
expect(isProperFraction(1, -3)).toEqual(true);
25+
expect(isProperFraction(-2, -5)).toEqual(true);
26+
});
27+
// Improper functions with absolute numerator >= absolute denominator
28+
test(`should return false for improper fractions`, () => {
29+
expect(isProperFraction(5, 3)).toEqual(false);
30+
expect(isProperFraction(10, 10)).toEqual(false);
31+
expect(isProperFraction(-7, 3)).toEqual(false);
32+
});
33+
// Special cases: numerator or denominator is NAN
34+
test(`should return false when numerator or denominator is NaN`, () => {
35+
expect(isProperFraction(NaN, 5)).toEqual(false);
36+
expect(isProperFraction(3, NaN)).toEqual(false);
37+
expect(isProperFraction(NaN, NaN)).toEqual(false);
38+
});
39+
// Special cases: non-number strings
40+
test(`should return false for non-numeric strings`, () => {
41+
expect(isProperFraction("a", 5)).toEqual(false);
42+
expect(isProperFraction(3, "b")).toEqual(false);
43+
});

Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/3-get-card-value.test.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,38 @@ test(`Should return 11 when given an ace card`, () => {
99
expect(getCardValue("A♠")).toEqual(11);
1010
});
1111

12+
// Case 2: Number Cards (2-10)
13+
test(`Should return correct values for number cards`, () => {
14+
expect(getCardValue("2♠")).toEqual(2);
15+
expect(getCardValue("7♥")).toEqual(7);
16+
expect(getCardValue("9♦")).toEqual(9);
17+
expect(getCardValue("10♣")).toEqual(10);
18+
});
19+
20+
// Case 3: Face Cards (J, Q, K)
21+
test(`Should return 10 for face cards J, Q, K`, () => {
22+
expect(getCardValue("J♠")).toEqual(10);
23+
expect(getCardValue("Q♥")).toEqual(10);
24+
expect(getCardValue("K♦")).toEqual(10);
25+
});
26+
27+
// Case 4: Invalid cards
28+
test(`Should throw an error for invalid rank`, () => {
29+
expect(() => getCardValue("1♠")).toThrowError("Invalid rank");
30+
});
31+
32+
test(`Should throw an error for invalid suit`, () => {
33+
expect(() => getCardValue("A?")).toThrowError("Invalid suit");
34+
});
35+
36+
test(`Should throw an error for missing suit`, () => {
37+
expect(() => getCardValue("A")).toThrowError();
38+
});
39+
40+
test(`Should throw an error for empty string`, () => {
41+
expect(() => getCardValue("")).toThrowError();
42+
});
43+
1244
// Suggestion: Group the remaining test data into these categories:
1345
// Number Cards (2-10)
1446
// Face Cards (J, Q, K)
@@ -17,4 +49,3 @@ test(`Should return 11 when given an ace card`, () => {
1749
// To learn how to test whether a function throws an error as expected in Jest,
1850
// please refer to the Jest documentation:
1951
// https://jestjs.io/docs/expect#tothrowerror
20-

0 commit comments

Comments
 (0)