Skip to content

Commit e3943d2

Browse files
tested cards with jest and updated comments
1 parent 44c463c commit e3943d2

File tree

2 files changed

+76
-41
lines changed

2 files changed

+76
-41
lines changed

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

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

2424
function getCardValue(card) {
25-
2625
if (typeof card !== "string" || card.length < 2) {
27-
throw new Error("Invalid card");
26+
throw new Error("Invalid card");
2827
}
29-
// Handle "10" which is 2 characters
28+
// Handle "10" which is like "10♥"
3029
if (card.startsWith("10")) {
3130
return 10;
3231
}
32+
//if more than 2 characters and not starting with a 10 card
33+
if (card.length > 2) {
34+
throw new Error("Invalid card");
35+
}
36+
37+
// remaining cards must be 2 characters long
38+
39+
const validSuits = ["♠", "♥", "♦", "♣"];
40+
const suit = card[card.length - 1];
3341

3442
const firstChar = card[0];
3543

3644
// check if picture cards
3745
if (firstChar === "A") return 11;
38-
if (firstChar === "J" || firstChar === "Q" || firstChar === "K" ) return 10;
46+
if (firstChar === "J" || firstChar === "Q" || firstChar === "K") return 10;
3947

4048
// check if number is between 2 and 9, 10 has already been checked for and there should be no other valid cards
4149

4250
const num = Number(firstChar);
4351

4452
if (!isNaN(num) && num >= 2 && num <= 9) {
4553
return num;
46-
// for everything else
47-
} else {
48-
throw new Error("Invalid card");
49-
}
54+
// for everything else
55+
} else {
56+
throw new Error("Invalid card");
57+
}
5058
}
5159
// The line below allows us to load the getCardValue function into tests in other files.
5260
// This will be useful in the "rewrite tests with jest" step.
@@ -69,17 +77,6 @@ assertEquals(getCardValue("A♠"), 11);
6977
assertEquals(getCardValue("Q♦"), 10);
7078
assertEquals(getCardValue("K♣"), 10);
7179

72-
// Handling invalid cards
73-
try {
74-
getCardValue("♠J");
75-
console.error("Error was not thrown for invalid card");
76-
} catch (e) {}
77-
78-
try {
79-
getCardValue("invalid");
80-
console.error("Error was not thrown for invalid card");
81-
} catch (e) {}
82-
8380
// Handling invalid cards
8481
try {
8582
getCardValue("♠J");
@@ -91,16 +88,8 @@ try {
9188
console.error("Error was not thrown for invalid card");
9289
} catch (e) {}
9390

94-
9591
try {
96-
getCardValue("♠J");
97-
console.error("Error was not thrown for invalid card");
98-
} catch (e) {
99-
console.log("Invalid card detected");
100-
}
101-
102-
try {
103-
getCardValue("invalid");
92+
getCardValue("22");
10493
console.error("Error was not thrown for invalid card");
10594
} catch (e) {}
10695

@@ -110,12 +99,24 @@ console.log(getCardValue("J♥"));
11099
console.log(getCardValue("A♠"));
111100
console.log(getCardValue("Q♦"));
112101
console.log(getCardValue("K♣"));
113-
114-
// This line will not be reached if an error is thrown as expected
102+
103+
// This line will not be reached if an error is thrown as expected
115104
try {
116-
sole.error("Error was not thrown for invalid card");
105+
console.error("Error was not thrown for invalid card");
117106
} catch (e) {}
118107

119108
// What other invalid card cases can you think of?
120-
// There could be cards with special characters
109+
110+
// There could be cards with special characters.
111+
121112
// There could be cards with two numbers rather than a number and a suite
113+
// These will not be picked up because the code only checks for if starts with 10 or if the first character
114+
// is a number between 2 and 9, so cards like "22" would be valid because the first number
115+
// is 2, but the second character is not checked for validity. It is also 2 characters
116+
// so will not cause an error when the length is checked.
117+
118+
// Since the second character is not checked it could be 2D which is not a valid card but
119+
// would be accepted because the first character is 2 and the second character is not checked for validity
120+
121+
// When the card is checked if it begins with 10 it does check if it has a valid suite
122+
// as only the first 2 characters are checked so it could be 10DEVON or 10♥♥.
Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,54 @@
11
// This statement loads the getCardValue function you wrote in the implement directory.
22
// We will use the same function, but write tests for it using Jest in this file.
3+
const { createTestScheduler } = require("jest");
34
const getCardValue = require("../implement/3-get-card-value");
45

5-
// TODO: Write tests in Jest syntax to cover all possible outcomes.
6-
76
// Case 1: Ace (A)
87
test(`Should return 11 when given an ace card`, () => {
98
expect(getCardValue("A♠")).toEqual(11);
109
});
1110

12-
// Suggestion: Group the remaining test data into these categories:
13-
// Number Cards (2-10)
14-
// Face Cards (J, Q, K)
15-
// Invalid Cards
11+
// Case 2: Face Cards (J, Q, K)
12+
test(`Should return 10 when given a Jack card`, () => {
13+
expect(getCardValue("J♥")).toEqual(10);
14+
});
1615

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
16+
test(`Should return 10 when given a Queen card`, () => {
17+
expect(getCardValue("Q♦")).toEqual(10);
18+
});
19+
20+
test(`Should return 10 when given a King card`, () => {
21+
expect(getCardValue("K♣")).toEqual(10);
22+
});
23+
24+
// Case 3: Number Cards (2-10)
25+
test(`Should return 2 when given a 2 card`, () => {
26+
expect(getCardValue("2♠")).toEqual(2);
27+
});
28+
29+
test(`Should return 10 when given a 10 card`, () => {
30+
expect(getCardValue("10♥")).toEqual(10);
31+
});
32+
33+
// Case 4: Invalid Cards
34+
test(`Should throw an error when given an invalid card`, () => {
35+
expect(() => getCardValue("♠J")).toThrow();
36+
});
37+
38+
test(`Should throw an error when given an invalid card`, () => {
39+
expect(() => getCardValue("invalid")).toThrow();
40+
});
41+
42+
test(`Should throw an error when given an invalid card`, () => {
43+
expect(() => getCardValue("12♠")).toThrow();
44+
});
45+
46+
test(`Should throw an error when given an invalid card`, () => {
47+
expect(() => getCardValue("1")).toThrow();
48+
});
2049

50+
// when I tested with test(`Should throw an error when given an invalid card`, () => {
51+
// expect(() => getCardValue("22")).toThrow(); it did not throw an error because 22 passes the
52+
// test of first number between and 9 and being 2 characters long,
53+
// but it is not a valid card because the second character is not a valid suite.
54+
// The second character is not checked.

0 commit comments

Comments
 (0)