@hyper-fetch/react 9.6.0 → 9.6.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/__tests__/features/helpers/use-request-events/use-request-events.async-mapper.spec.ts +243 -0
- package/__tests__/features/helpers/use-tracked-state/use-tacked-state.actions.spec.ts +1 -1
- package/__tests__/features/helpers/use-tracked-state/use-tracked-state.keys.spec.ts +294 -0
- package/__tests__/features/helpers/utils.spec.ts +127 -0
- package/__tests__/features/provider/provider.config.spec.tsx +172 -0
- package/__tests__/features/use-app-manager/use-app-manager.events.spec.tsx +497 -0
- package/__tests__/features/use-cache/use-cache.state.spec.tsx +326 -0
- package/__tests__/features/use-emitter/use-emitter.lifecycle.spec.tsx +417 -0
- package/__tests__/features/use-fetch/use-fetch.bounce-timers.spec.ts +199 -0
- package/__tests__/features/use-fetch/use-fetch.cache-events.spec.ts +212 -0
- package/__tests__/features/use-fetch/use-fetch.callbacks.spec.tsx +359 -0
- package/__tests__/features/use-fetch/use-fetch.cancel.spec.ts +6 -3
- package/__tests__/features/use-fetch/use-fetch.debounce.spec.ts +72 -80
- package/__tests__/features/use-fetch/use-fetch.hydration.spec.tsx +126 -0
- package/__tests__/features/use-fetch/use-fetch.infinite.spec.ts +146 -0
- package/__tests__/features/use-fetch/use-fetch.initial-state.spec.ts +340 -0
- package/__tests__/features/use-fetch/use-fetch.keys.spec.tsx +559 -0
- package/__tests__/features/use-fetch/use-fetch.lifecycle.spec.ts +348 -0
- package/__tests__/features/use-fetch/use-fetch.mounting.spec.tsx +411 -0
- package/__tests__/features/use-fetch/use-fetch.pagination.spec.ts +177 -0
- package/__tests__/features/use-fetch/use-fetch.refresh-interval.spec.ts +355 -0
- package/__tests__/features/use-fetch/use-fetch.refresh.spec.ts +36 -13
- package/__tests__/features/use-fetch/use-fetch.response-order.spec.ts +105 -0
- package/__tests__/features/use-fetch/use-fetch.retry.spec.ts +235 -0
- package/__tests__/features/use-fetch/use-fetch.return-types.spec.ts +186 -0
- package/__tests__/features/use-fetch/use-fetch.scope.spec.ts +333 -0
- package/__tests__/features/use-fetch/use-fetch.ssr.spec.tsx +195 -0
- package/__tests__/features/use-fetch/use-fetch.suspense-edge.spec.tsx +391 -0
- package/__tests__/features/use-fetch/use-fetch.throwing-callbacks.spec.tsx +112 -0
- package/__tests__/features/use-fetch/use-fetch.tracking.spec.tsx +364 -0
- package/__tests__/features/use-fetch/use-fetch.utils.spec.ts +11 -2
- package/__tests__/features/use-fetch/use-fetch.window-events.spec.ts +300 -0
- package/__tests__/features/use-infinite-fetch/use-infinite-fetch.parity.spec.tsx +1027 -0
- package/__tests__/features/use-infinite-fetch/use-infinite-fetch.resubscribe.spec.tsx +267 -0
- package/__tests__/features/use-infinite-fetch/use-infinite-fetch.typescript.spec.ts +64 -1
- package/__tests__/features/use-is-fetching/use-is-fetching.base.spec.tsx +19 -9
- package/__tests__/features/use-is-fetching/use-is-fetching.lifecycle.spec.tsx +301 -0
- package/__tests__/features/use-listener/use-listener.lifecycle.spec.tsx +507 -0
- package/__tests__/features/use-listener/use-listener.ssr.spec.tsx +89 -0
- package/__tests__/features/use-queue/use-queue.controls.spec.tsx +593 -0
- package/__tests__/features/use-queue/use-queue.list.spec.tsx +560 -0
- package/__tests__/features/use-queue/use-queue.progress.spec.tsx +650 -0
- package/__tests__/features/use-submit/use-submit.abort.spec.tsx +270 -0
- package/__tests__/features/use-submit/use-submit.actions.spec.tsx +446 -0
- package/__tests__/features/use-submit/use-submit.arguments.spec.tsx +732 -0
- package/__tests__/features/use-submit/use-submit.bounce.spec.tsx +644 -0
- package/__tests__/features/use-submit/use-submit.callbacks.spec.tsx +860 -0
- package/__tests__/features/use-submit/use-submit.chaining.spec.tsx +167 -0
- package/__tests__/features/use-submit/use-submit.concurrency.spec.ts +2 -1
- package/__tests__/features/use-submit/use-submit.debounce.spec.ts +100 -124
- package/__tests__/features/use-submit/use-submit.double-submit.spec.tsx +552 -0
- package/__tests__/features/use-submit/use-submit.offline.spec.tsx +285 -0
- package/__tests__/features/use-submit/use-submit.optimistic-cache.spec.tsx +619 -0
- package/__tests__/features/use-submit/use-submit.optimistic-settlement.spec.ts +8 -1
- package/__tests__/features/use-submit/use-submit.optimistic.spec.ts +5 -1
- package/__tests__/features/use-submit/use-submit.retry.spec.ts +51 -35
- package/__tests__/features/use-submit/use-submit.state.spec.tsx +1025 -0
- package/__tests__/features/use-submit/use-submit.submit-types.spec.ts +194 -0
- package/__tests__/features/use-submit/use-submit.submitting.spec.tsx +180 -0
- package/__tests__/features/use-submit/use-submit.unmount.spec.ts +2 -1
- package/__tests__/utils/gate.utils.ts +103 -0
- package/__tests__/utils/gated-client.utils.ts +158 -0
- package/__tests__/utils/helpers.utils.ts +19 -0
- package/__tests__/utils/use-queue.utils.ts +8 -4
- package/dist/helpers/use-request-events/use-request-events.hooks.d.ts.map +1 -1
- package/dist/helpers/use-request-events/use-request-events.types.d.ts +1 -1
- package/dist/helpers/use-request-events/use-request-events.types.d.ts.map +1 -1
- package/dist/helpers/use-tracked-state/use-tracked-state.hooks.d.ts.map +1 -1
- package/dist/helpers/use-tracked-state/use-tracked-state.utils.d.ts +8 -0
- package/dist/helpers/use-tracked-state/use-tracked-state.utils.d.ts.map +1 -1
- package/dist/hooks/use-cache/use-cache.hooks.d.ts.map +1 -1
- package/dist/hooks/use-fetch/use-fetch.hooks.d.ts.map +1 -1
- package/dist/hooks/use-infinite-fetch/use-infinite-fetch.hooks.d.ts.map +1 -1
- package/dist/hooks/use-infinite-fetch/use-infinite-fetch.store.d.ts +3 -0
- package/dist/hooks/use-infinite-fetch/use-infinite-fetch.store.d.ts.map +1 -1
- package/dist/hooks/use-infinite-fetch/use-infinite-fetch.types.d.ts +5 -3
- package/dist/hooks/use-infinite-fetch/use-infinite-fetch.types.d.ts.map +1 -1
- package/dist/hooks/use-infinite-fetch/use-infinite-fetch.utils.d.ts +11 -1
- package/dist/hooks/use-infinite-fetch/use-infinite-fetch.utils.d.ts.map +1 -1
- package/dist/hooks/use-listener/use-listener.hooks.d.ts.map +1 -1
- package/dist/hooks/use-queue/use-queue.hooks.d.ts.map +1 -1
- package/dist/hooks/use-submit/use-submit.hooks.d.ts.map +1 -1
- package/dist/index.mjs +633 -347
- package/dist/index.mjs.map +1 -1
- package/dist/provider/provider.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/helpers/use-request-events/use-request-events.hooks.ts +147 -32
- package/src/helpers/use-request-events/use-request-events.types.ts +1 -1
- package/src/helpers/use-tracked-state/use-tracked-state.hooks.ts +148 -101
- package/src/helpers/use-tracked-state/use-tracked-state.utils.ts +26 -5
- package/src/hooks/use-cache/use-cache.hooks.ts +20 -5
- package/src/hooks/use-fetch/use-fetch.hooks.ts +194 -113
- package/src/hooks/use-infinite-fetch/use-infinite-fetch.hooks.ts +55 -26
- package/src/hooks/use-infinite-fetch/use-infinite-fetch.store.ts +35 -29
- package/src/hooks/use-infinite-fetch/use-infinite-fetch.types.ts +5 -3
- package/src/hooks/use-infinite-fetch/use-infinite-fetch.utils.ts +25 -1
- package/src/hooks/use-listener/use-listener.hooks.ts +2 -1
- package/src/hooks/use-queue/use-queue.hooks.ts +85 -60
- package/src/hooks/use-submit/use-submit.hooks.ts +163 -53
- package/src/provider/provider.tsx +9 -1
- package/__tests__/features/use-fetch/use-fetch.pagination.ts +0 -36
- package/__tests__/features/use-fetch/use-fetch.retry.ts +0 -38
|
@@ -0,0 +1,333 @@
|
|
|
1
|
+
import { act, renderHook } from "@testing-library/react";
|
|
2
|
+
import { useCache } from "hooks/use-cache";
|
|
3
|
+
import { useFetch } from "hooks/use-fetch";
|
|
4
|
+
|
|
5
|
+
import { client, createRequest } from "../../utils";
|
|
6
|
+
import { advance, gateRequest, setCacheResponse, settle } from "../../utils/gate.utils";
|
|
7
|
+
|
|
8
|
+
type Data = { owner: string };
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Scoped requests keep their cache, queue and abort keys separated with the `setScope` prefix,
|
|
12
|
+
* while the events are emitted with the plain keys - hooks must handle only the events of their own scope.
|
|
13
|
+
*/
|
|
14
|
+
describe("useFetch [ Scope ]", () => {
|
|
15
|
+
let request = createRequest<Data>();
|
|
16
|
+
let calls = gateRequest(request);
|
|
17
|
+
|
|
18
|
+
const callOf = (scope: string | null) => {
|
|
19
|
+
const call = calls.find((item) => item.request.scope === scope);
|
|
20
|
+
if (!call) {
|
|
21
|
+
throw new Error(`Request of scope ${scope} was not sent`);
|
|
22
|
+
}
|
|
23
|
+
return call;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const renderScoped = (scope: string | null, options?: { revalidate?: boolean; disabled?: boolean }) => {
|
|
27
|
+
const scoped = scope ? request.setScope(scope) : request;
|
|
28
|
+
return renderHook(() => {
|
|
29
|
+
const { data, loading, error, refetch } = useFetch(scoped, options);
|
|
30
|
+
return { data, loading, error, refetch };
|
|
31
|
+
});
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
beforeEach(() => {
|
|
35
|
+
vi.useFakeTimers();
|
|
36
|
+
client.clear();
|
|
37
|
+
request = createRequest<Data>();
|
|
38
|
+
calls = gateRequest(request);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
afterEach(() => {
|
|
42
|
+
vi.useRealTimers();
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
describe("when the scoped request is fetched", () => {
|
|
46
|
+
it("should store the response under the scoped key", async () => {
|
|
47
|
+
const { result } = renderScoped("a");
|
|
48
|
+
await advance();
|
|
49
|
+
callOf("a").succeed({ owner: "a" });
|
|
50
|
+
await settle();
|
|
51
|
+
|
|
52
|
+
expect(result.current.data).toStrictEqual({ owner: "a" });
|
|
53
|
+
expect(client.cache.keys()).toStrictEqual([`a__${request.cacheKey}`]);
|
|
54
|
+
});
|
|
55
|
+
it("should be in the loading state during the refetch", async () => {
|
|
56
|
+
const { result } = renderScoped("a");
|
|
57
|
+
await advance();
|
|
58
|
+
callOf("a").succeed({ owner: "a" });
|
|
59
|
+
await settle();
|
|
60
|
+
expect(result.current.loading).toBeFalse();
|
|
61
|
+
|
|
62
|
+
act(() => {
|
|
63
|
+
result.current.refetch();
|
|
64
|
+
});
|
|
65
|
+
await advance();
|
|
66
|
+
|
|
67
|
+
expect(result.current.loading).toBeTrue();
|
|
68
|
+
|
|
69
|
+
calls[1].succeed({ owner: "a2" });
|
|
70
|
+
await settle();
|
|
71
|
+
|
|
72
|
+
expect(result.current.loading).toBeFalse();
|
|
73
|
+
expect(result.current.data).toStrictEqual({ owner: "a2" });
|
|
74
|
+
});
|
|
75
|
+
it("should read the scoped cache on mount", async () => {
|
|
76
|
+
setCacheResponse(request.setScope("a"), { owner: "a" });
|
|
77
|
+
|
|
78
|
+
const { result } = renderScoped("a", { revalidate: false });
|
|
79
|
+
await advance();
|
|
80
|
+
|
|
81
|
+
expect(result.current.data).toStrictEqual({ owner: "a" });
|
|
82
|
+
expect(calls).toHaveLength(0);
|
|
83
|
+
});
|
|
84
|
+
it("should join the running request of the same scope", async () => {
|
|
85
|
+
act(() => {
|
|
86
|
+
client.fetchDispatcher.add(request.setScope("a"));
|
|
87
|
+
});
|
|
88
|
+
await advance();
|
|
89
|
+
|
|
90
|
+
renderScoped("a");
|
|
91
|
+
await advance();
|
|
92
|
+
|
|
93
|
+
expect(calls).toHaveLength(1);
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
describe("when the same request is used in different scopes", () => {
|
|
98
|
+
it("should send a request for every scope", async () => {
|
|
99
|
+
renderScoped("a");
|
|
100
|
+
renderScoped("b");
|
|
101
|
+
renderScoped(null);
|
|
102
|
+
await advance();
|
|
103
|
+
|
|
104
|
+
expect(calls.map((call) => call.request.scope).toSorted()).toStrictEqual(["a", "b", null].toSorted());
|
|
105
|
+
});
|
|
106
|
+
it("should not show the data of another scope", async () => {
|
|
107
|
+
const scopeA = renderScoped("a");
|
|
108
|
+
const scopeB = renderScoped("b");
|
|
109
|
+
const unscoped = renderScoped(null);
|
|
110
|
+
await advance();
|
|
111
|
+
|
|
112
|
+
callOf("a").succeed({ owner: "a" });
|
|
113
|
+
await settle();
|
|
114
|
+
|
|
115
|
+
expect(scopeA.result.current.data).toStrictEqual({ owner: "a" });
|
|
116
|
+
expect(scopeB.result.current.data).toBeNull();
|
|
117
|
+
expect(unscoped.result.current.data).toBeNull();
|
|
118
|
+
expect(scopeB.result.current.loading).toBeTrue();
|
|
119
|
+
expect(unscoped.result.current.loading).toBeTrue();
|
|
120
|
+
});
|
|
121
|
+
it("should not show the error of another scope", async () => {
|
|
122
|
+
const scopeA = renderScoped("a");
|
|
123
|
+
const scopeB = renderScoped("b");
|
|
124
|
+
await advance();
|
|
125
|
+
|
|
126
|
+
callOf("a").fail(new Error("Scope a failed"));
|
|
127
|
+
await settle();
|
|
128
|
+
|
|
129
|
+
expect(scopeA.result.current.error?.message).toBe("Scope a failed");
|
|
130
|
+
expect(scopeB.result.current.error).toBeNull();
|
|
131
|
+
});
|
|
132
|
+
it("should not show the unscoped data in the scoped hook", async () => {
|
|
133
|
+
const scopeA = renderScoped("a");
|
|
134
|
+
const unscoped = renderScoped(null);
|
|
135
|
+
await advance();
|
|
136
|
+
|
|
137
|
+
callOf(null).succeed({ owner: "nobody" });
|
|
138
|
+
await settle();
|
|
139
|
+
|
|
140
|
+
expect(unscoped.result.current.data).toStrictEqual({ owner: "nobody" });
|
|
141
|
+
expect(scopeA.result.current.data).toBeNull();
|
|
142
|
+
});
|
|
143
|
+
it("should keep the scopes separated in useCache", async () => {
|
|
144
|
+
const cacheB = renderHook(() => useCache(request.setScope("b")).data);
|
|
145
|
+
renderScoped("a");
|
|
146
|
+
await advance();
|
|
147
|
+
|
|
148
|
+
callOf("a").succeed({ owner: "a" });
|
|
149
|
+
await settle();
|
|
150
|
+
|
|
151
|
+
expect(cacheB.result.current).toBeNull();
|
|
152
|
+
});
|
|
153
|
+
it("should not change the loading state of another scope", async () => {
|
|
154
|
+
const scopeA = renderScoped("a", { disabled: true });
|
|
155
|
+
renderScoped("b");
|
|
156
|
+
await advance();
|
|
157
|
+
|
|
158
|
+
expect(scopeA.result.current.loading).toBeFalse();
|
|
159
|
+
});
|
|
160
|
+
it("should call the callbacks of the matching scope only", async () => {
|
|
161
|
+
const onSuccessA = vi.fn();
|
|
162
|
+
const onSuccessB = vi.fn();
|
|
163
|
+
renderHook(() => {
|
|
164
|
+
useFetch(request.setScope("a")).onSuccess(onSuccessA);
|
|
165
|
+
useFetch(request.setScope("b")).onSuccess(onSuccessB);
|
|
166
|
+
});
|
|
167
|
+
await advance();
|
|
168
|
+
|
|
169
|
+
callOf("a").succeed({ owner: "a" });
|
|
170
|
+
await settle();
|
|
171
|
+
|
|
172
|
+
expect(onSuccessA).toHaveBeenCalledOnce();
|
|
173
|
+
expect(onSuccessB).not.toHaveBeenCalled();
|
|
174
|
+
});
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
describe("when the scoped cache changes", () => {
|
|
178
|
+
it("should refetch after invalidation by the request", async () => {
|
|
179
|
+
renderScoped("a");
|
|
180
|
+
await advance();
|
|
181
|
+
callOf("a").succeed({ owner: "a" });
|
|
182
|
+
await settle();
|
|
183
|
+
|
|
184
|
+
act(() => {
|
|
185
|
+
client.cache.invalidate(request.setScope("a"));
|
|
186
|
+
});
|
|
187
|
+
await advance();
|
|
188
|
+
|
|
189
|
+
expect(calls).toHaveLength(2);
|
|
190
|
+
});
|
|
191
|
+
it("should refetch after invalidation by RegExp", async () => {
|
|
192
|
+
renderScoped("a");
|
|
193
|
+
await advance();
|
|
194
|
+
callOf("a").succeed({ owner: "a" });
|
|
195
|
+
await settle();
|
|
196
|
+
|
|
197
|
+
act(() => {
|
|
198
|
+
client.cache.invalidate(/shared-endpoint/);
|
|
199
|
+
});
|
|
200
|
+
await advance();
|
|
201
|
+
|
|
202
|
+
expect(calls).toHaveLength(2);
|
|
203
|
+
});
|
|
204
|
+
it("should refetch after invalidation of the whole cache", async () => {
|
|
205
|
+
renderScoped("a");
|
|
206
|
+
await advance();
|
|
207
|
+
callOf("a").succeed({ owner: "a" });
|
|
208
|
+
await settle();
|
|
209
|
+
|
|
210
|
+
act(() => {
|
|
211
|
+
client.cache.invalidate();
|
|
212
|
+
});
|
|
213
|
+
await advance();
|
|
214
|
+
|
|
215
|
+
expect(calls).toHaveLength(2);
|
|
216
|
+
});
|
|
217
|
+
it("should not refetch when the request of another scope is invalidated", async () => {
|
|
218
|
+
renderScoped("a");
|
|
219
|
+
await advance();
|
|
220
|
+
callOf("a").succeed({ owner: "a" });
|
|
221
|
+
await settle();
|
|
222
|
+
setCacheResponse(request.setScope("b"), { owner: "b" });
|
|
223
|
+
|
|
224
|
+
act(() => {
|
|
225
|
+
client.cache.invalidate(request.setScope("b"));
|
|
226
|
+
client.cache.invalidate(`b__${request.cacheKey}`);
|
|
227
|
+
});
|
|
228
|
+
await advance();
|
|
229
|
+
|
|
230
|
+
expect(calls).toHaveLength(1);
|
|
231
|
+
});
|
|
232
|
+
it("should not refetch when the unscoped request is invalidated", async () => {
|
|
233
|
+
renderScoped("a");
|
|
234
|
+
await advance();
|
|
235
|
+
callOf("a").succeed({ owner: "a" });
|
|
236
|
+
await settle();
|
|
237
|
+
|
|
238
|
+
act(() => {
|
|
239
|
+
client.cache.invalidate(request);
|
|
240
|
+
});
|
|
241
|
+
await advance();
|
|
242
|
+
|
|
243
|
+
expect(calls).toHaveLength(1);
|
|
244
|
+
});
|
|
245
|
+
it("should not refetch when the entry of another scope is deleted", async () => {
|
|
246
|
+
renderScoped("a");
|
|
247
|
+
await advance();
|
|
248
|
+
callOf("a").succeed({ owner: "a" });
|
|
249
|
+
await settle();
|
|
250
|
+
setCacheResponse(request.setScope("b"), { owner: "b" });
|
|
251
|
+
|
|
252
|
+
act(() => {
|
|
253
|
+
client.cache.delete(`b__${request.cacheKey}`);
|
|
254
|
+
});
|
|
255
|
+
await advance();
|
|
256
|
+
|
|
257
|
+
expect(calls).toHaveLength(1);
|
|
258
|
+
});
|
|
259
|
+
it("should refetch every scope once when the whole cache is invalidated", async () => {
|
|
260
|
+
renderScoped("a");
|
|
261
|
+
renderScoped("b");
|
|
262
|
+
await advance();
|
|
263
|
+
callOf("a").succeed({ owner: "a" });
|
|
264
|
+
callOf("b").succeed({ owner: "b" });
|
|
265
|
+
await settle();
|
|
266
|
+
|
|
267
|
+
act(() => {
|
|
268
|
+
client.cache.invalidate();
|
|
269
|
+
});
|
|
270
|
+
await advance();
|
|
271
|
+
|
|
272
|
+
expect(calls.map((call) => call.request.scope)).toStrictEqual(["a", "b", "a", "b"]);
|
|
273
|
+
});
|
|
274
|
+
it("should refetch after the scoped entry is deleted", async () => {
|
|
275
|
+
renderScoped("a");
|
|
276
|
+
await advance();
|
|
277
|
+
callOf("a").succeed({ owner: "a" });
|
|
278
|
+
await settle();
|
|
279
|
+
|
|
280
|
+
act(() => {
|
|
281
|
+
client.cache.delete(`a__${request.cacheKey}`);
|
|
282
|
+
});
|
|
283
|
+
await advance();
|
|
284
|
+
|
|
285
|
+
expect(calls).toHaveLength(2);
|
|
286
|
+
});
|
|
287
|
+
it("should follow the updates of the scoped cache", async () => {
|
|
288
|
+
const { result } = renderScoped("a");
|
|
289
|
+
await advance();
|
|
290
|
+
callOf("a").succeed({ owner: "a" });
|
|
291
|
+
await settle();
|
|
292
|
+
|
|
293
|
+
act(() => {
|
|
294
|
+
setCacheResponse(request.setScope("a"), { owner: "updated" });
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
expect(result.current.data).toStrictEqual({ owner: "updated" });
|
|
298
|
+
});
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
describe("when the scope changes", () => {
|
|
302
|
+
it("should switch to the data of the new scope", async () => {
|
|
303
|
+
setCacheResponse(request.setScope("a"), { owner: "a" });
|
|
304
|
+
setCacheResponse(request.setScope("b"), { owner: "b" });
|
|
305
|
+
const { result, rerender } = renderHook(
|
|
306
|
+
({ scope }) => {
|
|
307
|
+
const { data } = useFetch(request.setScope(scope), { revalidate: false });
|
|
308
|
+
return { data };
|
|
309
|
+
},
|
|
310
|
+
{ initialProps: { scope: "a" } },
|
|
311
|
+
);
|
|
312
|
+
expect(result.current.data).toStrictEqual({ owner: "a" });
|
|
313
|
+
|
|
314
|
+
rerender({ scope: "b" });
|
|
315
|
+
|
|
316
|
+
expect(result.current.data).toStrictEqual({ owner: "b" });
|
|
317
|
+
});
|
|
318
|
+
it("should not keep the data of the previous scope", async () => {
|
|
319
|
+
setCacheResponse(request.setScope("a"), { owner: "a" });
|
|
320
|
+
const { result, rerender } = renderHook(
|
|
321
|
+
({ scope }) => {
|
|
322
|
+
const { data } = useFetch(request.setScope(scope), { revalidate: false });
|
|
323
|
+
return { data };
|
|
324
|
+
},
|
|
325
|
+
{ initialProps: { scope: "a" } },
|
|
326
|
+
);
|
|
327
|
+
|
|
328
|
+
rerender({ scope: "b" });
|
|
329
|
+
|
|
330
|
+
expect(result.current.data).toBeNull();
|
|
331
|
+
});
|
|
332
|
+
});
|
|
333
|
+
});
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
// @vitest-environment node
|
|
2
|
+
import type { RequestInstance } from "@hyper-fetch/core";
|
|
3
|
+
import { Client } from "@hyper-fetch/core";
|
|
4
|
+
import { useCache } from "hooks/use-cache";
|
|
5
|
+
import type { UseFetchOptionsType } from "hooks/use-fetch";
|
|
6
|
+
import { useFetch } from "hooks/use-fetch";
|
|
7
|
+
import { useIsFetching } from "hooks/use-is-fetching";
|
|
8
|
+
import { Provider } from "provider";
|
|
9
|
+
import { Suspense } from "react";
|
|
10
|
+
import { renderToString } from "react-dom/server";
|
|
11
|
+
|
|
12
|
+
type User = { id: number; name: string };
|
|
13
|
+
|
|
14
|
+
const user: User = { id: 1, name: "Maciej" };
|
|
15
|
+
|
|
16
|
+
const countListeners = (client: Client) => {
|
|
17
|
+
return [client.requestManager.emitter, client.cache.emitter, client.fetchDispatcher.emitter].reduce(
|
|
18
|
+
(sum, emitter) => sum + emitter.eventNames().reduce((count, name) => count + emitter.listenerCount(name), 0),
|
|
19
|
+
0,
|
|
20
|
+
);
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Server rendering - there is no window, effects never run and nothing can be left behind after the render
|
|
25
|
+
*/
|
|
26
|
+
describe("useFetch [ Server Side Rendering ]", () => {
|
|
27
|
+
let client = new Client({ url: "http://localhost" });
|
|
28
|
+
let request = client.createRequest<{ response: User }>()({ endpoint: "/users/1" });
|
|
29
|
+
let mock = vi.fn();
|
|
30
|
+
|
|
31
|
+
const Page = <T extends RequestInstance>({ req, options }: { req: T; options?: UseFetchOptionsType<T> }) => {
|
|
32
|
+
const { data, loading, error, success } = useFetch(req, options);
|
|
33
|
+
return <div>{JSON.stringify({ data, loading, error, success })}</div>;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
const readMarkup = (markup: string) => {
|
|
37
|
+
const json = markup.replaceAll(/<[^>]+>/g, "").replaceAll(""", '"');
|
|
38
|
+
return JSON.parse(json);
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const saveScopedResponse = (scope: string) => {
|
|
42
|
+
const scoped = request.setScope(scope);
|
|
43
|
+
client.cache.set(scoped, {
|
|
44
|
+
data: user,
|
|
45
|
+
error: null,
|
|
46
|
+
status: 200,
|
|
47
|
+
success: true,
|
|
48
|
+
extra: client.adapter.defaultExtra,
|
|
49
|
+
requestTimestamp: Date.now(),
|
|
50
|
+
responseTimestamp: Date.now(),
|
|
51
|
+
addedTimestamp: Date.now(),
|
|
52
|
+
triggerTimestamp: Date.now(),
|
|
53
|
+
retries: 0,
|
|
54
|
+
isCanceled: false,
|
|
55
|
+
isOffline: false,
|
|
56
|
+
willRetry: false,
|
|
57
|
+
});
|
|
58
|
+
return scoped;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
beforeEach(() => {
|
|
62
|
+
vi.useFakeTimers();
|
|
63
|
+
client = new Client({ url: "http://localhost" });
|
|
64
|
+
mock = vi.fn(() => ({ data: user, status: 200 }));
|
|
65
|
+
request = client.createRequest<{ response: User }>()({ endpoint: "/users/1" }).setMock(mock);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
afterEach(() => {
|
|
69
|
+
vi.useRealTimers();
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it("should run without the window", () => {
|
|
73
|
+
expect(typeof window).toBe("undefined");
|
|
74
|
+
expect(client.mode).toBe("server");
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
describe("when there is no data", () => {
|
|
78
|
+
it("should render the loading state", () => {
|
|
79
|
+
const markup = renderToString(<Page req={request} />);
|
|
80
|
+
|
|
81
|
+
expect(readMarkup(markup)).toStrictEqual({ data: null, loading: true, error: null, success: false });
|
|
82
|
+
});
|
|
83
|
+
it("should not send any request", async () => {
|
|
84
|
+
renderToString(<Page req={request} />);
|
|
85
|
+
await vi.advanceTimersByTimeAsync(1000);
|
|
86
|
+
|
|
87
|
+
expect(mock).not.toHaveBeenCalled();
|
|
88
|
+
expect(client.fetchDispatcher.getAllRunningRequests()).toHaveLength(0);
|
|
89
|
+
expect(client.fetchDispatcher.getQueuesKeys()).toHaveLength(0);
|
|
90
|
+
});
|
|
91
|
+
it("should not leave any timers", () => {
|
|
92
|
+
renderToString(
|
|
93
|
+
<Page req={request} options={{ refresh: true, refreshTime: 1000, bounce: true, dependencies: [1] }} />,
|
|
94
|
+
);
|
|
95
|
+
|
|
96
|
+
expect(vi.getTimerCount()).toBe(0);
|
|
97
|
+
});
|
|
98
|
+
it("should not leave any listeners", () => {
|
|
99
|
+
const baseline = countListeners(client);
|
|
100
|
+
|
|
101
|
+
renderToString(<Page req={request} />);
|
|
102
|
+
|
|
103
|
+
expect(countListeners(client)).toBe(baseline);
|
|
104
|
+
});
|
|
105
|
+
it("should render the idle state when disabled", () => {
|
|
106
|
+
const markup = renderToString(<Page req={request} options={{ disabled: true }} />);
|
|
107
|
+
|
|
108
|
+
expect(readMarkup(markup)).toStrictEqual({ data: null, loading: false, error: null, success: false });
|
|
109
|
+
});
|
|
110
|
+
it("should render the same markup every time", () => {
|
|
111
|
+
const first = renderToString(<Page req={request} />);
|
|
112
|
+
const second = renderToString(<Page req={request} />);
|
|
113
|
+
|
|
114
|
+
expect(second).toBe(first);
|
|
115
|
+
});
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
describe("when the data is available", () => {
|
|
119
|
+
it("should render the initial response", () => {
|
|
120
|
+
const markup = renderToString(<Page req={request} options={{ initialResponse: { data: user } }} />);
|
|
121
|
+
|
|
122
|
+
expect(readMarkup(markup).data).toStrictEqual(user);
|
|
123
|
+
expect(readMarkup(markup).success).toBeTrue();
|
|
124
|
+
});
|
|
125
|
+
it("should render the data cached for the scope of the request", () => {
|
|
126
|
+
const scoped = saveScopedResponse("session-1");
|
|
127
|
+
|
|
128
|
+
const markup = renderToString(<Page req={scoped} options={{ revalidate: false }} />);
|
|
129
|
+
|
|
130
|
+
expect(readMarkup(markup)).toStrictEqual({ data: user, loading: false, error: null, success: true });
|
|
131
|
+
});
|
|
132
|
+
it("should not render the data of another scope", () => {
|
|
133
|
+
saveScopedResponse("session-1");
|
|
134
|
+
|
|
135
|
+
const markup = renderToString(<Page req={request.setScope("session-2")} options={{ revalidate: false }} />);
|
|
136
|
+
|
|
137
|
+
expect(readMarkup(markup).data).toBeNull();
|
|
138
|
+
});
|
|
139
|
+
it("should render the data fetched on the server before rendering", async () => {
|
|
140
|
+
const scoped = request.setScope("session-1");
|
|
141
|
+
const sending = scoped.send();
|
|
142
|
+
await vi.advanceTimersByTimeAsync(1000);
|
|
143
|
+
await sending;
|
|
144
|
+
|
|
145
|
+
const markup = renderToString(<Page req={scoped} />);
|
|
146
|
+
|
|
147
|
+
expect(readMarkup(markup).data).toStrictEqual(user);
|
|
148
|
+
expect(mock).toHaveBeenCalledOnce();
|
|
149
|
+
});
|
|
150
|
+
it("should apply the selector", () => {
|
|
151
|
+
const scoped = saveScopedResponse("session-1");
|
|
152
|
+
const Name = () => {
|
|
153
|
+
const { data } = useFetch(scoped, { select: (value) => value.name });
|
|
154
|
+
return <div>{data}</div>;
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
expect(renderToString(<Name />)).toContain("Maciej");
|
|
158
|
+
});
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
describe("when other hooks are rendered on the server", () => {
|
|
162
|
+
it("should render the cached data with useCache", () => {
|
|
163
|
+
const scoped = saveScopedResponse("session-1");
|
|
164
|
+
const Cached = () => <div>{useCache(scoped).data?.name}</div>;
|
|
165
|
+
|
|
166
|
+
expect(renderToString(<Cached />)).toContain("Maciej");
|
|
167
|
+
});
|
|
168
|
+
it("should render zero running requests with useIsFetching", () => {
|
|
169
|
+
const Counter = () => <div>{useIsFetching(client)}</div>;
|
|
170
|
+
|
|
171
|
+
expect(renderToString(<Counter />)).toBe("<div>0</div>");
|
|
172
|
+
});
|
|
173
|
+
it("should read the provider config", () => {
|
|
174
|
+
const markup = renderToString(
|
|
175
|
+
<Provider config={{ useFetchConfig: { disabled: true } }}>
|
|
176
|
+
<Page req={request} />
|
|
177
|
+
</Provider>,
|
|
178
|
+
);
|
|
179
|
+
|
|
180
|
+
expect(readMarkup(markup).loading).toBeFalse();
|
|
181
|
+
});
|
|
182
|
+
it("should render the suspense fallback for the missing data", () => {
|
|
183
|
+
const Suspended = () => <div>{useFetch(request, { suspense: true }).data?.name}</div>;
|
|
184
|
+
|
|
185
|
+
const markup = renderToString(
|
|
186
|
+
<Suspense fallback={<span>fallback</span>}>
|
|
187
|
+
<Suspended />
|
|
188
|
+
</Suspense>,
|
|
189
|
+
);
|
|
190
|
+
|
|
191
|
+
expect(markup).toContain("fallback");
|
|
192
|
+
expect(markup).not.toContain("Maciej");
|
|
193
|
+
});
|
|
194
|
+
});
|
|
195
|
+
});
|