@hyper-fetch/react 7.5.2 → 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__/features/helpers/use-request-events/use-request-events.base.spec.ts +115 -0
- package/__tests__/features/helpers/use-request-events/use-request-events.utils.spec.ts +69 -0
- package/__tests__/features/helpers/use-socket-state/use-socket-state.base.spec.ts +213 -0
- package/__tests__/features/helpers/use-tracked-state/use-tacked-state.actions.spec.ts +550 -0
- package/__tests__/features/helpers/use-tracked-state/use-tracked-state.base.spec.ts +566 -0
- package/__tests__/features/helpers/use-tracked-state/use-tracked-state.utils.spec.ts +260 -0
- package/__tests__/features/provider/provider.hydration.spec.tsx +165 -0
- package/__tests__/features/provider/use-config-provider.base.spec.tsx +69 -0
- package/__tests__/features/use-app-manager/use-app-manager.base.spec.ts +85 -0
- package/__tests__/features/use-cache/use-cache.base.spec.ts +136 -0
- package/__tests__/features/use-cache/use-cache.helpers.ts +40 -0
- package/__tests__/features/use-cache/use-cache.typescript.spec.ts +111 -0
- package/__tests__/features/use-emitter/use-emitter.base.spec.ts +124 -0
- package/__tests__/features/use-fetch/use-fetch.base.spec.ts +372 -0
- package/__tests__/features/use-fetch/use-fetch.cancel.spec.ts +83 -0
- package/__tests__/features/use-fetch/use-fetch.clear-state.spec.ts +82 -0
- package/__tests__/features/use-fetch/use-fetch.concurrency.spec.ts +91 -0
- package/__tests__/features/use-fetch/use-fetch.debounce.spec.ts +153 -0
- package/__tests__/features/use-fetch/use-fetch.deduplication.spec.ts +80 -0
- package/__tests__/features/use-fetch/use-fetch.infinite.ts +39 -0
- package/__tests__/features/use-fetch/use-fetch.integration.spec.tsx +172 -0
- package/__tests__/features/use-fetch/use-fetch.keep-previous-data.spec.ts +215 -0
- package/__tests__/features/use-fetch/use-fetch.methods.spec.ts +233 -0
- package/__tests__/features/use-fetch/use-fetch.offline.spec.ts +103 -0
- package/__tests__/features/use-fetch/use-fetch.pagination.ts +36 -0
- package/__tests__/features/use-fetch/use-fetch.prefetch.spec.tsx +73 -0
- package/__tests__/features/use-fetch/use-fetch.refresh.spec.ts +187 -0
- package/__tests__/features/use-fetch/use-fetch.rerender.spec.tsx +151 -0
- package/__tests__/features/use-fetch/use-fetch.retry.ts +38 -0
- package/__tests__/features/use-fetch/use-fetch.revalidate.spec.ts +237 -0
- package/__tests__/features/use-fetch/use-fetch.suspense.spec.tsx +505 -0
- package/__tests__/features/use-fetch/use-fetch.typescript.spec.ts +182 -0
- package/__tests__/features/use-fetch/use-fetch.utils.spec.ts +26 -0
- package/__tests__/features/use-fetch/use-fetch.window.spec.ts +131 -0
- package/__tests__/features/use-listener/use-listener.base.spec.ts +57 -0
- package/__tests__/features/use-queue/use-queue.actions.spec.ts +238 -0
- package/__tests__/features/use-queue/use-queue.base.spec.ts +367 -0
- package/__tests__/features/use-queue/use-queue.resolve.spec.ts +173 -0
- package/__tests__/features/use-stream/use-stream.base.spec.ts +481 -0
- package/__tests__/features/use-submit/use-submit.base.spec.ts +76 -0
- package/__tests__/features/use-submit/use-submit.cancel.spec.ts +46 -0
- package/__tests__/features/use-submit/use-submit.concurrency.spec.ts +62 -0
- package/__tests__/features/use-submit/use-submit.debounce.spec.ts +320 -0
- package/__tests__/features/use-submit/use-submit.methods.spec.ts +276 -0
- package/__tests__/features/use-submit/use-submit.optimistic.spec.ts +548 -0
- package/__tests__/features/use-submit/use-submit.queue.spec.ts +84 -0
- package/__tests__/features/use-submit/use-submit.retry.spec.ts +92 -0
- package/__tests__/features/use-submit/use-submit.revalidate.spec.ts +99 -0
- package/__tests__/features/use-submit/use-submit.submit.spec.ts +234 -0
- package/__tests__/features/use-submit/use-submit.typescript.spec.ts +325 -0
- package/__tests__/shared/index.ts +2 -0
- package/__tests__/shared/isolation.tests.ts +12 -0
- package/__tests__/shared/response.tests.ts +128 -0
- package/__tests__/tsconfig.json +27 -0
- package/__tests__/utils/cache.utils.ts +55 -0
- package/__tests__/utils/client.utils.ts +3 -0
- package/__tests__/utils/emitter.utils.ts +8 -0
- package/__tests__/utils/helpers.utils.ts +9 -0
- package/__tests__/utils/index.ts +14 -0
- package/__tests__/utils/listener.utils.ts +11 -0
- package/__tests__/utils/request.utils.ts +12 -0
- package/__tests__/utils/state.utils.ts +7 -0
- package/__tests__/utils/use-app-manager.utils.ts +10 -0
- package/__tests__/utils/use-cache.utils.ts +12 -0
- package/__tests__/utils/use-emitter.utils.ts +12 -0
- package/__tests__/utils/use-fetch.utils.ts +12 -0
- package/__tests__/utils/use-listener.utils.ts +12 -0
- package/__tests__/utils/use-queue.utils.ts +70 -0
- package/__tests__/utils/use-request-events.utils.ts +43 -0
- package/__tests__/utils/use-socket-state.utils.ts +12 -0
- package/__tests__/utils/use-submit.utils.ts +12 -0
- package/__tests__/utils/use-tracked-state.utils.ts +27 -0
- package/__tests__/vitest.setup.ts +8 -0
- package/dist/helpers/index.d.ts +4 -0
- package/dist/helpers/index.d.ts.map +1 -0
- package/dist/helpers/use-request-events/index.d.ts +3 -0
- package/dist/helpers/use-request-events/index.d.ts.map +1 -0
- package/dist/helpers/use-request-events/use-request-events.hooks.d.ts +10 -0
- package/dist/helpers/use-request-events/use-request-events.hooks.d.ts.map +1 -0
- package/dist/helpers/use-request-events/use-request-events.types.d.ts +78 -0
- package/dist/helpers/use-request-events/use-request-events.types.d.ts.map +1 -0
- package/dist/helpers/use-socket-state/index.d.ts +4 -0
- package/dist/helpers/use-socket-state/index.d.ts.map +1 -0
- package/dist/helpers/use-socket-state/use-socket-state.constants.d.ts +4 -0
- package/dist/helpers/use-socket-state/use-socket-state.constants.d.ts.map +1 -0
- package/dist/helpers/use-socket-state/use-socket-state.hooks.d.ts +24 -0
- package/dist/helpers/use-socket-state/use-socket-state.hooks.d.ts.map +1 -0
- package/dist/helpers/use-socket-state/use-socket-state.types.d.ts +12 -0
- package/dist/helpers/use-socket-state/use-socket-state.types.d.ts.map +1 -0
- package/dist/helpers/use-tracked-state/index.d.ts +5 -0
- package/dist/helpers/use-tracked-state/index.d.ts.map +1 -0
- package/dist/helpers/use-tracked-state/use-tracked-state.constants.d.ts +3 -0
- package/dist/helpers/use-tracked-state/use-tracked-state.constants.d.ts.map +1 -0
- package/dist/helpers/use-tracked-state/use-tracked-state.hooks.d.ts +12 -0
- package/dist/helpers/use-tracked-state/use-tracked-state.hooks.d.ts.map +1 -0
- package/dist/helpers/use-tracked-state/use-tracked-state.types.d.ts +113 -0
- package/dist/helpers/use-tracked-state/use-tracked-state.types.d.ts.map +1 -0
- package/dist/helpers/use-tracked-state/use-tracked-state.utils.d.ts +29 -0
- package/dist/helpers/use-tracked-state/use-tracked-state.utils.d.ts.map +1 -0
- package/dist/hooks/use-app-manager/index.d.ts +3 -0
- package/dist/hooks/use-app-manager/index.d.ts.map +1 -0
- package/dist/hooks/use-app-manager/use-app-manager.hooks.d.ts +4 -0
- package/dist/hooks/use-app-manager/use-app-manager.hooks.d.ts.map +1 -0
- package/dist/hooks/use-app-manager/use-app-manager.types.d.ts +19 -0
- package/dist/hooks/use-app-manager/use-app-manager.types.d.ts.map +1 -0
- package/dist/hooks/use-cache/index.d.ts +4 -0
- package/dist/hooks/use-cache/index.d.ts.map +1 -0
- package/dist/hooks/use-cache/use-cache.constants.d.ts +6 -0
- package/dist/hooks/use-cache/use-cache.constants.d.ts.map +1 -0
- package/dist/hooks/use-cache/use-cache.hooks.d.ts +4 -0
- package/dist/hooks/use-cache/use-cache.hooks.d.ts.map +1 -0
- package/dist/hooks/use-cache/use-cache.types.d.ts +24 -0
- package/dist/hooks/use-cache/use-cache.types.d.ts.map +1 -0
- package/dist/hooks/use-emitter/index.d.ts +3 -0
- package/dist/hooks/use-emitter/index.d.ts.map +1 -0
- package/dist/hooks/use-emitter/use-emitter.hooks.d.ts +26 -0
- package/dist/hooks/use-emitter/use-emitter.hooks.d.ts.map +1 -0
- package/dist/hooks/use-emitter/use-emitter.types.d.ts +4 -0
- package/dist/hooks/use-emitter/use-emitter.types.d.ts.map +1 -0
- package/dist/hooks/use-fetch/index.d.ts +5 -0
- package/dist/hooks/use-fetch/index.d.ts.map +1 -0
- package/dist/hooks/use-fetch/use-fetch.constants.d.ts +8 -0
- package/dist/hooks/use-fetch/use-fetch.constants.d.ts.map +1 -0
- package/dist/hooks/use-fetch/use-fetch.hooks.d.ts +10 -0
- package/dist/hooks/use-fetch/use-fetch.hooks.d.ts.map +1 -0
- package/dist/hooks/use-fetch/use-fetch.types.d.ts +118 -0
- package/dist/hooks/use-fetch/use-fetch.types.d.ts.map +1 -0
- package/dist/hooks/use-fetch/use-fetch.utils.d.ts +2 -0
- package/dist/hooks/use-fetch/use-fetch.utils.d.ts.map +1 -0
- package/dist/hooks/use-listener/index.d.ts +3 -0
- package/dist/hooks/use-listener/index.d.ts.map +1 -0
- package/dist/hooks/use-listener/use-listener.hooks.d.ts +31 -0
- package/dist/hooks/use-listener/use-listener.hooks.d.ts.map +1 -0
- package/dist/hooks/use-listener/use-listener.types.d.ts +4 -0
- package/dist/hooks/use-listener/use-listener.types.d.ts.map +1 -0
- package/dist/hooks/use-queue/index.d.ts +4 -0
- package/dist/hooks/use-queue/index.d.ts.map +1 -0
- package/dist/hooks/use-queue/use-queue.constants.d.ts +4 -0
- package/dist/hooks/use-queue/use-queue.constants.d.ts.map +1 -0
- package/dist/hooks/use-queue/use-queue.hooks.d.ts +10 -0
- package/dist/hooks/use-queue/use-queue.hooks.d.ts.map +1 -0
- package/dist/hooks/use-queue/use-queue.types.d.ts +58 -0
- package/dist/hooks/use-queue/use-queue.types.d.ts.map +1 -0
- package/dist/hooks/use-stream/index.d.ts +3 -0
- package/dist/hooks/use-stream/index.d.ts.map +1 -0
- package/dist/hooks/use-stream/use-stream.hooks.d.ts +11 -0
- package/dist/hooks/use-stream/use-stream.hooks.d.ts.map +1 -0
- package/dist/hooks/use-stream/use-stream.types.d.ts +52 -0
- package/dist/hooks/use-stream/use-stream.types.d.ts.map +1 -0
- package/dist/hooks/use-submit/index.d.ts +4 -0
- package/dist/hooks/use-submit/index.d.ts.map +1 -0
- package/dist/hooks/use-submit/use-submit.constants.d.ts +8 -0
- package/dist/hooks/use-submit/use-submit.constants.d.ts.map +1 -0
- package/dist/hooks/use-submit/use-submit.hooks.d.ts +10 -0
- package/dist/hooks/use-submit/use-submit.hooks.d.ts.map +1 -0
- package/dist/hooks/use-submit/use-submit.types.d.ts +115 -0
- package/dist/hooks/use-submit/use-submit.types.d.ts.map +1 -0
- package/dist/index.d.ts +12 -733
- package/dist/index.d.ts.map +1 -0
- package/dist/index.mjs +1935 -0
- package/dist/index.mjs.map +1 -0
- package/dist/provider/index.d.ts +3 -0
- package/dist/provider/index.d.ts.map +1 -0
- package/dist/provider/provider.d.ts +13 -0
- package/dist/provider/provider.d.ts.map +1 -0
- package/dist/provider/provider.types.d.ts +31 -0
- package/dist/provider/provider.types.d.ts.map +1 -0
- package/dist/utils/bounce.utils.d.ts +8 -0
- package/dist/utils/bounce.utils.d.ts.map +1 -0
- package/dist/utils/deep-equal.utils.d.ts +14 -0
- package/dist/utils/deep-equal.utils.d.ts.map +1 -0
- package/dist/utils/index.d.ts +4 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/tracked-proxy.utils.d.ts +7 -0
- package/dist/utils/tracked-proxy.utils.d.ts.map +1 -0
- package/package.json +21 -13
- package/project.json +4 -0
- package/src/helpers/use-request-events/use-request-events.hooks.ts +60 -14
- package/src/helpers/use-request-events/use-request-events.types.ts +19 -12
- package/src/helpers/use-socket-state/use-socket-state.constants.ts +2 -2
- package/src/helpers/use-socket-state/use-socket-state.hooks.ts +47 -13
- package/src/helpers/use-socket-state/use-socket-state.types.ts +1 -1
- package/src/helpers/use-tracked-state/use-tracked-state.constants.ts +1 -1
- package/src/helpers/use-tracked-state/use-tracked-state.hooks.ts +74 -22
- package/src/helpers/use-tracked-state/use-tracked-state.types.ts +7 -2
- package/src/helpers/use-tracked-state/use-tracked-state.utils.ts +39 -4
- package/src/hooks/use-app-manager/use-app-manager.hooks.ts +2 -2
- package/src/hooks/use-cache/use-cache.hooks.ts +33 -41
- package/src/hooks/use-cache/use-cache.types.ts +3 -3
- package/src/hooks/use-emitter/use-emitter.hooks.ts +17 -16
- package/src/hooks/use-fetch/use-fetch.constants.ts +5 -2
- package/src/hooks/use-fetch/use-fetch.hooks.ts +109 -51
- package/src/hooks/use-fetch/use-fetch.types.ts +18 -3
- package/src/hooks/use-listener/use-listener.hooks.ts +19 -27
- package/src/hooks/use-queue/use-queue.constants.ts +2 -2
- package/src/hooks/use-queue/use-queue.hooks.ts +25 -6
- package/src/hooks/use-queue/use-queue.types.ts +8 -2
- package/src/hooks/use-stream/index.ts +2 -0
- package/src/hooks/use-stream/use-stream.hooks.ts +164 -0
- package/src/hooks/use-stream/use-stream.types.ts +53 -0
- package/src/hooks/use-submit/use-submit.constants.ts +2 -2
- package/src/hooks/use-submit/use-submit.hooks.ts +43 -48
- package/src/hooks/use-submit/use-submit.types.ts +9 -9
- package/src/index.ts +1 -0
- package/src/provider/provider.tsx +1 -1
- package/src/provider/provider.types.ts +8 -8
- package/src/utils/index.ts +1 -0
- package/src/utils/tracked-proxy.utils.ts +19 -0
- package/tsconfig.json +1 -1
- package/vite.config.ts +45 -0
- package/.eslintrc.json +0 -11
- package/dist/index.js +0 -1541
- package/dist/index.js.map +0 -7
package/.eslintrc.cjs
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
module.exports = {
|
|
2
|
+
extends: ["plugin:@nx/react", "../../.eslintrc.js"],
|
|
3
|
+
parserOptions: {
|
|
4
|
+
tsconfigRootDir: __dirname,
|
|
5
|
+
project: ["./tsconfig.json", "./__tests__/tsconfig.json"],
|
|
6
|
+
},
|
|
7
|
+
rules: {
|
|
8
|
+
"react/jsx-props-no-spreading": 0,
|
|
9
|
+
"react/require-default-props": 0,
|
|
10
|
+
},
|
|
11
|
+
ignorePatterns: ["dist/", "coverage/", "vite.config.ts", "/*.js"],
|
|
12
|
+
};
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { act } from "@testing-library/react";
|
|
2
|
+
import { createClient } from "@hyper-fetch/core";
|
|
3
|
+
import { createHttpMockingServer } from "@hyper-fetch/testing";
|
|
4
|
+
|
|
5
|
+
import { renderUseRequestEvents } from "../../../utils";
|
|
6
|
+
|
|
7
|
+
const { resetMocks, startServer, stopServer } = createHttpMockingServer();
|
|
8
|
+
|
|
9
|
+
describe("useRequestEvents [ Base ]", () => {
|
|
10
|
+
let client = createClient({
|
|
11
|
+
url: "http://localhost:3000",
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
let request = client.createRequest()({
|
|
15
|
+
endpoint: "/shared-endpoint",
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
beforeAll(() => {
|
|
19
|
+
startServer();
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
afterEach(() => {
|
|
23
|
+
resetMocks();
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
afterAll(() => {
|
|
27
|
+
stopServer();
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
beforeEach(() => {
|
|
31
|
+
client = createClient({
|
|
32
|
+
url: "http://localhost:3000",
|
|
33
|
+
});
|
|
34
|
+
request = client.createRequest()({
|
|
35
|
+
endpoint: "/shared-endpoint",
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
vi.resetModules();
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
describe("When using the useRequestEvents", () => {
|
|
42
|
+
it("should initialize with default state", async () => {
|
|
43
|
+
const { result } = renderUseRequestEvents(request);
|
|
44
|
+
expect(result.current).toBeDefined();
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it("should not change loading state during cache processing", async () => {
|
|
48
|
+
const { result, rerender } = renderUseRequestEvents(request, {
|
|
49
|
+
getIsDataProcessing: () => false,
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
const spy = vi.spyOn(request.client.fetchDispatcher, "hasRunningRequests");
|
|
53
|
+
|
|
54
|
+
act(() => {
|
|
55
|
+
result.current[1].addCacheDataListener(request);
|
|
56
|
+
request.client.requestManager.events.emitLoading({
|
|
57
|
+
request,
|
|
58
|
+
requestId: "1",
|
|
59
|
+
loading: true,
|
|
60
|
+
isRetry: false,
|
|
61
|
+
isOffline: false,
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
66
|
+
|
|
67
|
+
rerender({
|
|
68
|
+
getIsDataProcessing: () => true,
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
act(() => {
|
|
72
|
+
result.current[1].addCacheDataListener(request);
|
|
73
|
+
request.client.requestManager.events.emitLoading({
|
|
74
|
+
request,
|
|
75
|
+
requestId: "1",
|
|
76
|
+
loading: true,
|
|
77
|
+
isRetry: false,
|
|
78
|
+
isOffline: false,
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
// No record of the new call
|
|
83
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it("should abort requests with matching abort key", async () => {
|
|
87
|
+
const { result } = renderUseRequestEvents(request);
|
|
88
|
+
|
|
89
|
+
// Create a spy on the dispatcher's delete method
|
|
90
|
+
const deleteSpy = vi.spyOn(request.client.fetchDispatcher, "delete");
|
|
91
|
+
|
|
92
|
+
act(() => {
|
|
93
|
+
request.client.fetchDispatcher.addRunningRequest(request.queryKey, "1", request);
|
|
94
|
+
result.current[0].abort();
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
expect(deleteSpy).toHaveBeenCalled();
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it("should not abort requests if abort key does not match", async () => {
|
|
101
|
+
const { result } = renderUseRequestEvents(request);
|
|
102
|
+
|
|
103
|
+
// Create a spy on the dispatcher's delete method
|
|
104
|
+
const deleteSpy = vi.spyOn(request.client.fetchDispatcher, "delete");
|
|
105
|
+
const clone = request.clone().setAbortKey("completely-different-key");
|
|
106
|
+
|
|
107
|
+
act(() => {
|
|
108
|
+
request.client.fetchDispatcher.addRunningRequest(clone.queryKey, "1234", clone);
|
|
109
|
+
result.current[0].abort();
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
expect(deleteSpy).not.toHaveBeenCalled();
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
});
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { act } from "@testing-library/react";
|
|
2
|
+
import { createHttpMockingServer } from "@hyper-fetch/testing";
|
|
3
|
+
|
|
4
|
+
import { createRequest, renderUseRequestEvents } from "../../../utils";
|
|
5
|
+
|
|
6
|
+
const { resetMocks, startServer, stopServer, mockRequest } = createHttpMockingServer();
|
|
7
|
+
|
|
8
|
+
describe("useRequestEvents [ Utils ]", () => {
|
|
9
|
+
let request = createRequest();
|
|
10
|
+
|
|
11
|
+
beforeAll(() => {
|
|
12
|
+
startServer();
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
afterEach(() => {
|
|
16
|
+
resetMocks();
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
afterAll(() => {
|
|
20
|
+
stopServer();
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
beforeEach(() => {
|
|
24
|
+
vi.resetModules();
|
|
25
|
+
request = createRequest();
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
describe("When handling lifecycle events", () => {
|
|
29
|
+
it("should not throw when removing non existing event", async () => {
|
|
30
|
+
const response = renderUseRequestEvents(request);
|
|
31
|
+
|
|
32
|
+
expect(response.result.current[1].removeLifecycleListener).not.toThrow();
|
|
33
|
+
});
|
|
34
|
+
it("should unmount lifecycle events when handling requests by queue/cache keys", async () => {
|
|
35
|
+
const spy = vi.fn();
|
|
36
|
+
mockRequest(request);
|
|
37
|
+
const response = renderUseRequestEvents(request);
|
|
38
|
+
|
|
39
|
+
await act(async () => {
|
|
40
|
+
response.result.current[0].onRequestStart(spy);
|
|
41
|
+
response.result.current[1].addLifecycleListeners(request);
|
|
42
|
+
response.result.current[1].addLifecycleListeners(request);
|
|
43
|
+
response.result.current[1].addLifecycleListeners(request);
|
|
44
|
+
|
|
45
|
+
await request.send({});
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
49
|
+
});
|
|
50
|
+
it("should listen to every request id events", async () => {
|
|
51
|
+
const spy = vi.fn();
|
|
52
|
+
mockRequest(request);
|
|
53
|
+
const response = renderUseRequestEvents(request);
|
|
54
|
+
|
|
55
|
+
await act(async () => {
|
|
56
|
+
await request.send({
|
|
57
|
+
onBeforeSent: ({ requestId }) => {
|
|
58
|
+
response.result.current[0].onRequestStart(spy);
|
|
59
|
+
response.result.current[1].addLifecycleListeners(request, requestId);
|
|
60
|
+
response.result.current[1].addLifecycleListeners(request, requestId);
|
|
61
|
+
response.result.current[1].addLifecycleListeners(request, requestId);
|
|
62
|
+
},
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
expect(spy).toHaveBeenCalledTimes(3);
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
});
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
import { act } from "@testing-library/react";
|
|
2
|
+
import { Socket } from "@hyper-fetch/sockets";
|
|
3
|
+
import { createWebsocketMockingServer } from "@hyper-fetch/testing";
|
|
4
|
+
|
|
5
|
+
import { renderUseSocketState } from "../../../utils/use-socket-state.utils";
|
|
6
|
+
|
|
7
|
+
describe("useSocketState [ Base ]", () => {
|
|
8
|
+
const spy = vi.fn();
|
|
9
|
+
const { url, startServer, stopServer } = createWebsocketMockingServer();
|
|
10
|
+
const socket = new Socket({ url });
|
|
11
|
+
let view = renderUseSocketState(socket);
|
|
12
|
+
|
|
13
|
+
beforeEach(async () => {
|
|
14
|
+
view = renderUseSocketState(socket);
|
|
15
|
+
vi.resetModules();
|
|
16
|
+
vi.resetAllMocks();
|
|
17
|
+
startServer();
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
afterEach(() => {
|
|
21
|
+
stopServer();
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
describe("when using actions", () => {
|
|
25
|
+
it("should set state with data", async () => {
|
|
26
|
+
const value = 1;
|
|
27
|
+
const [state, actions] = view.result.current;
|
|
28
|
+
act(() => {
|
|
29
|
+
actions.setData(value);
|
|
30
|
+
});
|
|
31
|
+
expect(state.data).toBe(value);
|
|
32
|
+
});
|
|
33
|
+
it("should set state with timestamp", async () => {
|
|
34
|
+
const value = 1;
|
|
35
|
+
const [state, actions] = view.result.current;
|
|
36
|
+
act(() => {
|
|
37
|
+
actions.setTimestamp(value);
|
|
38
|
+
});
|
|
39
|
+
expect(state.timestamp).toBe(value);
|
|
40
|
+
});
|
|
41
|
+
it("should set state with connected value", async () => {
|
|
42
|
+
const value = true;
|
|
43
|
+
const [state, actions] = view.result.current;
|
|
44
|
+
act(() => {
|
|
45
|
+
actions.setConnected(value);
|
|
46
|
+
});
|
|
47
|
+
expect(state.connected).toBe(value);
|
|
48
|
+
});
|
|
49
|
+
it("should set state with connecting value", async () => {
|
|
50
|
+
const value = true;
|
|
51
|
+
const [state, actions] = view.result.current;
|
|
52
|
+
act(() => {
|
|
53
|
+
actions.setConnecting(value);
|
|
54
|
+
});
|
|
55
|
+
expect(state.connecting).toBe(value);
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
describe("when using callbacks", () => {
|
|
59
|
+
it("should call onConnected callback", async () => {
|
|
60
|
+
const [, , callbacks] = view.result.current;
|
|
61
|
+
act(() => {
|
|
62
|
+
socket.events.emitConnected();
|
|
63
|
+
callbacks.onConnected(spy);
|
|
64
|
+
socket.events.emitConnected();
|
|
65
|
+
});
|
|
66
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
67
|
+
});
|
|
68
|
+
it("should call onDisconnected callback", async () => {
|
|
69
|
+
const [, , callbacks] = view.result.current;
|
|
70
|
+
act(() => {
|
|
71
|
+
socket.events.emitDisconnected();
|
|
72
|
+
callbacks.onDisconnected(spy);
|
|
73
|
+
socket.events.emitDisconnected();
|
|
74
|
+
});
|
|
75
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
76
|
+
});
|
|
77
|
+
it("should call onError callback", async () => {
|
|
78
|
+
const value = {
|
|
79
|
+
error: new Error("Test error"),
|
|
80
|
+
};
|
|
81
|
+
const [, , callbacks] = view.result.current;
|
|
82
|
+
act(() => {
|
|
83
|
+
socket.events.emitError(value);
|
|
84
|
+
callbacks.onError(spy);
|
|
85
|
+
socket.events.emitError(value);
|
|
86
|
+
});
|
|
87
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
88
|
+
expect(spy).toHaveBeenCalledWith(value);
|
|
89
|
+
});
|
|
90
|
+
it("should call onConnecting callback", async () => {
|
|
91
|
+
const [, , callbacks] = view.result.current;
|
|
92
|
+
act(() => {
|
|
93
|
+
socket.events.emitConnecting({ connecting: true });
|
|
94
|
+
callbacks.onConnecting(spy);
|
|
95
|
+
socket.events.emitConnecting({ connecting: true });
|
|
96
|
+
});
|
|
97
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
98
|
+
});
|
|
99
|
+
it("should call onReconnecting callback", async () => {
|
|
100
|
+
const value = 2;
|
|
101
|
+
const [, , callbacks] = view.result.current;
|
|
102
|
+
act(() => {
|
|
103
|
+
socket.events.emitReconnecting({ attempts: value });
|
|
104
|
+
callbacks.onReconnecting(spy);
|
|
105
|
+
socket.events.emitReconnecting({ attempts: value });
|
|
106
|
+
});
|
|
107
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
108
|
+
expect(spy).toHaveBeenCalledWith({ attempts: value });
|
|
109
|
+
});
|
|
110
|
+
it("should call onReconnectingFailed callback", async () => {
|
|
111
|
+
const value = 2;
|
|
112
|
+
const [, , callbacks] = view.result.current;
|
|
113
|
+
act(() => {
|
|
114
|
+
socket.events.emitReconnectingFailed({ attempts: value });
|
|
115
|
+
callbacks.onReconnectingFailed(spy);
|
|
116
|
+
socket.events.emitReconnectingFailed({ attempts: value });
|
|
117
|
+
});
|
|
118
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
119
|
+
expect(spy).toHaveBeenCalledWith({ attempts: value });
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
describe("when using dependency tracking", () => {
|
|
124
|
+
it("should not trigger re-render when key is not tracked", async () => {
|
|
125
|
+
const renderSpy = vi.fn();
|
|
126
|
+
view = renderUseSocketState(socket, { onRender: renderSpy });
|
|
127
|
+
const [, actions] = view.result.current;
|
|
128
|
+
|
|
129
|
+
renderSpy.mockClear(); // Clear initial render count
|
|
130
|
+
|
|
131
|
+
act(() => {
|
|
132
|
+
actions.setData(123);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
expect(renderSpy).not.toHaveBeenCalled();
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it("should trigger re-render when key is tracked", async () => {
|
|
139
|
+
const renderSpy = vi.fn();
|
|
140
|
+
view = renderUseSocketState(socket, { onRender: renderSpy });
|
|
141
|
+
const [, actions, , { setRenderKey }] = view.result.current;
|
|
142
|
+
|
|
143
|
+
act(() => {
|
|
144
|
+
setRenderKey("data");
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
renderSpy.mockClear(); // Clear initial render count
|
|
148
|
+
|
|
149
|
+
act(() => {
|
|
150
|
+
actions.setData(123);
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
expect(renderSpy).toHaveBeenCalledTimes(1);
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
it("should not add duplicate render keys", async () => {
|
|
157
|
+
const renderSpy = vi.fn();
|
|
158
|
+
view = renderUseSocketState(socket, { onRender: renderSpy });
|
|
159
|
+
const [, , , { setRenderKey }] = view.result.current;
|
|
160
|
+
|
|
161
|
+
act(() => {
|
|
162
|
+
setRenderKey("data");
|
|
163
|
+
setRenderKey("data"); // Adding same key twice
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
renderSpy.mockClear(); // Clear initial render count
|
|
167
|
+
|
|
168
|
+
act(() => {
|
|
169
|
+
view.result.current[1].setData(123);
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
expect(renderSpy).toHaveBeenCalledTimes(1);
|
|
173
|
+
});
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
describe("when using clearState", () => {
|
|
177
|
+
it("should reset all state to initial values", async () => {
|
|
178
|
+
view = renderUseSocketState(socket);
|
|
179
|
+
const [, actions, , { setRenderKey }] = view.result.current;
|
|
180
|
+
|
|
181
|
+
act(() => {
|
|
182
|
+
setRenderKey("data");
|
|
183
|
+
setRenderKey("extra");
|
|
184
|
+
setRenderKey("connected");
|
|
185
|
+
setRenderKey("connecting");
|
|
186
|
+
setRenderKey("timestamp");
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
act(() => {
|
|
190
|
+
actions.setData("some-data");
|
|
191
|
+
actions.setExtra({ test: true } as any);
|
|
192
|
+
actions.setConnected(true);
|
|
193
|
+
actions.setConnecting(true);
|
|
194
|
+
actions.setTimestamp(12345);
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
expect(view.result.current[0].data).toBe("some-data");
|
|
198
|
+
expect(view.result.current[0].connected).toBe(true);
|
|
199
|
+
expect(view.result.current[0].connecting).toBe(true);
|
|
200
|
+
expect(view.result.current[0].timestamp).toBe(12345);
|
|
201
|
+
|
|
202
|
+
act(() => {
|
|
203
|
+
view.result.current[1].clearState();
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
expect(view.result.current[0].data).toBe(null);
|
|
207
|
+
expect(view.result.current[0].extra).toBe(null);
|
|
208
|
+
expect(view.result.current[0].connected).toBe(false);
|
|
209
|
+
expect(view.result.current[0].connecting).toBe(false);
|
|
210
|
+
expect(view.result.current[0].timestamp).toBe(null);
|
|
211
|
+
});
|
|
212
|
+
});
|
|
213
|
+
});
|