1- // Implement a function repeat
21const repeat = require ( "./repeat" ) ;
32// Given a target string str and a positive integer count,
43// When the repeat function is called with these inputs,
@@ -21,12 +20,34 @@ test("should repeat the string count times", () => {
2120// When the repeat function is called with these inputs,
2221// Then it should return the original str without repetition, ensuring that a count of 1 results in no repetition.
2322
23+ test ( "should return the original string when count is 1" , ( ) => {
24+ const str = "hi" ;
25+ const count = 1 ;
26+ const repeatedStr = repeat ( str , count ) ;
27+ expect ( repeatedStr ) . toEqual ( "hi" ) ;
28+ } ) ;
29+
2430// case: Handle Count of 0:
2531// Given a target string str and a count equal to 0,
2632// When the repeat function is called with these inputs,
2733// Then it should return an empty string, ensuring that a count of 0 results in an empty output.
2834
35+ test ( "should return an empty string when count is 0" , ( ) => {
36+ const str = "hi" ;
37+ const count = 0 ;
38+ const repeatedStr = repeat ( str , count ) ;
39+ expect ( repeatedStr ) . toEqual ( "" ) ;
40+ } ) ;
41+
2942// case: Negative Count:
3043// Given a target string str and a negative integer count,
3144// When the repeat function is called with these inputs,
3245// Then it should throw an error or return an appropriate error message, as negative counts are not valid.
46+
47+ test ( "should throw an error when count is negative" , ( ) => {
48+ const str = "hi" ;
49+ const count = - 2 ;
50+ expect ( ( ) => repeat ( str , count ) ) . toThrow (
51+ "Count must be a non-negative integer"
52+ ) ;
53+ } ) ;
0 commit comments