Skip to content

Commit 6331dc0

Browse files
committed
Practicing test+Jest tests
1 parent 8f3d6cf commit 6331dc0

File tree

6 files changed

+102
-2
lines changed

6 files changed

+102
-2
lines changed

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,19 @@ function getAngleType(angle) {
1111
if (angle === 90) {
1212
return "Right angle";
1313
}
14+
else if (angle < 90) {
15+
return "Acute angle"
16+
}
17+
else if (angle > 90 && angle < 180){
18+
return "Obtuse angle"
19+
}
20+
else if ( angle === 180){
21+
return "Straight angle"
22+
}
23+
else if (angle > 180 && angle < 360) {
24+
return "Reflex angle"
25+
}
26+
1427
// Run the tests, work out what Case 2 is testing, and implement the required code here.
1528
// Then keep going for the other cases, one at a time.
1629
}
@@ -50,14 +63,19 @@ assertEquals(acute, "Acute angle");
5063
// When the angle is greater than 90 degrees and less than 180 degrees,
5164
// Then the function should return "Obtuse angle"
5265
const obtuse = getAngleType(120);
66+
assertEquals(obtuse,"Obtuse angle");
5367
// ====> write your test here, and then add a line to pass the test in the function above
5468

5569
// Case 4: Identify Straight Angles:
5670
// When the angle is exactly 180 degrees,
5771
// Then the function should return "Straight angle"
5872
// ====> write your test here, and then add a line to pass the test in the function above
73+
const straight = getAngleType(180);
74+
assertEquals(straight, "Straight angle");
5975

6076
// Case 5: Identify Reflex Angles:
6177
// When the angle is greater than 180 degrees and less than 360 degrees,
6278
// Then the function should return "Reflex angle"
63-
// ====> write your test here, and then add a line to pass the test in the function above
79+
// ====> write your test here, and then add a line to pass the test in the function above
80+
const reflex = getAngleType(190);
81+
assertEquals(reflex, "Reflex angle" );

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@
88
// write one test at a time, and make it pass, build your solution up methodically
99

