|
1 | | -// Implement a function repeatStr |
2 | 1 | import { repeatStr } from "./repeat-str.js"; |
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: 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 | 2 |
|
12 | 3 | test("should repeat the string count times", () => { |
13 | 4 | const str = "hello"; |
@@ -43,16 +34,29 @@ test("should return Backstreet Boys lyrics", () => { |
43 | 34 | expect(repeatedStr).toEqual("byebyebyebyebye"); |
44 | 35 | }); |
45 | 36 |
|
46 | | -// Case: handle count of 1: |
47 | | -// Given a target string `str` and a `count` equal to 1, |
48 | | -// When the repeatStr function is called with these inputs, |
| 37 | +test("should return an empty string when repeating an empty string 3 times", () => { |
| 38 | + const str = ""; |
| 39 | + const count = 3; |
| 40 | + const repeatedStr = repeatStr(str, count); |
| 41 | + expect(repeatedStr).toEqual(""); |
| 42 | +}); |
| 43 | + |
| 44 | +test("should return an empty string when repeating an empty string once", () => { |
| 45 | + const str = ""; |
| 46 | + const count = 1; |
| 47 | + const repeatedStr = repeatStr(str, count); |
| 48 | + expect(repeatedStr).toEqual(""); |
| 49 | +}); |
49 | 50 |
|
50 | | -// Case: Handle count of 0: |
51 | | -// Given a target string `str` and a `count` equal to 0, |
52 | | -// When the repeatStr function is called with these inputs, |
53 | | -// Then it should return an empty string. |
| 51 | +test("should return an empty string when count is zero", () => { |
| 52 | + const str = ""; |
| 53 | + const count = 0; |
| 54 | + const repeatedStr = repeatStr(str, count); |
| 55 | + expect(repeatedStr).toEqual(""); |
| 56 | +}); |
54 | 57 |
|
55 | | -// Case: Handle negative count: |
56 | | -// Given a target string `str` and a negative integer `count`, |
57 | | -// When the repeatStr function is called with these inputs, |
58 | | -// Then it should throw an error, as negative counts are not valid. |
| 58 | +test("should throw an error when repeating an empty string a negative number of times", () => { |
| 59 | + const str = ""; |
| 60 | + const count = -2; |
| 61 | + expect(() => repeatStr(str, count)).toThrow("Count cannot be negative"); |
| 62 | +}); |
0 commit comments