Skip to content

Commit

Permalink
Added and improved coverage to tests + fixed vulnerabilities
Browse files Browse the repository at this point in the history
  • Loading branch information
Jialecl committed Sep 17, 2024
1 parent 9d35494 commit 7d138c6
Show file tree
Hide file tree
Showing 5 changed files with 416 additions and 71 deletions.
10 changes: 5 additions & 5 deletions lib/jest.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ module.exports = {
coverageThreshold: {
global: {
// global thresholds
branches: 20,
functions: 20,
lines: 20,
statements: 20,
branches: 50,
functions: 50,
lines: 80,
statements: 80,
},
},
collectCoverage: true,
collectCoverageFrom: ["**/src/**/*.{js}", "!**/node_modules/**", "!**/coverage/**", "!**/test/**"],
collectCoverageFrom: ["src/*.js", "src/**/*.js", "!src/main.js", "!**/node_modules/**"],
testURL: "http://localhost",
testEnvironment: "jsdom",
transform: {
Expand Down
71 changes: 24 additions & 47 deletions lib/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions lib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
"copy": "node ./scripts/build/copy-distribution-files.js",
"start": "rollup -c -w",
"build": "npm run bundle && npm run copy",
"test": "jest --transformIgnorePatterns node_modules/?!axios/",
"test:watch": "npm test -- --watch --coverage",
"test:coverage": "npm test -- --coverage --coverageReporters=cobertura",
"test": "jest --collect-coverage --transformIgnorePatterns node_modules/?!axios/",
"test:watch": "npm test --watch --coverage",
"test:coverage": "npm test --collect-coverage --coverageReporters=cobertura",
"test:coverage-dashboard": "npm test --collect-coverage"
},
"author": "DXC Halstack team",
Expand Down
45 changes: 29 additions & 16 deletions lib/test/hal-api-caller.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import axios from 'axios';
import MockAdapter from 'axios-mock-adapter';
import axios from "axios";
import MockAdapter from "axios-mock-adapter";

// Import the function to be tested
import apicaller from '../src/hal-api-caller';
import response from './mocks/options-response.json';
import apicaller from "../src/hal-api-caller";
import optionsResponse from "./mocks/options-response.json";
import getResponse from "./mocks/get-response.json";

describe('options function', () => {
describe("options function", () => {
let mockAxios;

beforeEach(() => {
Expand All @@ -16,16 +17,11 @@ describe('options function', () => {
mockAxios.restore();
});

it('should make a successful OPTIONS request and return HalResponse', async () => {
const url = 'https://example.com';
const headers = { CustomHeader: 'CustomValue' };
const responseData = response;
const responseHeaders = { 'Content-Type': 'application/json' };
const expectedHalResponse = {
body: { _options: responseData },
headers: responseHeaders,
status: 200,
};
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);
Expand All @@ -34,9 +30,26 @@ describe('options 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 () => {
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.getTitle()).toEqual('Options available on Quote ID');
expect(result.halResource.getItems().length).toBe(10);
});
});
Loading

0 comments on commit 7d138c6

Please sign in to comment.