11// Implement a function repeatStr
22const 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: 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.
113
4+ // Case: handle multiple repetitions
125test ( "should repeat the string count times" , ( ) => {
136 const str = "hello" ;
147 const count = 3 ;
158 const repeatedStr = repeatStr ( str , count ) ;
169 expect ( repeatedStr ) . toEqual ( "hellohellohello" ) ;
1710} ) ;
1811
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.
12+ // Case: handle count of 1
13+ test ( "should return the original string when count is 1" , ( ) => {
14+ const str = "Maryam" ;
15+ const count = 1 ;
16+ const repeatedStr = repeatStr ( str , count ) ;
17+ expect ( repeatedStr ) . toEqual ( str ) ;
18+ } ) ;
19+
20+ // Case: handle count of 0
21+ test ( "should return an empty string when count is 0" , ( ) => {
22+ const str = "Hello World!" ;
23+ const count = 0 ;
24+ const repeatedStr = repeatStr ( str , count ) ;
25+ expect ( repeatedStr ) . toEqual ( "" ) ;
26+ } ) ;
2327
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.
28+ // Case: handle negative count
29+ test ( "should throw an error when count is negative" , ( ) => {
30+ const str = "Hello World!" ;
31+ const count = - 1 ;
2832
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.
33+ expect ( ( ) => repeatStr ( str , count ) ) . toThrow ( ) ;
34+ } ) ;
0 commit comments