Skip to content

Commit 63df014

Browse files
committed
added Jest tests covering positives, negatives, zeros and decimals
1 parent b083488 commit 63df014

3 files changed

Lines changed: 49 additions & 2 deletions

File tree

Sprint-3/1-implement-and-rewrite-tests/implement/2-is-proper-fraction.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ function isProperFraction(numerator, denominator) {
1616
return false;
1717
}
1818
return Math.abs(numerator) < Math.abs(denominator);
19+
1920
}
2021

2122
// The line below allows us to load the isProperFraction function into tests in other files.
@@ -35,3 +36,19 @@ function assertEquals(actualOutput, targetOutput) {
3536

3637
// Example: 1/2 is a proper fraction
3738
assertEquals(isProperFraction(1, 2), true);
39+
assertEquals(isProperFraction(2, 4), true);
40+
assertEquals(isProperFraction(4, 2), false);
41+
42+
assertEquals(isProperFraction(0, 2), true);
43+
assertEquals(isProperFraction(2, 0), false);
44+
assertEquals(isProperFraction(0, -5), true);
45+
assertEquals(isProperFraction(
46+
47+
assertEquals(isProperFraction(3, 3), false);
48+
assertEquals(isProperFraction(-3, 3), false);
49+
assertEquals(isProperFraction(3, -3), false);
50+
assertEquals(isProperFraction(-3, -3), false);
51+
52+
assertEquals(isProperFraction(-1, 2), true);
53+
assertEquals(isProperFraction(1, -2), true);
54+
assertEquals(isProperFraction(-3, -4), true);

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,19 @@
2323

2424
function getCardValue(card) {
2525
// TODO: Implement this function
26+
if (typeof card !== "string") {
27+
throw new Error("Card must be a string");
28+
}
29+
30+
const match = card.match(/^(A|10|[2-9]|J|Q|K)([])$/);
31+
if (!match) {
32+
throw new Error("Invalid card format");
33+
}
34+
35+
const rank = match[1];
36+
if (rank === "A") return 11;
37+
if (rank === "J" || rank === "Q" || rank === "K") return 10;
38+
return Number(rank);
2639
}
2740

2841
// 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: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,28 @@ const getCardValue = require("../implement/3-get-card-value");
55
// TODO: Write tests in Jest syntax to cover all possible outcomes.
66

77
// Case 1: Ace (A)
8-
test(`Should return 11 when given an ace card`, () => {
8+
test("Should return 11 when given an ace card", () => {
99
expect(getCardValue("A♠")).toEqual(11);
1010
});
1111

12+
test("face cards return 10", () => {
13+
expect(getCardValue("J♣")).toEqual(10);
14+
expect(getCardValue("Q♦")).toEqual(10);
15+
expect(getCardValue("K♦")).toEqual(10);
16+
});
17+
18+
test("number cards return their numeric value", () => {
19+
expect(getCardValue("2♥")).toEqual(2);
20+
expect(getCardValue("10♥")).toEqual(10);
21+
expect(getCardValue("9♥")).toEqual(9);
22+
});
23+
24+
test("invalid input throw", () => {
25+
expect(() => getCardValue("invalid")).toThrow();
26+
expect(() => getCardValue("A")).toThrow();
27+
expect(() => getCardValue(null)).toThrow();
28+
});
29+
1230
// Suggestion: Group the remaining test data into these categories:
1331
// Number Cards (2-10)
1432
// Face Cards (J, Q, K)
@@ -17,4 +35,3 @@ test(`Should return 11 when given an ace card`, () => {
1735
// To learn how to test whether a function throws an error as expected in Jest,
1836
// please refer to the Jest documentation:
1937
// https://jestjs.io/docs/expect#tothrowerror
20-

0 commit comments

Comments
 (0)