Skip to content

Commit 7c99da6

Browse files
author
russom
committed
Test cases for all possible valid and invalid cards created and tested.
1 parent 6794148 commit 7c99da6

3 files changed

Lines changed: 34 additions & 7 deletions

File tree

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,18 @@ function getCardValue(card) {
4242
const validSuits = ["♠", "♥", "♦", "♣"]; // An array of suits are created here.
4343

4444
if (typeof card !== "string") { // Here the type of values are checked whether they are string.
45-
throw new Error("Invalid card: " + card);
45+
return card + " Invalid card";
4646
}
4747
const suit = card.slice(-1);
4848
const rank = card.slice(0, -1);
4949

5050
if (!validRanks.includes(rank) || !validSuits.includes(suit)) { //Here the values that are stored in rank and suit are checked whether they are valid are inside the validRank and validSuits arrays.
51-
throw new Error("Invalid card: " + card);
51+
return card + " Invalid card";
5252
}
5353

5454
if (rank === "A") { // Here the code is testing wether the rank is an Ace.
5555
return 11;
56-
} else if (cards === "J" || rank === "Q" || rank === "K") { // Here the ranks are checked if they are J, Q or K and it returns 10
56+
} else if (rank === "J" || rank === "Q" || rank === "K") { // Here the ranks are checked if they are J, Q or K and it returns 10
5757
return 10;
5858
} else { // Lastly any rank between 2 and 10 get checked and the same number of value returned.
5959
return Number(rank);

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

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

12+
// Case 2: Face Cards (J, Q, K)
13+
test(`Should return 10 when given a face card`, () => {
14+
expect(getCardValue("K♦")).toEqual(10);
15+
expect(getCardValue("Q♥")).toEqual(10);
16+
expect(getCardValue("J♣")).toEqual(10);
17+
});
18+
19+
// Case 3: Number Cards (2-10)
20+
test(`Should return the card value when given number card`, () => {
21+
expect(getCardValue("2♠")).toEqual(2);
22+
expect(getCardValue("3♥")).toEqual(3);
23+
expect(getCardValue("4♦")).toEqual(4);
24+
expect(getCardValue("5♥")).toEqual(5);
25+
expect(getCardValue("6♣")).toEqual(6);
26+
expect(getCardValue("7♠")).toEqual(7);
27+
expect(getCardValue("8♦")).toEqual(8);
28+
expect(getCardValue("9♥")).toEqual(9);
29+
expect(getCardValue("10♣")).toEqual(10);
30+
});
31+
32+
// Case 4: Invalid Cards
33+
test(`Should return invalid cards when given a face card`, () => {
34+
expect(getCardValue("1♦")).toEqual("Invalid card");
35+
expect(getCardValue("10")).toEqual("Invalid card");
36+
expect(getCardValue("AZ")).toEqual("Invalid card");
37+
expect(getCardValue("")).toEqual("Invalid card");
38+
});
39+
1240
// Suggestion: Group the remaining test data into these categories:
1341
// Number Cards (2-10)
1442
// Face Cards (J, Q, K)
@@ -17,4 +45,3 @@ test(`Should return 11 when given an ace card`, () => {
1745
// To learn how to test whether a function throws an error as expected in Jest,
1846
// please refer to the Jest documentation:
1947
// https://jestjs.io/docs/expect#tothrowerror
20-

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"keywords": [],
1010
"author": "Code Your Future",
1111
"license": "ISC",
12-
"dependencies": {
13-
"jest": "^29.7.0"
12+
"devDependencies": {
13+
"jest": "^30.4.2"
1414
}
15-
}
15+
}

0 commit comments

Comments
 (0)