Skip to content

Commit

Permalink
Merge pull request #103 from dxc-technology/feature/coverage
Browse files Browse the repository at this point in the history
Added tests to increment the coverage
  • Loading branch information
GomezIvann authored Dec 5, 2024
2 parents 25d27ff + 5f7188e commit 1136623
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 10 deletions.
113 changes: 103 additions & 10 deletions lib/test/hal-api-caller.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import axios from "axios";
import MockAdapter from "axios-mock-adapter";

// Import the function to be tested
import apicaller from "../src/hal-api-caller";
import optionsResponse from "./mocks/options-response.json";
import getResponse from "./mocks/get-response.json";
Expand All @@ -17,39 +15,134 @@ describe("Apicaller functions test", () => {
mockAxios.restore();
});

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

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

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

// Assertions
expect(result).toBeDefined();
expect(result.halResource).toBeDefined();
expect(result.halResource.getTitle()).toEqual("Options available on Quote ID");
});

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

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

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

// Assertions
expect(result).toBeDefined();
expect(result.halResource).toBeDefined();
expect(result.halResource.getItems().length).toBe(10);
});

it("Should make a successful PATCH request", async () => {
const url = "https://example.com";
const body = { data: "updatedData" };
const headers = { CustomHeader: "CustomValue" };
const responseData = { success: true };
const responseHeaders = { "Content-Type": "application/json" };

mockAxios.onPatch(url).reply(200, responseData, responseHeaders);

const result = await apicaller.patch({ url, body, headers });

expect(result).toBeDefined();
expect(result.status).toBe(200);
expect(result.body).toEqual(responseData);
});

it("Should handle PATCH request failure", async () => {
const url = "https://example.com";
const body = { data: "updatedData" };
const headers = { CustomHeader: "CustomValue" };

mockAxios.onPatch(url).reply(500);

await expect(apicaller.patch({ url, body, headers })).rejects.toThrow("Request failed with status code 500");
});

it("Should make a successful POST request", async () => {
const url = "https://example.com";
const body = { name: "New Resource" };
const headers = { CustomHeader: "CustomValue" };
const responseData = { id: 1 };
const responseHeaders = { "Content-Type": "application/json" };

mockAxios.onPost(url).reply(201, responseData, responseHeaders);

const result = await apicaller.post({ url, body, headers });

expect(result).toBeDefined();
expect(result.status).toBe(201);
expect(result.body).toEqual(responseData);
});

it("Should handle POST request failure", async () => {
const url = "https://example.com";
const body = { name: "New Resource" };
const headers = { CustomHeader: "CustomValue" };

mockAxios.onPost(url).reply(404);

await expect(apicaller.post({ url, body, headers })).rejects.toThrow("Request failed with status code 404");
});

it("Should make a successful PUT request", async () => {
const url = "https://example.com";
const body = { name: "Updated Resource" };
const headers = { CustomHeader: "CustomValue" };
const responseData = { success: true };
const responseHeaders = { "Content-Type": "application/json" };

mockAxios.onPut(url).reply(200, responseData, responseHeaders);

const result = await apicaller.put({ url, body, headers });

expect(result).toBeDefined();
expect(result.status).toBe(200);
expect(result.body).toEqual(responseData);
});

it("Should make a successful DELETE request", async () => {
const url = "https://example.com";
const headers = { CustomHeader: "CustomValue" };
const responseData = { success: true };
const responseHeaders = { "Content-Type": "application/json" };

mockAxios.onDelete(url).reply(204, responseData, responseHeaders);

const result = await apicaller.del({ url, headers });

expect(result).toBeDefined();
expect(result.status).toBe(204);
expect(result.body).toEqual(responseData);
});

it("Should handle DELETE request failure", async () => {
const url = "https://example.com";
const headers = { CustomHeader: "CustomValue" };

mockAxios.onDelete(url).reply(403);

await expect(apicaller.del({ url, headers })).rejects.toThrow("Request failed with status code 403");
});

it("Should handle OPTIONS request failure", async () => {
const url = "https://example.com";
const headers = { CustomHeader: "CustomValue" };

mockAxios.onOptions(url).reply(500);

await expect(apicaller.options({ url, headers })).rejects.toThrow("Request failed with status code 500");
});
});
2 changes: 2 additions & 0 deletions lib/test/hal-resource.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,7 @@ const interaction = {
href: "https://bgqrqjl2t2.execute-api.us-west-1.amazonaws.com/dev/realms/us-east-1_wCPANetpN/users",
title: "Search Users Collection",
};

const linkSelf = {
rel: "self",
name: "User",
Expand All @@ -570,6 +571,7 @@ const otherLink = {
href: "https://otherLink.dxc.com",
title: "otherLink",
};

const userResource = {
_links: {
self: {
Expand Down
61 changes: 61 additions & 0 deletions lib/test/hal-response-service.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { getType, containsHalResource } from "../src/response/service/hal-response-service";

describe("getType function tests", () => {
it("Should return the value of content-type if present", () => {
const headers = { "content-type": "application/json" };
const result = getType({ headers });
expect(result).toBe("application/json");
});

it("Should return null if content-type is not present", () => {
const headers = { "content-location": "http://example.com/resource" };
const result = getType({ headers });
expect(result).toBeNull();
});
});

describe("containsHalResource function tests", () => {
const HAL_JSON = "application/vnd.hal+json";
const HAL_JSON2 = "application/hal+json";

it("Should return true if content-type includes application/vnd.hal+json", () => {
const headers = { "content-type": HAL_JSON };
const result = containsHalResource({ headers });
expect(result).toBe(true);
});

it("Should return true if content-type includes application/hal+json", () => {
const headers = { "content-type": HAL_JSON2 };
const result = containsHalResource({ headers });
expect(result).toBe(true);
});

it("Should return false if content-location does not equal location", () => {
const headers = {
"content-location": "http://example.com/resource",
location: "http://example.com/another-resource",
};
const result = containsHalResource({ headers });
expect(result).toBe(false);
});

it("Should return false if headers do not meet any conditions", () => {
const headers = {
"content-type": "text/html",
"content-location": "http://example.com/resource",
location: "http://example.com/another-resource",
};
const result = containsHalResource({ headers });
expect(result).toBe(false);
});

it("Should return false if headers is null", () => {
const result = containsHalResource();
expect(result).toBe(false);
});

it("Should return false if headers is undefined", () => {
const result = containsHalResource({});
expect(result).toBe(false);
});
});

0 comments on commit 1136623

Please sign in to comment.