Skip to content

Commit 5343ebf

Browse files
rewrite tests with jests complete
1 parent d0fa6f8 commit 5343ebf

5 files changed

Lines changed: 20 additions & 21 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ function getAngleType(angle) {
3232
return "Reflex angle";
3333
}
3434
else if(angle === 0){
35-
return "Zero Angle"
35+
return "Zero angle"
3636
}
3737
else if(angle === 360){
3838
return "Complete angle"
@@ -78,7 +78,7 @@ assertEquals(getAngleType(249), "Reflex angle");
7878

7979
// Test for invalid angles
8080
assertEquals(getAngleType(-980), "Invalid angle");
81-
assertEquals(getAngleType(9082), "Right angle");
81+
assertEquals(getAngleType(9082), "Invalid angle");
8282
assertEquals(getAngleType(672), "Invalid angle");
8383
//
8484
assertEquals(getAngleType(0), "Zero angle");

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
// execute the code to ensure all tests pass.
1212

1313
function isProperFraction(numerator, denominator) {
14-
return numerator < denominator;
14+
return Math.abs(numerator) < Math.abs(denominator);
1515
// TODO: Implement this function
1616
}
1717

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

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323

2424
function getCardValue(card) {
2525
// TODO: Implement this function
26+
if (typeof card !== "string" || card.length < 2 || card.length > 3) {
27+
throw new Error("Invalid card");
28+
}
2629
const ranks = [
2730
"A",
2831
"2",
@@ -41,11 +44,12 @@ function getCardValue(card) {
4144
const suits = ["♠", "♥", "♦", "♣"];
4245
let rank = card.slice(0, -1)// This is extracting the bit before the suits alone
4346
let suit = card.slice(-1) // THis is extracting the suit in the string
47+
4448
if(!ranks.includes(rank)){
45-
throw new Error("Invalid rank")
49+
throw new Error("Invalid card")
4650
}
4751
if (!suits.includes(suit)){
48-
throw new Error("Invalid suit")
52+
throw new Error("Invalid card")
4953
}
5054
if (rank === "A"){
5155
return 11}
@@ -55,6 +59,7 @@ let suit = card.slice(-1) // THis is extracting the suit in the string
5559
else{
5660
return Number(rank)
5761
}
62+
5863
}
5964
// The line below allows us to load the getCardValue function into tests in other files.
6065
// This will be useful in the "rewrite tests with jest" step.
@@ -100,20 +105,14 @@ try {
100105

101106
try {
102107
getCardValue("A");
103-
console.error("Error not thrown for missing suit");
108+
console.error("Error not thrown for invalid card missing suit");
104109
} catch (e) {
105-
console.log("Error thrown for missing suit 🎉");
110+
console.log("Error thrown for invalid card missing suit🎉");
106111
}
107112

108113
try {
109114
getCardValue("");
110-
console.error("Error not thrown for empty string");
115+
console.error("Error not thrown for invalid card containing empty string");
111116
} catch (e) {
112-
console.log("Error thrown for empty string🎉");
113-
}
114-
try {
115-
getCardValue("Apple");
116-
console.error("Error not thrown");
117-
} catch {
118-
console.log("Error thrown 🎉");
117+
console.log("Error thrown for invalid card containing empty string");
119118
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ test(`should return true when negative/positive numerator is less than the posit
2323
// Special case: numerator or denominator is negative, we consider the absolute values for fractions so ignore the negative signs
2424
test(`should return false when negative/positive numerator is greater than the positive/negative denominator`, () => {
2525
expect(isProperFraction(-50, 10)).toEqual(false);
26-
expect(isProperFraction(100, -2)).toEqual(false);
26+
expect(isProperFraction(100, 2)).toEqual(false);
2727
expect(isProperFraction(-1, -0)).toEqual(false);
2828
});

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ test(`Should return the number when given a number card`, () => {
2121
expect(getCardValue("2♦")).toEqual(2);
2222
});
2323
//Case 4: Invalid cards
24-
test(`Should return the invalid suit or invalid rank when given an invalid card`, () => {
25-
expect(getCardValue("4🎉")).toEqual("Invalid suit");
26-
expect(getCardValue("20♣")).toEqual("Invalid rank");
27-
expect(getCardValue("Apple")).toEqual("Invalid card");
24+
test(`Should return the invalid suit, invalid card or invalid card`, () => {
25+
expect(() => getCardValue("Apple")).toThrow("Invalid card");
26+
expect(() => getCardValue("4🎉")).toThrow("Invalid card");
27+
expect(() => getCardValue("20♣")).toThrow("Invalid card");
2828
});
2929
// Suggestion: Group the remaining test data into these categories:
3030
// Number Cards (2-10)
@@ -33,5 +33,5 @@ test(`Should return the invalid suit or invalid rank when given an invalid card`
3333

3434
// To learn how to test whether a function throws an error as expected in Jest,
3535
// please refer to the Jest documentation:
36-
// https://jestjs.io/docs/expect#tothrowerror
36+
// https://jestjs.io/docs/expecttothrowerror
3737

0 commit comments

Comments
 (0)