@hyper-fetch/core 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.base.spec.ts +387 -0
- package/__tests__/features/adapter/adapter.bindings.spec.ts +1023 -0
- package/__tests__/features/adapter/adapter.browser.spec.ts +103 -0
- package/__tests__/features/adapter/adapter.server.spec.ts +177 -0
- package/__tests__/features/adapter/adapter.utils.spec.ts +165 -0
- package/__tests__/features/cache/cache.base.spec.ts +358 -0
- package/__tests__/features/cache/cache.events.spec.ts +215 -0
- package/__tests__/features/cache/cache.garbage-collector.spec.ts +172 -0
- package/__tests__/features/cache/cache.lazy-storage.spec.ts +129 -0
- package/__tests__/features/cache/cache.utils.spec.ts +119 -0
- package/__tests__/features/client/client.auth.spec.ts +125 -0
- package/__tests__/features/client/client.base.spec.ts +149 -0
- package/__tests__/features/client/client.hydration.spec.ts +171 -0
- package/__tests__/features/client/client.interceptor.spec.ts +225 -0
- package/__tests__/features/client/client.methods.spec.ts +188 -0
- package/__tests__/features/client/client.middleware.spec.ts +118 -0
- package/__tests__/features/client/client.utils.spec.ts +292 -0
- package/__tests__/features/dispatcher/dispatcher.base.spec.ts +123 -0
- package/__tests__/features/dispatcher/dispatcher.events.spec.ts +304 -0
- package/__tests__/features/dispatcher/dispatcher.queue.spec.ts +462 -0
- package/__tests__/features/dispatcher/dispatcher.requests.spec.ts +241 -0
- package/__tests__/features/dispatcher/dispatcher.utils.spec.ts +125 -0
- package/__tests__/features/managers/app/app.manager.base.spec.ts +235 -0
- package/__tests__/features/managers/app/app.manager.ssr.spec.ts +40 -0
- package/__tests__/features/managers/logger/logger.base.spec.ts +107 -0
- package/__tests__/features/managers/logger/logger.utils.spec.ts +100 -0
- package/__tests__/features/managers/request/request.manager.base.spec.ts +222 -0
- package/__tests__/features/managers/request/request.manager.events.spec.ts +280 -0
- package/__tests__/features/mocker/mocker.base.spec.ts +567 -0
- package/__tests__/features/plugin/plugin.base.spec.ts +255 -0
- package/__tests__/features/request/request.base.spec.ts +199 -0
- package/__tests__/features/request/request.cloning.spec.ts +47 -0
- package/__tests__/features/request/request.sending.spec.ts +272 -0
- package/__tests__/features/request/request.setters.spec.ts +157 -0
- package/__tests__/features/request/request.typescript.spec.ts +423 -0
- package/__tests__/features/request/request.utils.spec.ts +77 -0
- package/__tests__/features/sdk/sdk.base.spec.ts +130 -0
- package/__tests__/global.d.ts +1 -0
- package/__tests__/jest.setup.ts +13 -0
- package/__tests__/shared/callbacks.shared.ts +9 -0
- package/__tests__/shared/index.ts +2 -0
- package/__tests__/shared/progress.shared.ts +35 -0
- package/__tests__/tsconfig.json +13 -0
- package/__tests__/utils/adapter.utils.ts +38 -0
- package/__tests__/utils/cache.utils.ts +19 -0
- package/__tests__/utils/callbacks.utils.ts +47 -0
- package/__tests__/utils/dispatcher.utils.ts +6 -0
- package/__tests__/utils/index.ts +4 -0
- package/dist/index.js +12 -1
- package/dist/index.js.map +2 -2
- package/jest.config.ts +6 -0
- package/package.json +1 -1
- package/project.json +4 -0
- package/src/http-adapter/http-adapter.server.ts +15 -1
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { createHttpMockingServer } from "@hyper-fetch/testing";
|
|
2
|
+
|
|
3
|
+
import { middlewareCallback } from "../../utils";
|
|
4
|
+
import { testCallbacksExecution } from "../../shared";
|
|
5
|
+
import { Client } from "client";
|
|
6
|
+
|
|
7
|
+
const { resetMocks, startServer, stopServer, mockRequest } = createHttpMockingServer();
|
|
8
|
+
|
|
9
|
+
describe("Client [ Middleware ]", () => {
|
|
10
|
+
let client = new Client({ url: "shared-base-url" });
|
|
11
|
+
let request = client.createRequest()({ endpoint: "shared-base-endpoint" });
|
|
12
|
+
|
|
13
|
+
const spy1 = jest.fn();
|
|
14
|
+
const spy2 = jest.fn();
|
|
15
|
+
const spy3 = jest.fn();
|
|
16
|
+
|
|
17
|
+
beforeAll(() => {
|
|
18
|
+
startServer();
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
beforeEach(() => {
|
|
22
|
+
client = new Client({ url: "shared-base-url" });
|
|
23
|
+
request = client.createRequest()({ endpoint: "shared-base-endpoint" });
|
|
24
|
+
resetMocks();
|
|
25
|
+
jest.clearAllMocks();
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
afterAll(() => {
|
|
29
|
+
stopServer();
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
describe("When middleware callbacks being added", () => {
|
|
33
|
+
it("should assign onAuth middleware", async () => {
|
|
34
|
+
const callback = middlewareCallback();
|
|
35
|
+
client.onAuth(callback).onAuth(callback);
|
|
36
|
+
|
|
37
|
+
expect(client.unstable_onAuthCallbacks).toHaveLength(2);
|
|
38
|
+
expect(client.unstable_onAuthCallbacks[0]).toEqual(callback);
|
|
39
|
+
expect(client.unstable_onAuthCallbacks[1]).toEqual(callback);
|
|
40
|
+
});
|
|
41
|
+
it("should assign onRequest middleware", async () => {
|
|
42
|
+
const callback = middlewareCallback();
|
|
43
|
+
client.onRequest(callback).onRequest(callback);
|
|
44
|
+
|
|
45
|
+
expect(client.unstable_onRequestCallbacks).toHaveLength(2);
|
|
46
|
+
expect(client.unstable_onRequestCallbacks[0]).toEqual(callback);
|
|
47
|
+
expect(client.unstable_onRequestCallbacks[1]).toEqual(callback);
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
describe("When middleware callbacks go into the execution loop", () => {
|
|
52
|
+
it("should trigger unstable_modifyAuth async loop", async () => {
|
|
53
|
+
const callbackFirst = middlewareCallback({ callback: spy1, sleepTime: 10 });
|
|
54
|
+
const callbackMiddle = middlewareCallback({ callback: spy2 });
|
|
55
|
+
const callbackLast = middlewareCallback({ callback: spy3, sleepTime: 10 });
|
|
56
|
+
|
|
57
|
+
client.onAuth(callbackFirst).onAuth(callbackMiddle).onAuth(callbackLast);
|
|
58
|
+
await client.unstable_modifyAuth(request);
|
|
59
|
+
|
|
60
|
+
testCallbacksExecution([spy1, spy2, spy3]);
|
|
61
|
+
});
|
|
62
|
+
it("should trigger unstable_modifyRequest async loop", async () => {
|
|
63
|
+
const callbackFirst = middlewareCallback({ callback: spy1, sleepTime: 10 });
|
|
64
|
+
const callbackMiddle = middlewareCallback({ callback: spy2 });
|
|
65
|
+
const callbackLast = middlewareCallback({ callback: spy3, sleepTime: 10 });
|
|
66
|
+
|
|
67
|
+
client.onRequest(callbackFirst).onRequest(callbackMiddle).onRequest(callbackLast);
|
|
68
|
+
await client.unstable_modifyRequest(request);
|
|
69
|
+
|
|
70
|
+
testCallbacksExecution([spy1, spy2, spy3]);
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
describe("When middleware returns undefined value", () => {
|
|
75
|
+
it("should throw onRequest method when request is not returned", async () => {
|
|
76
|
+
client.onRequest(() => undefined as any);
|
|
77
|
+
|
|
78
|
+
expect(client.unstable_modifyRequest(request)).rejects.toThrow();
|
|
79
|
+
});
|
|
80
|
+
it("should throw onAuth method when request is not returned", async () => {
|
|
81
|
+
client.onAuth(() => undefined as any);
|
|
82
|
+
|
|
83
|
+
expect(client.unstable_modifyAuth(request)).rejects.toThrow();
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
describe("When user wants to remove listeners", () => {
|
|
88
|
+
it("should allow for removing middleware on request", async () => {
|
|
89
|
+
const firstCallback = middlewareCallback({ callback: spy1 });
|
|
90
|
+
const secondCallback = middlewareCallback({ callback: spy2 });
|
|
91
|
+
client.onRequest(firstCallback).onRequest(secondCallback);
|
|
92
|
+
mockRequest(request);
|
|
93
|
+
|
|
94
|
+
await request.send({});
|
|
95
|
+
client.removeOnRequestInterceptors([secondCallback]);
|
|
96
|
+
|
|
97
|
+
await request.send({});
|
|
98
|
+
|
|
99
|
+
expect(spy1).toHaveBeenCalledTimes(2);
|
|
100
|
+
expect(spy2).toHaveBeenCalledTimes(1);
|
|
101
|
+
});
|
|
102
|
+
it("should allow for removing interceptors on auth", async () => {
|
|
103
|
+
const authRequest = client.createRequest()({ endpoint: "/auth" }).setAuth(true);
|
|
104
|
+
const firstCallback = middlewareCallback({ callback: spy1 });
|
|
105
|
+
const secondCallback = middlewareCallback({ callback: spy2 });
|
|
106
|
+
client.onAuth(firstCallback).onAuth(secondCallback);
|
|
107
|
+
mockRequest(authRequest);
|
|
108
|
+
|
|
109
|
+
await authRequest.send({});
|
|
110
|
+
client.removeOnAuthInterceptors([secondCallback]);
|
|
111
|
+
|
|
112
|
+
await authRequest.send({});
|
|
113
|
+
|
|
114
|
+
expect(spy1).toHaveBeenCalledTimes(2);
|
|
115
|
+
expect(spy2).toHaveBeenCalledTimes(1);
|
|
116
|
+
});
|
|
117
|
+
});
|
|
118
|
+
});
|
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
import { createHttpMockingServer } from "@hyper-fetch/testing";
|
|
2
|
+
|
|
3
|
+
import { Client, interceptRequest, interceptResponse, stringifyDefaultOptions } from "client";
|
|
4
|
+
import { stringifyQueryParams } from "http-adapter";
|
|
5
|
+
|
|
6
|
+
const { resetMocks, startServer, stopServer } = createHttpMockingServer();
|
|
7
|
+
|
|
8
|
+
const options = {
|
|
9
|
+
...stringifyDefaultOptions,
|
|
10
|
+
encode: true,
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
describe("Client [ Utils ]", () => {
|
|
14
|
+
let client = new Client({ url: "shared-base-url" });
|
|
15
|
+
client.adapter.setQueryParamsMapperConfig(options);
|
|
16
|
+
let request = client.createRequest<{ payload: FormData; response: any }>()({ endpoint: "shared-nase-endpoint" });
|
|
17
|
+
|
|
18
|
+
beforeAll(() => {
|
|
19
|
+
startServer();
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
beforeEach(() => {
|
|
23
|
+
client = new Client({ url: "shared-base-url" });
|
|
24
|
+
client.adapter.setQueryParamsMapperConfig(options);
|
|
25
|
+
request = client.createRequest<{ response: any; payload: FormData }>()({ endpoint: "shared-nase-endpoint" });
|
|
26
|
+
resetMocks();
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
afterAll(() => {
|
|
30
|
+
stopServer();
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
describe("When using stringifyQueryParams util", () => {
|
|
34
|
+
it("should encode falsy values", async () => {
|
|
35
|
+
const newClient = new Client({ url: "shared-base-url" });
|
|
36
|
+
newClient.adapter.setQueryParamsMapperConfig({
|
|
37
|
+
encode: false,
|
|
38
|
+
arrayFormat: "bracket",
|
|
39
|
+
});
|
|
40
|
+
expect(
|
|
41
|
+
newClient.adapter.unstable_queryParamsMapper({ value: { test: new Date("9,9,2020").toISOString() } }),
|
|
42
|
+
).toBe(`?value={"test":"${new Date("9,9,2020").toISOString()}"}`);
|
|
43
|
+
expect(
|
|
44
|
+
newClient.adapter.unstable_queryParamsMapper({
|
|
45
|
+
page: 0,
|
|
46
|
+
status: ["status1", "status2"],
|
|
47
|
+
lastFreeDay: '{"from":"2023-06-20T10:37:27.508Z","to":"2023-06-24T10:37:27.508Z"}',
|
|
48
|
+
account: "test-account/123+323-123|111:;",
|
|
49
|
+
}),
|
|
50
|
+
).toBe(
|
|
51
|
+
'?page=0&status[]=status1&status[]=status2&lastFreeDay={"from":"2023-06-20T10:37:27.508Z","to":"2023-06-24T10:37:27.508Z"}&account=test-account/123+323-123|111:;',
|
|
52
|
+
);
|
|
53
|
+
});
|
|
54
|
+
it("should encode falsy values", async () => {
|
|
55
|
+
expect(stringifyQueryParams({ value: 0 }, options)).toBe("?value=0");
|
|
56
|
+
expect(stringifyQueryParams({ value: "" }, { ...options, skipEmptyString: false })).toBe("?value=");
|
|
57
|
+
expect(stringifyQueryParams({ value: null }, { ...options, skipNull: false })).toBe("?value=null");
|
|
58
|
+
expect(stringifyQueryParams({ value: "" }, { ...options, skipEmptyString: true })).toBe("");
|
|
59
|
+
expect(stringifyQueryParams({ value: undefined }, { ...options, skipEmptyString: true })).toBe("");
|
|
60
|
+
expect(stringifyQueryParams({ value: null }, { ...options, skipNull: true })).toBe("");
|
|
61
|
+
});
|
|
62
|
+
it("should encode date value to isoString by default", async () => {
|
|
63
|
+
expect(stringifyQueryParams({ value: new Date("2023-05-12T22:32:32") }, options)).toInclude(`?value=2023-05-12`);
|
|
64
|
+
});
|
|
65
|
+
it("should encode truthy values", async () => {
|
|
66
|
+
expect(stringifyQueryParams({ value: 10 }, options)).toBe("?value=10");
|
|
67
|
+
expect(stringifyQueryParams({ value: "test" }, options)).toBe("?value=test");
|
|
68
|
+
expect(stringifyQueryParams({ value: [1, 2, 3] }, options)).toBe("?value[]=1&value[]=2&value[]=3");
|
|
69
|
+
expect(stringifyQueryParams({ value: { data: "test" } }, options)).toBe("?value=%7B%22data%22%3A%22test%22%7D");
|
|
70
|
+
expect(
|
|
71
|
+
stringifyQueryParams(
|
|
72
|
+
{
|
|
73
|
+
value: {
|
|
74
|
+
filter: {
|
|
75
|
+
firstName: "john",
|
|
76
|
+
other: {
|
|
77
|
+
hobbies: ["dancing", "singing"],
|
|
78
|
+
},
|
|
79
|
+
},
|
|
80
|
+
},
|
|
81
|
+
},
|
|
82
|
+
options,
|
|
83
|
+
),
|
|
84
|
+
).toBe(
|
|
85
|
+
"?value=%7B%22filter%22%3A%7B%22firstName%22%3A%22john%22%2C%22other%22%3A%7B%22hobbies%22%3A%5B%22dancing%22%2C%22singing%22%5D%7D%7D%7D",
|
|
86
|
+
);
|
|
87
|
+
});
|
|
88
|
+
it("should encode not allowed characters values", async () => {
|
|
89
|
+
expect(stringifyQueryParams({ value: "[]" }, options)).toBe("?value=%5B%5D");
|
|
90
|
+
expect(stringifyQueryParams({ value: "/" }, options)).toBe("?value=%2F");
|
|
91
|
+
expect(stringifyQueryParams({ value: "," }, options)).toBe("?value=%2C");
|
|
92
|
+
expect(stringifyQueryParams({ value: "|" }, options)).toBe("?value=%7C");
|
|
93
|
+
expect(stringifyQueryParams({ value: ":" }, options)).toBe("?value=%3A");
|
|
94
|
+
expect(stringifyQueryParams({ value: ";" }, options)).toBe("?value=%3B");
|
|
95
|
+
expect(stringifyQueryParams({ value: "+" }, options)).toBe("?value=%2B");
|
|
96
|
+
expect(stringifyQueryParams({ value: "(" }, options)).toBe("?value=%28");
|
|
97
|
+
expect(stringifyQueryParams({ value: ")" }, options)).toBe("?value=%29");
|
|
98
|
+
expect(stringifyQueryParams({ value: "*" }, options)).toBe("?value=%2A");
|
|
99
|
+
expect(stringifyQueryParams({ value: "&" }, options)).toBe("?value=%26");
|
|
100
|
+
expect(stringifyQueryParams({ value: "^" }, options)).toBe("?value=%5E");
|
|
101
|
+
expect(stringifyQueryParams({ value: "%" }, options)).toBe("?value=%25");
|
|
102
|
+
expect(stringifyQueryParams({ value: "$" }, options)).toBe("?value=%24");
|
|
103
|
+
expect(stringifyQueryParams({ value: "#" }, options)).toBe("?value=%23");
|
|
104
|
+
expect(stringifyQueryParams({ value: "@" }, options)).toBe("?value=%40");
|
|
105
|
+
expect(stringifyQueryParams({ value: "!" }, options)).toBe("?value=%21");
|
|
106
|
+
});
|
|
107
|
+
it("should encode array values with different formats", async () => {
|
|
108
|
+
expect(
|
|
109
|
+
stringifyQueryParams({ value: [1, 2, 3] }, { ...options, arraySeparator: "", arrayFormat: "bracket" }),
|
|
110
|
+
).toBe("?value[]=1&value[]=2&value[]=3");
|
|
111
|
+
expect(
|
|
112
|
+
stringifyQueryParams(
|
|
113
|
+
{ value: [1, 2, 3] },
|
|
114
|
+
{ ...options, arraySeparator: "", arrayFormat: "bracket-separator" },
|
|
115
|
+
),
|
|
116
|
+
).toBe("?value[]=1|2|3");
|
|
117
|
+
expect(stringifyQueryParams({ value: [1, 2, 3] }, { ...options, arraySeparator: "", arrayFormat: "comma" })).toBe(
|
|
118
|
+
"?value=1,2,3",
|
|
119
|
+
);
|
|
120
|
+
expect(stringifyQueryParams({ value: [1, 2, 3] }, { ...options, arraySeparator: "", arrayFormat: "index" })).toBe(
|
|
121
|
+
"?value[0]=1&value[1]=2&value[2]=3",
|
|
122
|
+
);
|
|
123
|
+
expect(stringifyQueryParams({ value: [1, 2, 3] }, { ...options, arraySeparator: "", arrayFormat: "none" })).toBe(
|
|
124
|
+
"?value=1&value=2&value=3",
|
|
125
|
+
);
|
|
126
|
+
expect(
|
|
127
|
+
stringifyQueryParams({ value: [1, 2, 3] }, { ...options, arraySeparator: "", arrayFormat: "separator" }),
|
|
128
|
+
).toBe("?value=1|2|3");
|
|
129
|
+
expect(
|
|
130
|
+
stringifyQueryParams({ value: [1, 2, 3] }, { ...options, arrayFormat: "separator", arraySeparator: "#" }),
|
|
131
|
+
).toBe("?value=1#2#3");
|
|
132
|
+
});
|
|
133
|
+
it("should encode values without strict option", async () => {
|
|
134
|
+
expect(stringifyQueryParams({ value: "![]" }, { ...options, encode: true, strict: false })).toBe(
|
|
135
|
+
"?value=!%5B%5D",
|
|
136
|
+
);
|
|
137
|
+
expect(stringifyQueryParams({ value: "![]" }, { ...options, encode: false, strict: false })).toBe("?value=![]");
|
|
138
|
+
expect(stringifyQueryParams({ value: "![]" }, { ...options, encode: false, strict: true })).toBe("?value=![]");
|
|
139
|
+
});
|
|
140
|
+
it("should add the question mark", async () => {
|
|
141
|
+
expect(stringifyQueryParams("something", options)).toBe("?something");
|
|
142
|
+
expect(stringifyQueryParams("?something", options)).toBe("?something");
|
|
143
|
+
});
|
|
144
|
+
it("should return empty string when no query params are passed", async () => {
|
|
145
|
+
expect(stringifyQueryParams("", options)).toBe("");
|
|
146
|
+
expect(stringifyQueryParams({}, options)).toBe("");
|
|
147
|
+
expect(stringifyQueryParams(null, options)).toBe("");
|
|
148
|
+
expect(stringifyQueryParams(undefined, options)).toBe("");
|
|
149
|
+
});
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
describe("When using headerMapper", () => {
|
|
153
|
+
it("should assign default headers with headers mapper", async () => {
|
|
154
|
+
const defaultHeaders = {};
|
|
155
|
+
const headers = client.adapter.unstable_headerMapper(request, undefined);
|
|
156
|
+
|
|
157
|
+
expect(headers).toEqual(defaultHeaders);
|
|
158
|
+
});
|
|
159
|
+
it("should assign form data headers", async () => {
|
|
160
|
+
const defaultHeaders = {};
|
|
161
|
+
const data = new FormData();
|
|
162
|
+
const formDataRequest = request.setPayload(data);
|
|
163
|
+
const headers = client.adapter.unstable_headerMapper(formDataRequest, undefined);
|
|
164
|
+
|
|
165
|
+
expect(headers).toEqual(defaultHeaders);
|
|
166
|
+
});
|
|
167
|
+
it("should not re-assign form data headers", async () => {
|
|
168
|
+
const defaultHeaders = { "Content-Type": "some-custom-format" };
|
|
169
|
+
const formDataRequest = request.setHeaders(defaultHeaders);
|
|
170
|
+
const headers = client.adapter.unstable_headerMapper(formDataRequest, undefined);
|
|
171
|
+
|
|
172
|
+
expect(headers).toEqual(defaultHeaders);
|
|
173
|
+
});
|
|
174
|
+
it("should add content type header for object payload", async () => {
|
|
175
|
+
const defaultHeaders = { "Content-Type": "application/json" };
|
|
176
|
+
const data = { data: [] } as any;
|
|
177
|
+
const formDataRequest = request.setPayload(data);
|
|
178
|
+
const headers = client.adapter.unstable_headerMapper(formDataRequest, undefined);
|
|
179
|
+
|
|
180
|
+
expect(headers).toEqual(defaultHeaders);
|
|
181
|
+
});
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
describe("When using payloadMapper", () => {
|
|
185
|
+
it("should allow to stringify payload", async () => {
|
|
186
|
+
const data = { data: [] };
|
|
187
|
+
const payload = client.adapter.unstable_payloadMapper(data, undefined);
|
|
188
|
+
|
|
189
|
+
expect(payload).toEqual(JSON.stringify(data));
|
|
190
|
+
});
|
|
191
|
+
it("should not stringify FormData payload", async () => {
|
|
192
|
+
const data = new FormData();
|
|
193
|
+
const payload = client.adapter.unstable_payloadMapper(data, undefined);
|
|
194
|
+
|
|
195
|
+
expect(payload).toEqual(data);
|
|
196
|
+
});
|
|
197
|
+
it("should stringify null payload", async () => {
|
|
198
|
+
const data = null;
|
|
199
|
+
const payload = client.adapter.unstable_payloadMapper(data, undefined);
|
|
200
|
+
|
|
201
|
+
expect(payload).toEqual(JSON.stringify(data));
|
|
202
|
+
});
|
|
203
|
+
it("should not stringify undefined payload", async () => {
|
|
204
|
+
const payload = client.adapter.unstable_payloadMapper(undefined, undefined);
|
|
205
|
+
|
|
206
|
+
expect(payload).toBeUndefined();
|
|
207
|
+
});
|
|
208
|
+
it("should not stringify invalid payload", async () => {
|
|
209
|
+
const payload = client.adapter.unstable_payloadMapper(() => null, undefined);
|
|
210
|
+
expect(payload).toBeUndefined();
|
|
211
|
+
});
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
describe("When using stringifyValue util", () => {
|
|
215
|
+
it("should allow to stringify payload", async () => {
|
|
216
|
+
const data = { data: [] };
|
|
217
|
+
const payload = client.adapter.unstable_payloadMapper(data, undefined);
|
|
218
|
+
|
|
219
|
+
expect(payload).toEqual(JSON.stringify(data));
|
|
220
|
+
});
|
|
221
|
+
it("should not stringify FormData payload", async () => {
|
|
222
|
+
const data = new FormData();
|
|
223
|
+
const payload = client.adapter.unstable_payloadMapper(data, undefined);
|
|
224
|
+
|
|
225
|
+
expect(payload).toEqual(data);
|
|
226
|
+
});
|
|
227
|
+
it("should stringify null payload", async () => {
|
|
228
|
+
const data = null;
|
|
229
|
+
const payload = client.adapter.unstable_payloadMapper(data, undefined);
|
|
230
|
+
|
|
231
|
+
expect(payload).toEqual(JSON.stringify(data));
|
|
232
|
+
});
|
|
233
|
+
it("should not stringify undefined payload", async () => {
|
|
234
|
+
const payload = client.adapter.unstable_payloadMapper(undefined, undefined);
|
|
235
|
+
|
|
236
|
+
expect(payload).toBeUndefined();
|
|
237
|
+
});
|
|
238
|
+
it("should not stringify invalid payload", async () => {
|
|
239
|
+
const data: Record<string, unknown> = {};
|
|
240
|
+
data.a = { b: data };
|
|
241
|
+
|
|
242
|
+
const payload = client.adapter.unstable_payloadMapper(data, undefined);
|
|
243
|
+
expect(payload).toBe("");
|
|
244
|
+
});
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
describe("when interceptors are disabled", () => {
|
|
248
|
+
it("should skip request interceptors when disabled", async () => {
|
|
249
|
+
const mockInterceptor = jest.fn((req) => req);
|
|
250
|
+
const req = client.createRequest<{ response: any }>()({
|
|
251
|
+
endpoint: "shared-base-endpoint",
|
|
252
|
+
disableRequestInterceptors: true,
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
const result = await interceptRequest([mockInterceptor], req);
|
|
256
|
+
|
|
257
|
+
expect(mockInterceptor).not.toHaveBeenCalled();
|
|
258
|
+
expect(result).toBe(req);
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
it("should skip response interceptors when disabled", async () => {
|
|
262
|
+
const mockInterceptor = jest.fn((res) => res);
|
|
263
|
+
const response = {
|
|
264
|
+
data: { test: "data" },
|
|
265
|
+
status: 200,
|
|
266
|
+
success: true,
|
|
267
|
+
} as any;
|
|
268
|
+
const req = client.createRequest<{ response: any }>()({
|
|
269
|
+
endpoint: "shared-base-endpoint",
|
|
270
|
+
disableResponseInterceptors: true,
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
const result = await interceptResponse([mockInterceptor], response, req);
|
|
274
|
+
|
|
275
|
+
expect(mockInterceptor).not.toHaveBeenCalled();
|
|
276
|
+
expect(result).toBe(response);
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
it("should run interceptors when not disabled", async () => {
|
|
280
|
+
const mockInterceptor = jest.fn((req) => req);
|
|
281
|
+
|
|
282
|
+
const req = client.createRequest<{ response: any }>()({
|
|
283
|
+
endpoint: "shared-base-endpoint",
|
|
284
|
+
disableRequestInterceptors: false,
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
await interceptRequest([mockInterceptor], req);
|
|
288
|
+
|
|
289
|
+
expect(mockInterceptor).toHaveBeenCalledWith(req);
|
|
290
|
+
});
|
|
291
|
+
});
|
|
292
|
+
});
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { createHttpMockingServer } from "@hyper-fetch/testing";
|
|
2
|
+
|
|
3
|
+
import { QueueDataType } from "dispatcher";
|
|
4
|
+
import { createDispatcher } from "../../utils";
|
|
5
|
+
import { Client } from "client";
|
|
6
|
+
import { Plugin } from "plugin";
|
|
7
|
+
|
|
8
|
+
const { resetMocks, startServer, stopServer } = createHttpMockingServer();
|
|
9
|
+
|
|
10
|
+
describe("Dispatcher [ Basic ]", () => {
|
|
11
|
+
let client = new Client({ url: "shared-base-url" });
|
|
12
|
+
let request = client.createRequest()({ endpoint: "shared-endpoint" });
|
|
13
|
+
|
|
14
|
+
beforeAll(() => {
|
|
15
|
+
startServer();
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
beforeEach(() => {
|
|
19
|
+
client = new Client({ url: "shared-base-url" });
|
|
20
|
+
request = client.createRequest()({ endpoint: "shared-endpoint" });
|
|
21
|
+
resetMocks();
|
|
22
|
+
jest.resetAllMocks();
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
afterAll(() => {
|
|
26
|
+
stopServer();
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
describe("When lifecycle events get triggered", () => {
|
|
30
|
+
it("should allow to change storage", async () => {
|
|
31
|
+
const storage = new Map<string, QueueDataType<any>>();
|
|
32
|
+
const newDispatcher = createDispatcher(client, { storage });
|
|
33
|
+
|
|
34
|
+
const dispatcherDump = newDispatcher.createStorageItem(request);
|
|
35
|
+
newDispatcher.addQueueItem(request.queryKey, dispatcherDump);
|
|
36
|
+
|
|
37
|
+
expect(storage.get(request.queryKey)?.requests[0]).toBe(dispatcherDump);
|
|
38
|
+
});
|
|
39
|
+
it("should trigger on queue change callback", async () => {
|
|
40
|
+
const spy = jest.fn();
|
|
41
|
+
const plugin = new Plugin({ name: "change" }).onDispatcherQueueRunning(spy);
|
|
42
|
+
const newDispatcher = createDispatcher(client.addPlugin(plugin));
|
|
43
|
+
|
|
44
|
+
newDispatcher.stop(request.queryKey);
|
|
45
|
+
|
|
46
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
47
|
+
expect(spy).toHaveBeenCalledWith({
|
|
48
|
+
dispatcher: newDispatcher,
|
|
49
|
+
queue: { queryKey: request.queryKey, requests: [], stopped: true },
|
|
50
|
+
status: "stopped",
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
it("should trigger on add and delete callback", async () => {
|
|
54
|
+
const spy1 = jest.fn();
|
|
55
|
+
const spy2 = jest.fn();
|
|
56
|
+
const spy3 = jest.fn();
|
|
57
|
+
const plugin1 = new Plugin({ name: "add" }).onDispatcherItemAdded(spy1);
|
|
58
|
+
const plugin2 = new Plugin({ name: "delete" }).onDispatcherItemDeleted(spy2);
|
|
59
|
+
const plugin3 = new Plugin({ name: "clear" }).onDispatcherQueueCleared(spy3);
|
|
60
|
+
const newDispatcher = createDispatcher(client.addPlugin(plugin1).addPlugin(plugin2).addPlugin(plugin3));
|
|
61
|
+
const dispatcherDump = newDispatcher.createStorageItem(request);
|
|
62
|
+
|
|
63
|
+
newDispatcher.addQueueItem(request.queryKey, dispatcherDump);
|
|
64
|
+
expect(spy1).toHaveBeenCalledTimes(1);
|
|
65
|
+
expect(spy1).toHaveBeenCalledWith({
|
|
66
|
+
dispatcher: newDispatcher,
|
|
67
|
+
queue: { queryKey: request.queryKey, requests: [dispatcherDump], stopped: false },
|
|
68
|
+
queueItem: dispatcherDump,
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
newDispatcher.delete(request.queryKey, dispatcherDump.requestId, request.abortKey);
|
|
72
|
+
expect(spy2).toHaveBeenCalledTimes(1);
|
|
73
|
+
expect(spy2).toHaveBeenCalledWith({
|
|
74
|
+
dispatcher: newDispatcher,
|
|
75
|
+
queue: { queryKey: request.queryKey, requests: [], stopped: false },
|
|
76
|
+
queueItem: dispatcherDump,
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
newDispatcher.addQueueItem(request.queryKey, dispatcherDump);
|
|
80
|
+
|
|
81
|
+
expect(spy1).toHaveBeenCalledTimes(2);
|
|
82
|
+
expect(spy1).toHaveBeenCalledWith({
|
|
83
|
+
dispatcher: newDispatcher,
|
|
84
|
+
queue: { queryKey: request.queryKey, requests: [dispatcherDump], stopped: false },
|
|
85
|
+
queueItem: dispatcherDump,
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
newDispatcher.clearQueue(request.queryKey);
|
|
89
|
+
|
|
90
|
+
expect(spy3).toHaveBeenCalledTimes(1);
|
|
91
|
+
expect(spy3).toHaveBeenCalledWith({
|
|
92
|
+
dispatcher: newDispatcher,
|
|
93
|
+
queue: { queryKey: request.queryKey, requests: [], stopped: false },
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
it("should trigger onClearStorage callback", async () => {
|
|
97
|
+
const spy = jest.fn();
|
|
98
|
+
const plugin = new Plugin({ name: "clear" }).onDispatcherCleared(spy);
|
|
99
|
+
const newDispatcher = createDispatcher(client.addPlugin(plugin));
|
|
100
|
+
|
|
101
|
+
newDispatcher.clear();
|
|
102
|
+
|
|
103
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
104
|
+
expect(spy).toHaveBeenCalledWith({ dispatcher: newDispatcher });
|
|
105
|
+
});
|
|
106
|
+
it("should cancel running requests when clearing queues", async () => {
|
|
107
|
+
const spy = jest.spyOn(client.fetchDispatcher, "cancelRunningRequests");
|
|
108
|
+
|
|
109
|
+
// Create and add two requests to simulate multiple running requests
|
|
110
|
+
const dispatcherDump1 = client.fetchDispatcher.createStorageItem(request);
|
|
111
|
+
const dispatcherDump2 = client.fetchDispatcher.createStorageItem(request);
|
|
112
|
+
|
|
113
|
+
client.fetchDispatcher.addQueueItem(request.queryKey, dispatcherDump1);
|
|
114
|
+
client.fetchDispatcher.addQueueItem(`${request.queryKey}2`, dispatcherDump2);
|
|
115
|
+
|
|
116
|
+
// Clear all queues which should trigger cancellation
|
|
117
|
+
client.fetchDispatcher.clear();
|
|
118
|
+
|
|
119
|
+
// Verify that cancelRunningRequests was called for each request
|
|
120
|
+
expect(spy).toHaveBeenCalledTimes(2);
|
|
121
|
+
});
|
|
122
|
+
});
|
|
123
|
+
});
|