Skip to content

Commit

Permalink
test: add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
KillWolfVlad committed Aug 21, 2023
1 parent 7c10c0b commit 4d05306
Show file tree
Hide file tree
Showing 4 changed files with 515 additions and 6 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
"tslib": "^2.4.0"
},
"devDependencies": {
"@byndyusoft/eslint-config": "2.1.1",
"@byndyusoft/eslint-config": "2.4.1",
"@byndyusoft/tsconfig": "1.2.0",
"@commitlint/cli": "17.1.2",
"@commitlint/config-conventional": "17.1.0",
Expand All @@ -89,11 +89,13 @@
"husky": "8.0.1",
"jest": "29.2.0",
"jest-extended": "3.1.0",
"jest-mock-extended": "3.0.1",
"lint-staged": "13.0.3",
"markdownlint-cli": "0.32.2",
"pinst": "3.0.0",
"prettier": "2.7.1",
"prettier-plugin-packagejson": "2.3.0",
"reflect-metadata": "0.1.13",
"semantic-release": "19.0.5",
"shx": "0.3.4",
"ts-jest": "29.0.3",
Expand Down
135 changes: 135 additions & 0 deletions src/__tests__/httpClient.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*
* Copyright 2023 Byndyusoft
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { AxiosResponse } from "axios";
import { mock, MockProxy } from "jest-mock-extended";

import { HttpClient } from "../httpClient";
import { HttpCoreClient } from "../httpCoreClient";

describe("HttpClient", () => {
let httpClient: HttpClient;

let httpCoreClient: MockProxy<HttpCoreClient>;

const httpMethodsWithoutBody = [
"delete" as const,
"get" as const,
"head" as const,
];

const httpMethodsWithBody = [
"patch" as const,
"post" as const,
"put" as const,
];

const responseData = {
answer: 42,
};

const response = {
data: responseData,
} as unknown as AxiosResponse;

beforeAll(() => {
httpCoreClient = mock();

httpClient = new HttpClient(httpCoreClient);
});

it.each(httpMethodsWithoutBody)("must perform %s request", async (method) => {
httpCoreClient[method].mockResolvedValue(response);

await expect(
httpClient[method]("/some-path", {
params: {
q: "search",
},
}),
).resolves.toStrictEqual(responseData);

expect(httpCoreClient[method]).toHaveBeenCalledWith("/some-path", {
params: {
q: "search",
},
});
});

it.each(httpMethodsWithBody)("must perform %s request", async (method) => {
httpCoreClient[method].mockResolvedValue(response);

await expect(
httpClient[method](
"/some-path",
{
payload: 1,
},
{
params: {
q: "search",
},
},
),
).resolves.toStrictEqual(responseData);

expect(httpCoreClient[method]).toHaveBeenCalledWith(
"/some-path",
{
payload: 1,
},
{
params: {
q: "search",
},
},
);
});

it("must perform request", async () => {
httpCoreClient.request.mockResolvedValue(response);

await expect(
httpClient.request({
url: "/some-path",
params: {
q: "search",
},
}),
).resolves.toStrictEqual(responseData);

expect(httpCoreClient.request).toHaveBeenCalledWith({
url: "/some-path",
params: {
q: "search",
},
});
});

it("must perform endpoint request", async () => {
httpCoreClient.endpoint.mockResolvedValue(response);

await expect(
httpClient.endpoint("GET /some-path", {
q: "search",
}),
).resolves.toStrictEqual(responseData);

expect(httpCoreClient.endpoint).toHaveBeenCalledWith("GET /some-path", {
q: "search",
});
});
});

0 comments on commit 4d05306

Please sign in to comment.