@hyper-fetch/axios 7.5.2 → 7.5.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/__tests__/features/adapter/adapter.spec.ts +119 -0
- package/__tests__/global.d.ts +1 -0
- package/__tests__/jest.setup.ts +7 -0
- package/__tests__/tsconfig.json +10 -0
- package/jest.config.ts +6 -0
- package/package.json +1 -1
- package/project.json +4 -0
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { Client } from "@hyper-fetch/core";
|
|
2
|
+
import { createHttpMockingServer } from "@hyper-fetch/testing";
|
|
3
|
+
|
|
4
|
+
import { AxiosAdapter } from "../../../src/adapter/adapter";
|
|
5
|
+
|
|
6
|
+
const { startServer, stopServer, resetMocks, mockRequest } = createHttpMockingServer();
|
|
7
|
+
|
|
8
|
+
describe("Axios Adapter [ Base ]", () => {
|
|
9
|
+
let client = new Client({ url: "wrong/shared-base-url" }).setAdapter(AxiosAdapter);
|
|
10
|
+
let request = client.createRequest()({ endpoint: "/shared-endpoint" });
|
|
11
|
+
|
|
12
|
+
beforeAll(() => {
|
|
13
|
+
startServer();
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
beforeEach(() => {
|
|
17
|
+
client = new Client({ url: "shared-base-url" }).setAdapter(AxiosAdapter);
|
|
18
|
+
request = client.createRequest()({ endpoint: "/shared-endpoint" });
|
|
19
|
+
|
|
20
|
+
resetMocks();
|
|
21
|
+
jest.resetAllMocks();
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
afterAll(() => {
|
|
25
|
+
stopServer();
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it("should make a request and return success data with status", async () => {
|
|
29
|
+
const response = mockRequest(request);
|
|
30
|
+
const { data, status, error, extra } = await request.send();
|
|
31
|
+
|
|
32
|
+
expect(data).toStrictEqual(response);
|
|
33
|
+
expect(status).toBe(200);
|
|
34
|
+
expect(error).toBe(null);
|
|
35
|
+
expect({ ...extra?.headers }).toStrictEqual({
|
|
36
|
+
"content-type": "application/json",
|
|
37
|
+
"content-length": "2",
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("should make a request and return error with status", async () => {
|
|
42
|
+
mockRequest(request, { status: 400 });
|
|
43
|
+
const { data, error, status, extra } = await request.send();
|
|
44
|
+
|
|
45
|
+
expect(error?.message).toBe("Request failed with status code 400");
|
|
46
|
+
expect(data).toStrictEqual(null);
|
|
47
|
+
expect(status).toBe(400);
|
|
48
|
+
expect({ ...extra?.headers }).toStrictEqual({
|
|
49
|
+
"content-type": "application/json",
|
|
50
|
+
"content-length": "19",
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("should handle errors with no response object", async () => {
|
|
55
|
+
// Mock XMLHttpRequest to simulate a network error
|
|
56
|
+
const originalXHR = global.XMLHttpRequest;
|
|
57
|
+
global.XMLHttpRequest = jest.fn(() => ({
|
|
58
|
+
open: jest.fn(),
|
|
59
|
+
send: jest.fn(() => {
|
|
60
|
+
throw new Error("Network Error");
|
|
61
|
+
}),
|
|
62
|
+
setRequestHeader: jest.fn(),
|
|
63
|
+
addEventListener: jest.fn(),
|
|
64
|
+
})) as any;
|
|
65
|
+
|
|
66
|
+
const { data, error, status, extra } = await request.send();
|
|
67
|
+
|
|
68
|
+
expect(error).toBeInstanceOf(Error);
|
|
69
|
+
expect(data).toStrictEqual(null);
|
|
70
|
+
expect(status).toBe(0); // systemErrorStatus default
|
|
71
|
+
expect(extra?.headers).toBeUndefined();
|
|
72
|
+
|
|
73
|
+
// Restore original XMLHttpRequest
|
|
74
|
+
global.XMLHttpRequest = originalXHR;
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it("should allow to call the request callbacks", async () => {
|
|
78
|
+
mockRequest(request);
|
|
79
|
+
|
|
80
|
+
const spySettle = jest.fn();
|
|
81
|
+
const spyReqStart = jest.fn();
|
|
82
|
+
const spyResStart = jest.fn();
|
|
83
|
+
const spyUpload = jest.fn();
|
|
84
|
+
const spyDownload = jest.fn();
|
|
85
|
+
const spyResponse = jest.fn();
|
|
86
|
+
|
|
87
|
+
await request.send({
|
|
88
|
+
onBeforeSent: spySettle,
|
|
89
|
+
onRequestStart: spyReqStart,
|
|
90
|
+
onResponseStart: spyResStart,
|
|
91
|
+
onUploadProgress: spyUpload,
|
|
92
|
+
onDownloadProgress: spyDownload,
|
|
93
|
+
onResponse: spyResponse,
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
expect(spySettle).toHaveBeenCalledTimes(1);
|
|
97
|
+
expect(spyReqStart).toHaveBeenCalledTimes(1);
|
|
98
|
+
expect(spyResStart).toHaveBeenCalledTimes(1);
|
|
99
|
+
expect(spyUpload).toHaveBeenCalledTimes(3);
|
|
100
|
+
expect(spyDownload).toHaveBeenCalledTimes(3);
|
|
101
|
+
expect(spyResponse).toHaveBeenCalledTimes(1);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it("should allow to add upload progress", async () => {
|
|
105
|
+
const postRequest = client.createRequest<{ payload: { name: string } }>()({
|
|
106
|
+
endpoint: "/shared-endpoint",
|
|
107
|
+
method: "POST",
|
|
108
|
+
});
|
|
109
|
+
mockRequest(postRequest);
|
|
110
|
+
|
|
111
|
+
const spyUpload = jest.fn();
|
|
112
|
+
await postRequest.send({
|
|
113
|
+
payload: { name: "test" },
|
|
114
|
+
onUploadProgress: spyUpload,
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
expect(spyUpload).toHaveBeenCalledTimes(3);
|
|
118
|
+
});
|
|
119
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import "jest-extended";
|
package/jest.config.ts
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hyper-fetch/axios",
|
|
3
|
-
"version": "7.5.
|
|
3
|
+
"version": "7.5.3",
|
|
4
4
|
"description": "Simple axios adapter for Hyper Fetch",
|
|
5
5
|
"author": "Maciej Pyrc <maciekpyrc@gmail.com>, Kacper Skawina <kacper.skawina@gmail.com>",
|
|
6
6
|
"homepage": "https://hyperfetch.bettertyped.com/",
|
package/project.json
ADDED