Skip to content

Commit af97844

Browse files
committed
rewrite all three tests in Jest with full boundary coverage
1 parent d364d62 commit af97844

3 files changed

Lines changed: 71 additions & 0 deletions

File tree

Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/1-get-angle-type.test.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
// We will use the same function, but write tests for it using Jest in this file.
33
const getAngleType = require("../implement/1-get-angle-type");
44

5+
// Jest docs: https://jestjs.io/docs/getting-started
6+
// freeCodeCamp intro to testing: https://www.freecodecamp.org/news/how-to-start-unit-testing-javascript
7+
58
// TODO: Write tests in Jest syntax to cover all cases/outcomes,
69
// including boundary and invalid cases.
710

@@ -14,7 +17,33 @@ test(`should return "Acute angle" when (0 < angle < 90)`, () => {
1417
});
1518

1619
// Case 2: Right angle
20+
test(`should return "Right angle" when angle is exactly 90`, () => {
21+
expect(getAngleType(90)).toEqual("Right angle");
22+
});
23+
1724
// Case 3: Obtuse angles
25+
test(`should return "Obtuse angle" when (90 < angle < 180)`, () => {
26+
expect(getAngleType(91)).toEqual("Obtuse angle"); // boundary: just above 90
27+
expect(getAngleType(120)).toEqual("Obtuse angle"); // normal obtuse angle
28+
expect(getAngleType(179)).toEqual("Obtuse angle"); // boundary: just below 180
29+
});
30+
1831
// Case 4: Straight angle
32+
test(`should return "Straight angle" when angle is exactly 180`, () => {
33+
expect(getAngleType(180)).toEqual("Straight angle");
34+
});
35+
1936
// Case 5: Reflex angles
37+
test(`should return "Reflex angle" when (180 < angle < 360)`, () => {
38+
expect(getAngleType(181)).toEqual("Reflex angle"); // boundary: just above 180
39+
expect(getAngleType(270)).toEqual("Reflex angle"); // normal reflex angle
40+
expect(getAngleType(359)).toEqual("Reflex angle"); // boundary: just below 360
41+
});
42+
2043
// Case 6: Invalid angles
44+
test(`should return "Invalid angle" for angles outside valid range`, () => {
45+
expect(getAngleType(0)).toEqual("Invalid angle"); // boundary: exactly 0
46+
expect(getAngleType(360)).toEqual("Invalid angle"); // boundary: exactly 360
47+
expect(getAngleType(-10)).toEqual("Invalid angle"); // negative number
48+
expect(getAngleType(400)).toEqual("Invalid angle"); // over 360
49+
});

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,22 @@ const isProperFraction = require("../implement/2-is-proper-fraction");
88
test(`should return false when denominator is zero`, () => {
99
expect(isProperFraction(1, 0)).toEqual(false);
1010
});
11+
12+
// Proper fractions: numerator smaller than denominator
13+
test("should return true for a proper fraction", () => {
14+
expect(isProperFraction(1, 2)).toEqual(true); // basic proper fraction
15+
expect(isProperFraction(3, 4)).toEqual(true);
16+
expect(isProperFraction(-1, 2)).toEqual(true); // negative numerator still proper
17+
expect(isProperFraction(0, 5)).toEqual(true); // 0/5 = 0, which is proper
18+
});
19+
20+
// Improper fractions: numerator greater than denominator
21+
test("should return false for an improper fraction", () => {
22+
expect(isProperFraction(2, 1)).toEqual(false);
23+
expect(isProperFraction(5, 3)).toEqual(false);
24+
});
25+
26+
// Boundary: equal numerator and denominator - 4/4 = 1, a whole number not a fraction
27+
test("should return false when numerator equals denominator", () => {
28+
expect(isProperFraction(4, 4)).toEqual(false);
29+
});

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ 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);
1011
});
1112

1213
// Suggestion: Group the remaining test data into these categories:
@@ -18,3 +19,25 @@ test(`Should return 11 when given an ace card`, () => {
1819
// please refer to the Jest documentation:
1920
// https://jestjs.io/docs/expect#tothrowerror
2021

22+
// Number cards -> return their numeric value (2-10)
23+
test("should return the numeric value for number cards", () => {
24+
expect(getCardValue("2♦")).toEqual(2); // boundary: lowest number card
25+
expect(getCardValue("9♣")).toEqual(9);
26+
expect(getCardValue("10♥")).toEqual(10); // boundary: highest number card
27+
});
28+
29+
//Face cards -> Jack, Queen and King are all worth 10
30+
test("should return 10 for face cards (J, Q, K)", () => {
31+
expect(getCardValue("J♣")).toEqual(10);
32+
expect(getCardValue("Q♦")).toEqual(10);
33+
expect(getCardValue("K♠")).toEqual(10);
34+
});
35+
36+
// Invalid cards -> function should throw an error
37+
// We wrap the call in () => so Jest can catch the error without crashing the test
38+
// MDN on arrow functions: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions
39+
test("should throw an error for invalid cards", () => {
40+
expect(() => getCardValue("invalid")).toThrow();
41+
expect(() => getCardValue("1♠")).toThrow(); // 1 is not a valid rank
42+
expect(() => getCardValue("")).toThrow(); // empty string
43+
});

0 commit comments

Comments
 (0)