Skip to content

Commit 41fc857

Browse files
committed
card value testing with jest
1 parent f8727eb commit 41fc857

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

Sprint-3/1-implement-and-rewrite-tests/implement/3-get-card-value.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,23 +37,24 @@ function getCardValue(card) {
3737
"Q",
3838
"K",
3939
];
40-
const suite = ["♠", "♥", "♦", "♣"];
40+
const suit = ["♠", "♥", "♦", "♣"];
4141

42-
const cardFace = card.slice(0, -1);
42+
const cardValue = card.slice(0, -1);
43+
console.log("card value: " + cardValue);
4344
const cardSuit = card[card.length - 1];
4445

45-
if (!rank.includes(cardFace) || !suite.includes(cardSuit)) {
46+
if (!rank.includes(cardValue) || !suit.includes(cardSuit)) {
4647
throw new Error("invalid input");
4748
}
4849

49-
if ("A" === cardFace) {
50+
if ("A" === cardValue) {
5051
return 11;
5152
}
52-
if (["J", "Q", "K"].includes(cardFace)) {
53+
if (["J", "Q", "K"].includes(cardValue)) {
5354
return 10;
5455
}
5556

56-
return Number(cardFace);
57+
return Number(cardValue);
5758
}
5859

5960
// The line below allows us to load the getCardValue function into tests in other files.

Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/3-get-card-value.test.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,27 @@ test(`Should return 11 when given an ace card`, () => {
1313
// Number Cards (2-10)
1414
// Face Cards (J, Q, K)
1515
// Invalid Cards
16+
test(`Should return 10 when given a K, Q or J card`, () => {
17+
expect(getCardValue("K♠")).toEqual(10);
18+
expect(getCardValue("Q♠")).toEqual(10);
19+
expect(getCardValue("J♠")).toEqual(10);
20+
});
21+
22+
test(`Should return the number of the card given a numeric card card`, () => {
23+
expect(getCardValue("10♠")).toEqual(10);
24+
expect(getCardValue("2♠")).toEqual(2);
25+
expect(getCardValue("3♠")).toEqual(3);
26+
});
27+
28+
test(`Should throw an error if invalid card`, () => {
29+
expect(() => {
30+
getCardValue("2😬");
31+
}).toThrow();
32+
expect(() => {
33+
getCardValue("22♠");
34+
}).toThrow();
35+
});
1636

1737
// To learn how to test whether a function throws an error as expected in Jest,
1838
// please refer to the Jest documentation:
1939
// https://jestjs.io/docs/expect#tothrowerror
20-

0 commit comments

Comments
 (0)