|
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 | | -// TODO: Write tests in Jest syntax to cover all possible outcomes. |
6 | | - |
7 | | -// Case 1: Ace (A) |
8 | | -test(`Should return 11 when given an ace card`, () => { |
| 5 | +// Case 1: Ace (A) - Should return 11 |
| 6 | +test(`Should return 11 when given an ace card (A♠)`, () => { |
9 | 7 | expect(getCardValue("A♠")).toEqual(11); |
10 | 8 | }); |
11 | 9 |
|
12 | | -// Suggestion: Group the remaining test data into these categories: |
13 | | -// Number Cards (2-10) |
14 | | -// Face Cards (J, Q, K) |
15 | | -// Invalid Cards |
| 10 | +test(`Should return 11 when given an ace card (A♥)`, () => { |
| 11 | + expect(getCardValue("A♥")).toEqual(11); |
| 12 | +}); |
| 13 | + |
| 14 | +test(`Should return 11 when given an ace card (A♦)`, () => { |
| 15 | + expect(getCardValue("A♦")).toEqual(11); |
| 16 | +}); |
| 17 | + |
| 18 | +test(`Should return 11 when given an ace card (A♣)`, () => { |
| 19 | + expect(getCardValue("A♣")).toEqual(11); |
| 20 | +}); |
| 21 | + |
| 22 | +// Case 2: Number Cards (2-10) |
| 23 | +test(`Should return 2 when given a number card (2♠)`, () => { |
| 24 | + expect(getCardValue("2♠")).toEqual(2); |
| 25 | +}); |
| 26 | + |
| 27 | +test(`Should return 3 when given a number card (3♥)`, () => { |
| 28 | + expect(getCardValue("3♥")).toEqual(3); |
| 29 | +}); |
| 30 | + |
| 31 | +test(`Should return 4 when given a number card (4♦)`, () => { |
| 32 | + expect(getCardValue("4♦")).toEqual(4); |
| 33 | +}); |
16 | 34 |
|
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 |
| 35 | +test(`Should return 5 when given a number card (5♣)`, () => { |
| 36 | + expect(getCardValue("5♣")).toEqual(5); |
| 37 | +}); |
| 38 | + |
| 39 | +test(`Should return 6 when given a number card (6♠)`, () => { |
| 40 | + expect(getCardValue("6♠")).toEqual(6); |
| 41 | +}); |
| 42 | + |
| 43 | +test(`Should return 7 when given a number card (7♥)`, () => { |
| 44 | + expect(getCardValue("7♥")).toEqual(7); |
| 45 | +}); |
| 46 | + |
| 47 | +test(`Should return 8 when given a number card (8♦)`, () => { |
| 48 | + expect(getCardValue("8♦")).toEqual(8); |
| 49 | +}); |
| 50 | + |
| 51 | +test(`Should return 9 when given a number card (9♣)`, () => { |
| 52 | + expect(getCardValue("9♣")).toEqual(9); |
| 53 | +}); |
| 54 | + |
| 55 | +test(`Should return 10 when given a number card (10♠)`, () => { |
| 56 | + expect(getCardValue("10♠")).toEqual(10); |
| 57 | +}); |
| 58 | + |
| 59 | +// Case 3: Face Cards (J, Q, K) - Should return 10 |
| 60 | +test(`Should return 10 when given a Jack card (J♠)`, () => { |
| 61 | + expect(getCardValue("J♠")).toEqual(10); |
| 62 | +}); |
| 63 | + |
| 64 | +test(`Should return 10 when given a Queen card (Q♥)`, () => { |
| 65 | + expect(getCardValue("Q♥")).toEqual(10); |
| 66 | +}); |
| 67 | + |
| 68 | +test(`Should return 10 when given a King card (K♦)`, () => { |
| 69 | + expect(getCardValue("K♦")).toEqual(10); |
| 70 | +}); |
| 71 | + |
| 72 | +test(`Should return 10 when given a Jack card (J♣)`, () => { |
| 73 | + expect(getCardValue("J♣")).toEqual(10); |
| 74 | +}); |
| 75 | + |
| 76 | +test(`Should return 10 when given a Queen card (Q♠)`, () => { |
| 77 | + expect(getCardValue("Q♠")).toEqual(10); |
| 78 | +}); |
| 79 | + |
| 80 | +test(`Should return 10 when given a King card (K♥)`, () => { |
| 81 | + expect(getCardValue("K♥")).toEqual(10); |
| 82 | +}); |
| 83 | + |
| 84 | +// Case 4: Invalid Cards - Should throw error |
| 85 | +test(`Should throw an error when given an invalid rank`, () => { |
| 86 | + expect(() => getCardValue("X♠")).toThrow(); |
| 87 | +}); |
| 88 | + |
| 89 | +test(`Should throw an error when given an invalid suit`, () => { |
| 90 | + expect(() => getCardValue("A★")).toThrow(); |
| 91 | +}); |
| 92 | + |
| 93 | +test(`Should throw an error when given an empty string`, () => { |
| 94 | + expect(() => getCardValue("")).toThrow(); |
| 95 | +}); |
| 96 | + |
| 97 | +test(`Should throw an error when given only a suit (no rank)`, () => { |
| 98 | + expect(() => getCardValue("♠")).toThrow(); |
| 99 | +}); |
| 100 | + |
| 101 | +test(`Should throw an error when given an invalid format`, () => { |
| 102 | + expect(() => getCardValue("A")).toThrow(); |
| 103 | +}); |
20 | 104 |
|
0 commit comments