-
-
Notifications
You must be signed in to change notification settings - Fork 337
Expand file tree
/
Copy pathrepeat-str.test.js
More file actions
46 lines (40 loc) · 1.24 KB
/
repeat-str.test.js
File metadata and controls
46 lines (40 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/**
* repeatStr - Full Test Suite
* --------------------------
* These tests cover all the cases required by the assignment:
* 1. Multiple repetitions.
* 2. Count of 1.
* 3. Count of 0.
* 4. Negative count (Error handling).
*/
const repeatStr = require("./repeat-str");
// Case 1: Handle multiple repetitions
test("should repeat the string count times (e.g., 3 times)", () => {
const str = "hello";
const count = 3;
const result = repeatStr(str, count);
expect(result).toEqual("hellohellohello");
});
// Case 2: Handle count of 1
test("should return the original string without repetition when count is 1", () => {
const str = "hello";
const count = 1;
const result = repeatStr(str, count);
expect(result).toEqual("hello");
});
// Case 3: Handle count of 0
test("should return an empty string when count is 0", () => {
const str = "hello";
const count = 0;
const result = repeatStr(str, count);
expect(result).toEqual("");
});
// Case 4: Handle negative count
test("should throw an error when count is a negative integer", () => {
const str = "hello";
const count = -1;
// Note: To test for errors in Jest, we wrap the function call in an anonymous function
expect(() => {
repeatStr(str, count);
}).toThrow();
});