|
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: |
6 | | - |
7 | | -// case: repeat String: |
8 | | -// Given a target string str and a positive integer count, |
9 | | -// When the repeatStr function is called with these inputs, |
10 | | -// Then it should repeat the str count times and return a new string containing the repeated str values. |
11 | | - |
12 | | -test("should repeat the string count times", () => { |
13 | | - const str = "hello"; |
14 | | - const count = 3; |
15 | | - const repeatedStr = repeatStr(str, count); |
16 | | - expect(repeatedStr).toEqual("hellohellohello"); |
17 | | -}); |
18 | | - |
19 | 1 | // case: handle Count of 1: |
20 | 2 | // Given a target string str and a count equal to 1, |
21 | 3 | // When the repeatStr function is called with these inputs, |
22 | 4 | // Then it should return the original str without repetition, ensuring that a count of 1 results in no repetition. |
| 5 | +test("should return original string when count is 1", () => { |
| 6 | + const str = "hello"; |
| 7 | + const count = 1; |
| 8 | + const repeatedStr = repeatStr(str, count); |
| 9 | + expect(repeatedStr).toEqual("hello"); |
| 10 | +}); |
23 | 11 |
|
24 | 12 | // case: Handle Count of 0: |
25 | 13 | // Given a target string str and a count equal to 0, |
26 | 14 | // When the repeatStr function is called with these inputs, |
27 | 15 | // Then it should return an empty string, ensuring that a count of 0 results in an empty output. |
| 16 | +test("should return empty string when count is 0", () => { |
| 17 | + const str = "hello"; |
| 18 | + const count = 0; |
| 19 | + const repeatedStr = repeatStr(str, count); |
| 20 | + expect(repeatedStr).toEqual(""); |
| 21 | +}); |
28 | 22 |
|
29 | 23 | // case: Negative Count: |
30 | 24 | // Given a target string str and a negative integer count, |
31 | 25 | // When the repeatStr function is called with these inputs, |
32 | 26 | // Then it should throw an error or return an appropriate error message, as negative counts are not valid. |
| 27 | +test("should throw an error for negative count", () => { |
| 28 | + const str = "hello"; |
| 29 | + const count = -3; |
| 30 | + expect(() => repeatStr(str, count)).toThrow(); |
| 31 | +}); |
0 commit comments