Skip to content

Commit 2d01d60

Browse files
written jest tests for all the three functions and output tests as expected
1 parent b31a586 commit 2d01d60

4 files changed

Lines changed: 67 additions & 2 deletions

File tree

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,25 @@ test("should count multiple occurrences of a character", () => {
1818
});
1919

2020
// Scenario: No Occurrences
21+
test(`should return zero the character doesn't exist in the string`, () => {
22+
const str = "bravo";
23+
const char = "u";
24+
const count = countChar(str, char);
25+
expect(count).toEqual(0);
26+
});
27+
28+
// Empty String
29+
test(`should return zero when the string is empty`, () => {
30+
expect(countChar("", "a")).toEqual(0);
31+
});
32+
33+
// Scenario: Multiple Occurrences
34+
test("should count multiple occurrences of characters (including mixed and case-sensitive)", () => {
35+
expect(countChar("aaaaa", "a")).toEqual(5); // simple multiple
36+
expect(countChar("1-2-3-4-5-", "-")).toEqual(5); // mixed characters
37+
expect(countChar("AaAa", "A")).toEqual(2); // case sensitivity
38+
});
39+
2140
// Given the input string `str`,
2241
// And a character `char` that does not exist within `str`.
2342
// When the function is called with these inputs,

Sprint-3/2-practice-tdd/get-ordinal-number.test.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,37 @@ const getOrdinalNumber = require("./get-ordinal-number");
1313
// Case 1: Numbers ending with 1 (but not 11)
1414
// When the number ends with 1, except those ending with 11,
1515
// Then the function should return a string by appending "st" to the number.
16+
1617
test("should append 'st' for numbers ending with 1, except those ending with 11", () => {
1718
expect(getOrdinalNumber(1)).toEqual("1st");
1819
expect(getOrdinalNumber(21)).toEqual("21st");
1920
expect(getOrdinalNumber(131)).toEqual("131st");
2021
});
22+
23+
// Case 2: Numbers ending with 2 (but NOT 12)
24+
test("should append 'nd' for numbers ending with 2, except those ending with 12", () => {
25+
expect(getOrdinalNumber(2)).toEqual("2nd");
26+
expect(getOrdinalNumber(22)).toEqual("22nd");
27+
expect(getOrdinalNumber(102)).toEqual("102");
28+
});
29+
30+
// Case 3: Numbers ending with 3 (but NOT 13)
31+
test("should append 'rd' for numbers ending with 3, except those ending with 13", () => {
32+
expect(getOrdinalNumber(3)).toEqual("3rd");
33+
expect(getOrdinalNumber(33)).toEqual("33rd");
34+
expect(getOrdinalNumber(103)).toEqual("103rd");
35+
});
36+
37+
// Case 4: Numbers ending with 11, 12, or 13 --- 11,12,13,111,121,131,
38+
test("should append 'th' for numbers ending with 11, 12, 13", () => {
39+
expect(getOrdinalNumber(11)).toEqual("11th");
40+
expect(getOrdinalNumber(12)).toEqual("12th");
41+
expect(getOrdinalNumber(13)).toEqual("13th");
42+
});
43+
44+
// Case 5: Numbers ending with larger numbers of 111, 121, 131
45+
test("should append 'th' for numbers ending with 111, 121, 131", () => {
46+
expect(getOrdinalNumber(111)).toEqual("111th");
47+
expect(getOrdinalNumber(121)).toEqual("121th");
48+
expect(getOrdinalNumber(131)).toEqual("131th");
49+
});
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
function repeatStr() {
22
// Your implementation of this function must *not* call String.prototype.repeat (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat).
33
// The goal is to re-implement that function, not to use it.
4-
return "hellohellohello";
4+
return String.repeat(count);
55
}
66

77
module.exports = repeatStr;

Sprint-3/2-practice-tdd/repeat-str.test.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,39 @@ const repeatStr = require("./repeat-str");
1111

1212
test("should repeat the string count times", () => {
1313
const str = "hello";
14-
const count = 3;
14+
const count = -5;
1515
const repeatedStr = repeatStr(str, count);
1616
expect(repeatedStr).toEqual("hellohellohello");
1717
});
1818

1919
// Case: handle count of 1:
20+
test("should repeat the string count of 1", () => {
21+
const str = "fella";
22+
const count = 1;
23+
const repeatedStr = repeatStr(str, count);
24+
expect(repeatedStr).toEqual("fella");
25+
});
26+
2027
// Given a target string `str` and a `count` equal to 1,
2128
// When the repeatStr function is called with these inputs,
2229
// Then it should return the original `str` without repetition.
2330

2431
// Case: Handle count of 0:
32+
test("should repeat the string count of 0", () => {
33+
const str = "fella";
34+
const count = 0;
35+
const repeatedStr = repeatStr(str, count);
36+
expect(repeatedStr).toEqual("");
37+
});
38+
2539
// Given a target string `str` and a `count` equal to 0,
2640
// When the repeatStr function is called with these inputs,
2741
// Then it should return an empty string.
2842

2943
// Case: Handle negative count:
44+
test(`should throw an error for negative count`, () => {
45+
expect(() => repeatedStr("fella", -2)).toThrowError();
46+
});
3047
// Given a target string `str` and a negative integer `count`,
3148
// When the repeatStr function is called with these inputs,
3249
// Then it should throw an error, as negative counts are not valid.

0 commit comments

Comments
 (0)