Skip to content

Commit 9cc4680

Browse files
committed
write tests for function getCardValue
1 parent 9fd83a0 commit 9cc4680

File tree

2 files changed

+191
-2
lines changed

2 files changed

+191
-2
lines changed

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

Lines changed: 136 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,147 @@ function assertEquals(actualOutput, targetOutput) {
4141
// Examples:
4242
assertEquals(getCardValue("9♠"), 9);
4343

44+
assertEquals(getCardValue("4♣"), 4);
45+
46+
assertEquals(getCardValue("A♥"), 11);
47+
48+
assertEquals(getCardValue("J♦"), 10);
49+
50+
assertEquals(getCardValue("Q♣"), 10);
51+
52+
assertEquals(getCardValue("K♠"), 10);
53+
54+
assertEquals(getCardValue("K♦"), 10);
55+
56+
assertEquals(getCardValue("10♥"), 10);
57+
58+
assertEquals(getCardValue("2♦"), 2);
59+
4460
// Handling invalid cards
4561
try {
4662
getCardValue("invalid");
4763

4864
// This line will not be reached if an error is thrown as expected
4965
console.error("Error was not thrown for invalid card");
50-
} catch (e) {}
66+
} catch (err) {
67+
console.error(error);
68+
}
5169

5270
// What other invalid card cases can you think of?
71+
72+
try {
73+
getCardValue("");
74+
75+
// This line will not be reached if an error is thrown as expected
76+
console.error("Error was not thrown for invalid card");
77+
} catch (err) {
78+
console.error(error);
79+
}
80+
81+
try {
82+
getCardValue("1♥");
83+
84+
// This line will not be reached if an error is thrown as expected
85+
console.error("Error was not thrown for invalid card");
86+
} catch (err) {
87+
console.error(error);
88+
}
89+
90+
try {
91+
getCardValue("11♠");
92+
93+
// This line will not be reached if an error is thrown as expected
94+
console.error("Error was not thrown for invalid card");
95+
} catch (err) {
96+
console.error(error);
97+
}
98+
99+
try {
100+
getCardValue("0♦");
101+
102+
// This line will not be reached if an error is thrown as expected
103+
console.error("Error was not thrown for invalid card");
104+
} catch (err) {
105+
console.error(error);
106+
}
107+
108+
try {
109+
getCardValue("4.8♣");
110+
111+
// This line will not be reached if an error is thrown as expected
112+
console.error("Error was not thrown for invalid card");
113+
} catch (err) {
114+
console.error(error);
115+
}
116+
117+
try {
118+
getCardValue("G♦");
119+
120+
// This line will not be reached if an error is thrown as expected
121+
console.error("Error was not thrown for invalid card");
122+
} catch (err) {
123+
console.error(error);
124+
}
125+
126+
try {
127+
getCardValue("9");
128+
129+
// This line will not be reached if an error is thrown as expected
130+
console.error("Error was not thrown for invalid card");
131+
} catch (err) {
132+
console.error(error);
133+
}
134+
135+
try {
136+
getCardValue("K");
137+
138+
// This line will not be reached if an error is thrown as expected
139+
console.error("Error was not thrown for invalid card");
140+
} catch (err) {
141+
console.error(error);
142+
}
143+
144+
try {
145+
getCardValue("9M");
146+
147+
// This line will not be reached if an error is thrown as expected
148+
console.error("Error was not thrown for invalid card");
149+
} catch (err) {
150+
console.error(error);
151+
}
152+
153+
try {
154+
getCardValue("♠3");
155+
156+
// This line will not be reached if an error is thrown as expected
157+
console.error("Error was not thrown for invalid card");
158+
} catch (err) {
159+
console.error(error);
160+
}
161+
162+
try {
163+
getCardValue(6);
164+
165+
// This line will not be reached if an error is thrown as expected
166+
console.error("Error was not thrown for invalid card");
167+
} catch (err) {
168+
console.error(error);
169+
}
170+
171+
try {
172+
getCardValue("A♦!");
173+
174+
// This line will not be reached if an error is thrown as expected
175+
console.error("Error was not thrown for invalid card");
176+
} catch (err) {
177+
console.error(error);
178+
}
179+
180+
try {
181+
getCardValue("♥");
182+
183+
// This line will not be reached if an error is thrown as expected
184+
console.error("Error was not thrown for invalid card");
185+
} catch (err) {
186+
console.error(error);
187+
}

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

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,68 @@ const getCardValue = require("../implement/3-get-card-value");
77
// Case 1: Ace (A)
88
test(`Should return 11 when given an ace card`, () => {
99
expect(getCardValue("A♠")).toEqual(11);
10+
expect(getCardValue("A♦")).toEqual(11);
11+
expect(getCardValue("A♥")).toEqual(11);
12+
expect(getCardValue("A♣")).toEqual(11);
1013
});
1114

