@@ -22,3 +22,44 @@ test("should count multiple occurrences of a character", () => {
2222// And a character char that does not exist within the case-sensitive str,
2323// When the function is called with these inputs,
2424// Then it should return 0, indicating that no occurrences of the char were found in the case-sensitive str.
25+
26+ test ( "should return 0 when character does not exist in the string" , ( ) => {
27+ const str = "mnopqrst" ;
28+ const char = "z" ;
29+ const count = countChar ( str , char ) ;
30+ expect ( count ) . toEqual ( 0 ) ;
31+ } ) ;
32+
33+ // we can add more test cases to make sure our function is working as expected
34+ // Scenario: Case Sensitivity
35+ // Given the input string str,
36+ // And a character char that exists in str but with different casing (e.g., 'A' in 'aAaAa'),
37+ // When the function is called with these inputs,
38+ // Then it should treat char as case-sensitive and only count occurrences that match the exact casing (e.g., 'A' appears two times in 'aAaAa').
39+
40+ test ( "should treat character as case-sensitive" , ( ) => {
41+ const str = "aAaAa" ;
42+ const char = "A" ;
43+ const count = countChar ( str , char ) ;
44+ expect ( count ) . toEqual ( 2 ) ;
45+ } ) ;
46+
47+ // we can also add empty string test case
48+ // Scenario: Empty String
49+ // Given an empty input string str,
50+ // And any character char,
51+ // When the function is called with these inputs,
52+ // Then it should return 0, indicating that no occurrences of char can be found in an empty str.
53+
54+ test ( "should return 0 for empty string" , ( ) => {
55+ const str = "" ;
56+ const char = "a" ;
57+ const count = countChar ( str , char ) ;
58+ expect ( count ) . toEqual ( 0 ) ;
59+ } ) ;
60+
61+ // We can run this test file using the command `npx jest count.test.js`
62+ // in the terminal. Making sure we are in the directory where this file is located.
63+ // If we have Jest installed globally, you can simply run `jest count.test.js`
64+ // instead. If you have added a test script to your package.json file, you can also run
65+ // `npm test count.test.js` to execute the tests.
0 commit comments