Skip to content

Commit 18ef756

Browse files
author
Pretty Taruvinga
committed
test: add unit tests for countChar function
1 parent 0b65a57 commit 18ef756

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ const countChar = require("./count");
33
// Given a string `str` and a single character `char` to search for,
44
// When the countChar function is called with these inputs,
55
// Then it should:
6+
// - Iterate through each character in `str`.
7+
// - Compare each character to `char`.
8+
// - Increment a counter each time a match is found.
9+
// - Return the final count of occurrences of `char` in `str`.
610

711
// Scenario: Multiple Occurrences
812
// Given the input string `str`,
@@ -22,3 +26,17 @@ test("should count multiple occurrences of a character", () => {
2226
// And a character `char` that does not exist within `str`.
2327
// When the function is called with these inputs,
2428
// Then it should return 0, indicating that no occurrences of `char` were found.
29+
test("should return 0 when the character does not occur in the string", () => {
30+
const str = "hello world";
31+
const char = "x";
32+
const count = countChar(str, char);
33+
expect(count).toEqual(0);
34+
});
35+
36+
// Scenario: Case Sensitivity
37+
test("should be case sensitive", () => {
38+
const str = "Hello World";
39+
const char = "H";
40+
const count = countChar(str, char);
41+
expect(count).toEqual(1);
42+
});

0 commit comments

Comments
 (0)