|
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. |
3 | 1 | const getCardValue = require("../implement/3-get-card-value"); |
4 | | - |
5 | | -// TODO: Write tests in Jest syntax to cover all possible outcomes. |
| 2 | +const suits = ["♠", "♥", "♦", "♣"]; |
6 | 3 |
|
7 | 4 | // Case 1: Ace (A) |
8 | | -test(`Should return 11 when given an ace card`, () => { |
9 | | - expect(getCardValue("A♠")).toEqual(11); |
10 | | -}); |
| 5 | +for (const suit of suits) { |
| 6 | + const ace = `A${suit}`; |
| 7 | + test(`Should return 11 when given an ace card ${ace}`, () => { |
| 8 | + expect(getCardValue(ace)).toEqual(11); |
| 9 | + }); |
| 10 | +} |
11 | 11 |
|
12 | | -// Suggestion: Group the remaining test data into these categories: |
13 | | -// Number Cards (2-10) |
14 | | -// Face Cards (J, Q, K) |
15 | | -// Invalid Cards |
| 12 | +// Case 2: Face Cards (J, Q, K) |
| 13 | +const faceCards = ["J", "Q", "K"]; |
| 14 | +for (const rank of faceCards) { |
| 15 | + for (const suit of suits) { |
| 16 | + const cardFace = `${rank}${suit}`; |
| 17 | + test(`Should return 10 when given a face card ${cardFace}`, () => { |
| 18 | + expect(getCardValue(cardFace)).toEqual(10); |
| 19 | + }); |
| 20 | + } |
| 21 | +} |
16 | 22 |
|
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 |
| 23 | +// Case 3: Number Cards (2-10) |
| 24 | +const cardsNumbers = [2, 3, 4, 5, 6, 7, 8, 9, 10]; |
| 25 | +for (const rank of cardsNumbers) { |
| 26 | + for (const suit of suits) { |
| 27 | + const cardFace = `${rank}${suit}`; |
| 28 | + test(`Should return ${rank} when given a card ${cardFace}`, () => { |
| 29 | + expect(getCardValue(cardFace)).toEqual(rank); |
| 30 | + }); |
| 31 | + } |
| 32 | +} |
20 | 33 |
|
| 34 | +// Case 4: Invalid Cards |
| 35 | +const invalidCards = ["invalid", 7, "", "AA♠", "10♠♦"]; |
| 36 | +for (const invalidCard of invalidCards) { |
| 37 | + test(`Should throw an error when given an invalid card ${invalidCard}`, () => { |
| 38 | + expect(() => { |
| 39 | + getCardValue(`${invalidCard}`); |
| 40 | + }).toThrow(); |
| 41 | + }); |
| 42 | +} |
0 commit comments