Skip to content

Commit 889f34e

Browse files
committed
created and tested jest-testcases
1 parent 8f3d6cf commit 889f34e

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

Sprint-3/2-practice-tdd/count.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1-
function countChar(stringOfCharacters, findCharacter) {
2-
return 5
1+
function countChar(str, char) {
2+
if (!char || char.length !== 1) return 0; // ensure char is a single character
3+
let count = 0;
4+
for (let i = 0; i < str.length; i++) {
5+
if (str[i] === char) {
6+
count++;
7+
}
8+
}
9+
return count;
310
}
411

512
module.exports = countChar;

Sprint-3/2-practice-tdd/count.test.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)