Skip to content

Commit 4d05306

Browse files
committed
test: add unit tests
1 parent 7c10c0b commit 4d05306

File tree

4 files changed

+515
-6
lines changed

4 files changed

+515
-6
lines changed

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
"tslib": "^2.4.0"
7777
},
7878
"devDependencies": {
79-
"@byndyusoft/eslint-config": "2.1.1",
79+
"@byndyusoft/eslint-config": "2.4.1",
8080
"@byndyusoft/tsconfig": "1.2.0",
8181
"@commitlint/cli": "17.1.2",
8282
"@commitlint/config-conventional": "17.1.0",
@@ -89,11 +89,13 @@
8989
"husky": "8.0.1",
9090
"jest": "29.2.0",
9191
"jest-extended": "3.1.0",
92+
"jest-mock-extended": "3.0.1",
9293
"lint-staged": "13.0.3",
9394
"markdownlint-cli": "0.32.2",
9495
"pinst": "3.0.0",
9596
"prettier": "2.7.1",
9697
"prettier-plugin-packagejson": "2.3.0",
98+
"reflect-metadata": "0.1.13",
9799
"semantic-release": "19.0.5",
98100
"shx": "0.3.4",
99101
"ts-jest": "29.0.3",

src/__tests__/httpClient.test.ts

+135
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
/*
2+
* Copyright 2023 Byndyusoft
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import { AxiosResponse } from "axios";
18+
import { mock, MockProxy } from "jest-mock-extended";
19+
20+
import { HttpClient } from "../httpClient";
21+
import { HttpCoreClient } from "../httpCoreClient";
22+
23+
describe("HttpClient", () => {
24+
let httpClient: HttpClient;
25+
26+
let httpCoreClient: MockProxy<HttpCoreClient>;
27+
28+
const httpMethodsWithoutBody = [
29+
"delete" as const,
30+
"get" as const,
31+
"head" as const,
32+
];
33+
34+
const httpMethodsWithBody = [
35+
"patch" as const,
36+
"post" as const,
37+
"put" as const,
38+
];
39+
40+
const responseData = {
41+
answer: 42,
42+
};
43+
44+
const response = {
45+
data: responseData,
46+
} as unknown as AxiosResponse;
47+
48+
beforeAll(() => {
49+
httpCoreClient = mock();
50+
51+
httpClient = new HttpClient(httpCoreClient);
52+
});
53+
54+
it.each(httpMethodsWithoutBody)("must perform %s request", async (method) => {
55+
httpCoreClient[method].mockResolvedValue(response);
56+
57+
await expect(
58+
httpClient[method]("/some-path", {
59+
params: {
60+
q: "search",
61+
},
62+
}),
63+
).resolves.toStrictEqual(responseData);
64+
65+
expect(httpCoreClient[method]).toHaveBeenCalledWith("/some-path", {
66+
params: {
67+
q: "search",
68+
},
69+
});
70+
});
71+
72+
it.each(httpMethodsWithBody)("must perform %s request", async (method) => {
73+
httpCoreClient[method].mockResolvedValue(response);
74+
75+
await expect(
76+
httpClient[method](
77+
"/some-path",
78+
{
79+
payload: 1,
80+
},
81+
{
82+
params: {
83+
q: "search",
84+
},
85+
},
86+
),
87+
).resolves.toStrictEqual(responseData);
88+
89+
expect(httpCoreClient[method]).toHaveBeenCalledWith(
90+
"/some-path",
91+
{
92+
payload: 1,
93+
},
94+
{
95+
params: {
96+
q: "search",
97+
},
98+
},
99+
);
100+
});
101+
102+
it("must perform request", async () => {
103+
httpCoreClient.request.mockResolvedValue(response);
104+
105+
await expect(
106+
httpClient.request({
107+
url: "/some-path",
108+
params: {
109+
q: "search",
110+
},
111+
}),
112+
).resolves.toStrictEqual(responseData);
113+
114+
expect(httpCoreClient.request).toHaveBeenCalledWith({
115+
url: "/some-path",
116+
params: {
117+
q: "search",
118+
},
119+
});
120+
});
121+
122+
it("must perform endpoint request", async () => {
123+
httpCoreClient.endpoint.mockResolvedValue(response);
124+
125+
await expect(
126+
httpClient.endpoint("GET /some-path", {
127+
q: "search",
128+
}),
129+
).resolves.toStrictEqual(responseData);
130+
131+
expect(httpCoreClient.endpoint).toHaveBeenCalledWith("GET /some-path", {
132+
q: "search",
133+
});
134+
});
135+
});

0 commit comments

Comments
 (0)