1010
function isProperFraction(numerator, denominator) {
11-
if (numerator < denominator) {
11+
if (Math.abs(numerator) < Math.abs(denominator)) {
1212
return true;
1313
}
14+
else {
15+
return false;
16+
}
1417
}
1518

1619
// The line below allows us to load the isProperFraction function into tests in other files.
@@ -46,14 +49,21 @@ assertEquals(improperFraction, false);
4649
// target output: true
4750
// Explanation: The fraction -4/7 is a proper fraction because the absolute value of the numerator (4) is less than the denominator (7). The function should return true.
4851
const negativeFraction = isProperFraction(-4, 7);
52+
assertEquals(negativeFraction,true)
4953
// ====> complete with your assertion
5054

5155
// Equal Numerator and Denominator check:
5256
// Input: numerator = 3, denominator = 3
5357
// target output: false
5458
// Explanation: The fraction 3/3 is not a proper fraction because the numerator is equal to the denominator. The function should return false.
5559
const equalFraction = isProperFraction(3, 3);
60+
assertEquals(equalFraction,false);
5661
// ====> complete with your assertion
5762

5863
// Stretch:
5964
// What other scenarios could you test for?
65+
const bothNegativeProperFraction = isProperFraction(-3, -5);
66+
assertEquals(bothNegativeProperFraction,true);
67+
//
68+
const bothNegativeNotProperFraction = isProperFraction(-6, -5);
69+
assertEquals(bothNegativeNotProperFraction,false);

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,21 @@
88
// write one test at a time, and make it pass, build your solution up methodically
99
// just make one change at a time -- don't rush -- programmers are deep and careful thinkers
1010
function getCardValue(card) {
11+
let rank = card.slice(0, card.length-1)
1112
if (rank === "A") {
1213
return 11;
1314
}
15+
else if (parseInt(rank) >= 2 && parseInt(rank)){
16+
return parseInt(rank)
17+
}
18+
else if( rank === "J" || rank === "Q" || rank === "K") {
19+
return 10;
20+
}
21+
else{
22+
throw new Error("Invalid card rank.")
23+
}
24+
25+
1426
}
1527

1628
// The line below allows us to load the getCardValue function into tests in other files.
@@ -39,19 +51,35 @@ assertEquals(aceofSpades, 11);
3951
// When the function is called with such a card,
4052
// Then it should return the numeric value corresponding to the rank (e.g., "5" should return 5).
4153
const fiveofHearts = getCardValue("5♥");
54+
assertEquals(fiveofHearts, 5);
4255
// ====> write your test here, and then add a line to pass the test in the function above
4356

4457
// Handle Face Cards (J, Q, K):
4558
// Given a card with a rank of "10," "J," "Q," or "K",
4659
// When the function is called with such a card,
4760
// Then it should return the value 10, as these cards are worth 10 points each in blackjack.
61+
const cardOfJ = getCardValue("J♥");
62+
assertEquals(cardOfJ, 10);
63+
const cardOfQ = getCardValue("Q♠");
64+
assertEquals(cardOfQ, 10);
65+
const cardOfK = getCardValue("K♠");
66+
assertEquals(cardOfK, 10);
4867

4968
// Handle Ace (A):
5069
// Given a card with a rank of "A",
5170
// When the function is called with an Ace,
5271
// Then it should, by default, assume the Ace is worth 11 points, which is a common rule in blackjack.
72+
const aceOfHeart = getCardValue("A♥");
73+
assertEquals(aceOfHeart, 11);
5374

5475
// Handle Invalid Cards:
5576
// Given a card with an invalid rank (neither a number nor a recognized face card),
5677
// When the function is called with such a card,
5778
// Then it should throw an error indicating "Invalid card rank."
79+
80+
81+
try {
82+
assertEquals(getCardValue("W♥"), "Invalid card rank");
83+
} catch (error) {
84+
console.log(error.message);
85+
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,28 @@ test("should identify right angle (90°)", () => {
1212
// Case 2: Identify Acute Angles:
1313
// When the angle is less than 90 degrees,
1414
// Then the function should return "Acute angle"
15+
test("should identify acute angle less then 90", () => {
16+
expect(getAngleType(45)).toEqual("Acute angle");
17+
});
18+
1519

1620
// Case 3: Identify Obtuse Angles:
1721
// When the angle is greater than 90 degrees and less than 180 degrees,
1822
// Then the function should return "Obtuse angle"
23+
test("should identify obtuse angle greater then 90", () => {
24+
expect(getAngleType(120)).toEqual("Obtuse angle");
25+
});
1926

2027
// Case 4: Identify Straight Angles:
2128
// When the angle is exactly 180 degrees,
2229
// Then the function should return "Straight angle"
30+
test("should identify Straight angle that = 180", () => {
31+
expect(getAngleType(180)).toEqual("Straight angle");
32+
});
2333

2434
// Case 5: Identify Reflex Angles:
2535
// When the angle is greater than 180 degrees and less than 360 degrees,
2636
// Then the function should return "Reflex angle"
37+
test("should identify Reflex angle greater then 180 and less then 360", () => {
38+
expect(getAngleType(220)).toEqual("Reflex angle");
39+
});

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,16 @@ test("should return true for a proper fraction", () => {
77
});
88

99
// Case 2: Identify Improper Fractions:
10+
test("should return false for improper fraction", () => {
11+
expect(isProperFraction(5, 2)).toEqual(false);
12+
});
1013

1114
// Case 3: Identify Negative Fractions:
15+
test("should return true for negative fraction", () => {
16+
expect(isProperFraction(-4, 7)).toEqual(true);
17+
});
1218

1319
// Case 4: Identify Equal Numerator and Denominator:
20+
test("should return false for Equal Numerator fraction", () => {
21+
expect(isProperFraction(3, 3)).toEqual(false);
22+
});

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,28 @@ test("should return 11 for Ace of Spades", () => {
88
});
99

1010
// Case 2: Handle Number Cards (2-10):
11+
test("Handle Number Cards (2-10)", () => {
12+
const fiveofHearts = getCardValue("5♥");
13+
expect(fiveofHearts).toEqual(5);
14+
});
1115
// Case 3: Handle Face Cards (J, Q, K):
16+
test("Case 3: Handle Face Cards (J, Q, K)", () => {
17+
const cardOfJ = getCardValue("J♥");
18+
expect(cardOfJ).toEqual(10);
19+
});
1220
// Case 4: Handle Ace (A):
21+
test("Case 3: Handle Face Cards (J, Q, K)", () => {
22+
const aceOfHeart = getCardValue("A♥");
23+
expect(aceOfHeart).toEqual(11);
24+
});
1325
// Case 5: Handle Invalid Cards:
26+
test("Case 3: Handle Invalid Cards", () => {
27+
try {
28+
assertEquals(getCardValue("W♥"), "Invalid card rank");
29+
} catch (error) {
30+
console.log(error.message);
31+
}
32+
});
33+
34+
35+

0 commit comments

Comments
 (0)