Skip to content

Commit 3415822

Browse files
committed
update the test code to hundle expected cases.
1 parent e76a59c commit 3415822

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

Sprint-3/3-mandatory-practice/implement/repeat.test.js

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,30 @@ const repeat = require("./repeat");
99
// When the repeat function is called with these inputs,
1010
// Then it should repeat the str count times and return a new string containing the repeated str values.
1111

12-
test("should repeat the string count times", () => {
13-
const str = "hello";
14-
const count = 3;
15-
const repeatedStr = repeat(str, count);
16-
expect(repeatedStr).toEqual("hellohellohello");
17-
});
12+
test("should repeat string correctly for various valid counts", () => {
13+
const testCases = [
14+
{ str: "hello", count: 1, expected: "hellohello" },
15+
{ str: "hello", count: 0, expected: "" },
16+
];
17+
18+
testCases.forEach(({ str, count, expected }) => {
19+
const result = repeat(str, count);
20+
expect(result).toEqual(expected);
21+
});
22+
});
23+
// we combined the test cases for various valid counts into single test.
24+
// Why combine?
25+
// Reduce duplication.
26+
// Keeps test organized.
27+
// Easier to add more cases in the future.
28+
29+
// Separate test case for error case.
30+
31+
test("should throw error when count is negative", () => {
32+
expect(() => repeat("hello", -2)).toThrow(
33+
"Repeat count must be non-negative"
34+
);
35+
});
1836

1937
// case: handle Count of 1:
2038
// Given a target string str and a count equal to 1,

0 commit comments

Comments
 (0)