Skip to content

Commit 0717b53

Browse files
committed
testing functions
1 parent 1a80e44 commit 0717b53

File tree

8 files changed

+69
-11
lines changed

8 files changed

+69
-11
lines changed

project/js/functionality/capitalize.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
* @returns {string}
66
*/
77
function capitalize(word) {
8-
return word.charAt(0).toUpperCase() + word.slice(1);
8+
return !word ? "" : word.charAt(0).toUpperCase() + word.slice(1);
99
}
1010
module.exports = capitalize;

project/js/functionality/getMid.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
* @returns {number}
77
*/
88
function getMid(number1 = 0, number2) {
9+
if (typeof number1 !== "number" && !number2) {
10+
return 0;
11+
}
912
if (!number2 && typeof parseFloat(number1) === "number") {
1013
return number1;
1114
}

project/js/functionality/reverseString.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
* @returns {string}
55
*/
66
function reverseString(word) {
7-
return word.split("").reverse().join("");
7+
if (!word || typeof word !== "string") {
8+
return "";
9+
} else {
10+
return word.split("").reverse().join("");
11+
}
812
}
913
module.exports = reverseString;

tests/capitalize.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const capitalize = require("../project/js/functionality/capitalize.js");
2+
3+
it("always make the first letter capital", function () {
4+
expect(capitalize("hello")).toBe("Hello");
5+
});
6+
7+
it("returns any value type", function () {
8+
expect(capitalize("hello")).toEqual(expect.anything());
9+
});
10+
11+
it("returns empty if no parameter", function () {
12+
expect(capitalize()).toBe("");
13+
});

tests/getMid.test.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,38 @@ it("always returns a number", function () {
44
expect(typeof getMid(1, 2)).toBe("number");
55
});
66

7-
it("always returns a number2", function () {
7+
it("always returns a number also if the parameters are string", function () {
88
expect(typeof getMid("ali", "ahmed")).toBe("number");
99
});
1010

11-
it("always returns a number2", function () {
11+
it("always returns a zero if the first parameter is not a number", function () {
1212
expect(getMid("ali", 5)).toBe(0);
1313
});
1414

15-
it("always returns a number2", function () {
15+
it("always returns a zero if the second parameter is not a number", function () {
1616
expect(getMid(5, "ali")).toBe(0);
1717
});
1818

19-
it("always returns a number2", function () {
19+
it("returns the two parameters value if they are the same also if they are a string type", function () {
2020
expect(getMid("5", "5")).toBe(5);
2121
});
2222

23-
it("return the middle of two numbers", function () {
23+
it("returns the middle of two parameters", function () {
2424
expect(getMid(10, 20)).toBe(15);
2525
});
2626

27-
it("Entering Same Two numbers", function () {
27+
it("returns the two parameters value if they are the same", function () {
2828
expect(getMid(5, 5)).toBe(5);
2929
});
3030

31-
it("Not Entering The Second Number", function () {
31+
it("returns the first number if not Entering The Second Number", function () {
3232
expect(getMid(5)).toBe(5);
3333
});
3434

35-
it("Not Entering Any Number", function () {
35+
it("returns zero if the first parameter is not a number and the second parameter is not passing", function () {
3636
expect(getMid([1, 2, 3, 4])).toBe(0);
3737
});
3838

39-
it("Not Entering Any Number", function () {
39+
it("returns zero if there is no parameters", function () {
4040
expect(getMid()).toBe(0);
4141
});

tests/randomBoolean.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const randomBoolean = require("../project/js/functionality/randomBoolean.js");
2+
3+
it("always return a boolean value", function () {
4+
expect(typeof randomBoolean()).toBe("boolean");
5+
});
6+
7+
it("always return true or false only", function () {
8+
expect(randomBoolean()).toEqual(expect.any(Boolean));
9+
});
10+
11+
it("does not matter about parameters", function () {
12+
expect(randomBoolean(5, 7)).toEqual(expect.any(Boolean));
13+
});

tests/randomHex.test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
const randomHex = require("../project/js/functionality/randomHex");
22

3+
it("always returns a string", () => {
4+
expect(typeof randomHex()).toBe("string");
5+
});
6+
37
it("always returns a colour", () => {
48
const colours = Array(100)
59
.fill(null)

tests/reverseString.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const reversestring = require("../project/js/functionality/reverseString.js");
2+
3+
it("always return a string", function () {
4+
expect(typeof reversestring("hello")).toBe("string");
5+
});
6+
7+
it("always reverse the parameter", function () {
8+
expect(reversestring("hello")).toBe("olleh");
9+
});
10+
11+
it("returns empty string if no parameter", function () {
12+
expect(reversestring()).toBe("");
13+
});
14+
15+
it("returns empty string if the parameter is not a string", function () {
16+
expect(reversestring(5)).toBe("");
17+
});
18+
19+
it("does not matter about multiple parameters", function () {
20+
expect(reversestring("hello", "world")).toBe("olleh");
21+
});

0 commit comments

Comments
 (0)