Skip to content

Commit 5ef1835

Browse files
Merge pull request #7 from andresWeitzel/testing-02-add-unit-test-for-request-helper
testing-02-add-unit-test-for-request-helper
2 parents 338a0c9 + 41dabdd commit 5ef1835

File tree

4 files changed

+134
-14
lines changed

4 files changed

+134
-14
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"test": "jest --verbose --detectOpenHandles",
1414
"test:watch": "jest --watch --verbose --detectOpenHandles",
1515
"test:cov": "jest --coverage --verbose --detectOpenHandles",
16-
"test:format-helper": "jest --verbose --detectOpenHandles ./src/test/unit-test/api-integration/helpers/format/*"
16+
"test:format-helpers": "jest --verbose --detectOpenHandles ./src/test/unit-test/api-integration/helpers/format/*",
17+
"test:request-helpers": "jest --verbose --detectOpenHandles ./src/test/unit-test/api-integration/helpers/request/*"
1718
},
1819
"keywords": [],
1920
"author": "",

src/api-integration/helpers/request/get-data-from-address.js

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,34 @@
11
//External
22
const axios = require("axios");
3+
//Helpers
34
const { splitAddressByLastDot } = require("../format/address");
4-
//Const-vars
5+
//Const
56
const URL = process.env.WHOIS_BASE_URL;
6-
let ip;
7-
let ipsLength = 100;
7+
const IP_LENGTH = 100;
8+
const GET_DATA_FROM_SPECIFIC_ADDRESS_ERROR_NAME =
9+
"ERROR in getDataFromSpecificAddress helper function.";
10+
const GET_DATA_FROM_RANDOM_ADDRESS_ERROR_NAME =
11+
"ERROR in getDataFromRandomAddress helper function.";
12+
//Vars
813
let axiosResponse;
914
let responseData;
1015
let arrayDataAddress;
16+
let msgResponse;
17+
let msgLog;
1118

1219
const getDataFromSpecificAddress = async (ip) => {
1320
try {
1421
axiosResponse = await axios.get(URL + ip);
1522

1623
responseData = axiosResponse?.data || null;
24+
25+
return responseData;
1726
} catch (error) {
18-
console.log(error);
19-
responseData = null;
27+
msgResponse = GET_DATA_FROM_SPECIFIC_ADDRESS_ERROR_NAME;
28+
msgLog = msgResponse + `Caused by ${error}`;
29+
console.log(msgLog);
30+
return msgResponse;
2031
}
21-
return responseData;
2232
};
2333

