Skip to content

Commit 94de70c

Browse files
committed
repeat.test.js updated template
1 parent 6c17f89 commit 94de70c

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const repeat = require("./repeat");
2+
13
function repeat() {
24
return "hellohellohello";
35
}

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@ const repeat = require("./repeat");
33
// Given a target string str and a positive integer count,
44
// When the repeat function is called with these inputs,
55
// Then it should:
6+
function repeat(str, count) {
7+
if (count < 0) {
8+
throw new Error("Count must be a non-negative integer");
9+
}
10+
return str.repeat(count);
11+
}
12+
13+
module.exports = repeat;
614

715
// case: repeat String:
816
// Given a target string str and a positive integer count,
@@ -30,3 +38,35 @@ test("should repeat the string count times", () => {
3038
// Given a target string str and a negative integer count,
3139
// When the repeat function is called with these inputs,
3240
// Then it should throw an error or return an appropriate error message, as negative counts are not valid.
41+
const repeat = require("./repeat");
42+
43+
// Case 1: Repeat string multiple times
44+
test("should repeat the string count times", () => {
45+
const str = "hello";
46+
const count = 3;
47+
const repeatedStr = repeat(str, count);
48+
expect(repeatedStr).toEqual("hellohellohello");
49+
});
50+
51+
// Case 2: Handle count of 1
52+
test("should return the original string when count is 1", () => {
53+
const str = "world";
54+
const count = 1;
55+
const repeatedStr = repeat(str, count);
56+
expect(repeatedStr).toEqual("world");
57+
});
58+
59+
// Case 3: Handle count of 0
60+
test("should return an empty string when count is 0", () => {
61+
const str = "test";
62+
const count = 0;
63+
const repeatedStr = repeat(str, count);
64+
expect(repeatedStr).toEqual("");
65+
});
66+
67+
// Case 4: Negative count
68+
test("should throw an error when count is negative", () => {
69+
const str = "oops";
70+
const count = -2;
71+
expect(() => repeat(str, count)).toThrow("Count must be a non-negative integer");
72+
});

0 commit comments

Comments
 (0)