@hyper-fetch/react 9.6.1 → 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.
Files changed (91) hide show
  1. package/__tests__/features/helpers/use-tracked-state/use-tacked-state.actions.spec.ts +1 -1
  2. package/__tests__/features/helpers/use-tracked-state/use-tracked-state.keys.spec.ts +294 -0
  3. package/__tests__/features/helpers/utils.spec.ts +127 -0
  4. package/__tests__/features/provider/provider.config.spec.tsx +172 -0
  5. package/__tests__/features/use-app-manager/use-app-manager.events.spec.tsx +497 -0
  6. package/__tests__/features/use-cache/use-cache.state.spec.tsx +326 -0
  7. package/__tests__/features/use-emitter/use-emitter.lifecycle.spec.tsx +417 -0
  8. package/__tests__/features/use-fetch/use-fetch.bounce-timers.spec.ts +199 -0
  9. package/__tests__/features/use-fetch/use-fetch.cache-events.spec.ts +212 -0
  10. package/__tests__/features/use-fetch/use-fetch.callbacks.spec.tsx +359 -0
  11. package/__tests__/features/use-fetch/use-fetch.hydration.spec.tsx +126 -0
  12. package/__tests__/features/use-fetch/use-fetch.infinite.spec.ts +146 -0
  13. package/__tests__/features/use-fetch/use-fetch.initial-state.spec.ts +340 -0
  14. package/__tests__/features/use-fetch/use-fetch.keys.spec.tsx +559 -0
  15. package/__tests__/features/use-fetch/use-fetch.lifecycle.spec.ts +348 -0
  16. package/__tests__/features/use-fetch/use-fetch.mounting.spec.tsx +411 -0
  17. package/__tests__/features/use-fetch/use-fetch.pagination.spec.ts +177 -0
  18. package/__tests__/features/use-fetch/use-fetch.refresh-interval.spec.ts +355 -0
  19. package/__tests__/features/use-fetch/use-fetch.response-order.spec.ts +105 -0
  20. package/__tests__/features/use-fetch/use-fetch.retry.spec.ts +235 -0
  21. package/__tests__/features/use-fetch/use-fetch.return-types.spec.ts +186 -0
  22. package/__tests__/features/use-fetch/use-fetch.scope.spec.ts +333 -0
  23. package/__tests__/features/use-fetch/use-fetch.ssr.spec.tsx +195 -0
  24. package/__tests__/features/use-fetch/use-fetch.suspense-edge.spec.tsx +391 -0
  25. package/__tests__/features/use-fetch/use-fetch.throwing-callbacks.spec.tsx +112 -0
  26. package/__tests__/features/use-fetch/use-fetch.tracking.spec.tsx +364 -0
  27. package/__tests__/features/use-fetch/use-fetch.window-events.spec.ts +300 -0
  28. package/__tests__/features/use-infinite-fetch/use-infinite-fetch.parity.spec.tsx +1027 -0
  29. package/__tests__/features/use-infinite-fetch/use-infinite-fetch.resubscribe.spec.tsx +267 -0
  30. package/__tests__/features/use-infinite-fetch/use-infinite-fetch.typescript.spec.ts +64 -1
  31. package/__tests__/features/use-is-fetching/use-is-fetching.lifecycle.spec.tsx +301 -0
  32. package/__tests__/features/use-listener/use-listener.lifecycle.spec.tsx +507 -0
  33. package/__tests__/features/use-listener/use-listener.ssr.spec.tsx +89 -0
  34. package/__tests__/features/use-queue/use-queue.controls.spec.tsx +593 -0
  35. package/__tests__/features/use-queue/use-queue.list.spec.tsx +560 -0
  36. package/__tests__/features/use-queue/use-queue.progress.spec.tsx +650 -0
  37. package/__tests__/features/use-submit/use-submit.abort.spec.tsx +270 -0
  38. package/__tests__/features/use-submit/use-submit.actions.spec.tsx +446 -0
  39. package/__tests__/features/use-submit/use-submit.arguments.spec.tsx +732 -0
  40. package/__tests__/features/use-submit/use-submit.bounce.spec.tsx +644 -0
  41. package/__tests__/features/use-submit/use-submit.callbacks.spec.tsx +860 -0
  42. package/__tests__/features/use-submit/use-submit.chaining.spec.tsx +167 -0
  43. package/__tests__/features/use-submit/use-submit.double-submit.spec.tsx +552 -0
  44. package/__tests__/features/use-submit/use-submit.offline.spec.tsx +285 -0
  45. package/__tests__/features/use-submit/use-submit.optimistic-cache.spec.tsx +619 -0
  46. package/__tests__/features/use-submit/use-submit.optimistic-settlement.spec.ts +6 -0
  47. package/__tests__/features/use-submit/use-submit.optimistic.spec.ts +5 -1
  48. package/__tests__/features/use-submit/use-submit.state.spec.tsx +1025 -0
  49. package/__tests__/features/use-submit/use-submit.submit-types.spec.ts +194 -0
  50. package/__tests__/features/use-submit/use-submit.submitting.spec.tsx +180 -0
  51. package/__tests__/utils/gate.utils.ts +103 -0
  52. package/__tests__/utils/gated-client.utils.ts +158 -0
  53. package/__tests__/utils/helpers.utils.ts +19 -0
  54. package/dist/helpers/use-request-events/use-request-events.hooks.d.ts.map +1 -1
  55. package/dist/helpers/use-request-events/use-request-events.types.d.ts +1 -1
  56. package/dist/helpers/use-request-events/use-request-events.types.d.ts.map +1 -1
  57. package/dist/helpers/use-tracked-state/use-tracked-state.hooks.d.ts.map +1 -1
  58. package/dist/helpers/use-tracked-state/use-tracked-state.utils.d.ts +8 -0
  59. package/dist/helpers/use-tracked-state/use-tracked-state.utils.d.ts.map +1 -1
  60. package/dist/hooks/use-cache/use-cache.hooks.d.ts.map +1 -1
  61. package/dist/hooks/use-fetch/use-fetch.hooks.d.ts.map +1 -1
  62. package/dist/hooks/use-infinite-fetch/use-infinite-fetch.hooks.d.ts.map +1 -1
  63. package/dist/hooks/use-infinite-fetch/use-infinite-fetch.store.d.ts +3 -0
  64. package/dist/hooks/use-infinite-fetch/use-infinite-fetch.store.d.ts.map +1 -1
  65. package/dist/hooks/use-infinite-fetch/use-infinite-fetch.types.d.ts +5 -3
  66. package/dist/hooks/use-infinite-fetch/use-infinite-fetch.types.d.ts.map +1 -1
  67. package/dist/hooks/use-infinite-fetch/use-infinite-fetch.utils.d.ts +11 -1
  68. package/dist/hooks/use-infinite-fetch/use-infinite-fetch.utils.d.ts.map +1 -1
  69. package/dist/hooks/use-listener/use-listener.hooks.d.ts.map +1 -1
  70. package/dist/hooks/use-queue/use-queue.hooks.d.ts.map +1 -1
  71. package/dist/hooks/use-submit/use-submit.hooks.d.ts.map +1 -1
  72. package/dist/index.mjs +630 -346
  73. package/dist/index.mjs.map +1 -1
  74. package/dist/provider/provider.d.ts.map +1 -1
  75. package/package.json +1 -1
  76. package/src/helpers/use-request-events/use-request-events.hooks.ts +141 -28
  77. package/src/helpers/use-request-events/use-request-events.types.ts +1 -1
  78. package/src/helpers/use-tracked-state/use-tracked-state.hooks.ts +148 -101
  79. package/src/helpers/use-tracked-state/use-tracked-state.utils.ts +26 -5
  80. package/src/hooks/use-cache/use-cache.hooks.ts +20 -5
  81. package/src/hooks/use-fetch/use-fetch.hooks.ts +194 -113
  82. package/src/hooks/use-infinite-fetch/use-infinite-fetch.hooks.ts +55 -26
  83. package/src/hooks/use-infinite-fetch/use-infinite-fetch.store.ts +35 -29
  84. package/src/hooks/use-infinite-fetch/use-infinite-fetch.types.ts +5 -3
  85. package/src/hooks/use-infinite-fetch/use-infinite-fetch.utils.ts +25 -1
  86. package/src/hooks/use-listener/use-listener.hooks.ts +2 -1
  87. package/src/hooks/use-queue/use-queue.hooks.ts +85 -60
  88. package/src/hooks/use-submit/use-submit.hooks.ts +163 -53
  89. package/src/provider/provider.tsx +9 -1
  90. package/__tests__/features/use-fetch/use-fetch.pagination.ts +0 -36
  91. package/__tests__/features/use-fetch/use-fetch.retry.ts +0 -38
