-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Commons): introduce options for request functions
- Loading branch information
Showing
3 changed files
with
78 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import axios from "axios"; | ||
import { jest } from "@jest/globals"; | ||
import ApiClientBase from "./ApiClientBase.js"; | ||
|
||
const requestFn = jest.fn(); | ||
|
||
jest.mock("axios"); | ||
const mockedAxios = axios as jest.Mocked<typeof axios>; | ||
mockedAxios.create.mockReturnValue(mockedAxios); | ||
mockedAxios.request.mockImplementation(requestFn as never); | ||
|
||
class TestClient extends ApiClientBase { | ||
public testRequest = this.requestFunctionFactory({ | ||
path: "/test", | ||
method: "GET", | ||
operationId: "test", | ||
}); | ||
} | ||
const testClient = new TestClient(); | ||
|
||
test("onBeforeRequest is called before actual request", async () => { | ||
const onBeforeRequest = jest.fn(() => { | ||
expect(requestFn).not.toHaveBeenCalled(); | ||
}); | ||
|
||
await testClient.testRequest(undefined, { | ||
onBeforeRequest, | ||
}); | ||
|
||
expect(onBeforeRequest).toHaveBeenCalledTimes(1); | ||
expect(requestFn).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
test("onBeforeRequest configured in default options is called before actual request", async () => { | ||
const onBeforeRequest = jest.fn(() => { | ||
expect(requestFn).not.toHaveBeenCalled(); | ||
}); | ||
|
||
testClient.defaultRequestOptions.onBeforeRequest = onBeforeRequest; | ||
await testClient.testRequest(undefined); | ||
|
||
expect(onBeforeRequest).toHaveBeenCalledTimes(1); | ||
expect(requestFn).toHaveBeenCalledTimes(1); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters