Skip to content

Commit 1136623

Browse files
authored
Merge pull request #103 from dxc-technology/feature/coverage
Added tests to increment the coverage
2 parents 25d27ff + 5f7188e commit 1136623

File tree

3 files changed

+166
-10
lines changed

3 files changed

+166
-10
lines changed

lib/test/hal-api-caller.test.js

Lines changed: 103 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import axios from "axios";
22
import MockAdapter from "axios-mock-adapter";
3-
4-
// Import the function to be tested
53
import apicaller from "../src/hal-api-caller";
64
import optionsResponse from "./mocks/options-response.json";
75
import getResponse from "./mocks/get-response.json";
@@ -17,39 +15,134 @@ describe("Apicaller functions test", () => {
1715
mockAxios.restore();
1816
});
1917

20-
it("should make a successful OPTIONS request and return HalResponse", async () => {
18+
it("Should make a successful OPTIONS request and return HalResponse", async () => {
2119
const url = "https://example.com";
2220
const headers = { CustomHeader: "CustomValue" };
2321
const responseData = optionsResponse;
2422
const responseHeaders = { "Content-Type": "application/json" };
2523

26-
// Mocking the Axios request and response
2724
mockAxios.onOptions(url).reply(200, responseData, responseHeaders);
2825

29-
// Calling the function
3026
const result = await apicaller.options({ url, headers });
3127

32-
// Assertions
3328
expect(result).toBeDefined();
3429
expect(result.halResource).toBeDefined();
3530
expect(result.halResource.getTitle()).toEqual("Options available on Quote ID");
3631
});
3732

38-
it("should make a successful GET request and return HalResponse", async () => {
33+
it("Should make a successful GET request and return HalResponse", async () => {
3934
const url = "https://example.com";
4035
const headers = { CustomHeader: "CustomValue" };
4136
const responseData = getResponse;
4237
const responseHeaders = { "Content-Type": "application/json" };
4338

44-
// Mocking the Axios request and response
4539
mockAxios.onGet(url).reply(200, responseData, responseHeaders);
4640

47-
// Calling the function
4841
const result = await apicaller.get({ url, headers });
4942

50-
// Assertions
5143
expect(result).toBeDefined();
5244
expect(result.halResource).toBeDefined();
5345
expect(result.halResource.getItems().length).toBe(10);
5446
});
47+
48+
it("Should make a successful PATCH request", async () => {
49+
const url = "https://example.com";
50+
const body = { data: "updatedData" };
51+
const headers = { CustomHeader: "CustomValue" };
52+
const responseData = { success: true };
53+
const responseHeaders = { "Content-Type": "application/json" };
54+
55+
mockAxios.onPatch(url).reply(200, responseData, responseHeaders);
56+
57+
const result = await apicaller.patch({ url, body, headers });
58+
59+
expect(result).toBeDefined();
60+
expect(result.status).toBe(200);
61+
expect(result.body).toEqual(responseData);
62+
});
63+
64+
it("Should handle PATCH request failure", async () => {
65+
const url = "https://example.com";
66+
const body = { data: "updatedData" };
67+
const headers = { CustomHeader: "CustomValue" };
68+
69+
mockAxios.onPatch(url).reply(500);
70+
71+
await expect(apicaller.patch({ url, body, headers })).rejects.toThrow("Request failed with status code 500");
72+
});
73+
74+
it("Should make a successful POST request", async () => {
75+
const url = "https://example.com";
76+
const body = { name: "New Resource" };
77+
const headers = { CustomHeader: "CustomValue" };
78+
const responseData = { id: 1 };
79+
const responseHeaders = { "Content-Type": "application/json" };
80+
81+
mockAxios.onPost(url).reply(201, responseData, responseHeaders);
82+
83+
const result = await apicaller.post({ url, body, headers });
84+
85+
expect(result).toBeDefined();
86+
expect(result.status).toBe(201);
87+
expect(result.body).toEqual(responseData);
88+
});
89+
90+
it("Should handle POST request failure", async () => {
91+
const url = "https://example.com";
92+
const body = { name: "New Resource" };
93+
const headers = { CustomHeader: "CustomValue" };
94+
95+
mockAxios.onPost(url).reply(404);
96+
97+
await expect(apicaller.post({ url, body, headers })).rejects.toThrow("Request failed with status code 404");
98+
});
99+
100+
it("Should make a successful PUT request", async () => {
101+
const url = "https://example.com";
102+
const body = { name: "Updated Resource" };
103+
const headers = { CustomHeader: "CustomValue" };
104+
const responseData = { success: true };
105+
const responseHeaders = { "Content-Type": "application/json" };
106+
107+
mockAxios.onPut(url).reply(200, responseData, responseHeaders);
108+
109+
const result = await apicaller.put({ url, body, headers });
110+
111+
expect(result).toBeDefined();
112+
expect(result.status).toBe(200);
113+
expect(result.body).toEqual(responseData);
114+
});
115+
116+
it("Should make a successful DELETE request", async () => {
117+
const url = "https://example.com";
118+
const headers = { CustomHeader: "CustomValue" };
119+
const responseData = { success: true };
120+
const responseHeaders = { "Content-Type": "application/json" };
121+
122+
mockAxios.onDelete(url).reply(204, responseData, responseHeaders);
123+
124+
const result = await apicaller.del({ url, headers });
125+
126+
expect(result).toBeDefined();
127+
expect(result.status).toBe(204);
128+
expect(result.body).toEqual(responseData);
129+
});
130+
131+
it("Should handle DELETE request failure", async () => {
132+
const url = "https://example.com";
133+
const headers = { CustomHeader: "CustomValue" };
134+
135+
mockAxios.onDelete(url).reply(403);
136+
137+
await expect(apicaller.del({ url, headers })).rejects.toThrow("Request failed with status code 403");
138+
});
139+
140+
it("Should handle OPTIONS request failure", async () => {
141+
const url = "https://example.com";
142+
const headers = { CustomHeader: "CustomValue" };
143+
144+
mockAxios.onOptions(url).reply(500);
145+
146+
await expect(apicaller.options({ url, headers })).rejects.toThrow("Request failed with status code 500");
147+
});
55148
});

lib/test/hal-resource.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,7 @@ const interaction = {
558558
href: "https://bgqrqjl2t2.execute-api.us-west-1.amazonaws.com/dev/realms/us-east-1_wCPANetpN/users",
559559
title: "Search Users Collection",
560560
};
561+
561562
const linkSelf = {
562563
rel: "self",
563564
name: "User",
@@ -570,6 +571,7 @@ const otherLink = {
570571
href: "https://otherLink.dxc.com",
571572
title: "otherLink",
572573
};
574+
573575
const userResource = {
574576
_links: {
575577
self: {
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { getType, containsHalResource } from "../src/response/service/hal-response-service";
2+
3+
describe("getType function tests", () => {
4+
it("Should return the value of content-type if present", () => {
5+
const headers = { "content-type": "application/json" };
6+
const result = getType({ headers });
7+
expect(result).toBe("application/json");
8+
});
9+
10+
it("Should return null if content-type is not present", () => {
11+
const headers = { "content-location": "http://example.com/resource" };
12+
const result = getType({ headers });
13+
expect(result).toBeNull();
14+
});
15+
});
16+
17+
describe("containsHalResource function tests", () => {
18+
const HAL_JSON = "application/vnd.hal+json";
19+
const HAL_JSON2 = "application/hal+json";
20+
21+
it("Should return true if content-type includes application/vnd.hal+json", () => {
22+
const headers = { "content-type": HAL_JSON };
23+
const result = containsHalResource({ headers });
24+
expect(result).toBe(true);
25+
});
26+
27+
it("Should return true if content-type includes application/hal+json", () => {
28+
const headers = { "content-type": HAL_JSON2 };
29+
const result = containsHalResource({ headers });
30+
expect(result).toBe(true);
31+
});
32+
33+
it("Should return false if content-location does not equal location", () => {
34+
const headers = {
35+
"content-location": "http://example.com/resource",
36+
location: "http://example.com/another-resource",
37+
};
38+
const result = containsHalResource({ headers });
39+
expect(result).toBe(false);
40+
});
41+
42+
it("Should return false if headers do not meet any conditions", () => {
43+
const headers = {
44+
"content-type": "text/html",
45+
"content-location": "http://example.com/resource",
46+
location: "http://example.com/another-resource",
47+
};
48+
const result = containsHalResource({ headers });
49+
expect(result).toBe(false);
50+
});
51+
52+
it("Should return false if headers is null", () => {
53+
const result = containsHalResource();
54+
expect(result).toBe(false);
55+
});
56+
57+
it("Should return false if headers is undefined", () => {
58+
const result = containsHalResource({});
59+
expect(result).toBe(false);
60+
});
61+
});

0 commit comments

Comments
 (0)