Skip to content

Commit 88b62a4

Browse files
committed
added Jest tests to rewrite-tests-with-jest exercises 1-3
1 parent e836149 commit 88b62a4

6 files changed

Lines changed: 113 additions & 68 deletions

File tree

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

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,13 @@ function assertEquals(actualOutput, targetOutput) {
4646

4747
// TODO: Write tests to cover all cases, including boundary and invalid cases.
4848
// Example: Identify Right Angles
49-
const right = getAngleType(90);
50-
assertEquals(right, "Right angle");
5149

52-
const obtuse = getAngleType(120);
53-
assertEquals(obtuse, "Obtuse angle");
54-
55-
const straight = getAngleType(180);
56-
assertEquals(straight, "Straight angle");
57-
58-
const reflex = getAngleType(270);
59-
assertEquals(reflex, "Reflex angle");
60-
61-
const invalid = getAngleType(400);
62-
assertEquals(invalid, "Invalid angle");
50+
assertEquals(getAngleType(90), "Right angle");
51+
assertEquals(getAngleType(45), "Acute angle");
52+
assertEquals(getAngleType(135), "Obtuse angle");
53+
assertEquals(getAngleType(180), "Straight angle");
54+
assertEquals(getAngleType(270), "Reflex angle");
55+
assertEquals(getAngleType(-10), "Invalid angle");
56+
assertEquals(getAngleType(360), "Invalid angle");
57+
assertEquals(getAngleType(359.999), "Reflex angle");
58+
assertEquals(getAngleType("90"), "Invalid angle");

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,3 @@ assertEquals(isProperFraction(101, 100), false);
5757
assertEquals(isProperFraction(0.5, 1), true);
5858
assertEquals(isProperFraction(0.9999, 1), true);
5959
assertEquals(isProperFraction(1, 1.0001), true);
60-
61-
console.log("Completed tests in 2-is-proper-fraction.js");

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

Lines changed: 21 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -21,42 +21,25 @@
2121
// After you have implemented the function, write tests to cover all the cases, and
2222
// execute the code to ensure all tests pass.
2323

24-
function getCardValue(card) {
25-
// Student-friendly step-by-step parsing and validation.
26-
// 1) Expect a string like "A♠", "10♥", "K♦" where the last character is the suit emoji
27-
// 2) The rank is everything before the final character (so "10" or "A")
28-
// 3) Check the suit is one of the four allowed suits, and the rank is valid
29-
30-
if (typeof card !== "string") {
31-
throw new Error('Card must be a string, e.g. "A♠"');
32-
}
24+
const validSuits = ["♠", "♣", "♥", "♦"];
3325

34-
const text = card.trim();
35-
if (text.length < 2) {
36-
throw new Error(
37-
'Invalid card: too short. Expect a rank and a suit, e.g. "10♠"'
38-
);
26+
function getCardValue(card) {
27+
if (typeof card !== "string" || card.length < 2) {
28+
throw new Error("Invalid card");
3929
}
4030

41-
// The suit is the last character, the rank is the rest
42-
const suit = text[text.length - 1];
43-
const rank = text.slice(0, -1);
44-
45-
const validSuits = new Set(["♠", "♥", "♦", "♣"]);
46-
if (!validSuits.has(suit)) {
47-
throw new Error(`Invalid suit "${suit}". Use one of: ♠ ♥ ♦ ♣`);
31+
const suit = card.slice(-1);
32+
if (!validSuits.includes(suit)) {
33+
throw new Error("Invalid card");
4834
}
4935

50-
// Handle special ranks first
36+
const rank = card.slice(0, -1);
5137
if (rank === "A") return 11;
5238
if (rank === "J" || rank === "Q" || rank === "K") return 10;
5339

54-
// Otherwise expect a number between 2 and 10
55-
const n = Number(rank);
56-
if (Number.isInteger(n) && n >= 2 && n <= 10) return n;
57-
58-
// If we get here, the rank wasn't recognised
59-
throw new Error(`Invalid rank "${rank}". Use A, 2-10, J, Q or K`);
40+
const num = Number(rank);
41+
if (Number.isInteger(num) && num >= 2 && num <= 10) return num;
42+
throw new Error("Invalid card");
6043
}
6144

6245
// The line below allows us to load the getCardValue function into tests in other files.
@@ -73,8 +56,14 @@ function assertEquals(actualOutput, targetOutput) {
7356

7457
// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
7558
// Examples:
59+
assertEquals(getCardValue("2♠"), 2);
60+
assertEquals(getCardValue("3♣"), 3);
7661
assertEquals(getCardValue("9♠"), 9);
77-
62+
assertEquals(getCardValue("10♦"), 10);
63+
assertEquals(getCardValue("J♣"), 10);
64+
assertEquals(getCardValue("Q♦"), 10);
65+
assertEquals(getCardValue("K♦"), 10);
66+
assertEquals(getCardValue("A♥"), 11);
7867
// Handling invalid cards
7968
try {
8069
getCardValue("invalid");
@@ -86,26 +75,6 @@ try {
8675
}
8776

8877
// What other invalid card cases can you think of?
89-
90-
// === Student-friendly examples ===
91-
// These examples show how the function behaves. Run the file with node to see messages.
92-
console.log("\nRunning student-friendly examples for getCardValue:");
93-
94-
// Good cards
95-
console.log("A♠ =>", getCardValue("A♠"), "(expected 11)");
96-
console.log("K♦ =>", getCardValue("K♦"), "(expected 10)");
97-
console.log("10♥ =>", getCardValue("10♥"), "(expected 10)");
98-
console.log("3♣ =>", getCardValue("3♣"), "(expected 3)");
99-
100-
// Examples that should throw (wrapped in try/catch so the script keeps running)
101-
const examples = ["invalid", "9x", "a♠", ""];
102-
examples.forEach((example) => {
103-
try {
104-
const v = getCardValue(example);
105-
console.log(`${example} => ${v} (unexpected: should have thrown)`);
106-
} catch (e) {
107-
console.log(`${example} => throws: ${e.message}`);
108-
}
109-
});
110-
111-
console.log("\nCompleted student-friendly examples");
78+
// lowercase rank
79+
// invalid suit
80+
// non-string input

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+
});
1720
// Case 3: Obtuse angles
21+
test(`should return "Obtuse angle" when (90 < angle < 180)`, () => {
22+
expect(getAngleType(91)).toEqual("Obtuse angle");
23+
expect(getAngleType(135)).toEqual("Obtuse angle");
24+
expect(getAngleType(179)).toEqual("Obtuse angle");
25+
});
26+
1827
// Case 4: Straight angle
28+
test(`should return "Straight angle" when angle === 180`, () => {
29+
expect(getAngleType(180)).toEqual("Straight angle");
30+
});
31+
1932
// Case 5: Reflex angles
33+
test(`should return "Reflex angle" when (180 < angle < 360)`, () => {
34+
expect(getAngleType(181)).toEqual("Reflex angle");
35+
expect(getAngleType(270)).toEqual("Reflex angle");
36+
expect(getAngleType(359)).toEqual("Reflex angle");
37+
});
38+
2039
// Case 6: Invalid angles
40+
test(`should return "Invalid angle" when invalid`, () => {
41+
expect(getAngleType(-1)).toEqual("Invalid angle");
42+
expect(getAngleType(0)).toEqual("Invalid angle");
43+
expect(getAngleType("90")).toEqual("Invalid angle");
44+
expect(getAngleType(1000)).toEqual("Invalid angle");
45+
expect(getAngleType(9999.999)).toEqual("Invalid angle");
46+
});

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,36 @@ const isProperFraction = require("../implement/2-is-proper-fraction");
77
// Special case: numerator is zero
88
test(`should return false when denominator is zero`, () => {
99
expect(isProperFraction(1, 0)).toEqual(false);
10+
expect(isProperFraction(0, 0)).toEqual(false);
11+
expect(isProperFraction(-1, 0)).toEqual(false);
12+
});
13+
14+
test(`should return true when numerator is zero and denominator is non-zero`, () => {
15+
expect(isProperFraction(0, 1)).toEqual(true);
16+
expect(isProperFraction(0, -1)).toEqual(true);
17+
expect(isProperFraction(0, 100)).toEqual(true);
18+
expect(isProperFraction(0, -100)).toEqual(true);
19+
});
20+
21+
test(`should return true when absolute value of numerator is less than absolute value of denominator`, () => {
22+
expect(isProperFraction(1, 2)).toEqual(true);
23+
expect(isProperFraction(-1, 2)).toEqual(true);
24+
expect(isProperFraction(1, -2)).toEqual(true);
25+
expect(isProperFraction(-1, -2)).toEqual(true);
26+
});
27+
28+
test(`should return false when absolute value of numerator is greater than or equal to absolute value of denominator`, () => {
29+
expect(isProperFraction(1, 1)).toEqual(false);
30+
expect(isProperFraction(2, 1)).toEqual(false);
31+
expect(isProperFraction(-1, 1)).toEqual(false);
32+
expect(isProperFraction(1, -1)).toEqual(false);
33+
expect(isProperFraction(-1, -1)).toEqual(false);
34+
});
35+
36+
test(`floating point values should be compared using absolute values`, () => {
37+
expect(isProperFraction(0.5, 1)).toEqual(true);
38+
expect(isProperFraction(1, 0.5)).toEqual(false);
39+
expect(isProperFraction(-0.5, 1)).toEqual(true);
40+
expect(isProperFraction(0.5, -1)).toEqual(true);
41+
expect(isProperFraction(-0.5, -1)).toEqual(true);
1042
});

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,34 @@ test(`Should return 11 when given an ace card`, () => {
1111

1212
// Suggestion: Group the remaining test data into these categories:
1313
// Number Cards (2-10)
14+
test(`Should return the numeric value for number cards`, () => {
15+
expect(getCardValue("2♠")).toEqual(2);
16+
expect(getCardValue("3♠")).toEqual(3);
17+
expect(getCardValue("4♠")).toEqual(4);
18+
expect(getCardValue("5♠")).toEqual(5);
19+
expect(getCardValue("6♠")).toEqual(6);
20+
expect(getCardValue("7♠")).toEqual(7);
21+
expect(getCardValue("8♠")).toEqual(8);
22+
expect(getCardValue("9♠")).toEqual(9);
23+
expect(getCardValue("10♠")).toEqual(10);
24+
});
25+
1426
// Face Cards (J, Q, K)
27+
test(`Should return 10 for face cards (J, Q, K)`, () => {
28+
expect(getCardValue("J♠")).toEqual(10);
29+
expect(getCardValue("Q♠")).toEqual(10);
30+
expect(getCardValue("K♠")).toEqual(10);
31+
});
32+
1533
// Invalid Cards
34+
test(`Should throw an error for invalid cards`, () => {
35+
expect(() => getCardValue("1♠")).toThrow("Invalid card");
36+
expect(() => getCardValue("A")).toThrow("Invalid card");
37+
expect(() => getCardValue("J")).toThrow("Invalid card");
38+
expect(() => getCardValue("♠")).toThrow("Invalid card");
39+
expect(() => getCardValue("")).toThrow("Invalid card");
40+
});
1641

1742
// To learn how to test whether a function throws an error as expected in Jest,
1843
// please refer to the Jest documentation:
1944
// https://jestjs.io/docs/expect#tothrowerror
20-

0 commit comments

Comments
 (0)