@hyper-fetch/react 7.5.1 → 7.5.3

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 (70) hide show
  1. package/__tests__/features/helpers/use-request-events/use-request-events.base.spec.ts +115 -0
  2. package/__tests__/features/helpers/use-request-events/use-request-events.utils.spec.ts +69 -0
  3. package/__tests__/features/helpers/use-socket-state/use-socket-state.base.spec.ts +175 -0
  4. package/__tests__/features/helpers/use-tracked-state/use-tacked-state.actions.spec.ts +382 -0
  5. package/__tests__/features/helpers/use-tracked-state/use-tracked-state.base.spec.ts +527 -0
  6. package/__tests__/features/helpers/use-tracked-state/use-tracked-state.utils.spec.ts +260 -0
  7. package/__tests__/features/provider/provider.hydration.spec.tsx +161 -0
  8. package/__tests__/features/provider/use-config-provider.base.spec.tsx +68 -0
  9. package/__tests__/features/use-app-manager/use-app-manager.base.spec.ts +85 -0
  10. package/__tests__/features/use-cache/use-cache.base.spec.ts +133 -0
  11. package/__tests__/features/use-cache/use-cache.helpers.ts +40 -0
  12. package/__tests__/features/use-emitter/use-emitter.base.spec.ts +124 -0
  13. package/__tests__/features/use-fetch/use-fetch.base.spec.ts +284 -0
  14. package/__tests__/features/use-fetch/use-fetch.cancel.spec.ts +83 -0
  15. package/__tests__/features/use-fetch/use-fetch.concurrency.spec.ts +91 -0
  16. package/__tests__/features/use-fetch/use-fetch.debounce.spec.ts +153 -0
  17. package/__tests__/features/use-fetch/use-fetch.deduplication.spec.ts +80 -0
  18. package/__tests__/features/use-fetch/use-fetch.infinite.ts +39 -0
  19. package/__tests__/features/use-fetch/use-fetch.integration.spec.tsx +172 -0
  20. package/__tests__/features/use-fetch/use-fetch.methods.spec.ts +233 -0
  21. package/__tests__/features/use-fetch/use-fetch.offline.spec.ts +103 -0
  22. package/__tests__/features/use-fetch/use-fetch.pagination.ts +36 -0
  23. package/__tests__/features/use-fetch/use-fetch.prefetch.spec.tsx +73 -0
  24. package/__tests__/features/use-fetch/use-fetch.refresh.spec.ts +153 -0
  25. package/__tests__/features/use-fetch/use-fetch.rerender.spec.tsx +151 -0
  26. package/__tests__/features/use-fetch/use-fetch.retry.ts +38 -0
  27. package/__tests__/features/use-fetch/use-fetch.revalidate.spec.ts +235 -0
  28. package/__tests__/features/use-fetch/use-fetch.suspense.ts +37 -0
  29. package/__tests__/features/use-fetch/use-fetch.typescript.spec.ts +59 -0
  30. package/__tests__/features/use-fetch/use-fetch.utils.spec.ts +26 -0
  31. package/__tests__/features/use-fetch/use-fetch.window.spec.ts +59 -0
  32. package/__tests__/features/use-listener/use-listener.base.spec.ts +57 -0
  33. package/__tests__/features/use-queue/use-queue.actions.spec.ts +238 -0
  34. package/__tests__/features/use-queue/use-queue.base.spec.ts +109 -0
  35. package/__tests__/features/use-submit/use-submit.base.spec.ts +76 -0
  36. package/__tests__/features/use-submit/use-submit.cancel.spec.ts +46 -0
  37. package/__tests__/features/use-submit/use-submit.concurrency.spec.ts +62 -0
  38. package/__tests__/features/use-submit/use-submit.debounce.spec.ts +320 -0
  39. package/__tests__/features/use-submit/use-submit.methods.spec.ts +278 -0
  40. package/__tests__/features/use-submit/use-submit.queue.spec.ts +84 -0
  41. package/__tests__/features/use-submit/use-submit.retry.spec.ts +92 -0
  42. package/__tests__/features/use-submit/use-submit.revalidate.spec.ts +99 -0
  43. package/__tests__/features/use-submit/use-submit.submit.spec.ts +233 -0
  44. package/__tests__/global.d.ts +1 -0
  45. package/__tests__/jest.setup.ts +9 -0
  46. package/__tests__/shared/index.ts +2 -0
  47. package/__tests__/shared/isolation.tests.ts +12 -0
  48. package/__tests__/shared/response.tests.ts +127 -0
  49. package/__tests__/tsconfig.json +8 -0
  50. package/__tests__/utils/cache.utils.ts +53 -0
  51. package/__tests__/utils/client.utils.ts +3 -0
  52. package/__tests__/utils/emitter.utils.ts +7 -0
  53. package/__tests__/utils/helpers.utils.ts +9 -0
  54. package/__tests__/utils/index.ts +14 -0
  55. package/__tests__/utils/listener.utils.ts +10 -0
  56. package/__tests__/utils/request.utils.ts +12 -0
  57. package/__tests__/utils/state.utils.ts +7 -0
  58. package/__tests__/utils/use-app-manager.utils.ts +10 -0
  59. package/__tests__/utils/use-cache.utils.ts +11 -0
  60. package/__tests__/utils/use-emitter.utils.ts +11 -0
  61. package/__tests__/utils/use-fetch.utils.ts +11 -0
  62. package/__tests__/utils/use-listener.utils.ts +11 -0
  63. package/__tests__/utils/use-queue.utils.ts +68 -0
  64. package/__tests__/utils/use-request-events.utils.ts +42 -0
  65. package/__tests__/utils/use-socket-state.utils.ts +11 -0
  66. package/__tests__/utils/use-submit.utils.ts +11 -0
  67. package/__tests__/utils/use-tracked-state.utils.ts +26 -0
  68. package/jest.config.ts +6 -0
  69. package/package.json +1 -1
  70. package/project.json +4 -0
