|
| 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