|
3 | 3 | const getCardValue = require("../implement/3-get-card-value"); |
4 | 4 |
|
5 | 5 |
|
| 6 | + |
6 | 7 | // TODO: Write tests in Jest syntax to cover all possible outcomes. |
7 | 8 |
|
8 | 9 |
|
| 10 | + |
9 | 11 | // Case 1: Ace (A) |
10 | | -test(`Should return 11 when given an ace card`, () => { |
| 12 | +test("should return 11 for any ace (all suits)", () => { |
11 | 13 | expect(getCardValue("A♠")).toEqual(11); |
| 14 | + expect(getCardValue("A♥")).toEqual(11); |
| 15 | + expect(getCardValue("A♦")).toEqual(11); |
| 16 | + expect(getCardValue("A♣")).toEqual(11); |
12 | 17 | }); |
13 | 18 |
|
14 | 19 |
|
| 20 | + |
15 | 21 | // Suggestion: Group the remaining test data into these categories: |
16 | 22 | // Number Cards (2-10) |
17 | 23 | // Face Cards (J, Q, K) |
18 | 24 | // Invalid Cards |
19 | 25 |
|
20 | 26 |
|
| 27 | + |
21 | 28 | // To learn how to test whether a function throws an error as expected in Jest, |
22 | 29 | // please refer to the Jest documentation: |
23 | 30 | // [https://jestjs.io/docs/expect#tothrowerror](https://jestjs.io/docs/expect#tothrowerror) |
24 | 31 |
|
25 | 32 |
|
26 | | -// Number Cards (2–10) |
27 | | -test(`Should return 2 when given "2♦"`, () => { |
28 | | - expect(getCardValue("2♦")).toEqual(2); |
29 | | -}); |
30 | | - |
31 | | -test(`Should return 5 when given "5♣"`, () => { |
32 | | - expect(getCardValue("5♣")).toEqual(5); |
33 | | -}); |
34 | 33 |
|
35 | | -test(`Should return 10 when given "10♥"`, () => { |
| 34 | +// Number Cards (2–10) |
| 35 | +test("should return the numeric value of number cards (2-10)", () => { |
| 36 | + expect(getCardValue("2♣")).toEqual(2); |
| 37 | + expect(getCardValue("5♠")).toEqual(5); |
36 | 38 | expect(getCardValue("10♥")).toEqual(10); |
| 39 | + // Loop to verify all values 2–10 across one suit |
| 40 | + for (let n = 2; n <= 10; n++) { |
| 41 | + expect(getCardValue(`${n}♠`)).toEqual(n); |
| 42 | + } |
37 | 43 | }); |
38 | 44 |
|
39 | 45 |
|
| 46 | + |
40 | 47 | // Face Cards (J, Q, K) |
41 | | -test(`Should return 10 when given "J♣"`, () => { |
| 48 | +test("should return 10 for all face cards (J, Q, K)", () => { |
42 | 49 | expect(getCardValue("J♣")).toEqual(10); |
43 | | -}); |
44 | | - |
45 | | -test(`Should return 10 when given "Q♦"`, () => { |
46 | 50 | expect(getCardValue("Q♦")).toEqual(10); |
47 | | -}); |
48 | | - |
49 | | -test(`Should return 10 when given "K♥"`, () => { |
50 | 51 | expect(getCardValue("K♥")).toEqual(10); |
51 | 52 | }); |
52 | 53 |
|
53 | 54 |
|
54 | | -// Invalid Cards |
55 | | -test(`Should throw an error when given an invalid card symbol like "Z♠"`, () => { |
56 | | - expect(() => getCardValue("Z♠")).toThrowError(); |
57 | | -}); |
58 | 55 |
|
59 | | -test(`Should throw an error when given an empty string`, () => { |
60 | | - expect(() => getCardValue("")).toThrowError(); |
| 56 | +// Invalid Cards |
| 57 | +test("should throw an error for invalid cards", () => { |
| 58 | + expect(() => getCardValue("Z♠")).toThrow(); |
| 59 | + expect(() => getCardValue("")).toThrow(); |
| 60 | + expect(() => getCardValue(null)).toThrow(); |
61 | 61 | }); |
62 | 62 |
|
63 | | -test(`Should throw an error when given null`, () => { |
64 | | - expect(() => getCardValue(null)).toThrowError(); |
65 | | -}); |
0 commit comments