Skip to content

Commit 743daf6

Browse files
committed
Refactor tests for getCardValue function to handle all Aces and number cards dynamically; update invalid card handling
1 parent 9c67445 commit 743daf6

File tree

2 files changed

+30
-30
lines changed

2 files changed

+30
-30
lines changed

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,6 @@ const kingofDiamonds = getCardValue("K♦");
5252
assertEquals(kingofDiamonds, 10);
5353

5454
// Case 4
55-
// Handle Ace (A):
56-
// Given a card with a rank of "A",
57-
// When the function is called with an Ace,
58-
// Then it should, by default, assume the Ace is worth 11 points, which is a common rule in blackjack.
59-
const aceofHearts = getCardValue("A♥");
60-
assertEquals(aceofHearts, 11);
61-
62-
// Case 5
6355
// Handle Invalid Cards:
6456
// Given a card with an invalid rank (neither a number nor a recognized face card),
6557
// When the function is called with such a card,

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

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,39 @@
22
// We will use the same function, but write tests for it using Jest in this file.
33
const getCardValue = require("../implement/3-get-card-value");
44

5-
test("should return 11 for Ace of Spades", () => {
6-
const aceofSpades = getCardValue("A♠");
7-
expect(aceofSpades).toEqual(11);
8-
});
5+
test("should return 11 for all Aces", () => {
6+
const suits = ["♠", "♥", "♦", "♣"];
7+
for (const suit of suits) {
8+
expect(getCardValue(`A${suit}`)).toEqual(11);
9+
}
10+
}); // One array handles all suits for Aces
911

1012
// Case 2: Handle Number Cards (2-10):
11-
test("should return 7 for 7 of Hearts", () => {
12-
const sevenOfHearts = getCardValue("7♥");
13-
expect(sevenOfHearts).toEqual(7);
14-
});
13+
test("should return correct value for number cards (2-10)", () => {
14+
const suits = ["♠", "♥", "♦", "♣"];
15+
for (let i = 2; i <= 10; i++) {
16+
for (const suit of suits) {
17+
expect(getCardValue(`${i}${suit}`)).toEqual(i);
18+
}
19+
}
20+
}); // Loop through numbers 2-10 for all suits; 36 test cases
1521

1622
// Case 3: Handle Face Cards (J, Q, K):
17-
test("should return 10 for King of Hearts", () => {
18-
const kingOfHearts = getCardValue("K♥");
19-
expect(kingOfHearts).toEqual(10);
20-
});
23+
test("should return 10 for all face cards (J, Q, K)", () => {
24+
const faceCards = ["J", "Q", "K"];
25+
const suits = ["♠", "♥", "♦", "♣"];
26+
for (const card of faceCards) {
27+
for (const suit of suits) {
28+
expect(getCardValue(`${card}${suit}`)).toEqual(10);
29+
}
30+
}
31+
}); // Loop through face cards for all suits; 12 test cases
2132

22-
// Case 4: Handle Ace (A):
23-
test("should return 11 for Ace of Diamonds", () => {
24-
const aceOfDiamonds = getCardValue("A♦");
25-
expect(aceOfDiamonds).toEqual(11);
26-
});
2733

28-
// Case 5: Handle Invalid Cards:
29-
test("should return 0 for invalid card", () => {
30-
const invalidCard = getCardValue("1♣");
31-
expect(invalidCard).toEqual(0);
32-
});
34+
// Case 4: Handle Invalid Cards:
35+
test("should throw error for all invalid cards", () => {
36+
const invalidCards = ["1♣", "11♦", "B♠", "Z♥", "15♣", "", " "];
37+
for (const card of invalidCards) {
38+
expect(() => getCardValue(card)).toThrow("Invalid card rank");
39+
}
40+
}); // Loop through all invalid cards; 7 test cases

0 commit comments

Comments
 (0)