|
1 | 1 | // This statement loads the getCardValue function you wrote in the implement directory. |
2 | | -// We will use the same function, but write tests for it using Jest in this file. |
| 2 | +// We will use the same function, but write tests for it using Jest in this file |
3 | 3 | const getCardValue = require("../implement/3-get-card-value"); |
4 | 4 |
|
5 | | -// TODO: Write tests in Jest syntax to cover all possible outcomes. |
| 5 | +describe("getCardValue", () => { |
6 | 6 |
|
7 | | -// Case 1: Ace (A) |
8 | | -test(`Should return 11 when given an ace card`, () => { |
9 | | - expect(getCardValue("A♠")).toEqual(11); |
10 | | -}); |
| 7 | + // Case 1: Aces |
| 8 | + test("Should return 11 when given an ace card", () => { |
| 9 | + expect(getCardValue("A♠")).toBe(11); |
| 10 | + expect(getCardValue("A♥")).toBe(11); |
| 11 | + expect(getCardValue("A♦")).toBe(11); |
| 12 | + expect(getCardValue("A♣")).toBe(11); |
| 13 | + }); |
| 14 | + |
| 15 | + // Case 2: Number Cards (2-10) |
| 16 | + describe("Number Cards (2-10)", () => { |
| 17 | + test("should return the exact numeric value for standard cards", () => { |
| 18 | + expect(getCardValue("2♥")).toBe(2); |
| 19 | + expect(getCardValue("5♦")).toBe(5); |
| 20 | + expect(getCardValue("9♠")).toBe(9); |
| 21 | + }); |
| 22 | + |
| 23 | + test("should correctly parse the two-digit boundary card 10", () => { |
| 24 | + expect(getCardValue("10♣")).toBe(10); |
| 25 | + expect(getCardValue("10♦")).toBe(10); |
| 26 | + }); |
| 27 | + }); |
| 28 | + |
| 29 | + // Case 3: Face Cards (J, Q, K) |
| 30 | + describe("Face Cards (J, Q, K)", () => { |
| 31 | + test("should return 10 for Jacks (J)", () => { |
| 32 | + expect(getCardValue("J♣")).toBe(10); |
| 33 | + }); |
| 34 | + |
| 35 | + test("should return 10 for Queens (Q)", () => { |
| 36 | + expect(getCardValue("Q♦")).toBe(10); |
| 37 | + }); |
11 | 38 |
|
12 | | -// Suggestion: Group the remaining test data into these categories: |
13 | | -// Number Cards (2-10) |
14 | | -// Face Cards (J, Q, K) |
15 | | -// Invalid Cards |
| 39 | + test("should return 10 for Kings (K)", () => { |
| 40 | + expect(getCardValue("K♠")).toBe(10); |
| 41 | + }); |
| 42 | + }); |
16 | 43 |
|
17 | | -// To learn how to test whether a function throws an error as expected in Jest, |
18 | | -// please refer to the Jest documentation: |
19 | | -// https://jestjs.io/docs/expect#tothrowerror |
| 44 | + // Case 4: Invalid Cards (Error Handling) |
| 45 | + describe("Invalid Cards", () => { |
| 46 | + // Note: When testing exceptions in Jest, wrap the execution in an anonymous function. |
| 47 | + test("should throw an error for text strings unrelated to cards", () => { |
| 48 | + expect(() => getCardValue("invalid")).toThrow(); |
| 49 | + }); |
20 | 50 |
|
| 51 | + test("should throw an error if missing the rank or the suit", () => { |
| 52 | + expect(() => getCardValue("A")).toThrow(); |
| 53 | + expect(() => getCardValue("♠")).toThrow(); |
| 54 | + }); |
| 55 | + |
| 56 | + test("should throw an error for numeric values out of boundaries", () => { |
| 57 | + expect(() => getCardValue("1♠")).toThrow(); // Should be an Ace |
| 58 | + expect(() => getCardValue("11♥")).toThrow(); // Invalid face range |
| 59 | + expect(() => getCardValue("0♦")).toThrow(); // Zero baseline invalid |
| 60 | + }); |
| 61 | + |
| 62 | + test("should throw an error for unsupported suits or symbols", () => { |
| 63 | + expect(() => getCardValue("Q⭐️")).toThrow(); |
| 64 | + expect(() => getCardValue("10X")).toThrow(); |
| 65 | + }); |
| 66 | + |
| 67 | + test("should throw an error for spacing anomalies", () => { |
| 68 | + expect(() => getCardValue("J ♠")).toThrow(); |
| 69 | + }); |
| 70 | + }); |
| 71 | + |
| 72 | +}); |
0 commit comments