@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
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { act } from "@testing-library/react";
|
|
2
|
+
import type { CacheValueType } from "@hyper-fetch/core";
|
|
3
|
+
import { createHttpMockingServer } from "@hyper-fetch/testing";
|
|
4
|
+
|
|
5
|
+
import { client, createRequest } from "../../utils";
|
|
6
|
+
import { renderUseCache } from "../../utils/use-cache.utils";
|
|
7
|
+
import { testInitialState, testSuccessState } from "../../shared";
|
|
8
|
+
|
|
9
|
+
const { resetMocks, startServer, stopServer } = createHttpMockingServer();
|
|
10
|
+
|
|
11
|
+
describe("useCache [ Base ]", () => {
|
|
12
|
+
let request = createRequest();
|
|
13
|
+
const cacheData: CacheValueType = {
|
|
14
|
+
data: { test: 1 },
|
|
15
|
+
error: null,
|
|
16
|
+
status: 200,
|
|
17
|
+
success: true,
|
|
18
|
+
extra: {
|
|
19
|
+
headers: {
|
|
20
|
+
"content-type": "application/json",
|
|
21
|
+
},
|
|
22
|
+
},
|
|
23
|
+
retries: 0,
|
|
24
|
+
requestTimestamp: +new Date(),
|
|
25
|
+
responseTimestamp: +new Date(),
|
|
26
|
+
addedTimestamp: +new Date(),
|
|
27
|
+
triggerTimestamp: +new Date(),
|
|
28
|
+
cacheKey: request.cacheKey,
|
|
29
|
+
scope: request.scope,
|
|
30
|
+
isCanceled: false,
|
|
31
|
+
isOffline: false,
|
|
32
|
+
willRetry: false,
|
|
33
|
+
staleTime: request.staleTime,
|
|
34
|
+
version: request.client.cache.version,
|
|
35
|
+
cacheTime: Infinity,
|
|
36
|
+
cached: true,
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
beforeAll(() => {
|
|
40
|
+
startServer();
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
afterEach(() => {
|
|
44
|
+
resetMocks();
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
afterAll(() => {
|
|
48
|
+
stopServer();
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
beforeEach(() => {
|
|
52
|
+
vi.resetModules();
|
|
53
|
+
client.clear();
|
|
54
|
+
request = createRequest();
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
describe("given hook is mounting", () => {
|
|
58
|
+
describe("when cache data read is pending", () => {
|
|
59
|
+
it("should initialize with non-loading state", async () => {
|
|
60
|
+
const { result } = renderUseCache(request);
|
|
61
|
+
|
|
62
|
+
expect(result.current.loading).toBeFalse();
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
describe("given cache is empty", () => {
|
|
67
|
+
describe("when reading the state", () => {
|
|
68
|
+
it("should return empty state", async () => {
|
|
69
|
+
const response = renderUseCache(request);
|
|
70
|
+
|
|
71
|
+
testInitialState(response, false);
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
describe("given cache is present", () => {
|
|
76
|
+
describe("when reading the state", () => {
|
|
77
|
+
it("should return state", async () => {
|
|
78
|
+
client.cache.set(request, cacheData);
|
|
79
|
+
const response = renderUseCache(request);
|
|
80
|
+
|
|
81
|
+
await testSuccessState(cacheData.data, response);
|
|
82
|
+
expect(+(response.result.current?.responseTimestamp || 0)).toBe(cacheData.responseTimestamp);
|
|
83
|
+
expect(+(response.result.current?.requestTimestamp || 0)).toBe(cacheData.requestTimestamp);
|
|
84
|
+
expect(response.result.current.retries).toBe(0);
|
|
85
|
+
});
|
|
86
|
+
it("should allow to invalidate by Request", async () => {
|
|
87
|
+
const spy = vi.spyOn(client.cache, "invalidate");
|
|
88
|
+
|
|
89
|
+
const { result } = renderUseCache(request);
|
|
90
|
+
|
|
91
|
+
act(() => {
|
|
92
|
+
result.current.invalidate(request);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
96
|
+
expect(spy).toHaveBeenCalledWith(request);
|
|
97
|
+
});
|
|
98
|
+
it("should allow to invalidate by RegExp", async () => {
|
|
99
|
+
const spy = vi.spyOn(client.cache, "invalidate");
|
|
100
|
+
|
|
101
|
+
const { result } = renderUseCache(request);
|
|
102
|
+
|
|
103
|
+
act(() => {
|
|
104
|
+
result.current.invalidate(new RegExp(request.cacheKey));
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
108
|
+
expect(spy).toHaveBeenCalledWith(new RegExp(request.cacheKey));
|
|
109
|
+
});
|
|
110
|
+
it("should allow to invalidate by cacheKey", async () => {
|
|
111
|
+
const spy = vi.spyOn(client.cache, "invalidate");
|
|
112
|
+
|
|
113
|
+
const { result } = renderUseCache(request);
|
|
114
|
+
|
|
115
|
+
act(() => {
|
|
116
|
+
result.current.invalidate(request.cacheKey);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
120
|
+
expect(spy).toHaveBeenCalledWith(request.cacheKey);
|
|
121
|
+
});
|
|
122
|
+
it("should allow to invalidate by default key", async () => {
|
|
123
|
+
const spy = vi.spyOn(client.cache, "invalidate");
|
|
124
|
+
|
|
125
|
+
const { result } = renderUseCache(request);
|
|
126
|
+
|
|
127
|
+
act(() => {
|
|
128
|
+
result.current.invalidate();
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
132
|
+
expect(spy).toHaveBeenCalledWith(request.cacheKey);
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
});
|
|
136
|
+
});
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { createHttpMockingServer } from "@hyper-fetch/testing";
|
|
2
|
+
|
|
3
|
+
const { resetMocks, startServer, stopServer } = createHttpMockingServer();
|
|
4
|
+
|
|
5
|
+
describe("useCache [ Helpers ]", () => {
|
|
6
|
+
beforeAll(() => {
|
|
7
|
+
startServer();
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
afterEach(() => {
|
|
11
|
+
resetMocks();
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
afterAll(() => {
|
|
15
|
+
stopServer();
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
beforeEach(() => {
|
|
19
|
+
vi.resetModules();
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
describe("given hook is mounted", () => {
|
|
23
|
+
describe("when getting the response", () => {
|
|
24
|
+
it("should trigger onCacheSuccess helper", async () => {
|
|
25
|
+
// Todo
|
|
26
|
+
});
|
|
27
|
+
it("should trigger onCacheError helper", async () => {
|
|
28
|
+
// Todo
|
|
29
|
+
});
|
|
30
|
+
it("should trigger onCacheUpdate helper", async () => {
|
|
31
|
+
// Todo
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
describe("when invalidate gets triggered", () => {
|
|
35
|
+
it("should remove cached data", async () => {
|
|
36
|
+
// Todo
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
});
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { expectType, expectNotType, expectFunctionParametersType } from "@hyper-fetch/testing";
|
|
2
|
+
import { Client } from "@hyper-fetch/core";
|
|
3
|
+
|
|
4
|
+
import type { UseCacheReturnType } from "hooks/use-cache";
|
|
5
|
+
|
|
6
|
+
const client = new Client({
|
|
7
|
+
url: "http://localhost:3000",
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
// ---- Request definitions ----
|
|
11
|
+
|
|
12
|
+
const getUsers = client.createRequest<{
|
|
13
|
+
response: { id: string; name: string }[];
|
|
14
|
+
}>()({
|
|
15
|
+
method: "GET",
|
|
16
|
+
endpoint: "/users",
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
const getUserWithError = client.createRequest<{
|
|
20
|
+
response: { id: string; name: string };
|
|
21
|
+
error: { code: number; message: string };
|
|
22
|
+
}>()({
|
|
23
|
+
method: "GET",
|
|
24
|
+
endpoint: "/users/:userId",
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
// ---- Type-level tests using return type directly ----
|
|
28
|
+
|
|
29
|
+
type GetUsersReturn = UseCacheReturnType<typeof getUsers>;
|
|
30
|
+
type GetUserWithErrorReturn = UseCacheReturnType<typeof getUserWithError>;
|
|
31
|
+
|
|
32
|
+
describe("useCache [Types]", () => {
|
|
33
|
+
describe("return type: data", () => {
|
|
34
|
+
it("should type data as response type or null", () => {
|
|
35
|
+
const data = null as unknown as GetUsersReturn["data"];
|
|
36
|
+
expectType<{ id: string; name: string }[] | null>().assert(data);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("should not type data as just the response type (missing null)", () => {
|
|
40
|
+
const data = null as unknown as GetUsersReturn["data"];
|
|
41
|
+
expectNotType<{ id: string; name: string }[]>().assert(data);
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
describe("return type: error", () => {
|
|
46
|
+
it("should type error as Error or null for default error type", () => {
|
|
47
|
+
const error = null as unknown as GetUsersReturn["error"];
|
|
48
|
+
expectType<Error | null>().assert(error);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it("should type error with custom error type", () => {
|
|
52
|
+
const error = null as unknown as GetUserWithErrorReturn["error"];
|
|
53
|
+
expectType<{ code: number; message: string } | Error | null>().assert(error);
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
describe("return type: loading", () => {
|
|
58
|
+
it("should type loading as boolean", () => {
|
|
59
|
+
const loading = false as GetUsersReturn["loading"];
|
|
60
|
+
expectType<boolean>().assert(loading);
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
describe("return type: success", () => {
|
|
65
|
+
it("should type success as boolean", () => {
|
|
66
|
+
const success = false as GetUsersReturn["success"];
|
|
67
|
+
expectType<boolean>().assert(success);
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
describe("return type: actions", () => {
|
|
72
|
+
it("should have setData accepting the response type or null", () => {
|
|
73
|
+
const setData = (() => null) as unknown as GetUsersReturn["setData"];
|
|
74
|
+
expectFunctionParametersType(setData).assert([[{ id: "1", name: "John" }]]);
|
|
75
|
+
expectFunctionParametersType(setData).assert([null]);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it("should have setLoading accepting boolean", () => {
|
|
79
|
+
const setLoading = (() => null) as unknown as GetUsersReturn["setLoading"];
|
|
80
|
+
expectFunctionParametersType(setLoading).assert([true]);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it("should have setError accepting the error type or null", () => {
|
|
84
|
+
const setError = (() => null) as unknown as GetUserWithErrorReturn["setError"];
|
|
85
|
+
expectFunctionParametersType(setError).assert([{ code: 404, message: "Not found" }]);
|
|
86
|
+
expectFunctionParametersType(setError).assert([null]);
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
describe("return type: invalidate", () => {
|
|
91
|
+
it("should have invalidate function", () => {
|
|
92
|
+
const invalidate = (() => null) as unknown as GetUsersReturn["invalidate"];
|
|
93
|
+
expectType<(...args: any[]) => any>().assert(invalidate);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it("should allow invalidate with no arguments", () => {
|
|
97
|
+
const invalidate = (() => null) as unknown as GetUsersReturn["invalidate"];
|
|
98
|
+
expectFunctionParametersType(invalidate).assert([undefined]);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it("should allow invalidate with a cache key string", () => {
|
|
102
|
+
const invalidate = (() => null) as unknown as GetUsersReturn["invalidate"];
|
|
103
|
+
expectFunctionParametersType(invalidate).assert(["custom-cache-key"]);
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it("should allow invalidate with a regex", () => {
|
|
107
|
+
const invalidate = (() => null) as unknown as GetUsersReturn["invalidate"];
|
|
108
|
+
expectFunctionParametersType(invalidate).assert([/users/]);
|
|
109
|
+
});
|
|
110
|
+
});
|
|
111
|
+
});
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { Socket } from "@hyper-fetch/sockets";
|
|
2
|
+
import { act, waitFor } from "@testing-library/react";
|
|
3
|
+
import { createWebsocketMockingServer, sleep, waitForConnection } from "@hyper-fetch/testing";
|
|
4
|
+
|
|
5
|
+
import { renderUseEmitter } from "../../utils/use-emitter.utils";
|
|
6
|
+
|
|
7
|
+
describe("useEmitter [ Base ]", () => {
|
|
8
|
+
const { url, startServer, stopServer, expectEmitterEvent } = createWebsocketMockingServer();
|
|
9
|
+
let socket = new Socket({ url });
|
|
10
|
+
let emitter = socket.createEmitter<{ name: string; age: number }>()({ topic: "test/:testId" });
|
|
11
|
+
|
|
12
|
+
beforeEach(async () => {
|
|
13
|
+
startServer();
|
|
14
|
+
socket = new Socket({ url });
|
|
15
|
+
emitter = socket.createEmitter<{ name: string; age: number }>()({ topic: "test/:testId" });
|
|
16
|
+
await waitForConnection(socket);
|
|
17
|
+
|
|
18
|
+
vi.resetModules();
|
|
19
|
+
vi.resetAllMocks();
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
afterEach(() => {
|
|
23
|
+
stopServer();
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
describe("when hook emit event", () => {
|
|
27
|
+
it("should set state with data", async () => {
|
|
28
|
+
const myEmitter = emitter.setParams({ testId: "1" });
|
|
29
|
+
|
|
30
|
+
const message = { name: "Maciej", age: 99 };
|
|
31
|
+
const view = renderUseEmitter(myEmitter);
|
|
32
|
+
|
|
33
|
+
act(() => {
|
|
34
|
+
view.result.current.emit({ payload: message });
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
await expectEmitterEvent(myEmitter.setParams({ testId: "1" }), message);
|
|
38
|
+
|
|
39
|
+
await waitFor(() => {
|
|
40
|
+
expect(view.result.current.connected).toBeTrue();
|
|
41
|
+
expect(view.result.current.connecting).toBeFalse();
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it("should handle emit error correctly", async () => {
|
|
46
|
+
const error = new Error("Emit error");
|
|
47
|
+
const view = renderUseEmitter(emitter);
|
|
48
|
+
let capturedError: { error: Error } | null = null;
|
|
49
|
+
|
|
50
|
+
view.result.current.onEmitError((err) => {
|
|
51
|
+
capturedError = err;
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
act(() => {
|
|
55
|
+
emitter.socket.events.emitEmitterError({ error, emitter });
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
await waitFor(() => {
|
|
59
|
+
expect(capturedError).toEqual({ error });
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it("should trigger onEmit callback when emitting", async () => {
|
|
64
|
+
const message = { name: "Test", age: 25 };
|
|
65
|
+
const instance = emitter.setParams({ testId: "1" });
|
|
66
|
+
const view = renderUseEmitter(instance);
|
|
67
|
+
let emitCalled = false;
|
|
68
|
+
|
|
69
|
+
view.result.current.onEmit(() => {
|
|
70
|
+
emitCalled = true;
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
act(() => {
|
|
74
|
+
view.result.current.emit({ payload: message });
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
await expectEmitterEvent(instance, message);
|
|
78
|
+
|
|
79
|
+
expect(emitCalled).toBeTrue();
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it("should handle undefined onEmit callback", async () => {
|
|
83
|
+
const myEmitter = emitter.setParams({ testId: "1" });
|
|
84
|
+
renderUseEmitter(myEmitter);
|
|
85
|
+
|
|
86
|
+
// Should not throw when callback is undefined
|
|
87
|
+
act(() => {
|
|
88
|
+
myEmitter.socket.events.emitEmitterStartEvent({ emitter: myEmitter });
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
// No assertion needed - test passes if no error is thrown
|
|
92
|
+
await sleep(10);
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
describe("when emitter topic changes", () => {
|
|
97
|
+
it("should update event listeners", async () => {
|
|
98
|
+
const emitter1 = emitter.setParams({ testId: "1" });
|
|
99
|
+
const view = renderUseEmitter(emitter1);
|
|
100
|
+
let callCount = 0;
|
|
101
|
+
|
|
102
|
+
view.result.current.onEmit(() => {
|
|
103
|
+
callCount += 1;
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
act(() => {
|
|
107
|
+
view.result.current.emit({ payload: { name: "Maciej", age: 99 } });
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
await expectEmitterEvent(emitter1, { name: "Maciej", age: 99 });
|
|
111
|
+
expect(callCount).toBe(1);
|
|
112
|
+
|
|
113
|
+
const emitter2 = socket.createEmitter()({ topic: "test/:testId" }).setParams({ testId: "2" });
|
|
114
|
+
view.rerender({ emitter: emitter2 });
|
|
115
|
+
|
|
116
|
+
act(() => {
|
|
117
|
+
view.result.current.emit({ payload: { name: "Maciej", age: 12 } });
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
await expectEmitterEvent(emitter2, { name: "Maciej", age: 12 });
|
|
121
|
+
expect(callCount).toBe(2);
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
});
|