@@ -18,7 +18,51 @@ test("should count multiple occurrences of a character", () => {
1818} ) ;
1919
2020// Scenario: No Occurrences
21- // Given the input string str,
22- // And a character char that does not exist within the case-sensitive str,
23- // When the function is called with these inputs,
24- // Then it should return 0, indicating that no occurrences of the char were found in the case-sensitive str.
21+ test ( "should return 0 when the character is not in the string" , ( ) => {
22+ const str = "hello" ;
23+ const char = "x" ;
24+ const count = countChar ( str , char ) ;
25+ expect ( count ) . toEqual ( 0 ) ;
26+ } ) ;
27+
28+ // Scenario: Case Sensitivity
29+
30+ test ( "should be case-sensitive when counting characters" , ( ) => {
31+ const str = "Banana" ;
32+ expect ( countChar ( str , "a" ) ) . toEqual ( 3 ) ;
33+ expect ( countChar ( str , "A" ) ) . toEqual ( 0 ) ;
34+ } ) ;
35+
36+ // Scenario: Empty String
37+
38+ test ( "should return 0 when the input string is empty" , ( ) => {
39+ const str = "" ;
40+ const char = "a" ;
41+ const count = countChar ( str , char ) ;
42+ expect ( count ) . toEqual ( 0 ) ;
43+ } ) ;
44+
45+ // Scenario : Special characters
46+
47+ test ( "should count special characters like spaces or punctuation" , ( ) => {
48+ const str1 = "a b a b" ;
49+ const char1 = " " ;
50+ expect ( countChar ( str1 , char1 ) ) . toEqual ( 2 ) ;
51+
52+ const str2 = "NO !!!" ;
53+ const char2 = "!" ;
54+ expect ( countChar ( str2 , char2 ) ) . toEqual ( 3 ) ;
55+ } ) ;
56+
57+ // Scenario: Invalid inputs
58+
59+ test ( "should throw error for invalid string input" , ( ) => {
60+ expect ( ( ) => countChar ( 123 , "a" ) ) . toThrow ( "Input should be a string" ) ;
61+ } ) ;
62+
63+ test ( "should throw error for invalid character input" , ( ) => {
64+ expect ( ( ) => countChar ( "hello" , "ab" ) ) . toThrow ( "Input must be a single character" ) ;
65+ } ) ;
66+
67+
68+ // Added different cases and test using npx jest
0 commit comments