|
2 | 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 | + |
5 | 6 | // TODO: Write tests in Jest syntax to cover all possible outcomes. |
6 | 7 |
|
| 8 | + |
7 | 9 | // Case 1: Ace (A) |
8 | 10 | test(`Should return 11 when given an ace card`, () => { |
9 | 11 | expect(getCardValue("A♠")).toEqual(11); |
10 | 12 | }); |
11 | 13 |
|
| 14 | + |
12 | 15 | // Suggestion: Group the remaining test data into these categories: |
13 | 16 | // Number Cards (2-10) |
14 | 17 | // Face Cards (J, Q, K) |
15 | 18 | // Invalid Cards |
16 | 19 |
|
| 20 | + |
17 | 21 | // To learn how to test whether a function throws an error as expected in Jest, |
18 | 22 | // please refer to the Jest documentation: |
19 | | -// https://jestjs.io/docs/expect#tothrowerror |
| 23 | +// [https://jestjs.io/docs/expect#tothrowerror](https://jestjs.io/docs/expect#tothrowerror) |
| 24 | + |
| 25 | + |
| 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 | + |
| 35 | +test(`Should return 10 when given "10♥"`, () => { |
| 36 | + expect(getCardValue("10♥")).toEqual(10); |
| 37 | +}); |
| 38 | + |
20 | 39 |
|
| 40 | +// Face Cards (J, Q, K) |
| 41 | +test(`Should return 10 when given "J♣"`, () => { |
| 42 | + expect(getCardValue("J♣")).toEqual(10); |
| 43 | +}); |
| 44 | + |
| 45 | +test(`Should return 10 when given "Q♦"`, () => { |
| 46 | + expect(getCardValue("Q♦")).toEqual(10); |
| 47 | +}); |
| 48 | + |
| 49 | +test(`Should return 10 when given "K♥"`, () => { |
| 50 | + expect(getCardValue("K♥")).toEqual(10); |
| 51 | +}); |
| 52 | + |
| 53 | + |
| 54 | +// Invalid Cards |
| 55 | +test(`Should throw an error when given an invalid card symbol like "Z♠"`, () => { |
| 56 | + expect(() => getCardValue("Z♠")).toThrowError(); |
| 57 | +}); |
| 58 | + |
| 59 | +test(`Should throw an error when given an empty string`, () => { |
| 60 | + expect(() => getCardValue("")).toThrowError(); |
| 61 | +}); |
| 62 | + |
| 63 | +test(`Should throw an error when given null`, () => { |
| 64 | + expect(() => getCardValue(null)).toThrowError(); |
| 65 | +}); |
0 commit comments