@hyper-fetch/core 7.5.1 → 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/browser/index.js +2 -1
- package/dist/browser/index.js.map +2 -2
- package/dist/index.js +14 -2
- 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/adapter/adapter.utils.ts +2 -1
- package/src/http-adapter/http-adapter.server.ts +15 -1
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import { Client } from "client";
|
|
2
|
+
|
|
3
|
+
describe("Client [ Hydration ]", () => {
|
|
4
|
+
let client = new Client({ url: "shared-base-url" });
|
|
5
|
+
let request = client.createRequest<{ response: any }>()({ endpoint: "shared-base-endpoint" });
|
|
6
|
+
|
|
7
|
+
beforeEach(() => {
|
|
8
|
+
client = new Client({ url: "shared-base-url" });
|
|
9
|
+
request = client.createRequest<{ response: any }>()({ endpoint: "shared-base-endpoint" });
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
describe("When using hydrate", () => {
|
|
13
|
+
it("should not dehydrate from empty cache", async () => {
|
|
14
|
+
const dehydratedResponse = request.dehydrate();
|
|
15
|
+
|
|
16
|
+
expect(dehydratedResponse).toBeUndefined();
|
|
17
|
+
});
|
|
18
|
+
it("should dehydrate from cache", async () => {
|
|
19
|
+
const mockedRequest = request.setMock(() => ({ data: "test", status: 200 }));
|
|
20
|
+
const response = await mockedRequest.send();
|
|
21
|
+
|
|
22
|
+
const dehydratedResponse = mockedRequest.dehydrate();
|
|
23
|
+
|
|
24
|
+
expect(dehydratedResponse).toStrictEqual(expect.objectContaining({ cacheKey: request.cacheKey, response }));
|
|
25
|
+
|
|
26
|
+
const newClient = new Client({ url: "shared-base-url" });
|
|
27
|
+
|
|
28
|
+
expect(newClient.cache.get(mockedRequest.cacheKey)).not.toBeDefined();
|
|
29
|
+
|
|
30
|
+
newClient.hydrate([dehydratedResponse]);
|
|
31
|
+
|
|
32
|
+
expect(newClient.cache.get(mockedRequest.cacheKey)).toStrictEqual(
|
|
33
|
+
expect.objectContaining({ ...response, cacheKey: request.cacheKey, staleTime: request.staleTime }),
|
|
34
|
+
);
|
|
35
|
+
});
|
|
36
|
+
it("should dehydrate from response", async () => {
|
|
37
|
+
const mockedRequest = request.setMock(() => ({ data: "test", status: 200 }));
|
|
38
|
+
const response = await mockedRequest.send();
|
|
39
|
+
|
|
40
|
+
const dehydratedResponse = mockedRequest.dehydrate({ response });
|
|
41
|
+
|
|
42
|
+
expect(dehydratedResponse).toStrictEqual(expect.objectContaining({ cacheKey: request.cacheKey, response }));
|
|
43
|
+
|
|
44
|
+
const newClient = new Client({ url: "shared-base-url" });
|
|
45
|
+
|
|
46
|
+
expect(newClient.cache.get(mockedRequest.cacheKey)).not.toBeDefined();
|
|
47
|
+
|
|
48
|
+
newClient.hydrate([dehydratedResponse]);
|
|
49
|
+
|
|
50
|
+
expect(newClient.cache.get(mockedRequest.cacheKey)).toStrictEqual(
|
|
51
|
+
expect.objectContaining({ ...response, cacheKey: request.cacheKey, staleTime: request.staleTime }),
|
|
52
|
+
);
|
|
53
|
+
});
|
|
54
|
+
it("should not override cache data", async () => {
|
|
55
|
+
const mockedRequest = request.setMock(() => ({ data: "test", status: 200 }));
|
|
56
|
+
const response = await mockedRequest.send();
|
|
57
|
+
|
|
58
|
+
const dehydratedResponse = mockedRequest.dehydrate({ response, override: false });
|
|
59
|
+
|
|
60
|
+
expect(dehydratedResponse).toStrictEqual(expect.objectContaining({ cacheKey: request.cacheKey, response }));
|
|
61
|
+
|
|
62
|
+
const newClient = new Client({ url: "shared-base-url" });
|
|
63
|
+
const cacheData: CacheValueType = {
|
|
64
|
+
data: "123456789",
|
|
65
|
+
status: 200,
|
|
66
|
+
error: null,
|
|
67
|
+
success: true,
|
|
68
|
+
extra: {},
|
|
69
|
+
responseTimestamp: +new Date(),
|
|
70
|
+
requestTimestamp: +new Date(),
|
|
71
|
+
addedTimestamp: +new Date(),
|
|
72
|
+
triggerTimestamp: +new Date(),
|
|
73
|
+
retries: 0,
|
|
74
|
+
isCanceled: false,
|
|
75
|
+
isOffline: false,
|
|
76
|
+
hydrated: false,
|
|
77
|
+
cached: true,
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
newClient.cache.set(mockedRequest, cacheData);
|
|
81
|
+
|
|
82
|
+
expect(newClient.cache.get(mockedRequest.cacheKey)).toStrictEqual({
|
|
83
|
+
...cacheData,
|
|
84
|
+
cacheKey: mockedRequest.cacheKey,
|
|
85
|
+
cacheTime: mockedRequest.cacheTime,
|
|
86
|
+
staleTime: mockedRequest.staleTime,
|
|
87
|
+
version: newClient.cache.version,
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
newClient.hydrate([dehydratedResponse]);
|
|
91
|
+
|
|
92
|
+
expect(newClient.cache.get(mockedRequest.cacheKey)).toStrictEqual({
|
|
93
|
+
...cacheData,
|
|
94
|
+
cacheKey: mockedRequest.cacheKey,
|
|
95
|
+
cacheTime: mockedRequest.cacheTime,
|
|
96
|
+
staleTime: mockedRequest.staleTime,
|
|
97
|
+
version: newClient.cache.version,
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
it("should handle null/undefined items in hydration data", async () => {
|
|
101
|
+
const mockedRequest = request.setMock(() => ({ data: "test", status: 200 }));
|
|
102
|
+
await mockedRequest.send();
|
|
103
|
+
const dehydratedResponse = mockedRequest.dehydrate();
|
|
104
|
+
|
|
105
|
+
const newClient = new Client({ url: "shared-base-url" });
|
|
106
|
+
// Should not throw when processing null/undefined items
|
|
107
|
+
newClient.hydrate([dehydratedResponse, null, undefined]);
|
|
108
|
+
|
|
109
|
+
expect(newClient.cache.get(mockedRequest.cacheKey)).toBeDefined();
|
|
110
|
+
});
|
|
111
|
+
it("should apply function-based options", async () => {
|
|
112
|
+
const mockedRequest = request.setMock(() => ({ data: "test", status: 200 }));
|
|
113
|
+
await mockedRequest.send();
|
|
114
|
+
const dehydratedResponse = mockedRequest.dehydrate();
|
|
115
|
+
|
|
116
|
+
const newClient = new Client({ url: "shared-base-url" });
|
|
117
|
+
const optionsFn = jest.fn().mockReturnValue({ cache: false });
|
|
118
|
+
|
|
119
|
+
newClient.hydrate([dehydratedResponse], optionsFn);
|
|
120
|
+
|
|
121
|
+
expect(optionsFn).toHaveBeenCalledWith(dehydratedResponse);
|
|
122
|
+
// Since cache is false, data should not be in cache
|
|
123
|
+
expect(newClient.cache.get(mockedRequest.cacheKey)).toBeUndefined();
|
|
124
|
+
});
|
|
125
|
+
it("should merge options correctly", async () => {
|
|
126
|
+
const mockedRequest = request.setMock(() => ({ data: "test", status: 200 }));
|
|
127
|
+
const response = await mockedRequest.send();
|
|
128
|
+
// Test with fallback options in dehydrated response
|
|
129
|
+
const dehydratedResponse = mockedRequest.dehydrate({
|
|
130
|
+
response,
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
const newClient = new Client({ url: "shared-base-url" });
|
|
134
|
+
|
|
135
|
+
// Add global options
|
|
136
|
+
newClient.hydrate([dehydratedResponse], {
|
|
137
|
+
staleTime: 3000,
|
|
138
|
+
cacheTime: 2000,
|
|
139
|
+
override: true,
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
const cachedData = newClient.cache.get(mockedRequest.cacheKey);
|
|
143
|
+
expect(cachedData).toBeDefined();
|
|
144
|
+
expect(cachedData?.staleTime).toBe(3000);
|
|
145
|
+
expect(cachedData?.cacheTime).toBe(2000);
|
|
146
|
+
});
|
|
147
|
+
it("should respect cache option", async () => {
|
|
148
|
+
const mockedRequest = request.setMock(() => ({ data: "test", status: 200 }));
|
|
149
|
+
await mockedRequest.send();
|
|
150
|
+
const dehydratedResponse = mockedRequest.dehydrate();
|
|
151
|
+
|
|
152
|
+
const newClient = new Client({ url: "shared-base-url" });
|
|
153
|
+
|
|
154
|
+
newClient.hydrate([dehydratedResponse], { cache: false });
|
|
155
|
+
|
|
156
|
+
expect(newClient.cache.get(mockedRequest.cacheKey)).toBeUndefined();
|
|
157
|
+
});
|
|
158
|
+
it("should override if override is false and cache is not set", async () => {
|
|
159
|
+
const mockedRequest = request.setMock(() => ({ data: "test", status: 200 }));
|
|
160
|
+
await mockedRequest.send();
|
|
161
|
+
const dehydratedResponse = mockedRequest.dehydrate();
|
|
162
|
+
|
|
163
|
+
client.cache.clear();
|
|
164
|
+
|
|
165
|
+
const spy = jest.spyOn(client.cache, "set");
|
|
166
|
+
client.hydrate([dehydratedResponse], { override: false });
|
|
167
|
+
|
|
168
|
+
expect(spy).toHaveBeenCalled();
|
|
169
|
+
});
|
|
170
|
+
});
|
|
171
|
+
});
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
import { createHttpMockingServer } from "@hyper-fetch/testing";
|
|
2
|
+
|
|
3
|
+
import { interceptorCallback } from "../../utils";
|
|
4
|
+
import { testCallbacksExecution } from "../../shared";
|
|
5
|
+
import { Client } from "client";
|
|
6
|
+
import { xhrExtra } from "http-adapter";
|
|
7
|
+
|
|
8
|
+
const { resetMocks, startServer, stopServer, mockRequest } = createHttpMockingServer();
|
|
9
|
+
|
|
10
|
+
describe("Client [ Interceptor ]", () => {
|
|
11
|
+
let client = new Client({ url: "shared-base-url/" });
|
|
12
|
+
let request = client.createRequest<{ response: any }>()({ endpoint: "shared-base-endpoint" });
|
|
13
|
+
|
|
14
|
+
const spy1 = jest.fn();
|
|
15
|
+
const spy2 = jest.fn();
|
|
16
|
+
const spy3 = jest.fn();
|
|
17
|
+
|
|
18
|
+
beforeAll(() => {
|
|
19
|
+
startServer();
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
beforeEach(() => {
|
|
23
|
+
client = new Client({ url: "shared-base-url/" });
|
|
24
|
+
request = client.createRequest<{ response: any }>()({ endpoint: "shared-base-endpoint" });
|
|
25
|
+
resetMocks();
|
|
26
|
+
jest.clearAllMocks();
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
afterAll(() => {
|
|
30
|
+
stopServer();
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
describe("When interceptor callbacks being added", () => {
|
|
34
|
+
it("should assign onError interceptors", async () => {
|
|
35
|
+
const callback = interceptorCallback();
|
|
36
|
+
client.onError(callback).onError(callback);
|
|
37
|
+
|
|
38
|
+
expect(client.unstable_onErrorCallbacks).toHaveLength(2);
|
|
39
|
+
expect(client.unstable_onErrorCallbacks[0]).toEqual(callback);
|
|
40
|
+
expect(client.unstable_onErrorCallbacks[1]).toEqual(callback);
|
|
41
|
+
});
|
|
42
|
+
it("should assign onSuccess interceptors", async () => {
|
|
43
|
+
const callback = interceptorCallback();
|
|
44
|
+
client.onSuccess(callback).onSuccess(callback);
|
|
45
|
+
|
|
46
|
+
expect(client.unstable_onSuccessCallbacks).toHaveLength(2);
|
|
47
|
+
expect(client.unstable_onSuccessCallbacks[0]).toEqual(callback);
|
|
48
|
+
expect(client.unstable_onSuccessCallbacks[1]).toEqual(callback);
|
|
49
|
+
});
|
|
50
|
+
it("should assign onResponse interceptors", async () => {
|
|
51
|
+
const callback = interceptorCallback();
|
|
52
|
+
client.onResponse(callback).onResponse(callback);
|
|
53
|
+
|
|
54
|
+
expect(client.unstable_onResponseCallbacks).toHaveLength(2);
|
|
55
|
+
expect(client.unstable_onResponseCallbacks[0]).toEqual(callback);
|
|
56
|
+
expect(client.unstable_onResponseCallbacks[1]).toEqual(callback);
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
describe("When interceptor callbacks go into the execution loop", () => {
|
|
61
|
+
it("should trigger unstable_modifyErrorResponse async loop", async () => {
|
|
62
|
+
const callbackAsync = interceptorCallback({ callback: spy1, sleepTime: 20 });
|
|
63
|
+
const callbackSync = interceptorCallback({ callback: spy2 });
|
|
64
|
+
const callbackLast = interceptorCallback({ callback: spy3, sleepTime: 10 });
|
|
65
|
+
|
|
66
|
+
client.onError(callbackAsync).onError(callbackSync).onError(callbackLast);
|
|
67
|
+
await client.unstable_modifyErrorResponse(
|
|
68
|
+
{
|
|
69
|
+
data: null,
|
|
70
|
+
error: null,
|
|
71
|
+
status: 400,
|
|
72
|
+
success: false,
|
|
73
|
+
extra: xhrExtra,
|
|
74
|
+
responseTimestamp: Date.now(),
|
|
75
|
+
requestTimestamp: Date.now(),
|
|
76
|
+
},
|
|
77
|
+
request,
|
|
78
|
+
);
|
|
79
|
+
|
|
80
|
+
testCallbacksExecution([spy1, spy2, spy3]);
|
|
81
|
+
});
|
|
82
|
+
it("should trigger __modifySuccessResponse async loop", async () => {
|
|
83
|
+
const callbackAsync = interceptorCallback({ callback: spy1, sleepTime: 20 });
|
|
84
|
+
const callbackSync = interceptorCallback({ callback: spy2 });
|
|
85
|
+
const callbackLast = interceptorCallback({ callback: spy3, sleepTime: 10 });
|
|
86
|
+
|
|
87
|
+
client.onSuccess(callbackAsync).onSuccess(callbackSync).onSuccess(callbackLast);
|
|
88
|
+
await client.unstable_modifySuccessResponse(
|
|
89
|
+
{
|
|
90
|
+
data: null,
|
|
91
|
+
error: null,
|
|
92
|
+
status: 400,
|
|
93
|
+
success: false,
|
|
94
|
+
extra: xhrExtra,
|
|
95
|
+
responseTimestamp: Date.now(),
|
|
96
|
+
requestTimestamp: Date.now(),
|
|
97
|
+
},
|
|
98
|
+
request,
|
|
99
|
+
);
|
|
100
|
+
|
|
101
|
+
testCallbacksExecution([spy1, spy2, spy3]);
|
|
102
|
+
});
|
|
103
|
+
it("should trigger __modifyResponse async loop", async () => {
|
|
104
|
+
const callbackAsync = interceptorCallback({ callback: spy1, sleepTime: 20 });
|
|
105
|
+
const callbackSync = interceptorCallback({ callback: spy2 });
|
|
106
|
+
const callbackLast = interceptorCallback({ callback: spy3, sleepTime: 10 });
|
|
107
|
+
|
|
108
|
+
client.onResponse(callbackAsync).onResponse(callbackSync).onResponse(callbackLast);
|
|
109
|
+
await client.unstable_modifyResponse(
|
|
110
|
+
{
|
|
111
|
+
data: null,
|
|
112
|
+
error: null,
|
|
113
|
+
status: 400,
|
|
114
|
+
success: false,
|
|
115
|
+
extra: xhrExtra,
|
|
116
|
+
requestTimestamp: Date.now(),
|
|
117
|
+
responseTimestamp: Date.now(),
|
|
118
|
+
},
|
|
119
|
+
request,
|
|
120
|
+
);
|
|
121
|
+
|
|
122
|
+
testCallbacksExecution([spy1, spy2, spy3]);
|
|
123
|
+
});
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
describe("When interceptor returns undefined value", () => {
|
|
127
|
+
it("should throw onError method when request is not returned", async () => {
|
|
128
|
+
client.onError(() => undefined as any);
|
|
129
|
+
|
|
130
|
+
await expect(
|
|
131
|
+
client.unstable_modifyErrorResponse(
|
|
132
|
+
{
|
|
133
|
+
data: null,
|
|
134
|
+
error: null,
|
|
135
|
+
status: 400,
|
|
136
|
+
success: false,
|
|
137
|
+
extra: xhrExtra,
|
|
138
|
+
requestTimestamp: Date.now(),
|
|
139
|
+
responseTimestamp: Date.now(),
|
|
140
|
+
},
|
|
141
|
+
request,
|
|
142
|
+
),
|
|
143
|
+
).rejects.toThrow();
|
|
144
|
+
});
|
|
145
|
+
it("should throw onSuccess method when request is not returned", async () => {
|
|
146
|
+
client.onSuccess(() => undefined as any);
|
|
147
|
+
|
|
148
|
+
await expect(
|
|
149
|
+
client.unstable_modifySuccessResponse(
|
|
150
|
+
{
|
|
151
|
+
data: null,
|
|
152
|
+
error: null,
|
|
153
|
+
status: 400,
|
|
154
|
+
success: false,
|
|
155
|
+
extra: xhrExtra,
|
|
156
|
+
requestTimestamp: Date.now(),
|
|
157
|
+
responseTimestamp: Date.now(),
|
|
158
|
+
},
|
|
159
|
+
request,
|
|
160
|
+
),
|
|
161
|
+
).rejects.toThrow();
|
|
162
|
+
});
|
|
163
|
+
it("should throw onResponse method when request is not returned", async () => {
|
|
164
|
+
client.onResponse(() => undefined as any);
|
|
165
|
+
|
|
166
|
+
await expect(
|
|
167
|
+
client.unstable_modifyResponse(
|
|
168
|
+
{
|
|
169
|
+
data: null,
|
|
170
|
+
error: null,
|
|
171
|
+
status: 400,
|
|
172
|
+
success: false,
|
|
173
|
+
extra: xhrExtra,
|
|
174
|
+
requestTimestamp: Date.now(),
|
|
175
|
+
responseTimestamp: Date.now(),
|
|
176
|
+
},
|
|
177
|
+
request,
|
|
178
|
+
),
|
|
179
|
+
).rejects.toThrow();
|
|
180
|
+
});
|
|
181
|
+
});
|
|
182
|
+
describe("When user wants to remove listeners", () => {
|
|
183
|
+
it("should allow for removing interceptors on error", async () => {
|
|
184
|
+
const firstCallback = interceptorCallback({ callback: spy1 });
|
|
185
|
+
const secondCallback = interceptorCallback({ callback: spy2 });
|
|
186
|
+
client.onError(firstCallback).onError(secondCallback);
|
|
187
|
+
mockRequest(request, { status: 400 });
|
|
188
|
+
|
|
189
|
+
await request.send({});
|
|
190
|
+
client.removeOnErrorInterceptors([secondCallback]);
|
|
191
|
+
await request.send({});
|
|
192
|
+
|
|
193
|
+
expect(spy1).toHaveBeenCalledTimes(2);
|
|
194
|
+
expect(spy2).toHaveBeenCalledTimes(1);
|
|
195
|
+
});
|
|
196
|
+
it("should allow for removing interceptors on success", async () => {
|
|
197
|
+
mockRequest(request, { data: { data: [1, 2, 3] } });
|
|
198
|
+
const firstCallback = interceptorCallback({ callback: spy1 });
|
|
199
|
+
const secondCallback = interceptorCallback({ callback: spy2 });
|
|
200
|
+
client.onSuccess(firstCallback).onSuccess(secondCallback);
|
|
201
|
+
|
|
202
|
+
await request.send({});
|
|
203
|
+
client.removeOnSuccessInterceptors([secondCallback]);
|
|
204
|
+
|
|
205
|
+
await request.send({});
|
|
206
|
+
|
|
207
|
+
expect(spy1).toHaveBeenCalledTimes(2);
|
|
208
|
+
expect(spy2).toHaveBeenCalledTimes(1);
|
|
209
|
+
});
|
|
210
|
+
it("should allow for removing interceptors on response", async () => {
|
|
211
|
+
const firstCallback = interceptorCallback({ callback: spy1 });
|
|
212
|
+
const secondCallback = interceptorCallback({ callback: spy2 });
|
|
213
|
+
client.onResponse(firstCallback).onResponse(secondCallback);
|
|
214
|
+
mockRequest(request);
|
|
215
|
+
|
|
216
|
+
await request.send({});
|
|
217
|
+
client.removeOnResponseInterceptors([secondCallback]);
|
|
218
|
+
|
|
219
|
+
await request.send({});
|
|
220
|
+
|
|
221
|
+
expect(spy1).toHaveBeenCalledTimes(2);
|
|
222
|
+
expect(spy2).toHaveBeenCalledTimes(1);
|
|
223
|
+
});
|
|
224
|
+
});
|
|
225
|
+
});
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
import { createHttpMockingServer } from "@hyper-fetch/testing";
|
|
2
|
+
|
|
3
|
+
import { Plugin } from "plugin";
|
|
4
|
+
import { Client } from "client";
|
|
5
|
+
import { RequestOptionsType, Request } from "request";
|
|
6
|
+
import { LoggerManager } from "managers";
|
|
7
|
+
import { createAdapter, interceptorCallback, middlewareCallback } from "../../utils";
|
|
8
|
+
import { HttpAdapterOptionsType, QueryStringifyOptionsType } from "http-adapter";
|
|
9
|
+
|
|
10
|
+
const { resetMocks, startServer, stopServer } = createHttpMockingServer();
|
|
11
|
+
|
|
12
|
+
describe("Client [ Methods ]", () => {
|
|
13
|
+
let client = new Client({ url: "shared-base-url" });
|
|
14
|
+
const request = client.createRequest()({ endpoint: "shared-base-endpoint" });
|
|
15
|
+
beforeAll(() => {
|
|
16
|
+
startServer();
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
beforeEach(() => {
|
|
20
|
+
client = new Client({ url: "shared-base-url" });
|
|
21
|
+
resetMocks();
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
afterAll(() => {
|
|
25
|
+
stopServer();
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
describe("When using config methods", () => {
|
|
29
|
+
it("should assign default request config [setRequestDefaults]", async () => {
|
|
30
|
+
const options: Partial<RequestOptionsType<string, HttpAdapterOptionsType>> = { method: "POST" };
|
|
31
|
+
client.adapter.setRequestDefaults(() => options);
|
|
32
|
+
const req = client.createRequest()({ endpoint: "test" });
|
|
33
|
+
|
|
34
|
+
expect(client.adapter.unstable_getRequestDefaults?.(request.requestOptions)).toEqual(options);
|
|
35
|
+
expect(req.method).toBe("POST");
|
|
36
|
+
});
|
|
37
|
+
it("should assign default adapter config [setAdapterDefaultOptions]", async () => {
|
|
38
|
+
const options: HttpAdapterOptionsType = { timeout: 12312312 };
|
|
39
|
+
client.adapter.setAdapterDefaults(() => options);
|
|
40
|
+
|
|
41
|
+
expect(client.adapter.unstable_getAdapterDefaults?.(request)).toEqual(options);
|
|
42
|
+
});
|
|
43
|
+
it("should assign query params stringify config [setQueryParamsConfig]", async () => {
|
|
44
|
+
const options: QueryStringifyOptionsType = {
|
|
45
|
+
strict: false,
|
|
46
|
+
dateParser: () => "date",
|
|
47
|
+
objectParser: () => "object",
|
|
48
|
+
};
|
|
49
|
+
client.adapter.setQueryParamsMapperConfig(options);
|
|
50
|
+
|
|
51
|
+
expect(client.adapter.unstable_queryParamsMapperConfig).toEqual(options);
|
|
52
|
+
expect(client.adapter.unstable_queryParamsMapper({ date: new Date() })).toBe("?date=date");
|
|
53
|
+
expect(client.adapter.unstable_queryParamsMapper({ object: {} })).toBe("?object=object");
|
|
54
|
+
});
|
|
55
|
+
it("should assign debug value [setDebug]", async () => {
|
|
56
|
+
client.setLogger(() => new LoggerManager({ logger: () => null })).setDebug(true);
|
|
57
|
+
expect(client.debug).toEqual(true);
|
|
58
|
+
});
|
|
59
|
+
it("should assign query params handling callback [setStringifyQueryParams]", async () => {
|
|
60
|
+
const callback = () => "?test=1";
|
|
61
|
+
client.adapter.setQueryParamsMapper(callback);
|
|
62
|
+
|
|
63
|
+
expect(client.adapter.unstable_queryParamsMapper({}, {})).toEqual("?test=1");
|
|
64
|
+
});
|
|
65
|
+
it("should assign logger levels [setLoggerLevel]", async () => {
|
|
66
|
+
client.setLogLevel("info");
|
|
67
|
+
|
|
68
|
+
expect(client.loggerManager.level).toEqual("info");
|
|
69
|
+
});
|
|
70
|
+
it("should assign new adapter [setAdapter]", async () => {
|
|
71
|
+
const callback = createAdapter();
|
|
72
|
+
client.setAdapter(callback);
|
|
73
|
+
|
|
74
|
+
expect(client.adapter).toEqual(callback);
|
|
75
|
+
});
|
|
76
|
+
it("should assign auth middleware [onAuth]", async () => {
|
|
77
|
+
const callback = middlewareCallback();
|
|
78
|
+
client.onAuth(callback).onAuth(callback);
|
|
79
|
+
|
|
80
|
+
expect(client.unstable_onAuthCallbacks).toHaveLength(2);
|
|
81
|
+
expect(client.unstable_onAuthCallbacks[0]).toEqual(callback);
|
|
82
|
+
expect(client.unstable_onAuthCallbacks[1]).toEqual(callback);
|
|
83
|
+
});
|
|
84
|
+
it("should assign error interceptors [onError]", async () => {
|
|
85
|
+
const callback = interceptorCallback();
|
|
86
|
+
client.onError(callback).onError(callback);
|
|
87
|
+
|
|
88
|
+
expect(client.unstable_onErrorCallbacks).toHaveLength(2);
|
|
89
|
+
expect(client.unstable_onErrorCallbacks[0]).toEqual(callback);
|
|
90
|
+
expect(client.unstable_onErrorCallbacks[1]).toEqual(callback);
|
|
91
|
+
});
|
|
92
|
+
it("should assign success interceptors [onSuccess]", async () => {
|
|
93
|
+
const callback = interceptorCallback();
|
|
94
|
+
client.onSuccess(callback).onSuccess(callback);
|
|
95
|
+
|
|
96
|
+
expect(client.unstable_onSuccessCallbacks).toHaveLength(2);
|
|
97
|
+
expect(client.unstable_onSuccessCallbacks[0]).toEqual(callback);
|
|
98
|
+
expect(client.unstable_onSuccessCallbacks[1]).toEqual(callback);
|
|
99
|
+
});
|
|
100
|
+
it("should assign request triggers [onRequest]", async () => {
|
|
101
|
+
const callback = middlewareCallback();
|
|
102
|
+
client.onRequest(callback).onRequest(callback);
|
|
103
|
+
|
|
104
|
+
expect(client.unstable_onRequestCallbacks).toHaveLength(2);
|
|
105
|
+
expect(client.unstable_onRequestCallbacks[0]).toEqual(callback);
|
|
106
|
+
expect(client.unstable_onRequestCallbacks[1]).toEqual(callback);
|
|
107
|
+
});
|
|
108
|
+
it("should assign response triggers [onResponse]", async () => {
|
|
109
|
+
const callback = interceptorCallback();
|
|
110
|
+
client.onResponse(callback).onResponse(callback);
|
|
111
|
+
|
|
112
|
+
expect(client.unstable_onResponseCallbacks).toHaveLength(2);
|
|
113
|
+
expect(client.unstable_onResponseCallbacks[0]).toEqual(callback);
|
|
114
|
+
expect(client.unstable_onResponseCallbacks[1]).toEqual(callback);
|
|
115
|
+
});
|
|
116
|
+
it("should create new request on createRequest method trigger", async () => {
|
|
117
|
+
const endpoint = "some-endpoint";
|
|
118
|
+
const newRequest = client.createRequest()({ endpoint });
|
|
119
|
+
|
|
120
|
+
expect(newRequest instanceof Request).toBeTrue();
|
|
121
|
+
expect(newRequest.endpoint).toBe(endpoint);
|
|
122
|
+
});
|
|
123
|
+
it("should add single effect listeners on addPlugin method trigger", async () => {
|
|
124
|
+
const plugin = new Plugin({ name: "NEW-EFFECT" });
|
|
125
|
+
|
|
126
|
+
client.addPlugin(plugin);
|
|
127
|
+
|
|
128
|
+
expect(client.plugins[0]).toBe(plugin);
|
|
129
|
+
expect(client.plugins).toHaveLength(1);
|
|
130
|
+
});
|
|
131
|
+
it("should add plugins array listeners on addPlugin method trigger", async () => {
|
|
132
|
+
const plugin = new Plugin({ name: "NEW-EFFECT" });
|
|
133
|
+
|
|
134
|
+
client.addPlugin(plugin);
|
|
135
|
+
|
|
136
|
+
expect(client.plugins[0]).toBe(plugin);
|
|
137
|
+
expect(client.plugins).toHaveLength(1);
|
|
138
|
+
});
|
|
139
|
+
it("should remove plugin listener on removePlugin method trigger", async () => {
|
|
140
|
+
const plugin = new Plugin({ name: "NEW-EFFECT" });
|
|
141
|
+
|
|
142
|
+
client.addPlugin(plugin);
|
|
143
|
+
client.removePlugin(plugin);
|
|
144
|
+
expect(client.plugins).toHaveLength(0);
|
|
145
|
+
|
|
146
|
+
client.addPlugin(plugin);
|
|
147
|
+
expect(client.plugins).toHaveLength(1);
|
|
148
|
+
});
|
|
149
|
+
it("should not remove plugin when wrong pluginKey is passed", async () => {
|
|
150
|
+
const plugin = new Plugin({ name: "NEW-EFFECT" });
|
|
151
|
+
const otherEffect = new Plugin({ name: "OTHER-EFFECT" });
|
|
152
|
+
|
|
153
|
+
client.addPlugin(plugin);
|
|
154
|
+
client.removePlugin(otherEffect);
|
|
155
|
+
expect(client.plugins).toHaveLength(1);
|
|
156
|
+
|
|
157
|
+
expect(client.plugins).toHaveLength(1);
|
|
158
|
+
});
|
|
159
|
+
it("should assign query params handling callback [setHeaderMapper]", async () => {
|
|
160
|
+
const callback = () => ({ test: 123 }) as any as HeadersInit;
|
|
161
|
+
client.adapter.setHeaderMapper(callback);
|
|
162
|
+
|
|
163
|
+
expect(client.adapter.unstable_headerMapper({}, {})).toEqual({ test: 123 });
|
|
164
|
+
});
|
|
165
|
+
it("should assign payload handling callback [setPayloadMapper]", async () => {
|
|
166
|
+
const callback = () => "test-123";
|
|
167
|
+
client.adapter.setPayloadMapper(callback);
|
|
168
|
+
|
|
169
|
+
expect(client.adapter.unstable_payloadMapper({}, {})).toEqual("test-123");
|
|
170
|
+
});
|
|
171
|
+
it("should assign endpoint handling callback [setEndpointMapper]", async () => {
|
|
172
|
+
const callback = () => "test-endpoint";
|
|
173
|
+
client.adapter.setEndpointMapper(callback);
|
|
174
|
+
|
|
175
|
+
expect(client.adapter.unstable_endpointMapper({}, {})).toEqual("test-endpoint");
|
|
176
|
+
});
|
|
177
|
+
it("should assign key mappers", async () => {
|
|
178
|
+
const callback = () => "";
|
|
179
|
+
client.setAbortKeyMapper(callback);
|
|
180
|
+
client.setCacheKeyMapper(callback);
|
|
181
|
+
client.setQueryKeyMapper(callback);
|
|
182
|
+
|
|
183
|
+
expect(client.unstable_abortKeyMapper).toEqual(callback);
|
|
184
|
+
expect(client.unstable_cacheKeyMapper).toEqual(callback);
|
|
185
|
+
expect(client.unstable_queryKeyMapper).toEqual(callback);
|
|
186
|
+
});
|
|
187
|
+
});
|
|
188
|
+
});
|