Skip to content

Commit 3f0d84e

Browse files
committed
Complete remaining Jest updates and tighten card rank validation
1 parent f7e112c commit 3f0d84e

File tree

2 files changed

+50
-8
lines changed

2 files changed

+50
-8
lines changed

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

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

2424
function getCardValue(card) {
25-
const suits = ["♠", "♥", "♦", "♣"];
25+
const validSuits = ["♠", "♥", "♦", "♣"];
26+
const validNumberRanks = ["2", "3", "4", "5", "6", "7", "8", "9", "10"];
2627

27-
// Card must be a string
28-
if (typeof card !== "string") {
28+
if (typeof card !== "string" || card.length < 2) {
2929
throw new Error("Invalid card");
3030
}
3131

3232
const suit = card.slice(-1);
3333
const rank = card.slice(0, -1);
3434

35-
if (!suits.includes(suit)) {
35+
if (!validSuits.includes(suit)) {
3636
throw new Error("Invalid card");
3737
}
3838

@@ -44,10 +44,8 @@ function getCardValue(card) {
4444
return 10;
4545
}
4646

47-
const number = Number(rank);
48-
49-
if (number >= 2 && number <= 10) {
50-
return number;
47+
if (validNumberRanks.includes(rank)) {
48+
return Number(rank);
5149
}
5250

5351
throw new Error("Invalid card");
@@ -104,4 +102,19 @@ try {
104102
try {
105103
getCardValue("♠");
106104
console.error("Error was not thrown for invalid card");
105+
} catch (e) {}
106+
107+
try {
108+
getCardValue("0x02♠");
109+
console.error("Error was not thrown for invalid card");
110+
} catch (e) {}
111+
112+
try {
113+
getCardValue("2.1♠");
114+
console.error("Error was not thrown for invalid card");
115+
} catch (e) {}
116+
117+
try {
118+
getCardValue("0002♠");
119+
console.error("Error was not thrown for invalid card");
107120
} catch (e) {}

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,32 @@ test(`Should return 11 when given an ace card`, () => {
1818
// please refer to the Jest documentation:
1919
// https://jestjs.io/docs/expect#tothrowerror
2020

21+
// Case 2: Number cards
22+
test("should return the numeric value when given a valid number card", () => {
23+
expect(getCardValue("2♠")).toEqual(2);
24+
expect(getCardValue("9♠")).toEqual(9);
25+
expect(getCardValue("10♥")).toEqual(10);
26+
});
27+
28+
// Case 3: Face cards
29+
test("should return 10 when given a face card", () => {
30+
expect(getCardValue("J♣")).toEqual(10);
31+
expect(getCardValue("Q♦")).toEqual(10);
32+
expect(getCardValue("K♥")).toEqual(10);
33+
});
34+
35+
// Case 4: Invalid cards
36+
test("should throw an error for invalid card strings", () => {
37+
expect(() => getCardValue("invalid")).toThrow();
38+
expect(() => getCardValue("1♠")).toThrow();
39+
expect(() => getCardValue("11♠")).toThrow();
40+
expect(() => getCardValue("A")).toThrow();
41+
expect(() => getCardValue("♠")).toThrow();
42+
});
43+
44+
// Case 5: Invalid numeric literal strings
45+
test("should throw an error for strings that JavaScript can coerce to numbers but are not valid card ranks", () => {
46+
expect(() => getCardValue("0x02♠")).toThrow();
47+
expect(() => getCardValue("2.1♠")).toThrow();
48+
expect(() => getCardValue("0002♠")).toThrow();
49+
});

0 commit comments

Comments
 (0)