Skip to content

Commit 29f2751

Browse files
test the 3 with jest
1 parent 195accb commit 29f2751

1 file changed

Lines changed: 76 additions & 1 deletion

File tree

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

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,82 @@ import { getCardValue } from "../implement/3-get-card-value.js";
66
test(`Should return 11 when given an ace card`, () => {
77
expect(getCardValue("A♠")).toEqual(11);
88
});
9+
// Case 2: Face Cards (J, Q, K)
10+
test(`Should return 10 when given a Face card`, () => {
11+
expect(getCardValue("J♠")).toEqual(10);
12+
});
13+
test(`Should return 10 when given a Face card`, () => {
14+
expect(getCardValue("K♠")).toEqual(10);
15+
});
16+
test(`Should return 10 when given a Face card`, () => {
17+
expect(getCardValue("Q♠")).toEqual(10);
18+
});
19+
//case 3: Number Cards (2-10)
20+
test(`Should return 2 when given a number card`, () => {
21+
expect(getCardValue("2♠")).toEqual(2);
22+
});
23+
test(`Should return 3 when given a number card`, () => {
24+
expect(getCardValue("3♠")).toEqual(3);
25+
});
26+
test(`Should return 4 when given a number card`, () => {
27+
expect(getCardValue("4♠")).toEqual(4);
28+
});
29+
test(`Should return 5 when given a number card`, () => {
30+
expect(getCardValue("5♠")).toEqual(5);
31+
});
32+
test(`Should return 6 when given a number card`, () => {
33+
expect(getCardValue("6♠")).toEqual(6);
34+
});
35+
test(`Should return 8 when given a number card`, () => {
36+
expect(getCardValue("8♠")).toEqual(8);
37+
});
38+
test(`Should return 7 when given a number card`, () => {
39+
expect(getCardValue("7♠")).toEqual(7);
40+
});
41+
test(`Should return 9 when given a number card`, () => {
42+
expect(getCardValue("9♠")).toEqual(9);
43+
});
44+
test(`Should return 10 when given a number card`, () => {
45+
expect(getCardValue("10♠")).toEqual(10);
46+
});
47+
// Case 4: Invalid cards
48+
test(`Should throw an error when given a non-card string`, () => {
49+
expect(() => getCardValue("invalid")).toThrow(
50+
'Expected a number followed by a suit, but got "invalid"'
51+
);
52+
});
53+
54+
test(`Should throw an error when the card has no suit`, () => {
55+
expect(() => getCardValue("A")).toThrow();
56+
});
57+
58+
test(`Should throw an error when the suit is invalid`, () => {
59+
expect(() => getCardValue("5X")).toThrow();
60+
});
61+
62+
test(`Should throw an error when the rank is invalid`, () => {
63+
expect(() => getCardValue("Z♠")).toThrow();
64+
});
65+
66+
test(`Should throw an error when the rank is too low`, () => {
67+
expect(() => getCardValue("1♥")).toThrow();
68+
});
69+
70+
test(`Should throw an error when the rank is too high`, () => {
71+
expect(() => getCardValue("11♦")).toThrow();
72+
});
73+
74+
test(`Should throw an error when the rank is lowercase`, () => {
75+
expect(() => getCardValue("a♠")).toThrow();
76+
});
77+
78+
test(`Should throw an error when the suit comes before the rank`, () => {
79+
expect(() => getCardValue("♠A")).toThrow();
80+
});
81+
82+
test(`Should throw an error when given an empty string`, () => {
83+
expect(() => getCardValue("")).toThrow();
84+
});
985

1086
// Suggestion: Group the remaining test data into these categories:
1187
// Number Cards (2-10)
@@ -15,4 +91,3 @@ test(`Should return 11 when given an ace card`, () => {
1591
// To learn how to test whether a function throws an error as expected in Jest,
1692
// please refer to the Jest documentation:
1793
// https://jestjs.io/docs/expect#tothrowerror
18-

0 commit comments

Comments
 (0)