Skip to content

Commit 02b9cb7

Browse files
refactor tests for getCardValue function to improve organization and clarity
1 parent ac66473 commit 02b9cb7

1 file changed

Lines changed: 46 additions & 6 deletions

File tree

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

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,57 @@ import { getCardValue } from "../implement/3-get-card-value.js";
22

33
// TODO: Write tests in Jest syntax to cover all possible outcomes.
44

5+
56
// Case 1: Ace (A)
6-
test(`Should return 11 when given an ace card`, () => {
7+
test("Should return 11 when given an ace card", () => {
78
expect(getCardValue("A♠")).toEqual(11);
9+
expect(getCardValue("A♦")).toEqual(11);
810
});
911

10-
// Suggestion: Group the remaining test data into these categories:
11-
// Number Cards (2-10)
12-
// Face Cards (J, Q, K)
13-
// Invalid Cards
12+
// Case 2: Number Cards (2-10)
13+
test("Should return numeric value for single-digit number cards", () => {
14+
expect(getCardValue("2♠")).toEqual(2);
15+
expect(getCardValue("5♥")).toEqual(5);
16+
expect(getCardValue("9♦")).toEqual(9);
17+
});
18+
19+
test("Should return 10 for '10' cards across all suits", () => {
20+
expect(getCardValue("10♥")).toEqual(10);
21+
expect(getCardValue("10♣")).toEqual(10);
22+
});
23+
24+
// Case 3: Face Cards (J, Q, K)
25+
test("Should return 10 for face cards (J, Q, K)", () => {
26+
expect(getCardValue("J♣")).toEqual(10);
27+
expect(getCardValue("Q♦")).toEqual(10);
28+
expect(getCardValue("K♠")).toEqual(10);
29+
});
30+
31+
// Case 4: Invalid Cards
32+
test("Should throw an error for arbitrary non-card strings", () => {
33+
expect(() => getCardValue("invalid")).toThrow();
34+
});
35+
36+
test("Should throw an error for invalid ranks or suit-like characters", () => {
37+
expect(() => getCardValue("1♠")).toThrow();
38+
expect(() => getCardValue("11♥")).toThrow();
39+
expect(() => getCardValue("Z♠")).toThrow();
40+
});
41+
42+
test("Should throw an error for missing or invalid suit", () => {
43+
expect(() => getCardValue("10")).toThrow();
44+
expect(() => getCardValue("AX")).toThrow();
45+
});
46+
47+
test("Should throw an error when suit is before rank", () => {
48+
expect(() => getCardValue("♠A")).toThrow();
49+
});
50+
51+
test("Should throw an error for non-string or empty inputs", () => {
52+
expect(() => getCardValue("")).toThrow();
53+
expect(() => getCardValue(10)).toThrow();
54+
});
1455

1556
// To learn how to test whether a function throws an error as expected in Jest,
1657
// please refer to the Jest documentation:
1758
// https://jestjs.io/docs/expect#tothrowerror
18-

0 commit comments

Comments
 (0)