@@ -0,0 +1,10 @@
1
+ import { ListenerOptionsType, Socket, SocketAdapterType } from "@hyper-fetch/sockets";
2
+
3
+ export const createListener = <ResponseType = { name: string; age: number }>(
4
+ options?: Partial<ListenerOptionsType<any, SocketAdapterType>>,
5
+ ) => {
6
+ return new Socket({ url: "ws://localhost:1234" }).createListener<ResponseType>()({
7
+ ...options,
8
+ topic: "some-event",
9
+ });
10
+ };
@@ -0,0 +1,12 @@
1
+ import { RequestOptionsType, HttpAdapterOptionsType } from "@hyper-fetch/core";
2
+
3
+ import { client } from "./client.utils";
4
+
5
+ export const createRequest = <ResponseType = any, PayloadType = any>(
6
+ options?: Partial<RequestOptionsType<string, HttpAdapterOptionsType>>,
7
+ ) => {
8
+ return client.createRequest<{ response: ResponseType; payload: PayloadType; error: Error }>()({
9
+ endpoint: "/shared-endpoint",
10
+ ...options,
11
+ });
12
+ };
@@ -0,0 +1,7 @@
1
+ import { RenderHookResult } from "@testing-library/react";
2
+
3
+ export const getCurrentState = <T extends RenderHookResult<any, any>>(
4
+ render: T,
5
+ ): T extends RenderHookResult<any, infer State> ? State : never => {
6
+ return render.result?.current || {};
7
+ };
@@ -0,0 +1,10 @@
1
+ import { ClientInstance } from "@hyper-fetch/core";
2
+ import { renderHook } from "@testing-library/react";
3
+
4
+ import { useAppManager } from "hooks/use-app-manager";
5
+
6
+ export const renderUseAppManager = <B extends ClientInstance>(client: B) => {
7
+ return renderHook(() => {
8
+ return useAppManager(client);
9
+ });
10
+ };
@@ -0,0 +1,11 @@
1
+ import { RequestInstance } from "@hyper-fetch/core";
2
+ import { renderHook } from "@testing-library/react";
3
+
4
+ import { useCache, UseCacheOptionsType } from "hooks/use-cache";
5
+
6
+ export const renderUseCache = <T extends RequestInstance>(request: T, options?: UseCacheOptionsType<T>) => {
7
+ return renderHook((rerenderOptions: UseCacheOptionsType<RequestInstance> & { request?: RequestInstance }) => {
8
+ const { request: cmd, ...rest } = rerenderOptions || {};
9
+ return useCache(cmd || request, { dependencyTracking: false, ...options, ...rest });
10
+ });
11
+ };
@@ -0,0 +1,11 @@
1
+ import { renderHook } from "@testing-library/react";
2
+ import { EmitterInstance } from "@hyper-fetch/sockets";
3
+
4
+ import { useEmitter, UseEmitterOptionsType } from "hooks/use-emitter";
5
+
6
+ export const renderUseEmitter = <T extends EmitterInstance>(Emitter: T, options?: UseEmitterOptionsType) => {
7
+ return renderHook((rerenderOptions: UseEmitterOptionsType & { emitter?: EmitterInstance }) => {
8
+ const { emitter: lst, ...rest } = rerenderOptions || {};
9
+ return useEmitter((lst || Emitter) as T, { dependencyTracking: false, ...options, ...rest });
10
+ });
11
+ };
@@ -0,0 +1,11 @@
1
+ import { RequestInstance } from "@hyper-fetch/core";
2
+ import { renderHook } from "@testing-library/react";
3
+
4
+ import { useFetch, UseFetchOptionsType } from "hooks/use-fetch";
5
+
6
+ export const renderUseFetch = <T extends RequestInstance>(request: T, options?: UseFetchOptionsType<T>) => {
7
+ return renderHook((rerenderOptions: UseFetchOptionsType<RequestInstance> & { request?: RequestInstance }) => {
8
+ const { request: req, ...rest } = rerenderOptions || {};
9
+ return useFetch(req || request, { dependencyTracking: false, ...options, ...rest });
10
+ });
11
+ };
@@ -0,0 +1,11 @@
1
+ import { renderHook } from "@testing-library/react";
2
+ import { ListenerInstance } from "@hyper-fetch/sockets";
3
+
4
+ import { useListener, UseListenerOptionsType } from "hooks/use-listener";
5
+
6
+ export const renderUseListener = <T extends ListenerInstance>(listener: T, options?: UseListenerOptionsType) => {
7
+ return renderHook((rerenderOptions: UseListenerOptionsType & { listener?: ListenerInstance }) => {
8
+ const { listener: lst, ...rest } = rerenderOptions || {};
9
+ return useListener(lst || listener, { dependencyTracking: false, ...options, ...rest });
10
+ });
11
+ };
@@ -0,0 +1,68 @@
1
+ import { renderHook } from "@testing-library/react";
2
+ import { RequestInstance, getRequestDispatcher, getProgressData } from "@hyper-fetch/core";
3
+
4
+ import { useQueue, UseQueueOptionsType } from "hooks/use-queue";
5
+
6
+ export const renderUseQueue = <T extends RequestInstance>(request: T, options?: UseQueueOptionsType) => {
7
+ return renderHook((rerenderOptions: UseQueueOptionsType & { request?: RequestInstance }) => {
8
+ const { request: cmd, ...rest } = rerenderOptions || {};
9
+ return useQueue(cmd || request, { ...options, ...rest });
10
+ });
11
+ };
12
+
13
+ export const addQueueElement = <T extends RequestInstance>(
14
+ request: T,
15
+ options?: {
16
+ stop: boolean;
17
+ dispatcherType?: "fetch" | "submit" | "auto";
18
+ },
19
+ ) => {
20
+ const { stop = false, dispatcherType = "auto" } = options || {};
21
+ const [dispatcher] = getRequestDispatcher(request, dispatcherType);
22
+ if (stop) dispatcher.stop(request.queryKey);
23
+ return dispatcher.add(request);
24
+ };
25
+
26
+ export const emitDownloadProgress = <T extends RequestInstance>(
27
+ requestId: string,
28
+ request: T,
29
+ options?: {
30
+ total: number;
31
+ loaded: number;
32
+ },
33
+ ) => {
34
+ const values = options || { total: 50, loaded: 25 };
35
+ const startTimestamp = new Date(+new Date() - 20000);
36
+ const progressTimestamp = new Date();
37
+
38
+ const progress = getProgressData(startTimestamp, progressTimestamp, values);
39
+ request.client.requestManager.events.emitDownloadProgress({
40
+ ...progress,
41
+ requestId,
42
+ request,
43
+ });
44
+
45
+ return [startTimestamp, progressTimestamp];
46
+ };
47
+
48
+ export const emitUploadProgress = <T extends RequestInstance>(
49
+ requestId: string,
50
+ request: T,
51
+ options?: {
52
+ total: number;
53
+ loaded: number;
54
+ },
55
+ ) => {
56
+ const values = options || { total: 50, loaded: 25 };
57
+ const startTimestamp = new Date(+new Date() - 20000);
58
+ const progressTimestamp = new Date();
59
+
60
+ const progress = getProgressData(startTimestamp, progressTimestamp, values);
61
+ request.client.requestManager.events.emitUploadProgress({
62
+ ...progress,
63
+ requestId,
64
+ request,
65
+ });
66
+
67
+ return [startTimestamp, progressTimestamp];
68
+ };
@@ -0,0 +1,42 @@
1
+ import { RequestInstance } from "@hyper-fetch/core";
2
+ import { renderHook } from "@testing-library/react";
3
+
4
+ import { useTrackedState, useRequestEvents, UseRequestEventsPropsType, UseTrackedStateProps } from "helpers";
5
+ import { isEqual } from "utils";
6
+
7
+ export const renderUseRequestEvents = <Request extends RequestInstance>(
8
+ request: Request,
9
+ options?: Partial<UseRequestEventsPropsType<Request>>,
10
+ trackedOptions?: Partial<UseTrackedStateProps<Request>>,
11
+ ) => {
12
+ const { client } = request;
13
+ const { fetchDispatcher: dispatcher, loggerManager } = client;
14
+
15
+ const logger = loggerManager.initialize(client, "test");
16
+
17
+ const { result } = renderHook(() => {
18
+ return useTrackedState({
19
+ logger,
20
+ request,
21
+ dispatcher,
22
+ initialResponse: null,
23
+ deepCompare: isEqual,
24
+ dependencyTracking: false,
25
+ ...trackedOptions,
26
+ });
27
+ });
28
+ const [, actions, { setCacheData, getIsDataProcessing }] = result.current;
29
+
30
+ return renderHook((args?: Partial<Parameters<typeof useRequestEvents>[0]>) => {
31
+ return useRequestEvents({
32
+ logger,
33
+ actions,
34
+ dispatcher,
35
+ setCacheData,
36
+ getIsDataProcessing,
37
+ ...options,
38
+ ...args,
39
+ request,
40
+ });
41
+ });
42
+ };
@@ -0,0 +1,11 @@
1
+ import { Socket } from "@hyper-fetch/sockets";
2
+ import { renderHook } from "@testing-library/react";
3
+
4
+ import { useSocketState } from "helpers";
5
+
6
+ export const renderUseSocketState = (socket: Socket, { onRender }: { onRender?: jest.Mock } = {}) => {
7
+ return renderHook(() => {
8
+ onRender?.();
9
+ return useSocketState(socket, { dependencyTracking: true });
10
+ });
11
+ };
@@ -0,0 +1,11 @@
1
+ import { RequestInstance } from "@hyper-fetch/core";
2
+ import { renderHook } from "@testing-library/react";
3
+
4
+ import { useSubmit, UseSubmitOptionsType } from "hooks/use-submit";
5
+
6
+ export const renderUseSubmit = <T extends RequestInstance>(request: T, options?: UseSubmitOptionsType<T>) => {
7
+ return renderHook((rerenderOptions: UseSubmitOptionsType<T> & { request?: T }) => {
8
+ const { request: cmd, ...rest } = rerenderOptions || {};
9
+ return useSubmit(cmd || request, { dependencyTracking: false, ...options, ...rest });
10
+ });
11
+ };
@@ -0,0 +1,26 @@
1
+ import { RequestInstance } from "@hyper-fetch/core";
2
+ import { renderHook } from "@testing-library/react";
3
+
4
+ import { useTrackedState, UseTrackedStateProps } from "helpers";
5
+ import { isEqual } from "utils";
6
+
7
+ export const renderUseTrackedState = <Request extends RequestInstance>(
8
+ request: Request,
9
+ options?: Partial<UseTrackedStateProps<Request>>,
10
+ ) => {
11
+ const { client } = request;
12
+ const { fetchDispatcher: dispatcher, loggerManager } = client;
13
+
14
+ const logger = loggerManager.initialize(client, "test");
15
+ return renderHook(() => {
16
+ return useTrackedState({
17
+ logger,
18
+ request,
19
+ dispatcher,
20
+ initialResponse: null,
21
+ deepCompare: isEqual,
22
+ dependencyTracking: false,
23
+ ...options,
24
+ });
25
+ });
26
+ };
package/jest.config.ts ADDED
@@ -0,0 +1,6 @@
1
+ import type { Config } from "@jest/types";
2
+
3
+ import { getJestConfig } from "../../jest.config";
4
+
5
+ const config: Config.InitialOptions = getJestConfig();
6
+ export default config;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hyper-fetch/react",
3
- "version": "7.5.1",
3
+ "version": "7.5.3",
4
4
  "description": "React hooks and utils for the hyper-fetch",
5
5
  "author": "Maciej Pyrc <maciekpyrc@gmail.com>, Kacper Skawina <kacper.skawina@gmail.com>",
6
6
  "homepage": "https://hyperfetch.bettertyped.com/",
package/project.json ADDED
@@ -0,0 +1,4 @@
1
+ {
2
+ "name": "react",
3
+ "$schema": "../../node_modules/nx/schemas/project-schema.json"
4
+ }