|
1 | | -// Implement a function repeatStr |
2 | | -const repeatStr = require("./repeat-str"); |
3 | | -// Given a target string `str` and a positive integer `count`, |
4 | | -// When the repeatStr function is called with these inputs, |
5 | | -// Then it should: |
| 1 | +/** |
| 2 | + * repeatStr - Full Test Suite |
| 3 | + * -------------------------- |
| 4 | + * These tests cover all the cases required by the assignment: |
| 5 | + * 1. Multiple repetitions. |
| 6 | + * 2. Count of 1. |
| 7 | + * 3. Count of 0. |
| 8 | + * 4. Negative count (Error handling). |
| 9 | + */ |
6 | 10 |
|
7 | | -// Case: handle multiple repetitions: |
8 | | -// Given a target string `str` and a positive integer `count` greater than 1, |
9 | | -// When the repeatStr function is called with these inputs, |
10 | | -// Then it should return a string that contains the original `str` repeated `count` times. |
| 11 | +const repeatStr = require("./repeat-str"); |
11 | 12 |
|
12 | | -test("should repeat the string count times", () => { |
| 13 | +// Case 1: Handle multiple repetitions |
| 14 | +test("should repeat the string count times (e.g., 3 times)", () => { |
13 | 15 | const str = "hello"; |
14 | 16 | const count = 3; |
15 | | - const repeatedStr = repeatStr(str, count); |
16 | | - expect(repeatedStr).toEqual("hellohellohello"); |
| 17 | + const result = repeatStr(str, count); |
| 18 | + expect(result).toEqual("hellohellohello"); |
17 | 19 | }); |
18 | 20 |
|
19 | | -// Case: handle count of 1: |
20 | | -// Given a target string `str` and a `count` equal to 1, |
21 | | -// When the repeatStr function is called with these inputs, |
22 | | -// Then it should return the original `str` without repetition. |
| 21 | +// Case 2: Handle count of 1 |
| 22 | +test("should return the original string without repetition when count is 1", () => { |
| 23 | + const str = "hello"; |
| 24 | + const count = 1; |
| 25 | + const result = repeatStr(str, count); |
| 26 | + expect(result).toEqual("hello"); |
| 27 | +}); |
23 | 28 |
|
24 | | -// Case: Handle count of 0: |
25 | | -// Given a target string `str` and a `count` equal to 0, |
26 | | -// When the repeatStr function is called with these inputs, |
27 | | -// Then it should return an empty string. |
| 29 | +// Case 3: Handle count of 0 |
| 30 | +test("should return an empty string when count is 0", () => { |
| 31 | + const str = "hello"; |
| 32 | + const count = 0; |
| 33 | + const result = repeatStr(str, count); |
| 34 | + expect(result).toEqual(""); |
| 35 | +}); |
| 36 | + |
| 37 | +// Case 4: Handle negative count |
| 38 | +test("should throw an error when count is a negative integer", () => { |
| 39 | + const str = "hello"; |
| 40 | + const count = -1; |
28 | 41 |
|
29 | | -// Case: Handle negative count: |
30 | | -// Given a target string `str` and a negative integer `count`, |
31 | | -// When the repeatStr function is called with these inputs, |
32 | | -// Then it should throw an error, as negative counts are not valid. |
| 42 | + // Note: To test for errors in Jest, we wrap the function call in an anonymous function |
| 43 | + expect(() => { |
| 44 | + repeatStr(str, count); |
| 45 | + }).toThrow(); |
| 46 | +}); |
0 commit comments