Skip to content

Commit c156ae9

Browse files
committed
Test: get card value testing
1 parent e31dece commit c156ae9

1 file changed

Lines changed: 36 additions & 14 deletions

File tree

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,42 @@
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.
31
const getCardValue = require("../implement/3-get-card-value");
4-
5-
// TODO: Write tests in Jest syntax to cover all possible outcomes.
2+
const suits = ["♠", "♥", "♦", "♣"];
63

74
// 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+
}
1111

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+
}
1622

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+
}
2033

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

Comments
 (0)