1215
// Suggestion: Group the remaining test data into these categories:
1316
// Number Cards (2-10)
17+
test(`Should return the number value of a card when given an a number card`, () => {
18+
expect(getCardValue("7♠")).toEqual(7);
19+
expect(getCardValue("10♠")).toEqual(10);
20+
expect(getCardValue("2♦")).toEqual(2);
21+
expect(getCardValue("5♥")).toEqual(5);
22+
expect(getCardValue("10♣")).toEqual(10);
23+
});
24+
1425
// Face Cards (J, Q, K)
26+
test(`Should return 10 when given a face card`, () => {
27+
expect(getCardValue("J♦")).toEqual(10);
28+
expect(getCardValue("Q♠")).toEqual(10);
29+
expect(getCardValue("K♠")).toEqual(10);
30+
expect(getCardValue("K♦")).toEqual(10);
31+
expect(getCardValue("J♥")).toEqual(10);
32+
expect(getCardValue("Q♣")).toEqual(10);
33+
});
34+
1535
// Invalid Cards
36+
test(`Should throw an error when given an invalid card`, () => {
37+
expect(() => getCardValue("")).toThrow(
38+
"Invalid card format! Expected rank followed by suit, for example A♠, 10♥, K♦, or 7♣."
39+
);
40+
expect(() => getCardValue("invalid")).toThrow(
41+
"Invalid card format! Expected rank followed by suit, for example A♠, 10♥, K♦, or 7♣."
42+
);
43+
expect(() => getCardValue("11♠")).toThrow(
44+
"Invalid card format! Expected rank followed by suit, for example A♠, 10♥, K♦, or 7♣."
45+
);
46+
expect(() => getCardValue("0♦")).toThrow(
47+
"Invalid card format! Expected rank followed by suit, for example A♠, 10♥, K♦, or 7♣."
48+
);
49+
expect(() => getCardValue("4.8♣")).toThrow(
50+
"Invalid card format! Expected rank followed by suit, for example A♠, 10♥, K♦, or 7♣."
51+
);
52+
expect(() => getCardValue("G♦")).toThrow(
53+
"Invalid card format! Expected rank followed by suit, for example A♠, 10♥, K♦, or 7♣."
54+
);
55+
expect(() => getCardValue("9")).toThrow(
56+
"Invalid card format! Expected rank followed by suit, for example A♠, 10♥, K♦, or 7♣."
57+
);
58+
expect(() => getCardValue("K")).toThrow(
59+
"Invalid card format! Expected rank followed by suit, for example A♠, 10♥, K♦, or 7♣."
60+
);
61+
expect(() => getCardValue(6)).toThrow(
62+
"Invalid card format! Expected rank followed by suit, for example A♠, 10♥, K♦, or 7♣."
63+
);
64+
expect(() => getCardValue("♠3")).toThrow(
65+
"Invalid card format! Expected rank followed by suit, for example A♠, 10♥, K♦, or 7♣."
66+
);
67+
expect(() => getCardValue("9M")).toThrow(
68+
"Invalid card format! Expected rank followed by suit, for example A♠, 10♥, K♦, or 7♣."
69+
);
70+
});
1671

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

0 commit comments

Comments
 (0)