Skip to content

Commit 922023b

Browse files
refactor: update tests for getCardValue to use Jest syntax and improve error handling
1 parent 21ded3e commit 922023b

1 file changed

Lines changed: 48 additions & 70 deletions

File tree

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

Lines changed: 48 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -5,73 +5,51 @@ import { getCardValue } from "../implement/3-get-card-value.js";
55

66
// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
77

8-
test("Valid single-digit number cards return correct values", () => {
9-
assert.equal(getCardValue("2♠"), 2);
10-
assert.equal(getCardValue("5♥"), 5);
11-
assert.equal(getCardValue("9♦"), 9);
12-
});
13-
14-
test("Valid double-digit card (10) returns 10", () => {
15-
assert.equal(getCardValue("10♥"), 10);
16-
assert.equal(getCardValue("10♣"), 10);
17-
});
18-
19-
test("Ace cards return 11", () => {
20-
assert.equal(getCardValue("A♠"), 11);
21-
assert.equal(getCardValue("A♦"), 11);
22-
});
23-
24-
test("Face cards (J, Q, K) return 10", () => {
25-
assert.equal(getCardValue("J♣"), 10);
26-
assert.equal(getCardValue("Q♦"), 10);
27-
assert.equal(getCardValue("K♠"), 10);
28-
});
29-
30-
// Invalid Input Cases
31-
test("Throws error for arbitrary non-card string", () => {
32-
assert.throws(
33-
() => getCardValue("invalid"),
34-
/Expected a card rank followed by a suit/
35-
);
36-
});
37-
38-
test("Throws error for invalid ranks or suit-like characters", () => {
39-
assert.throws(
40-
() => getCardValue("1♠"),
41-
/Expected a card rank followed by a suit/
42-
);
43-
assert.throws(
44-
() => getCardValue("11♥"),
45-
/Expected a card rank followed by a suit/
46-
);
47-
assert.throws(
48-
() => getCardValue("Z♠"),
49-
/Expected a card rank followed by a suit/
50-
);
51-
});
52-
53-
test("Throws error for invalid or missing suit", () => {
54-
assert.throws(
55-
() => getCardValue("10"),
56-
/Expected a card rank followed by a suit/
57-
);
58-
assert.throws(
59-
() => getCardValue("AX"),
60-
/Expected a card rank followed by a suit/
61-
);
62-
});
63-
64-
test("Throws error for reversed order (suit before rank)", () => {
65-
assert.throws(
66-
() => getCardValue("♠A"),
67-
/Expected a card rank followed by a suit/
68-
);
69-
});
70-
71-
test("Throws error for empty string or non-string input", () => {
72-
assert.throws(
73-
() => getCardValue(""),
74-
/Expected a card rank followed by a suit/
75-
);
76-
assert.throws(() => getCardValue(10), /Expected a string/);
77-
});
8+
describe("getCardValue", () => {
9+
test("Valid single-digit number cards return correct values", () => {
10+
expect(getCardValue("2♠")).toBe(2);
11+
expect(getCardValue("5♥")).toBe(5);
12+
expect(getCardValue("9♦")).toBe(9);
13+
});
14+
15+
test("Valid double-digit card (10) returns 10", () => {
16+
expect(getCardValue("10♥")).toBe(10);
17+
expect(getCardValue("10♣")).toBe(10);
18+
});
19+
20+
test("Ace cards return 11", () => {
21+
expect(getCardValue("A♠")).toBe(11);
22+
expect(getCardValue("A♦")).toBe(11);
23+
});
24+
25+
test("Face cards (J, Q, K) return 10", () => {
26+
expect(getCardValue("J♣")).toBe(10);
27+
expect(getCardValue("Q♦")).toBe(10);
28+
expect(getCardValue("K♠")).toBe(10);
29+
});
30+
31+
// Robust Error-Handling Tests using Jest's toThrow with Regex
32+
test("Throws error for arbitrary non-card string", () => {
33+
expect(() => getCardValue("invalid")).toThrow(/Expected a card rank followed by a suit/);
34+
});
35+
36+
test("Throws error for invalid ranks or suit-like characters", () => {
37+
expect(() => getCardValue("1♠")).toThrow(/Expected a card rank followed by a suit/);
38+
expect(() => getCardValue("11♥")).toThrow(/Expected a card rank followed by a suit/);
39+
expect(() => getCardValue("Z♠")).toThrow(/Expected a card rank followed by a suit/);
40+
});
41+
42+
test("Throws error for invalid or missing suit", () => {
43+
expect(() => getCardValue("10")).toThrow(/Expected a card rank followed by a suit/);
44+
expect(() => getCardValue("AX")).toThrow(/Expected a card rank followed by a suit/);
45+
});
46+
47+
test("Throws error for reversed order (suit before rank)", () => {
48+
expect(() => getCardValue("♠A")).toThrow(/Expected a card rank followed by a suit/);
49+
});
50+
51+
test("Throws error for empty string or non-string input", () => {
52+
expect(() => getCardValue("")).toThrow(/Expected a card rank followed by a suit/);
53+
expect(() => getCardValue(10)).toThrow(/Expected a string/);
54+
});
55+
});

0 commit comments

Comments
 (0)