Skip to content

Commit 65578d2

Browse files
committed
updating tests for TDD approach
1 parent c9b1c7d commit 65578d2

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

Sprint-3/2-practice-tdd/repeat.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
function repeat(str, count) {
2-
if ((Number(count) < 0) | (typeof str === "object")) {
3-
throw new Error("Invalid data value");
2+
if (typeof str !== "string" && typeof str !== "number") {
3+
throw new Error("Input should be a string");
44
}
5-
try {
6-
return String(str).repeat(Number(count));
7-
} catch (error) {
8-
throw new Error("Invalid data value");
5+
if (Number(count) < 0) {
6+
throw new Error("Count should be a positive number");
97
}
8+
if (typeof count !== "number" && typeof count !== "string") {
9+
throw new Error("Count should be a number");
10+
}
11+
return String(str).repeat(Number(count));
1012
}
1113

1214
module.exports = repeat;

Sprint-3/2-practice-tdd/repeat.test.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ test("should return an empty string when count is 0", () => {
4545
test("should throw an error when count is negative", () => {
4646
const str = "hello";
4747
const count = -2;
48-
expect(() => repeat(str, count)).toThrow("Invalid data value");
48+
expect(() => repeat(str, count)).toThrow("Count should be a positive number");
4949
});
5050

5151
// case: str is not a string:
@@ -96,8 +96,15 @@ test("should return an empty string when str is an empty string", () => {
9696
// Given invalid types for both str (e.g., an object) and count (e.g., an array),
9797
// When the repeat function is called with these inputs,
9898
// Then it should throw an error or return an appropriate error message, indicating that the input types are not supported.
99-
test("should throw an error when str and count are of invalid types", () => {
99+
test("should throw an error when str is invalid type", () => {
100100
const str = { text: "hello" };
101-
const count = [3];
102-
expect(() => repeat(str, count)).toThrow("Invalid data value");
101+
const count = 3;
102+
expect(() => repeat(str, count)).toThrow("Input should be a string");
103+
});
104+
105+
// case: for throw error
106+
test("should throw an error when count is an array", () => {
107+
const str = "hello";
108+
const count = [2];
109+
expect(() => repeat(str, count)).toThrow("Count should be a number");
103110
});

0 commit comments

Comments
 (0)