Skip to content

Commit 0b216f8

Browse files
committed
changes made to angle test
1 parent 70ebf35 commit 0b216f8

2 files changed

Lines changed: 22 additions & 31 deletions

File tree

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

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,12 @@
2222
// execute the code to ensure all tests pass.
2323

2424
function getCardValue(card) {
25-
// Basic validation: validating the input before slicing
2625
if (card === "") {
2726
throw new Error("No card was played");
2827
}
29-
30-
const rank = card.slice(0, -1).toUpperCase();
31-
const suit = card.slice(-1);
28+
if (card.length < 2 || card.length > 3) {
29+
throw new Error("Invalid card");
30+
}
3231

3332
const validSuits = ["♠", "♥", "♦", "♣"];
3433
const validRanks = [
@@ -47,21 +46,19 @@ function getCardValue(card) {
4746
"K",
4847
];
4948

50-
// Suit and rank validation
49+
const suit = card.slice(-1);
5150
if (!validSuits.includes(suit)) {
5251
throw new Error("Invalid card: suit is not recognised");
5352
}
54-
if (validRanks.includes(rank)) {
55-
if (rank === "A") {
56-
return 11;
57-
} else if (rank === "J" || rank === "Q" || rank === "K") {
58-
return 10;
59-
} else {
60-
return Number(rank);
61-
}
62-
} else {
63-
throw new Error("Invalid rank");
53+
54+
const rank = card.slice(0, -1).toUpperCase();
55+
if (!validRanks.includes(rank)) {
56+
throw new Error("Invalid card: rank is not recognised");
6457
}
58+
if (rank === "A") return 11;
59+
if (["J", "Q", "K"].includes(rank)) return 10;
60+
61+
return Number(rank);
6562
}
6663

6764
// 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: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@
22
// We will use the same function, but write tests for it using Jest in this file.
33
const getCardValue = require("../implement/3-get-card-value");
44

5-
6-
// Case 1: Ace (A)
5+
// Ace (A)
76
test(`Should return 11 when given an ace card`, () => {
87
expect(getCardValue("A♠")).toEqual(11);
98
});
109

11-
// Suggestion: Group the remaining test data into these categories: // ♠ ♥ ♦ ♣
1210
// Number Cards (2-10)
13-
test(`Should return the card's numeric rank for cards 2 through 10`, () =>{
11+
test(`Should return the card's numeric rank for cards 2 through 10`, () => {
1412
expect(getCardValue("2♠")).toEqual(2);
1513
expect(getCardValue("3♥")).toEqual(3);
1614
expect(getCardValue("4♦")).toEqual(4);
@@ -20,26 +18,22 @@ test(`Should return the card's numeric rank for cards 2 through 10`, () =>{
2018
expect(getCardValue("8♦")).toEqual(8);
2119
expect(getCardValue("9♣")).toEqual(9);
2220
expect(getCardValue("10♠")).toEqual(10);
21+
});
2322

24-
})
2523
// Face Cards (J, Q, K)
2624
test(`should return 10 When the card is a face card ("J", "Q", "K")`, () => {
2725
expect(getCardValue("j♠")).toEqual(10);
2826
expect(getCardValue("k♦")).toEqual(10);
2927
expect(getCardValue("q♣")).toEqual(10);
28+
});
3029

31-
})
3230
// Invalid Cards
3331
test("should throw an error when an invalid card is played", () => {
3432
expect(() => getCardValue("")).toThrow("No card was played");
35-
expect(() => getCardValue("1009♣")).toThrow("Invalid card played, rank and suit cannot be less " +
36-
"than 1 or more than 3")
37-
33+
expect(() => getCardValue("X♠")).toThrow(
34+
"Invalid card: rank is not recognised"
35+
);
36+
expect(() => getCardValue("9X")).toThrow(
37+
"Invalid card: suit is not recognised"
38+
);
3839
});
39-
40-
41-
42-
// To learn how to test whether a function throws an error as expected in Jest,
43-
// please refer to the Jest documentation:
44-
// https://jestjs.io/docs/expect#tothrowerror
45-

0 commit comments

Comments
 (0)