@@ -0,0 +1,497 @@
1
+ import { AppManager, Client } from "@hyper-fetch/core";
2
+ import { act, render, renderHook, screen } from "@testing-library/react";
3
+ import type { UseAppManagerReturnType } from "hooks/use-app-manager";
4
+ import { useAppManager } from "hooks/use-app-manager";
5
+ import { StrictMode } from "react";
6
+ import { renderToString } from "react-dom/server";
7
+ import { expectTypeOf } from "vitest";
8
+
9
+ const createClient = (options?: { initiallyOnline?: boolean; initiallyFocused?: boolean }) => {
10
+ return new Client({
11
+ url: "http://app-manager",
12
+ appManager: () =>
13
+ new AppManager({
14
+ initiallyOnline: options?.initiallyOnline ?? true,
15
+ initiallyFocused: options?.initiallyFocused ?? true,
16
+ }),
17
+ });
18
+ };
19
+
20
+ const countListeners = (client: Client) => {
21
+ const { emitter } = client.appManager;
22
+ return emitter.eventNames().reduce((sum, name) => sum + emitter.listenerCount(name), 0);
23
+ };
24
+
25
+ describe("useAppManager [ Events ]", () => {
26
+ describe("when the hook mounts", () => {
27
+ it("should read the online and focused state of the client", () => {
28
+ const client = createClient();
29
+ const { result } = renderHook(() => useAppManager(client));
30
+
31
+ expect(result.current.isOnline).toBeTrue();
32
+ expect(result.current.isFocused).toBeTrue();
33
+ });
34
+ it("should start offline when the client is offline", () => {
35
+ const client = createClient({ initiallyOnline: false });
36
+ const { result } = renderHook(() => useAppManager(client));
37
+
38
+ expect(result.current.isOnline).toBeFalse();
39
+ expect(result.current.isFocused).toBeTrue();
40
+ });
41
+ it("should start blurred when the client is not focused", () => {
42
+ const client = createClient({ initiallyFocused: false });
43
+ const { result } = renderHook(() => useAppManager(client));
44
+
45
+ expect(result.current.isFocused).toBeFalse();
46
+ expect(result.current.isOnline).toBeTrue();
47
+ });
48
+ it("should show the right state on the very first render", () => {
49
+ const client = createClient({ initiallyOnline: false, initiallyFocused: false });
50
+ const renders: string[] = [];
51
+
52
+ renderHook(() => {
53
+ const state = useAppManager(client);
54
+ renders.push(`${state.isOnline}:${state.isFocused}`);
55
+ return state;
56
+ });
57
+
58
+ expect(renders[0]).toBe("false:false");
59
+ });
60
+ it("should read the state changed before the mount", () => {
61
+ const client = createClient();
62
+ client.appManager.setOnline(false);
63
+ client.appManager.setFocused(false);
64
+
65
+ const { result } = renderHook(() => useAppManager(client));
66
+
67
+ expect(result.current.isOnline).toBeFalse();
68
+ expect(result.current.isFocused).toBeFalse();
69
+ });
70
+ });
71
+
72
+ describe("when the app manager emits events", () => {
73
+ it("should follow the offline and online events", () => {
74
+ const client = createClient();
75
+ const { result } = renderHook(() => useAppManager(client));
76
+
77
+ act(() => {
78
+ client.appManager.setOnline(false);
79
+ });
80
+ expect(result.current.isOnline).toBeFalse();
81
+
82
+ act(() => {
83
+ client.appManager.setOnline(true);
84
+ });
85
+ expect(result.current.isOnline).toBeTrue();
86
+ });
87
+ it("should follow the blur and focus events", () => {
88
+ const client = createClient();
89
+ const { result } = renderHook(() => useAppManager(client));
90
+
91
+ act(() => {
92
+ client.appManager.setFocused(false);
93
+ });
94
+ expect(result.current.isFocused).toBeFalse();
95
+
96
+ act(() => {
97
+ client.appManager.setFocused(true);
98
+ });
99
+ expect(result.current.isFocused).toBeTrue();
100
+ });
101
+ it("should keep the online and focused state apart", () => {
102
+ const client = createClient();
103
+ const { result } = renderHook(() => useAppManager(client));
104
+
105
+ act(() => {
106
+ client.appManager.setOnline(false);
107
+ });
108
+ expect(result.current.isFocused).toBeTrue();
109
+
110
+ act(() => {
111
+ client.appManager.setFocused(false);
112
+ client.appManager.setOnline(true);
113
+ });
114
+ expect(result.current.isOnline).toBeTrue();
115
+ expect(result.current.isFocused).toBeFalse();
116
+ });
117
+ it("should end with the last of many quick changes", () => {
118
+ const client = createClient();
119
+ const { result } = renderHook(() => useAppManager(client));
120
+
121
+ act(() => {
122
+ for (let i = 0; i < 25; i += 1) {
123
+ client.appManager.setOnline(i % 2 === 0);
124
+ client.appManager.setFocused(i % 2 === 1);
125
+ }
126
+ });
127
+
128
+ expect(result.current.isOnline).toBeTrue();
129
+ expect(result.current.isFocused).toBeFalse();
130
+ });
131
+ it("should not rerender when an event repeats the current state", () => {
132
+ const client = createClient();
133
+ let renders = 0;
134
+ renderHook(() => {
135
+ renders += 1;
136
+ return useAppManager(client);
137
+ });
138
+ const rendersAfterMount = renders;
139
+
140
+ act(() => {
141
+ client.appManager.setOnline(true);
142
+ client.appManager.setFocused(true);
143
+ });
144
+
145
+ expect(renders).toBe(rendersAfterMount);
146
+ });
147
+ it("should follow the online and offline events of the window", () => {
148
+ const client = new Client({ url: "http://app-manager" });
149
+ const { result } = renderHook(() => useAppManager(client));
150
+
151
+ act(() => {
152
+ window.dispatchEvent(new Event("offline"));
153
+ });
154
+ expect(result.current.isOnline).toBeFalse();
155
+
156
+ act(() => {
157
+ window.dispatchEvent(new Event("online"));
158
+ });
159
+ expect(result.current.isOnline).toBeTrue();
160
+ client.appManager.unmount();
161
+ });
162
+ it("should follow the blur and focus events of the window", () => {
163
+ const client = new Client({ url: "http://app-manager" });
164
+ const { result } = renderHook(() => useAppManager(client));
165
+
166
+ act(() => {
167
+ window.dispatchEvent(new Event("blur"));
168
+ });
169
+ expect(result.current.isFocused).toBeFalse();
170
+
171
+ act(() => {
172
+ window.dispatchEvent(new Event("focus"));
173
+ });
174
+ expect(result.current.isFocused).toBeTrue();
175
+ client.appManager.unmount();
176
+ });
177
+ });
178
+
179
+ describe("when using the setters", () => {
180
+ it("should change the online state of the client", () => {
181
+ const client = createClient();
182
+ const { result } = renderHook(() => useAppManager(client));
183
+
184
+ act(() => {
185
+ result.current.setOnline(false);
186
+ });
187
+
188
+ expect(result.current.isOnline).toBeFalse();
189
+ expect(client.appManager.isOnline).toBeFalse();
190
+ });
191
+ it("should change the focused state of the client", () => {
192
+ const client = createClient();
193
+ const { result } = renderHook(() => useAppManager(client));
194
+
195
+ act(() => {
196
+ result.current.setFocused(false);
197
+ });
198
+
199
+ expect(result.current.isFocused).toBeFalse();
200
+ expect(client.appManager.isFocused).toBeFalse();
201
+ });
202
+ it("should bring the state back", () => {
203
+ const client = createClient({ initiallyOnline: false, initiallyFocused: false });
204
+ const { result } = renderHook(() => useAppManager(client));
205
+
206
+ act(() => {
207
+ result.current.setOnline(true);
208
+ result.current.setFocused(true);
209
+ });
210
+
211
+ expect(result.current.isOnline).toBeTrue();
212
+ expect(result.current.isFocused).toBeTrue();
213
+ });
214
+ it("should notify the other listeners of the app manager", () => {
215
+ const client = createClient();
216
+ const onOffline = vi.fn();
217
+ const onBlur = vi.fn();
218
+ client.appManager.events.onOffline(onOffline);
219
+ client.appManager.events.onBlur(onBlur);
220
+ const { result } = renderHook(() => useAppManager(client));
221
+
222
+ act(() => {
223
+ result.current.setOnline(false);
224
+ result.current.setFocused(false);
225
+ });
226
+
227
+ expect(onOffline).toHaveBeenCalledTimes(1);
228
+ expect(onBlur).toHaveBeenCalledTimes(1);
229
+ });
230
+ it("should update every hook using the same client", () => {
231
+ const client = createClient();
232
+ const first = renderHook(() => useAppManager(client));
233
+ const second = renderHook(() => useAppManager(client));
234
+
235
+ act(() => {
236
+ first.result.current.setOnline(false);
237
+ second.result.current.setFocused(false);
238
+ });
239
+
240
+ expect(first.result.current.isOnline).toBeFalse();
241
+ expect(second.result.current.isOnline).toBeFalse();
242
+ expect(first.result.current.isFocused).toBeFalse();
243
+ expect(second.result.current.isFocused).toBeFalse();
244
+ });
245
+ it("should not touch the hooks of another client", () => {
246
+ const client = createClient();
247
+ const otherClient = createClient();
248
+ const view = renderHook(() => useAppManager(client));
249
+ const other = renderHook(() => useAppManager(otherClient));
250
+
251
+ act(() => {
252
+ view.result.current.setOnline(false);
253
+ });
254
+
255
+ expect(other.result.current.isOnline).toBeTrue();
256
+ expect(otherClient.appManager.isOnline).toBeTrue();
257
+ });
258
+ it("should update a component from a button click", () => {
259
+ const client = createClient();
260
+ function Status() {
261
+ const { isOnline, setOnline } = useAppManager(client);
262
+ return (
263
+ <button type="button" onClick={() => setOnline(!isOnline)}>
264
+ {isOnline ? "online" : "offline"}
265
+ </button>
266
+ );
267
+ }
268
+ render(<Status />);
269
+
270
+ act(() => {
271
+ screen.getByRole("button").click();
272
+ });
273
+ expect(screen.getByRole("button").textContent).toBe("offline");
274
+
275
+ act(() => {
276
+ screen.getByRole("button").click();
277
+ });
278
+ expect(screen.getByRole("button").textContent).toBe("online");
279
+ });
280
+ });
281
+
282
+ describe("when the client changes", () => {
283
+ it("should show the state of the new client", () => {
284
+ const client = createClient();
285
+ const offlineClient = createClient({ initiallyOnline: false, initiallyFocused: false });
286
+ const { result, rerender } = renderHook(({ current }: { current: Client }) => useAppManager(current), {
287
+ initialProps: { current: client },
288
+ });
289
+
290
+ rerender({ current: offlineClient });
291
+
292
+ expect(result.current.isOnline).toBeFalse();
293
+ expect(result.current.isFocused).toBeFalse();
294
+ });
295
+ it("should follow the events of the new client only", () => {
296
+ const client = createClient();
297
+ const nextClient = createClient();
298
+ const { result, rerender } = renderHook(({ current }: { current: Client }) => useAppManager(current), {
299
+ initialProps: { current: client },
300
+ });
301
+ rerender({ current: nextClient });
302
+
303
+ act(() => {
304
+ client.appManager.setOnline(false);
305
+ });
306
+ expect(result.current.isOnline).toBeTrue();
307
+
308
+ act(() => {
309
+ nextClient.appManager.setFocused(false);
310
+ });
311
+ expect(result.current.isFocused).toBeFalse();
312
+ });
313
+ it("should use the setters of the new client", () => {
314
+ const client = createClient();
315
+ const nextClient = createClient();
316
+ const { result, rerender } = renderHook(({ current }: { current: Client }) => useAppManager(current), {
317
+ initialProps: { current: client },
318
+ });
319
+ rerender({ current: nextClient });
320
+
321
+ act(() => {
322
+ result.current.setOnline(false);
323
+ });
324
+
325
+ expect(nextClient.appManager.isOnline).toBeFalse();
326
+ expect(client.appManager.isOnline).toBeTrue();
327
+ });
328
+ it("should remove the listeners from the previous client", () => {
329
+ const client = createClient();
330
+ const nextClient = createClient();
331
+ const baseline = countListeners(client);
332
+ const { rerender } = renderHook(({ current }: { current: Client }) => useAppManager(current), {
333
+ initialProps: { current: client },
334
+ });
335
+
336
+ rerender({ current: nextClient });
337
+
338
+ expect(countListeners(client)).toBe(baseline);
339
+ });
340
+ });
341
+
342
+ describe("when the hook unmounts", () => {
343
+ it("should remove every listener", () => {
344
+ const client = createClient();
345
+ const baseline = countListeners(client);
346
+ const { unmount } = renderHook(() => useAppManager(client));
347
+ expect(countListeners(client)).toBe(baseline + 4);
348
+
349
+ unmount();
350
+
351
+ expect(countListeners(client)).toBe(baseline);
352
+ });
353
+ it("should not update or warn after the unmount", () => {
354
+ const errorSpy = vi.spyOn(console, "error").mockImplementation(() => null);
355
+ const client = createClient();
356
+ const { result, unmount } = renderHook(() => useAppManager(client));
357
+
358
+ unmount();
359
+ client.appManager.setOnline(false);
360
+ client.appManager.setFocused(false);
361
+
362
+ expect(result.current.isOnline).toBeTrue();
363
+ expect(result.current.isFocused).toBeTrue();
364
+ expect(errorSpy).not.toHaveBeenCalled();
365
+ errorSpy.mockRestore();
366
+ });
367
+ it("should keep the other hooks subscribed", () => {
368
+ const client = createClient();
369
+ const first = renderHook(() => useAppManager(client));
370
+ const second = renderHook(() => useAppManager(client));
371
+
372
+ first.unmount();
373
+ act(() => {
374
+ client.appManager.setOnline(false);
375
+ });
376
+
377
+ expect(second.result.current.isOnline).toBeFalse();
378
+ });
379
+ it("should read the current state when mounted again", () => {
380
+ const client = createClient();
381
+ const first = renderHook(() => useAppManager(client));
382
+ first.unmount();
383
+ client.appManager.setOnline(false);
384
+
385
+ const second = renderHook(() => useAppManager(client));
386
+
387
+ expect(second.result.current.isOnline).toBeFalse();
388
+ });
389
+ it("should not add listeners over many rerenders", () => {
390
+ const client = createClient();
391
+ const { rerender } = renderHook(() => useAppManager(client));
392
+ const mounted = countListeners(client);
393
+
394
+ for (let i = 0; i < 10; i += 1) {
395
+ rerender();
396
+ }
397
+ act(() => {
398
+ client.appManager.setOnline(false);
399
+ client.appManager.setOnline(true);
400
+ });
401
+
402
+ expect(countListeners(client)).toBe(mounted);
403
+ });
404
+ });
405
+
406
+ describe("when rendered in StrictMode", () => {
407
+ it("should follow the events after the double mount", () => {
408
+ const client = createClient();
409
+ const { result } = renderHook(() => useAppManager(client), { wrapper: StrictMode });
410
+
411
+ act(() => {
412
+ client.appManager.setOnline(false);
413
+ client.appManager.setFocused(false);
414
+ });
415
+
416
+ expect(result.current.isOnline).toBeFalse();
417
+ expect(result.current.isFocused).toBeFalse();
418
+ });
419
+ it("should subscribe once", () => {
420
+ const client = createClient();
421
+ const baseline = countListeners(client);
422
+
423
+ renderHook(() => useAppManager(client), { wrapper: StrictMode });
424
+
425
+ expect(countListeners(client)).toBe(baseline + 4);
426
+ });
427
+ it("should remove every listener on unmount", () => {
428
+ const client = createClient();
429
+ const baseline = countListeners(client);
430
+ const { unmount } = renderHook(() => useAppManager(client), { wrapper: StrictMode });
431
+
432
+ unmount();
433
+
434
+ expect(countListeners(client)).toBe(baseline);
435
+ });
436
+ });
437
+
438
+ describe("when rendered on the server", () => {
439
+ function Status({ client }: { client: Client }) {
440
+ const { isOnline, isFocused } = useAppManager(client);
441
+ return (
442
+ <p>
443
+ {isOnline ? "online" : "offline"}-{isFocused ? "focused" : "blurred"}
444
+ </p>
445
+ );
446
+ }
447
+
448
+ it("should render the state of the client to a string", () => {
449
+ const client = createClient();
450
+
451
+ expect(renderToString(<Status client={client} />)).toContain("online");
452
+ expect(renderToString(<Status client={client} />)).toContain("focused");
453
+ });
454
+ it("should render the offline state to a string", () => {
455
+ const client = createClient({ initiallyOnline: false, initiallyFocused: false });
456
+ const html = renderToString(<Status client={client} />);
457
+
458
+ expect(html).toContain("offline");
459
+ expect(html).toContain("blurred");
460
+ });
461
+ it("should not subscribe during the server render", () => {
462
+ const client = createClient();
463
+ const baseline = countListeners(client);
464
+
465
+ renderToString(<Status client={client} />);
466
+
467
+ expect(countListeners(client)).toBe(baseline);
468
+ });
469
+ it("should render without a window", () => {
470
+ const client = createClient();
471
+ const noWindow: unknown = undefined;
472
+ vi.stubGlobal("window", noWindow);
473
+
474
+ let html = "";
475
+ try {
476
+ html = renderToString(<Status client={client} />);
477
+ } finally {
478
+ vi.unstubAllGlobals();
479
+ }
480
+
481
+ expect(html).toContain("online");
482
+ });
483
+ });
484
+
485
+ describe("types", () => {
486
+ it("should type the returned state and setters", () => {
487
+ expectTypeOf<UseAppManagerReturnType["isOnline"]>().toEqualTypeOf<boolean>();
488
+ expectTypeOf<UseAppManagerReturnType["isFocused"]>().toEqualTypeOf<boolean>();
489
+ expectTypeOf<UseAppManagerReturnType["setOnline"]>().toEqualTypeOf<(isOnline: boolean) => void>();
490
+ expectTypeOf<UseAppManagerReturnType["setFocused"]>().toEqualTypeOf<(isFocused: boolean) => void>();
491
+ });
492
+ it("should accept any client", () => {
493
+ expectTypeOf(useAppManager).toBeCallableWith(createClient());
494
+ expectTypeOf(useAppManager).returns.toEqualTypeOf<UseAppManagerReturnType>();
495
+ });
496
+ });
497
+ });