Skip to content

Commit 12da92f

Browse files
committed
Rewrite get-card-value test in Jest, fix the Node test, modify the implementation
1 parent 29c23e6 commit 12da92f

3 files changed

Lines changed: 113 additions & 18 deletions

File tree

‎implement-and-rewrite-tests/implement-tests-with-node-test/3-get-card-value.test.js‎

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,64 @@ import { getCardValue } from "../implement/3-get-card-value.js";
55

66
// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
77

8+
// test("Valid single-digit card", () => {
9+
// assert.equal(getCardValue("9♠"), 9);
10+
// });
11+
12+
// test("Arbitrary non-card string", () => {
13+
// assert.throws(
14+
// () => getCardValue("invalid"),
15+
// /Expected a number followed by a suit, but got "invalid"/
16+
// );
17+
// });
18+
819
test("Valid single-digit card", () => {
9-
assert.equal(getCardValue("9♠"), 9);
20+
assert.equal(getCardValue("2♠"), 2);
1021
});
11-
12-
test("Arbitrary non-card string", () => {
13-
assert.throws(
14-
() => getCardValue("invalid"),
15-
/Expected a number followed by a suit, but got "invalid"/,
16-
"Expected clear error"
17-
);
22+
test("Valid double-digit card", () => {
23+
assert.equal(getCardValue("10♥"), 10);
24+
});
25+
test("Valid king card", () => {
26+
assert.equal(getCardValue("Q♣"), 10);
27+
});
28+
test("Valid queen card", () => {
29+
assert.equal(getCardValue("K♠"), 10);
30+
});
31+
test("Valid jack card", () => {
32+
assert.equal(getCardValue("J♦"), 10);
33+
});
34+
test("Valid ace card", () => {
35+
assert.equal(getCardValue("A♠"), 11);
1836
});
1937

2038
// TODO: What other invalid card cases can you think of?
2139

22-
test("Valid double digit card", () => {
23-
assert.equal(getCardValue("10♠"), 10);
24-
});
25-
2640
test("No suit symbol", () => {
2741
assert.throws(
2842
() => getCardValue("A"),
2943
Error,
3044
"Should throw an error for card without suit"
3145
);
3246
});
47+
48+
test("Invalid number", () => {
49+
assert.throws(
50+
() => getCardValue("0"),
51+
Error,
52+
"Should throw an error for card with invalid number"
53+
);
54+
});
55+
test("Empty input", () => {
56+
assert.throws(
57+
() => getCardValue(""),
58+
Error,
59+
"Should throw an error for empty input"
60+
);
61+
});
62+
test("Any other invalid input", () => {
63+
assert.throws(
64+
() => getCardValue("10♥♥"),
65+
Error,
66+
"Should throw an error for an invalid input"
67+
);
68+
});

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,14 @@
2424
// execute the code to ensure all tests pass.
2525

2626
function getCardValue(card) {
27+
if (card == "") {
28+
throw new Error(`Missing a specific number and specific suit symbol`);
29+
}
2730
const cardRank = card.slice(0, -1);
2831
const suit = card.slice(-1);
2932

30-
if (cardRank === card) {
31-
throw new Error(`Expected a number followed by a suit, but got "${card}"`);
32-
}
33-
3433
if (!["♠", "♥", "♦", "♣"].includes(suit)) {
35-
throw new Error(`Expected a number followed by a suit, but got "${card}"`);
34+
throw new Error(`Missing a specific suit symbol`);
3635
}
3736

3837
if (cardRank == "A") {
@@ -54,7 +53,7 @@ function getCardValue(card) {
5453
) {
5554
return Number(cardRank);
5655
} else {
57-
throw new Error(`Expected a number followed by a suit, but got "${card}"`);
56+
throw new Error(`Missing a specific number and specific suit symbol`);
5857
}
5958
}
6059

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

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,64 @@ test(`Should return 11 when given an ace card`, () => {
1515
// To learn how to test whether a function throws an error as expected in Jest,
1616
// please refer to the Jest documentation:
1717
// https://jestjs.io/docs/expect#tothrowerror
18+
// -----number cards------
19+
test(`Should return 2 when given a 2 card`, () => {
20+
expect(getCardValue("2♥")).toEqual(2);
21+
});
1822

23+
test(`Should return 3 when given a 3 card`, () => {
24+
expect(getCardValue("3♣")).toEqual(3);
25+
});
26+
test(`Should return 4 when given a 4 card`, () => {
27+
expect(getCardValue("4♦")).toEqual(4);
28+
});
29+
test(`Should return 5 when given a 5 card`, () => {
30+
expect(getCardValue("5♠")).toEqual(5);
31+
});
32+
test(`Should return 6 when given a 6 card`, () => {
33+
expect(getCardValue("6♦")).toEqual(6);
34+
});
35+
test(`Should return 7 when given a 7 card`, () => {
36+
expect(getCardValue("7♥")).toEqual(7);
37+
});
38+
test(`Should return 8 when given a 8 card`, () => {
39+
expect(getCardValue("8♠")).toEqual(8);
40+
});
41+
test(`Should return 9 when given a 9 card`, () => {
42+
expect(getCardValue("9♥")).toEqual(9);
43+
});
44+
test(`Should return 10 when given a 10 card`, () => {
45+
expect(getCardValue("10♣")).toEqual(10);
46+
});
47+
// -----remaining face cards-----
48+
test(`Should return 10 when given a jack card`, () => {
49+
expect(getCardValue("J♥")).toEqual(10);
50+
});
51+
test(`Should return 10 when given a king card`, () => {
52+
expect(getCardValue("K♣")).toEqual(10);
53+
});
54+
test(`Should return 10 when given a queen card`, () => {
55+
expect(getCardValue("Q♥")).toEqual(10);
56+
});
57+
// -----invalid cards-----
58+
test("Should throw an error for card without suit", () => {
59+
expect(() => getCardValue("A")).toThrow("Missing a specific suit symbol");
60+
});
61+
62+
test("Should throw an error for card with invalid number", () => {
63+
expect(() => getCardValue("0♥")).toThrow(
64+
"Missing a specific number and specific suit symbol"
65+
);
66+
});
67+
68+
test("Should throw an error for empty input", () => {
69+
expect(() => getCardValue("")).toThrow(
70+
"Missing a specific number and specific suit symbol"
71+
);
72+
});
73+
74+
test("Should throw an error for an invalid input", () => {
75+
expect(() => getCardValue("J♥♥")).toThrow(
76+
"Missing a specific number and specific suit symbol"
77+
);
78+
});

0 commit comments

Comments
 (0)