2434
const getDataFromRandomAddress = async (ip) => {
@@ -28,24 +38,24 @@ const getDataFromRandomAddress = async (ip) => {
2838
firstValueForIp = splitIp[0];
2939
lastValueForIp = parseInt(splitIp[1]);
3040

31-
for (let i = 0; i < ipsLength; i++) {
41+
for (let i = 0; i < IP_LENGTH; i++) {
3242
if (lastValueForIp >= 253) return;
3343
ip = `${firstValueForIp}.${lastValueForIp}`;
3444
lastValueForIp++;
3545
await axios.get(URL + ip).then(function (response) {
3646
responseData = response?.data;
3747
if (responseData != undefined || null) {
3848
arrayDataAddress.push(responseData);
39-
//console.log(responseData);
4049
}
4150
});
4251
}
52+
return arrayDataAddress;
4353
} catch (error) {
44-
console.log(error);
45-
arrayDataAddress = null;
54+
msgResponse = GET_DATA_FROM_RANDOM_ADDRESS_ERROR_NAME;
55+
msgLog = msgResponse + `Caused by ${error}`;
56+
console.log(msgLog);
57+
return msgResponse;
4658
}
47-
48-
return arrayDataAddress;
4959
};
5060

5161
module.exports = {

src/test/unit-test/api-integration/helpers/format/address.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ describe("- splitAddressByLastDot helper (Unit Test)", () => {
3535
);
3636
await expect(typeof splitAddressByLastDotResult == "object").toBe(true);
3737
});
38-
38+
3939
msg =
4040
"Should return a string type if an invalid argument is passed (This function expects a single argument)";
4141
it(msg, async () => {
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
"use strict";
2+
//Helpers
3+
const {
4+
getDataFromSpecificAddress,
5+
} = require("../../../../../api-integration/helpers/request/get-data-from-address");
6+
//Const
7+
const MOCK_OBJECT_VALUE = { mock_object_key: "mock_object_value" };
8+
const MOCK_INVALID_IP_VALUE = "192.77";
9+
const MOCK_VALID_IP_VALUE = "8.8.8.8";
10+
//Vars
11+
let msg;
12+
let getDataFromSpecificAddressResult;
13+
14+
describe("- getDataFromSpecificAddress helper (Unit Test)", () => {
15+
describe("1) Check cases for arguments.", () => {
16+
msg =
17+
"Should return a string type if no arguments are passed (This function expects a single argument of string type)";
18+
it(msg, async () => {
19+
getDataFromSpecificAddressResult = await getDataFromSpecificAddress();
20+
await expect(typeof getDataFromSpecificAddressResult == "string").toBe(
21+
true
22+
);
23+
});
24+
25+
msg =
26+
"Should return a string type if not string argument is passed (This function expects a single argument of string type)";
27+
it(msg, async () => {
28+
getDataFromSpecificAddressResult = await getDataFromSpecificAddress(
29+
MOCK_OBJECT_VALUE
30+
);
31+
await expect(typeof getDataFromSpecificAddressResult == "string").toBe(
32+
true
33+
);
34+
});
35+
36+
msg =
37+
"Should return a string type if an invalid ip is passed (This function expects a single argument of string type)";
38+
it(msg, async () => {
39+
getDataFromSpecificAddressResult = await getDataFromSpecificAddress(
40+
MOCK_INVALID_IP_VALUE
41+
);
42+
await expect(typeof getDataFromSpecificAddressResult == "string").toBe(
43+
true
44+
);
45+
});
46+
47+
msg =
48+
"Should return a string type if a valid ip is passed (This function expects a single argument of string type)";
49+
it(msg, async () => {
50+
getDataFromSpecificAddressResult = await getDataFromSpecificAddress(
51+
MOCK_VALID_IP_VALUE
52+
);
53+
await expect(typeof getDataFromSpecificAddressResult == "string").toBe(
54+
true
55+
);
56+
});
57+
58+
msg =
59+
"Should return a string type if a null value is passed (This function expects a single argument of string type)";
60+
it(msg, async () => {
61+
getDataFromSpecificAddressResult = await getDataFromSpecificAddress(null);
62+
await expect(typeof getDataFromSpecificAddressResult == "string").toBe(
63+
true
64+
);
65+
});
66+
67+
msg =
68+
"Should return a string type if an undefined value is passed (This function expects a single argument of string type)";
69+
it(msg, async () => {
70+
getDataFromSpecificAddressResult = await getDataFromSpecificAddress(
71+
undefined
72+
);
73+
await expect(typeof getDataFromSpecificAddressResult == "string").toBe(
74+
true
75+
);
76+
});
77+
});
78+
79+
// describe("2) Check cases return cases.", () => {
80+
// msg =
81+
// "Should return a object type with data attribute if a valid ip is passed (This function expects a single argument of string type)";
82+
// it(msg, async () => {
83+
// getDataFromSpecificAddressResult = await getDataFromSpecificAddress(
84+
// MOCK_VALID_IP_VALUE
85+
// );
86+
// // await expect(typeof getDataFromSpecificAddressResult == "string").toBe(
87+
// // true
88+
// // );
89+
// });
90+
// });
91+
92+
// describe("2) Check cases for error.", () => {
93+
// msg =
94+
// "Should not return a error message and not throw an error if no argument is passed to the function.";
95+
// it(msg, async () => {
96+
// await expect(async () => await getDataFromSpecificAddress()).not.toThrow(
97+
// Error
98+
// );
99+
// });
100+
101+
// msg =
102+
// "Should not return a error message and not throw an error if a new Error() is passed to the function.";
103+
// it(msg, async () => {
104+
// await expect(
105+
// async () => await getDataFromSpecificAddress(new Error())
106+
// ).not.toThrow(Error);
107+
// });
108+
// });
109+
});

0 commit comments

Comments
 (0)