Skip to content

Commit 892f63b

Browse files
committed
updated assertion tests and added input type tests for card value
1 parent 4b1ad2d commit 892f63b

File tree

2 files changed

+40
-7
lines changed

2 files changed

+40
-7
lines changed

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88
// write one test at a time, and make it pass, build your solution up methodically
99
// just make one change at a time -- don't rush -- programmers are deep and careful thinkers
1010
function getCardValue(card) {
11+
const errorMessage =
12+
"Error: Invalid card rank. Input should be a string in the format 'R♠', where R is 2-10, J, Q, K, or A, followed by either ♠, ♥, ♦, or ♣.";
13+
14+
if (typeof card !== "string" || card.length < 2) {
15+
return errorMessage;
16+
}
1117
const rank = card.slice(0, -1);
1218
if (rank === "A") {
1319
return 11;
@@ -19,7 +25,7 @@ function getCardValue(card) {
1925
if (numericRank >= 2 && numericRank <= 9) {
2026
return numericRank;
2127
}
22-
return "Error: Invalid card rank";
28+
return errorMessage;
2329
}
2430

2531
// The line below allows us to load the getCardValue function into tests in other files.
@@ -75,9 +81,9 @@ assertEquals(aceOfHearts, 11);
7581
// Given a card with an invalid rank (neither a number nor a recognized face card),
7682
// When the function is called with such a card,
7783
// Then it should throw an error indicating "Invalid card rank."
78-
const invalidCard = "1♠";
84+
const invalidCard = getCardValue("1♠");
7985
assertEquals(invalidCard, "Error: Invalid card rank");
80-
const anotherInvalidCard = "Z♠";
86+
const anotherInvalidCard = getCardValue("Z♠");
8187
assertEquals(anotherInvalidCard, "Error: Invalid card rank");
82-
const emptyCard = "";
88+
const emptyCard = getCardValue("");
8389
assertEquals(emptyCard, "Error: Invalid card rank");

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

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,42 @@ test("should return 11 for Ace of Hearts", () => {
4343
expect(aceOfHearts).toEqual(11);
4444
});
4545
// Case 5: Handle Invalid Cards:
46+
const errorMessage =
47+
"Error: Invalid card rank. Input should be a string in the format 'R♠', where R is 2-10, J, Q, K, or A, followed by either ♠, ♥, ♦, or ♣.";
48+
4649
test("should throw an error for invalid card rank", () => {
4750
const invalidCard = getCardValue("1♠");
48-
expect(invalidCard).toEqual("Error: Invalid card rank");
51+
expect(invalidCard).toEqual(errorMessage);
4952
});
5053
test("should throw an error for another invalid card rank", () => {
5154
const anotherInvalidCard = getCardValue("Z♠");
52-
expect(anotherInvalidCard).toEqual("Error: Invalid card rank");
55+
expect(anotherInvalidCard).toEqual(errorMessage);
5356
});
5457
test("should throw an error for empty card string", () => {
5558
const emptyCard = getCardValue("");
56-
expect(emptyCard).toEqual("Error: Invalid card rank");
59+
expect(emptyCard).toEqual(errorMessage);
60+
});
61+
test("should throw an error for boolean (true) input", () => {
62+
const booleanCard = getCardValue(true);
63+
expect(booleanCard).toEqual(errorMessage);
64+
});
65+
test("should throw an error for boolean (false) input", () => {
66+
const booleanCard = getCardValue(false);
67+
expect(booleanCard).toEqual(errorMessage);
68+
});
69+
test("should throw an error for numeric input", () => {
70+
const numericCard = getCardValue(10);
71+
expect(numericCard).toEqual(errorMessage);
72+
});
73+
test("should throw an error for null input", () => {
74+
const nullCard = getCardValue(null);
75+
expect(nullCard).toEqual(errorMessage);
76+
});
77+
test("should throw an error for undefined input", () => {
78+
const undefinedCard = getCardValue(undefined);
79+
expect(undefinedCard).toEqual(errorMessage);
80+
});
81+
test("should throw an error for object input", () => {
82+
const objectCard = getCardValue({ rank: "A", suit: "♠" });
83+
expect(objectCard).toEqual(errorMessage);
5784
});

0 commit comments

Comments
 (0)