Skip to content

Commit 4027919

Browse files
committed
Add test cases for repeat function to cover edge cases and various input types
1 parent 4548cb5 commit 4027919

File tree

1 file changed

+68
-1
lines changed

1 file changed

+68
-1
lines changed

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

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ test("should return the original input with no repetition", () => {
3131
// Given a target string str and a count equal to 0,
3232
// When the repeat function is called with these inputs,
3333
// Then it should return an empty string, ensuring that a count of 0 results in an empty output.
34-
test("should return an empty string", () => {
34+
test("should return an empty string for zero count times", () => {
3535
const str = "rice";
3636
const count = 0;
3737
const repeatedStr = repeat(str, count);
@@ -48,3 +48,70 @@ test("should return an error message", () => {
4848
const repeatedStr = repeat(str, count);
4949
expect(repeatedStr).toEqual("Negative number invalid");
5050
})
51+
52+
test("should return an empty string when empty string is expected count number of times", () => {
53+
const str = "";
54+
const count = 23;
55+
const repeatedStr = repeat(str, count);
56+
expect(repeatedStr).toEqual("");
57+
})
58+
59+
test("should repeat the number count times as a string", () => {
60+
const str = 1;
61+
const count = 3;
62+
const repeatedStr = repeat(str, count);
63+
expect(repeatedStr).toEqual("111");
64+
});
65+
66+
test("should repeat boolean count times as a string", () => {
67+
const str = true;
68+
const count = 2;
69+
const repeatedStr = repeat(str, count);
70+
expect(repeatedStr).toEqual("truetrue");
71+
});
72+
73+
test("should repeat null count times as a string", () => {
74+
const str = null;
75+
const count = 2;
76+
const repeatedStr = repeat(str, count);
77+
expect(repeatedStr).toEqual("nullnull");
78+
});
79+
80+
test("should repeat undefined count times as a string", () => {
81+
const str = undefined;
82+
const count = 2;
83+
const repeatedStr = repeat(str, count);
84+
expect(repeatedStr).toEqual("undefinedundefined");
85+
});
86+
87+
// case: array input
88+
test("should repeat [] count times as a string", () => {
89+
const str = [];
90+
const count = 2;
91+
const repeatedStr = repeat(str, count);
92+
expect(repeatedStr).toEqual("");
93+
});
94+
95+
// case: Non-integer positive count
96+
test("should return an error message for non-integer positive count", () => {
97+
const str = "apple";
98+
const count = 2.5;
99+
const repeatedStr = repeat(str, count);
100+
expect(repeatedStr).toEqual("Invalid count: count should be an integer");
101+
});
102+
103+
// case: Non-integer negative count
104+
test("should return an error message for non-integer negative count", () => {
105+
const str = "banana";
106+
const count = -1.7;
107+
const repeatedStr = repeat(str, count);
108+
expect(repeatedStr).toEqual("Negative number invalid");
109+
});
110+
111+
// case: Object input
112+
test("should repeat an object count times as a string", () => {
113+
const str = {};
114+
const count = 2;
115+
const repeatedStr = repeat(str, count);
116+
expect(repeatedStr).toEqual("[object Object][object Object]");
117+
});

0 commit comments

Comments
 (0)