@hyper-fetch/core 7.5.3 → 8.0.0
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/.eslintrc.cjs +12 -0
- package/__tests__/e2e/e2e.abort-timeout.spec.ts +101 -0
- package/__tests__/e2e/e2e.advanced.spec.ts +423 -0
- package/__tests__/e2e/e2e.methods.spec.ts +243 -0
- package/__tests__/e2e/e2e.payloads.spec.ts +154 -0
- package/__tests__/features/adapter/adapter.base.spec.ts +44 -17
- package/__tests__/features/adapter/adapter.bindings.spec.ts +35 -33
- package/__tests__/features/adapter/adapter.browser.spec.ts +5 -20
- package/__tests__/features/adapter/adapter.server.spec.ts +229 -37
- package/__tests__/features/cache/cache.base.spec.ts +23 -15
- package/__tests__/features/cache/cache.events.spec.ts +9 -5
- package/__tests__/features/cache/cache.garbage-collector.spec.ts +9 -6
- package/__tests__/features/cache/cache.lazy-storage.spec.ts +11 -8
- package/__tests__/features/cache/cache.modes.spec.ts +145 -0
- package/__tests__/features/cache/cache.scoping.spec.ts +171 -0
- package/__tests__/features/cache/cache.typescript.spec.ts +147 -0
- package/__tests__/features/cache/cache.utils.spec.ts +10 -3
- package/__tests__/features/client/client.auth.spec.ts +5 -5
- package/__tests__/features/client/client.base.spec.ts +16 -4
- package/__tests__/features/client/client.from-json.spec.ts +182 -0
- package/__tests__/features/client/client.hydration.spec.ts +69 -4
- package/__tests__/features/client/client.interceptor.spec.ts +4 -4
- package/__tests__/features/client/client.methods.spec.ts +5 -4
- package/__tests__/features/client/client.middleware.spec.ts +4 -4
- package/__tests__/features/client/client.utils.spec.ts +3 -3
- package/__tests__/features/dispatcher/dispatcher.base.spec.ts +8 -8
- package/__tests__/features/dispatcher/dispatcher.custom-storage.spec.ts +189 -0
- package/__tests__/features/dispatcher/dispatcher.events.spec.ts +17 -12
- package/__tests__/features/dispatcher/dispatcher.queue.spec.ts +31 -31
- package/__tests__/features/dispatcher/dispatcher.requests.spec.ts +15 -15
- package/__tests__/features/dispatcher/dispatcher.utils.spec.ts +22 -5
- package/__tests__/features/managers/app/app.manager.base.spec.ts +23 -23
- package/__tests__/features/managers/app/app.manager.ssr.spec.ts +6 -3
- package/__tests__/features/managers/logger/logger.base.spec.ts +4 -2
- package/__tests__/features/managers/logger/logger.utils.spec.ts +10 -4
- package/__tests__/features/managers/request/request.manager.base.spec.ts +21 -19
- package/__tests__/features/managers/request/request.manager.events.spec.ts +137 -53
- package/__tests__/features/mocker/mocker.base.spec.ts +82 -10
- package/__tests__/features/plugin/plugin.base.spec.ts +19 -19
- package/__tests__/features/request/request.base.spec.ts +39 -13
- package/__tests__/features/request/request.cloning.spec.ts +1 -1
- package/__tests__/features/request/request.hooks.spec.ts +173 -0
- package/__tests__/features/request/request.optimistic.spec.ts +93 -0
- package/__tests__/features/request/request.retry-on-error.spec.ts +359 -0
- package/__tests__/features/request/request.sending.spec.ts +107 -15
- package/__tests__/features/request/request.setters.spec.ts +11 -4
- package/__tests__/features/request/request.typescript.spec.ts +1 -1
- package/__tests__/features/request/request.utils.spec.ts +2 -2
- package/__tests__/features/sdk/sdk.base.spec.ts +5 -3
- package/__tests__/features/sdk/sdk.configuration.spec.ts +262 -0
- package/__tests__/features/utils/emitter.utils.spec.ts +88 -0
- package/__tests__/shared/callbacks.shared.ts +3 -1
- package/__tests__/shared/progress.shared.ts +2 -1
- package/__tests__/tsconfig.json +14 -3
- package/__tests__/type-stress/generate-stress-test.ts +92 -0
- package/__tests__/type-stress/stress-10.ts +35 -0
- package/__tests__/type-stress/stress-just-create.ts +2107 -0
- package/__tests__/type-stress/stress-minimal.ts +23 -0
- package/__tests__/type-stress/stress-test-500.ts +2319 -0
- package/__tests__/type-stress/stress-test-sdk.ts +210 -0
- package/__tests__/type-stress/tsconfig.json +6 -0
- package/__tests__/utils/adapter.utils.ts +3 -2
- package/__tests__/utils/cache.utils.ts +3 -2
- package/__tests__/utils/callbacks.utils.ts +5 -4
- package/__tests__/utils/dispatcher.utils.ts +3 -2
- package/__tests__/vitest.setup.ts +15 -0
- package/dist/adapter/adapter.bindings.d.ts +71 -0
- package/dist/adapter/adapter.bindings.d.ts.map +1 -0
- package/dist/adapter/adapter.d.ts +127 -0
- package/dist/adapter/adapter.d.ts.map +1 -0
- package/dist/adapter/adapter.types.d.ts +108 -0
- package/dist/adapter/adapter.types.d.ts.map +1 -0
- package/dist/adapter/adapter.utils.d.ts +23 -0
- package/dist/adapter/adapter.utils.d.ts.map +1 -0
- package/dist/adapter/index.d.ts +5 -0
- package/dist/adapter/index.d.ts.map +1 -0
- package/dist/cache/cache.d.ts +95 -0
- package/dist/cache/cache.d.ts.map +1 -0
- package/dist/cache/cache.events.d.ts +51 -0
- package/dist/cache/cache.events.d.ts.map +1 -0
- package/dist/cache/cache.types.d.ts +44 -0
- package/dist/cache/cache.types.d.ts.map +1 -0
- package/dist/cache/cache.utils.d.ts +11 -0
- package/dist/cache/cache.utils.d.ts.map +1 -0
- package/dist/cache/index.d.ts +5 -0
- package/dist/cache/index.d.ts.map +1 -0
- package/dist/client/client.constants.d.ts +10 -0
- package/dist/client/client.constants.d.ts.map +1 -0
- package/dist/client/client.create.d.ts +8 -0
- package/dist/client/client.create.d.ts.map +1 -0
- package/dist/client/client.d.ts +195 -0
- package/dist/client/client.d.ts.map +1 -0
- package/dist/client/client.types.d.ts +71 -0
- package/dist/client/client.types.d.ts.map +1 -0
- package/dist/client/client.utils.d.ts +11 -0
- package/dist/client/client.utils.d.ts.map +1 -0
- package/dist/client/index.d.ts +6 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/constants/http.constants.d.ts +8 -0
- package/dist/constants/http.constants.d.ts.map +1 -0
- package/dist/constants/time.constants.d.ts +12 -0
- package/dist/constants/time.constants.d.ts.map +1 -0
- package/dist/dispatcher/dispatcher.constants.d.ts +7 -0
- package/dist/dispatcher/dispatcher.constants.d.ts.map +1 -0
- package/dist/dispatcher/dispatcher.d.ts +168 -0
- package/dist/dispatcher/dispatcher.d.ts.map +1 -0
- package/dist/dispatcher/dispatcher.events.d.ts +49 -0
- package/dist/dispatcher/dispatcher.events.d.ts.map +1 -0
- package/dist/dispatcher/dispatcher.types.d.ts +44 -0
- package/dist/dispatcher/dispatcher.types.d.ts.map +1 -0
- package/dist/dispatcher/dispatcher.utils.d.ts +12 -0
- package/dist/dispatcher/dispatcher.utils.d.ts.map +1 -0
- package/dist/dispatcher/index.d.ts +6 -0
- package/dist/dispatcher/index.d.ts.map +1 -0
- package/dist/http-adapter/http-adapter.constants.d.ts +4 -0
- package/dist/http-adapter/http-adapter.constants.d.ts.map +1 -0
- package/dist/http-adapter/http-adapter.d.ts +3 -0
- package/dist/http-adapter/http-adapter.d.ts.map +1 -0
- package/dist/http-adapter/http-adapter.fetch.d.ts +6 -0
- package/dist/http-adapter/http-adapter.fetch.d.ts.map +1 -0
- package/dist/http-adapter/http-adapter.types.d.ts +73 -0
- package/dist/http-adapter/http-adapter.types.d.ts.map +1 -0
- package/dist/http-adapter/http-adapter.utils.d.ts +10 -0
- package/dist/http-adapter/http-adapter.utils.d.ts.map +1 -0
- package/dist/http-adapter/index.d.ts +6 -0
- package/dist/http-adapter/index.d.ts.map +1 -0
- package/dist/index.d.ts +15 -2083
- package/dist/index.d.ts.map +1 -0
- package/dist/index.mjs +4084 -0
- package/dist/index.mjs.map +1 -0
- package/dist/managers/app/app.manager.constants.d.ts +10 -0
- package/dist/managers/app/app.manager.constants.d.ts.map +1 -0
- package/dist/managers/app/app.manager.d.ts +35 -0
- package/dist/managers/app/app.manager.d.ts.map +1 -0
- package/dist/managers/app/app.manager.events.d.ts +12 -0
- package/dist/managers/app/app.manager.events.d.ts.map +1 -0
- package/dist/managers/app/app.manager.types.d.ts +7 -0
- package/dist/managers/app/app.manager.types.d.ts.map +1 -0
- package/dist/managers/app/app.manager.utils.d.ts +5 -0
- package/dist/managers/app/app.manager.utils.d.ts.map +1 -0
- package/dist/managers/app/index.d.ts +6 -0
- package/dist/managers/app/index.d.ts.map +1 -0
- package/dist/managers/index.d.ts +4 -0
- package/dist/managers/index.d.ts.map +1 -0
- package/dist/managers/logger/index.d.ts +5 -0
- package/dist/managers/logger/index.d.ts.map +1 -0
- package/dist/managers/logger/logger.manager.constants.d.ts +5 -0
- package/dist/managers/logger/logger.manager.constants.d.ts.map +1 -0
- package/dist/managers/logger/logger.manager.d.ts +22 -0
- package/dist/managers/logger/logger.manager.d.ts.map +1 -0
- package/dist/managers/logger/logger.manager.types.d.ts +43 -0
- package/dist/managers/logger/logger.manager.types.d.ts.map +1 -0
- package/dist/managers/logger/logger.manager.utils.d.ts +4 -0
- package/dist/managers/logger/logger.manager.utils.d.ts.map +1 -0
- package/dist/managers/request/index.d.ts +5 -0
- package/dist/managers/request/index.d.ts.map +1 -0
- package/dist/managers/request/request.manager.d.ts +60 -0
- package/dist/managers/request/request.manager.d.ts.map +1 -0
- package/dist/managers/request/request.manager.events.d.ts +55 -0
- package/dist/managers/request/request.manager.events.d.ts.map +1 -0
- package/dist/managers/request/request.manager.types.d.ts +48 -0
- package/dist/managers/request/request.manager.types.d.ts.map +1 -0
- package/dist/managers/request/request.manager.utils.d.ts +30 -0
- package/dist/managers/request/request.manager.utils.d.ts.map +1 -0
- package/dist/mocker/index.d.ts +3 -0
- package/dist/mocker/index.d.ts.map +1 -0
- package/dist/mocker/mocker.d.ts +3 -0
- package/dist/mocker/mocker.d.ts.map +1 -0
- package/dist/mocker/mocker.types.d.ts +25 -0
- package/dist/mocker/mocker.types.d.ts.map +1 -0
- package/dist/plugin/index.d.ts +3 -0
- package/dist/plugin/index.d.ts.map +1 -0
- package/dist/plugin/plugin.d.ts +55 -0
- package/dist/plugin/plugin.d.ts.map +1 -0
- package/dist/plugin/plugin.types.d.ts +122 -0
- package/dist/plugin/plugin.types.d.ts.map +1 -0
- package/dist/request/index.d.ts +5 -0
- package/dist/request/index.d.ts.map +1 -0
- package/dist/request/request.d.ts +195 -0
- package/dist/request/request.d.ts.map +1 -0
- package/dist/request/request.hooks.d.ts +19 -0
- package/dist/request/request.hooks.d.ts.map +1 -0
- package/dist/request/request.types.d.ts +240 -0
- package/dist/request/request.types.d.ts.map +1 -0
- package/dist/request/request.utils.d.ts +25 -0
- package/dist/request/request.utils.d.ts.map +1 -0
- package/dist/sdk/index.d.ts +2 -0
- package/dist/sdk/index.d.ts.map +1 -0
- package/dist/sdk/sdk.d.ts +69 -0
- package/dist/sdk/sdk.d.ts.map +1 -0
- package/dist/types/adapter.types.d.ts +22 -0
- package/dist/types/adapter.types.d.ts.map +1 -0
- package/dist/types/client.types.d.ts +4 -0
- package/dist/types/client.types.d.ts.map +1 -0
- package/dist/types/helpers.types.d.ts +17 -0
- package/dist/types/helpers.types.d.ts.map +1 -0
- package/dist/types/http.types.d.ts +3 -0
- package/dist/types/http.types.d.ts.map +1 -0
- package/dist/types/hydration.types.d.ts +15 -0
- package/dist/types/hydration.types.d.ts.map +1 -0
- package/dist/types/index.d.ts +7 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/request.types.d.ts +34 -0
- package/dist/types/request.types.d.ts.map +1 -0
- package/dist/utils/emitter.utils.d.ts +13 -0
- package/dist/utils/emitter.utils.d.ts.map +1 -0
- package/dist/utils/index.d.ts +3 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/uuid.utils.d.ts +3 -0
- package/dist/utils/uuid.utils.d.ts.map +1 -0
- package/package.json +10 -25
- package/src/adapter/adapter.bindings.ts +8 -12
- package/src/adapter/adapter.ts +8 -7
- package/src/adapter/adapter.types.ts +10 -6
- package/src/adapter/adapter.utils.ts +1 -1
- package/src/cache/cache.events.ts +4 -11
- package/src/cache/cache.ts +28 -22
- package/src/cache/cache.types.ts +8 -4
- package/src/cache/cache.utils.ts +3 -3
- package/src/client/client.create.ts +4 -3
- package/src/client/client.ts +44 -12
- package/src/client/client.types.ts +25 -3
- package/src/client/client.utils.ts +24 -4
- package/src/dispatcher/dispatcher.events.ts +3 -3
- package/src/dispatcher/dispatcher.ts +59 -28
- package/src/dispatcher/dispatcher.types.ts +19 -2
- package/src/dispatcher/dispatcher.utils.ts +5 -4
- package/src/http-adapter/http-adapter.constants.ts +1 -1
- package/src/http-adapter/http-adapter.fetch.ts +174 -0
- package/src/http-adapter/http-adapter.ts +3 -8
- package/src/http-adapter/http-adapter.types.ts +17 -9
- package/src/http-adapter/http-adapter.utils.ts +6 -9
- package/src/http-adapter/index.ts +1 -0
- package/src/managers/app/app.manager.constants.ts +2 -2
- package/src/managers/app/app.manager.events.ts +1 -1
- package/src/managers/app/app.manager.ts +2 -1
- package/src/managers/logger/logger.manager.constants.ts +1 -1
- package/src/managers/logger/logger.manager.ts +2 -1
- package/src/managers/logger/logger.manager.types.ts +2 -2
- package/src/managers/logger/logger.manager.utils.ts +2 -1
- package/src/managers/request/request.manager.events.ts +13 -11
- package/src/managers/request/request.manager.types.ts +6 -3
- package/src/mocker/mocker.ts +3 -2
- package/src/mocker/mocker.types.ts +1 -1
- package/src/plugin/plugin.ts +2 -2
- package/src/plugin/plugin.types.ts +9 -9
- package/src/request/index.ts +1 -0
- package/src/request/request.hooks.ts +87 -0
- package/src/request/request.ts +155 -77
- package/src/request/request.types.ts +54 -22
- package/src/request/request.utils.ts +86 -23
- package/src/sdk/sdk.ts +153 -8
- package/src/types/adapter.types.ts +1 -1
- package/src/types/client.types.ts +1 -1
- package/src/types/hydration.types.ts +3 -3
- package/src/types/request.types.ts +30 -20
- package/src/utils/uuid.utils.ts +1 -1
- package/tsconfig.base.json +1 -6
- package/tsconfig.json +1 -4
- package/vite.config.ts +45 -0
- package/.eslintrc.json +0 -11
- package/__tests__/global.d.ts +0 -1
- package/__tests__/jest.setup.ts +0 -13
- package/dist/browser/index.js +0 -3751
- package/dist/browser/index.js.map +0 -7
- package/dist/index.js +0 -4002
- package/dist/index.js.map +0 -7
- package/jest.config.ts +0 -6
- package/src/http-adapter/http-adapter.browser.ts +0 -102
- package/src/http-adapter/http-adapter.server.ts +0 -149
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { waitFor } from "@testing-library/dom";
|
|
1
2
|
import { createHttpMockingServer, sleep } from "@hyper-fetch/testing";
|
|
2
3
|
|
|
3
4
|
import { getErrorMessage } from "adapter";
|
|
@@ -20,7 +21,7 @@ describe("Request [ Sending ]", () => {
|
|
|
20
21
|
client = new Client({ url: "shared-base-url" });
|
|
21
22
|
request = client.createRequest<{ response: any }>()({ endpoint: "shared-base-endpoint" });
|
|
22
23
|
resetMocks();
|
|
23
|
-
|
|
24
|
+
vi.resetAllMocks();
|
|
24
25
|
mockRequest(request, { data: fixture, delay: 40 });
|
|
25
26
|
});
|
|
26
27
|
|
|
@@ -43,7 +44,9 @@ describe("Request [ Sending ]", () => {
|
|
|
43
44
|
});
|
|
44
45
|
});
|
|
45
46
|
it("should return mapped adapter response", async () => {
|
|
46
|
-
const requestExecution = request
|
|
47
|
+
const requestExecution = request
|
|
48
|
+
.setResponseMapper((res) => ({ ...res, data: { nested: res.data } }) as typeof res)
|
|
49
|
+
.exec({});
|
|
47
50
|
await sleep(5);
|
|
48
51
|
expect(client.fetchDispatcher.getAllRunningRequests()).toHaveLength(0);
|
|
49
52
|
const { responseTimestamp, requestTimestamp, ...response } = await requestExecution;
|
|
@@ -70,7 +73,7 @@ describe("Request [ Sending ]", () => {
|
|
|
70
73
|
});
|
|
71
74
|
it("should return mapped adapter response", async () => {
|
|
72
75
|
const { responseTimestamp, requestTimestamp, ...response } = await request
|
|
73
|
-
.setResponseMapper((res) => ({ ...res, data: { nested: res.data } }))
|
|
76
|
+
.setResponseMapper((res) => ({ ...res, data: { nested: res.data } }) as typeof res)
|
|
74
77
|
.send({});
|
|
75
78
|
|
|
76
79
|
expect(response).toStrictEqual({
|
|
@@ -83,7 +86,7 @@ describe("Request [ Sending ]", () => {
|
|
|
83
86
|
});
|
|
84
87
|
it("should return async mapped adapter response", async () => {
|
|
85
88
|
const { responseTimestamp, requestTimestamp, ...response } = await request
|
|
86
|
-
.setResponseMapper(async (res) => Promise.resolve({ ...res, data: { nested: res.data } }))
|
|
89
|
+
.setResponseMapper(async (res) => Promise.resolve({ ...res, data: { nested: res.data } } as typeof res))
|
|
87
90
|
.send({});
|
|
88
91
|
|
|
89
92
|
expect(response).toStrictEqual({
|
|
@@ -95,7 +98,7 @@ describe("Request [ Sending ]", () => {
|
|
|
95
98
|
});
|
|
96
99
|
});
|
|
97
100
|
it("should wait to resolve request in online mode", async () => {
|
|
98
|
-
const spy =
|
|
101
|
+
const spy = vi.fn();
|
|
99
102
|
mockRequest(request, { delay: 10, status: 400 });
|
|
100
103
|
const requestExecution = request.send({});
|
|
101
104
|
await sleep(5);
|
|
@@ -119,7 +122,7 @@ describe("Request [ Sending ]", () => {
|
|
|
119
122
|
expect(spy).toHaveBeenCalledTimes(1);
|
|
120
123
|
});
|
|
121
124
|
it("should wait to resolve request retries", async () => {
|
|
122
|
-
const spy =
|
|
125
|
+
const spy = vi.fn();
|
|
123
126
|
mockRequest(request, { delay: 10, status: 400 });
|
|
124
127
|
const requestExecution = request.setRetry(1).setRetryTime(30).send({});
|
|
125
128
|
await sleep(5);
|
|
@@ -158,7 +161,7 @@ describe("Request [ Sending ]", () => {
|
|
|
158
161
|
});
|
|
159
162
|
});
|
|
160
163
|
it("should call remove error", async () => {
|
|
161
|
-
const spy =
|
|
164
|
+
const spy = vi.fn();
|
|
162
165
|
mockRequest(request, { delay: 10, status: 400 });
|
|
163
166
|
const requestExecution = request.send({ onRemove: spy });
|
|
164
167
|
await sleep(2);
|
|
@@ -245,12 +248,12 @@ describe("Request [ Sending ]", () => {
|
|
|
245
248
|
});
|
|
246
249
|
|
|
247
250
|
it("should allow to call the request callbacks", async () => {
|
|
248
|
-
const spy1 =
|
|
249
|
-
const spy2 =
|
|
250
|
-
const spy3 =
|
|
251
|
-
const spy4 =
|
|
252
|
-
const spy5 =
|
|
253
|
-
const spy6 =
|
|
251
|
+
const spy1 = vi.fn();
|
|
252
|
+
const spy2 = vi.fn();
|
|
253
|
+
const spy3 = vi.fn();
|
|
254
|
+
const spy4 = vi.fn();
|
|
255
|
+
const spy5 = vi.fn();
|
|
256
|
+
const spy6 = vi.fn();
|
|
254
257
|
|
|
255
258
|
await request.send({
|
|
256
259
|
onBeforeSent: spy1,
|
|
@@ -264,9 +267,98 @@ describe("Request [ Sending ]", () => {
|
|
|
264
267
|
expect(spy1).toHaveBeenCalledTimes(1);
|
|
265
268
|
expect(spy2).toHaveBeenCalledTimes(1);
|
|
266
269
|
expect(spy3).toHaveBeenCalledTimes(1);
|
|
267
|
-
expect(spy4).
|
|
268
|
-
expect(spy5).
|
|
270
|
+
expect(spy4.mock.calls.length).toBeGreaterThanOrEqual(1);
|
|
271
|
+
expect(spy5.mock.calls.length).toBeGreaterThanOrEqual(1);
|
|
269
272
|
expect(spy6).toHaveBeenCalledTimes(1);
|
|
270
273
|
});
|
|
274
|
+
|
|
275
|
+
describe("cachePolicy", () => {
|
|
276
|
+
it("cache-first uses read() and does not call adapter when cache is primed", async () => {
|
|
277
|
+
const fetchSpy = vi.fn(() => ({ data: fixture, status: 200 }));
|
|
278
|
+
const r = request.setMock(fetchSpy);
|
|
279
|
+
await r.send({});
|
|
280
|
+
fetchSpy.mockClear();
|
|
281
|
+
|
|
282
|
+
const { responseTimestamp, requestTimestamp, ...response } = await r.send({ cachePolicy: "cache-first" });
|
|
283
|
+
|
|
284
|
+
expect(response).toStrictEqual({
|
|
285
|
+
data: fixture,
|
|
286
|
+
error: null,
|
|
287
|
+
status: 200,
|
|
288
|
+
success: true,
|
|
289
|
+
extra: xhrExtra,
|
|
290
|
+
});
|
|
291
|
+
expect(fetchSpy).not.toHaveBeenCalled();
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
it("cache-first fetches when cache is empty", async () => {
|
|
295
|
+
const fetchSpy = vi.fn(() => ({ data: fixture, status: 200 }));
|
|
296
|
+
const r = request.setMock(fetchSpy);
|
|
297
|
+
client.cache.clear();
|
|
298
|
+
|
|
299
|
+
await r.send({ cachePolicy: "cache-first" });
|
|
300
|
+
|
|
301
|
+
expect(fetchSpy).toHaveBeenCalledTimes(1);
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
it("revalidate resolves with cache immediately and refreshes in the background", async () => {
|
|
305
|
+
const fetchSpy = vi.fn(() => ({ data: fixture, status: 200 }));
|
|
306
|
+
const r = request.setMock(fetchSpy);
|
|
307
|
+
await r.send({});
|
|
308
|
+
fetchSpy.mockClear();
|
|
309
|
+
fetchSpy.mockImplementation(() => ({ data: { refreshed: true } as any, status: 200 }));
|
|
310
|
+
|
|
311
|
+
const { responseTimestamp, requestTimestamp, ...immediate } = await r.send({ cachePolicy: "revalidate" });
|
|
312
|
+
|
|
313
|
+
expect(immediate.data).toStrictEqual(fixture);
|
|
314
|
+
|
|
315
|
+
await waitFor(() => {
|
|
316
|
+
expect(fetchSpy).toHaveBeenCalledTimes(1);
|
|
317
|
+
});
|
|
318
|
+
|
|
319
|
+
await waitFor(() => {
|
|
320
|
+
expect(r.read()?.data).toStrictEqual({ refreshed: true });
|
|
321
|
+
});
|
|
322
|
+
});
|
|
323
|
+
|
|
324
|
+
it("revalidate awaits network when cache is empty", async () => {
|
|
325
|
+
const fetchSpy = vi.fn(() => ({ data: fixture, status: 200 }));
|
|
326
|
+
const r = request.setMock(fetchSpy);
|
|
327
|
+
client.cache.clear();
|
|
328
|
+
|
|
329
|
+
const { responseTimestamp, requestTimestamp, ...response } = await r.send({ cachePolicy: "revalidate" });
|
|
330
|
+
|
|
331
|
+
expect(response.data).toStrictEqual(fixture);
|
|
332
|
+
expect(fetchSpy).toHaveBeenCalledTimes(1);
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
it("network-only matches default send", async () => {
|
|
336
|
+
const { responseTimestamp, requestTimestamp, ...response } = await request.send({
|
|
337
|
+
cachePolicy: "network-only",
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
expect(response).toStrictEqual({
|
|
341
|
+
data: fixture,
|
|
342
|
+
error: null,
|
|
343
|
+
status: 200,
|
|
344
|
+
success: true,
|
|
345
|
+
extra: { headers: { "content-type": "application/json", "content-length": "25" } },
|
|
346
|
+
});
|
|
347
|
+
});
|
|
348
|
+
|
|
349
|
+
it("cache-first applies response mapper without calling adapter", async () => {
|
|
350
|
+
const fetchSpy = vi.fn(() => ({ data: fixture, status: 200 }));
|
|
351
|
+
const r = request
|
|
352
|
+
.setResponseMapper((res) => ({ ...res, data: { nested: res.data } }) as typeof res)
|
|
353
|
+
.setMock(fetchSpy);
|
|
354
|
+
await r.send({});
|
|
355
|
+
fetchSpy.mockClear();
|
|
356
|
+
|
|
357
|
+
const { responseTimestamp, requestTimestamp, ...response } = await r.send({ cachePolicy: "cache-first" });
|
|
358
|
+
|
|
359
|
+
expect(response.data).toStrictEqual({ nested: fixture });
|
|
360
|
+
expect(fetchSpy).not.toHaveBeenCalled();
|
|
361
|
+
});
|
|
362
|
+
});
|
|
271
363
|
});
|
|
272
364
|
});
|
|
@@ -1,21 +1,22 @@
|
|
|
1
1
|
import { createHttpMockingServer } from "@hyper-fetch/testing";
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import type { RequestInstance } from "../../../src";
|
|
4
|
+
import { Client, Time } from "../../../src";
|
|
4
5
|
|
|
5
6
|
const { resetMocks, startServer, stopServer } = createHttpMockingServer();
|
|
6
7
|
|
|
7
8
|
describe("Request [ Setters ]", () => {
|
|
8
9
|
let client = new Client({ url: "shared-base-url" });
|
|
9
|
-
let request = client.createRequest()({ endpoint: "/users/:userId" });
|
|
10
|
+
let request = client.createRequest<{ response: any; queryParams: any }>()({ endpoint: "/users/:userId" });
|
|
10
11
|
beforeAll(() => {
|
|
11
12
|
startServer();
|
|
12
13
|
});
|
|
13
14
|
|
|
14
15
|
beforeEach(() => {
|
|
15
16
|
client = new Client({ url: "shared-base-url" });
|
|
16
|
-
request = client.createRequest()({ endpoint: "/users/:userId" });
|
|
17
|
+
request = client.createRequest<{ response: any; queryParams: any }>()({ endpoint: "/users/:userId" });
|
|
17
18
|
resetMocks();
|
|
18
|
-
|
|
19
|
+
vi.resetAllMocks();
|
|
19
20
|
});
|
|
20
21
|
|
|
21
22
|
afterAll(() => {
|
|
@@ -128,6 +129,12 @@ describe("Request [ Setters ]", () => {
|
|
|
128
129
|
const updatedRequest = request.setCacheTime(Time.MIN);
|
|
129
130
|
expect(updatedRequest.cacheTime).toBe(Time.MIN);
|
|
130
131
|
});
|
|
132
|
+
it("should allow for setting retryOnError", async () => {
|
|
133
|
+
expect(request.retryOnError).toBeUndefined();
|
|
134
|
+
const callback = (response: any) => response.status !== 404;
|
|
135
|
+
const updatedRequest = request.setRetryOnError(callback);
|
|
136
|
+
expect(updatedRequest.retryOnError).toBeDefined();
|
|
137
|
+
});
|
|
131
138
|
it("should allow for setting data mapper", async () => {
|
|
132
139
|
const mapper = (data: { name: string; email: string }) => {
|
|
133
140
|
const formData = new FormData();
|
|
@@ -34,7 +34,7 @@ describe("Request [ Utils ]", () => {
|
|
|
34
34
|
it("should return exact endpoint key", async () => {
|
|
35
35
|
const client = new Client({ url: "shared-base-url" });
|
|
36
36
|
const request = client
|
|
37
|
-
.createRequest()({ endpoint: "/users/:userId" })
|
|
37
|
+
.createRequest<{ response: any; queryParams: any }>()({ endpoint: "/users/:userId" })
|
|
38
38
|
.setParams({ userId: 1 })
|
|
39
39
|
.setQueryParams("?test=1");
|
|
40
40
|
expect(getRequestKey(request)).toBe("GET_/users/1_?test=1");
|
|
@@ -42,7 +42,7 @@ describe("Request [ Utils ]", () => {
|
|
|
42
42
|
it("should useInitialValues and return generic endpoint key", async () => {
|
|
43
43
|
const client = new Client({ url: "shared-base-url" });
|
|
44
44
|
const request = client
|
|
45
|
-
.createRequest()({ endpoint: "/users/:userId" })
|
|
45
|
+
.createRequest<{ response: any; queryParams: any }>()({ endpoint: "/users/:userId" })
|
|
46
46
|
.setParams({ userId: 1 })
|
|
47
47
|
.setQueryParams("?test=1");
|
|
48
48
|
expect(getRequestKey(request, true)).toBe("GET_/users/:userId_");
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { createHttpMockingServer } from "@hyper-fetch/testing";
|
|
2
2
|
|
|
3
|
-
import { Client
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
3
|
+
import type { Client } from "client";
|
|
4
|
+
import { createClient } from "client";
|
|
5
|
+
import type { HttpAdapterType } from "http-adapter";
|
|
6
|
+
import type { RequestInstance } from "request";
|
|
7
|
+
import { Request } from "request";
|
|
6
8
|
import { createSdk } from "sdk";
|
|
7
9
|
|
|
8
10
|
const { resetMocks, startServer, stopServer } = createHttpMockingServer();
|
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
import type { Client } from "client";
|
|
2
|
+
import { createClient } from "client";
|
|
3
|
+
import type { HttpAdapterType } from "http-adapter";
|
|
4
|
+
import type { RequestInstance } from "request";
|
|
5
|
+
import { Request } from "request";
|
|
6
|
+
import { createSdk, createConfiguration } from "sdk";
|
|
7
|
+
|
|
8
|
+
type TestClient = Client<Error, HttpAdapterType>;
|
|
9
|
+
|
|
10
|
+
type TestSchema = {
|
|
11
|
+
users: {
|
|
12
|
+
$get: RequestInstance<{
|
|
13
|
+
client: TestClient;
|
|
14
|
+
endpoint: "/users";
|
|
15
|
+
}>;
|
|
16
|
+
$userId: {
|
|
17
|
+
$get: RequestInstance<{
|
|
18
|
+
client: TestClient;
|
|
19
|
+
response: { id: string; name: string };
|
|
20
|
+
endpoint: "/users/:userId";
|
|
21
|
+
}>;
|
|
22
|
+
};
|
|
23
|
+
};
|
|
24
|
+
posts: {
|
|
25
|
+
$get: RequestInstance<{
|
|
26
|
+
client: TestClient;
|
|
27
|
+
endpoint: "/posts";
|
|
28
|
+
}>;
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
describe("SDK [ Configuration ]", () => {
|
|
33
|
+
let client: TestClient;
|
|
34
|
+
|
|
35
|
+
beforeEach(() => {
|
|
36
|
+
client = createClient<{ error: Error }>({ url: "http://localhost:3000" });
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
describe("createConfiguration", () => {
|
|
40
|
+
it("should create a configuration map", () => {
|
|
41
|
+
const config = createConfiguration<TestSchema>()({
|
|
42
|
+
"*": { retry: 3, retryTime: 1000 },
|
|
43
|
+
"/users/*": { cache: false },
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
expect(config).toStrictEqual({
|
|
47
|
+
"*": { retry: 3, retryTime: 1000 },
|
|
48
|
+
"/users/*": { cache: false },
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
describe("$configure", () => {
|
|
54
|
+
it("should apply global defaults to all requests", () => {
|
|
55
|
+
const sdk = createSdk<TestClient, TestSchema>(client);
|
|
56
|
+
const configured = sdk.$configure({ "*": { retry: 5, retryTime: 2000 } });
|
|
57
|
+
|
|
58
|
+
const request = configured.users.$get;
|
|
59
|
+
expect(request).toBeInstanceOf(Request);
|
|
60
|
+
expect(request.retry).toBe(5);
|
|
61
|
+
expect(request.retryTime).toBe(2000);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it("should apply endpoint-specific defaults", () => {
|
|
65
|
+
const sdk = createSdk<TestClient, TestSchema>(client);
|
|
66
|
+
const configured = sdk.$configure({
|
|
67
|
+
"/users": { retry: 3, cache: false },
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
const usersRequest = configured.users.$get;
|
|
71
|
+
expect(usersRequest.retry).toBe(3);
|
|
72
|
+
expect(usersRequest.cache).toBe(false);
|
|
73
|
+
|
|
74
|
+
// Posts should not be affected
|
|
75
|
+
const postsRequest = configured.posts.$get;
|
|
76
|
+
expect(postsRequest.retry).toBe(0);
|
|
77
|
+
expect(postsRequest.cache).toBe(true);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it("should apply wildcard defaults to matching endpoints", () => {
|
|
81
|
+
const sdk = createSdk<TestClient, TestSchema>(client);
|
|
82
|
+
const configured = sdk.$configure({
|
|
83
|
+
"/users/*": { cancelable: true },
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
const userRequest = configured.users.$userId.$get;
|
|
87
|
+
expect(userRequest.cancelable).toBe(true);
|
|
88
|
+
|
|
89
|
+
const postsRequest = configured.posts.$get;
|
|
90
|
+
expect(postsRequest.cancelable).toBe(false);
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it("should merge global and specific defaults (specific wins)", () => {
|
|
94
|
+
const sdk = createSdk<TestClient, TestSchema>(client);
|
|
95
|
+
const configured = sdk.$configure({
|
|
96
|
+
"*": { retry: 1 },
|
|
97
|
+
"/users": { retry: 5 },
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
const usersRequest = configured.users.$get;
|
|
101
|
+
// "/users" matches both "*" and "/users"; both apply in order, so "/users" overwrites
|
|
102
|
+
expect(usersRequest.retry).toBe(5);
|
|
103
|
+
|
|
104
|
+
const postsRequest = configured.posts.$get;
|
|
105
|
+
expect(postsRequest.retry).toBe(1);
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it("should return a new SDK instance (immutable)", () => {
|
|
109
|
+
const sdk = createSdk<TestClient, TestSchema>(client);
|
|
110
|
+
const configured = sdk.$configure({ "*": { retry: 10 } });
|
|
111
|
+
|
|
112
|
+
expect(sdk.users.$get.retry).toBe(0);
|
|
113
|
+
expect(configured.users.$get.retry).toBe(10);
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it("should apply headers defaults", () => {
|
|
117
|
+
const sdk = createSdk<TestClient, TestSchema>(client);
|
|
118
|
+
const configured = sdk.$configure({
|
|
119
|
+
"*": { headers: { "X-Custom": "value" } },
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
const request = configured.users.$get;
|
|
123
|
+
expect(request.headers).toStrictEqual({ "X-Custom": "value" });
|
|
124
|
+
});
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
describe("defaults via createSdk options", () => {
|
|
128
|
+
it("should apply defaults passed through createSdk options", () => {
|
|
129
|
+
const sdk = createSdk<TestClient, TestSchema>(client, {
|
|
130
|
+
defaults: {
|
|
131
|
+
"*": { retry: 7 },
|
|
132
|
+
},
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
const request = sdk.users.$get;
|
|
136
|
+
expect(request.retry).toBe(7);
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
describe("getMethod with custom methodTransform", () => {
|
|
141
|
+
it("should use custom methodTransform when provided", () => {
|
|
142
|
+
const sdk = createSdk<TestClient, TestSchema>(client, {
|
|
143
|
+
methodTransform: (method: string) => `CUSTOM_${method}`,
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
const request = sdk.users.$get;
|
|
147
|
+
expect(request.method).toBe("CUSTOM_get");
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
it("should use default toUpperCase when no methodTransform provided", () => {
|
|
151
|
+
const sdk = createSdk<TestClient, TestSchema>(client);
|
|
152
|
+
|
|
153
|
+
const request = sdk.users.$get;
|
|
154
|
+
expect(request.method).toBe("GET");
|
|
155
|
+
});
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
describe("applyDefaults with all config properties", () => {
|
|
159
|
+
it("should apply all config properties to the request", () => {
|
|
160
|
+
const sdk = createSdk<TestClient, TestSchema>(client);
|
|
161
|
+
const configured = sdk.$configure({
|
|
162
|
+
"*": {
|
|
163
|
+
headers: { "X-Test": "value" },
|
|
164
|
+
auth: false,
|
|
165
|
+
cache: false,
|
|
166
|
+
cacheTime: 5000,
|
|
167
|
+
staleTime: 3000,
|
|
168
|
+
retry: 3,
|
|
169
|
+
retryTime: 1000,
|
|
170
|
+
cancelable: true,
|
|
171
|
+
queued: true,
|
|
172
|
+
offline: true,
|
|
173
|
+
deduplicate: true,
|
|
174
|
+
deduplicateTime: 2000,
|
|
175
|
+
},
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
const request = configured.users.$get;
|
|
179
|
+
expect(request.headers).toStrictEqual({ "X-Test": "value" });
|
|
180
|
+
expect(request.auth).toBe(false);
|
|
181
|
+
expect(request.cache).toBe(false);
|
|
182
|
+
expect(request.cacheTime).toBe(5000);
|
|
183
|
+
expect(request.staleTime).toBe(3000);
|
|
184
|
+
expect(request.retry).toBe(3);
|
|
185
|
+
expect(request.retryTime).toBe(1000);
|
|
186
|
+
expect(request.cancelable).toBe(true);
|
|
187
|
+
expect(request.queued).toBe(true);
|
|
188
|
+
expect(request.offline).toBe(true);
|
|
189
|
+
expect(request.deduplicate).toBe(true);
|
|
190
|
+
expect(request.deduplicateTime).toBe(2000);
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
it("should not apply deduplicateTime when value is null", () => {
|
|
194
|
+
const sdk = createSdk<TestClient, TestSchema>(client);
|
|
195
|
+
const configured = sdk.$configure({
|
|
196
|
+
"*": {
|
|
197
|
+
deduplicateTime: null,
|
|
198
|
+
retry: 1,
|
|
199
|
+
},
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
const request = configured.users.$get;
|
|
203
|
+
// null means "don't change" - deduplicateTime should not be set by null
|
|
204
|
+
// The branch `config.deduplicateTime !== null` prevents null from being applied
|
|
205
|
+
expect(request.retry).toBe(1);
|
|
206
|
+
});
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
describe("endpointMatchesPattern with wildcard edge case", () => {
|
|
210
|
+
it("should match endpoint equal to prefix without trailing slash", () => {
|
|
211
|
+
const sdk = createSdk<TestClient, TestSchema>(client);
|
|
212
|
+
const configured = sdk.$configure({
|
|
213
|
+
"/users/*": { retry: 9 },
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
const usersRequest = configured.users.$get;
|
|
217
|
+
expect(usersRequest.retry).toBe(9);
|
|
218
|
+
});
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
describe("$configure merging", () => {
|
|
222
|
+
it("should merge existing defaults with new ones from $configure", () => {
|
|
223
|
+
const sdk = createSdk<TestClient, TestSchema>(client, {
|
|
224
|
+
defaults: {
|
|
225
|
+
"*": { retry: 2 },
|
|
226
|
+
},
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
const configured = sdk.$configure({
|
|
230
|
+
"/users": { cache: false },
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
const usersRequest = configured.users.$get;
|
|
234
|
+
expect(usersRequest.retry).toBe(2);
|
|
235
|
+
expect(usersRequest.cache).toBe(false);
|
|
236
|
+
});
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
describe("proxy special keys", () => {
|
|
240
|
+
it("should return undefined for symbol keys", () => {
|
|
241
|
+
const sdk = createSdk<TestClient, TestSchema>(client);
|
|
242
|
+
const sym = Symbol("test");
|
|
243
|
+
expect((sdk as any)[sym]).toBeUndefined();
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
it("should return undefined for inspect key", () => {
|
|
247
|
+
const sdk = createSdk<TestClient, TestSchema>(client);
|
|
248
|
+
expect((sdk as any).inspect).toBeUndefined();
|
|
249
|
+
});
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
describe("getMethod default fallback in options without methodTransform", () => {
|
|
253
|
+
it("should use inline default toUpperCase when options exist but have no methodTransform", () => {
|
|
254
|
+
const sdk = createSdk<TestClient, TestSchema>(client, {
|
|
255
|
+
camelCaseToKebabCase: false,
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
const request = sdk.users.$get;
|
|
259
|
+
expect(request.method).toBe("GET");
|
|
260
|
+
});
|
|
261
|
+
});
|
|
262
|
+
});
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { EventEmitter } from "utils/emitter.utils";
|
|
2
|
+
|
|
3
|
+
describe("EventEmitter [ Utils ]", () => {
|
|
4
|
+
let emitter: EventEmitter;
|
|
5
|
+
|
|
6
|
+
beforeEach(() => {
|
|
7
|
+
emitter = new EventEmitter();
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
describe("When using emit with isTriggeredExternally", () => {
|
|
11
|
+
it("should emit with isTriggeredExternally=true and pass the flag to listeners", () => {
|
|
12
|
+
const spy = vi.fn();
|
|
13
|
+
emitter.on("test-event", spy);
|
|
14
|
+
|
|
15
|
+
emitter.emit("test-event", { foo: "bar" }, true);
|
|
16
|
+
|
|
17
|
+
expect(spy).toHaveBeenCalledWith({ foo: "bar" }, true);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it("should emit without isTriggeredExternally flag when false", () => {
|
|
21
|
+
const spy = vi.fn();
|
|
22
|
+
emitter.on("test-event", spy);
|
|
23
|
+
|
|
24
|
+
emitter.emit("test-event", { foo: "bar" }, false);
|
|
25
|
+
|
|
26
|
+
expect(spy).toHaveBeenCalledWith({ foo: "bar" });
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
describe("When using onEmit", () => {
|
|
31
|
+
it("should register a callback that fires on every emit", () => {
|
|
32
|
+
const callback = vi.fn();
|
|
33
|
+
emitter.onEmit(callback);
|
|
34
|
+
|
|
35
|
+
emitter.emit("some-event", "some-data", false);
|
|
36
|
+
|
|
37
|
+
expect(callback).toHaveBeenCalledTimes(1);
|
|
38
|
+
expect(callback).toHaveBeenCalledWith("some-event", "some-data");
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("should pass isTriggeredExternally to onEmit callback when true", () => {
|
|
42
|
+
const callback = vi.fn();
|
|
43
|
+
emitter.onEmit(callback);
|
|
44
|
+
|
|
45
|
+
emitter.emit("some-event", "some-data", true);
|
|
46
|
+
|
|
47
|
+
expect(callback).toHaveBeenCalledWith("some-event", "some-data", true);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it("should support multiple onEmit callbacks", () => {
|
|
51
|
+
const callback1 = vi.fn();
|
|
52
|
+
const callback2 = vi.fn();
|
|
53
|
+
emitter.onEmit(callback1);
|
|
54
|
+
emitter.onEmit(callback2);
|
|
55
|
+
|
|
56
|
+
emitter.emit("event", "data", false);
|
|
57
|
+
|
|
58
|
+
expect(callback1).toHaveBeenCalledTimes(1);
|
|
59
|
+
expect(callback2).toHaveBeenCalledTimes(1);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it("should return a cleanup function that removes the callback", () => {
|
|
63
|
+
const callback = vi.fn();
|
|
64
|
+
const cleanup = emitter.onEmit(callback);
|
|
65
|
+
|
|
66
|
+
emitter.emit("event", "data", false);
|
|
67
|
+
expect(callback).toHaveBeenCalledTimes(1);
|
|
68
|
+
|
|
69
|
+
cleanup();
|
|
70
|
+
|
|
71
|
+
emitter.emit("event", "data2", false);
|
|
72
|
+
expect(callback).toHaveBeenCalledTimes(1);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it("should only remove the specific callback when cleanup is called", () => {
|
|
76
|
+
const callback1 = vi.fn();
|
|
77
|
+
const callback2 = vi.fn();
|
|
78
|
+
const cleanup1 = emitter.onEmit(callback1);
|
|
79
|
+
emitter.onEmit(callback2);
|
|
80
|
+
|
|
81
|
+
cleanup1();
|
|
82
|
+
|
|
83
|
+
emitter.emit("event", "data", false);
|
|
84
|
+
expect(callback1).not.toHaveBeenCalled();
|
|
85
|
+
expect(callback2).toHaveBeenCalledTimes(1);
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
});
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
import type { Mock } from "vitest";
|
|
2
|
+
|
|
3
|
+
export const testCallbacksExecution = (callbacks: Array<Mock>) => {
|
|
2
4
|
callbacks.forEach((spyFn, index) => {
|
|
3
5
|
const nextSpy = callbacks[index + 1];
|
|
4
6
|
if (nextSpy) {
|
package/__tests__/tsconfig.json
CHANGED
|
@@ -1,13 +1,24 @@
|
|
|
1
1
|
{
|
|
2
2
|
"compilerOptions": {
|
|
3
|
+
"target": "esnext",
|
|
4
|
+
"module": "esnext",
|
|
5
|
+
"moduleResolution": "bundler",
|
|
6
|
+
"lib": ["dom", "dom.iterable", "ESNext"],
|
|
3
7
|
"strict": true,
|
|
8
|
+
"strictPropertyInitialization": false,
|
|
4
9
|
"noEmit": true,
|
|
5
|
-
"
|
|
6
|
-
"moduleResolution": "node",
|
|
10
|
+
"skipLibCheck": true,
|
|
7
11
|
"esModuleInterop": true,
|
|
8
12
|
"allowSyntheticDefaultImports": true,
|
|
13
|
+
"resolveJsonModule": true,
|
|
14
|
+
"isolatedModules": true,
|
|
15
|
+
"noUnusedLocals": false,
|
|
16
|
+
"noUnusedParameters": false,
|
|
17
|
+
"types": ["vitest/globals", "jest-extended", "@types/node"],
|
|
18
|
+
"baseUrl": "../src",
|
|
9
19
|
"paths": {
|
|
10
20
|
"@hyper-fetch/core": ["../src"]
|
|
11
21
|
}
|
|
12
|
-
}
|
|
22
|
+
},
|
|
23
|
+
"include": ["**/*.ts", "**/*.tsx", "../../../global.d.ts", "../../../vitest.d.ts"]
|
|
13
24
|
}
|