@hyper-fetch/react 7.5.3 → 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 +4 -4
- package/__tests__/features/helpers/use-request-events/use-request-events.utils.spec.ts +3 -3
- package/__tests__/features/helpers/use-socket-state/use-socket-state.base.spec.ts +44 -6
- package/__tests__/features/helpers/use-tracked-state/use-tacked-state.actions.spec.ts +177 -9
- package/__tests__/features/helpers/use-tracked-state/use-tracked-state.base.spec.ts +47 -8
- package/__tests__/features/helpers/use-tracked-state/use-tracked-state.utils.spec.ts +6 -6
- package/__tests__/features/provider/provider.hydration.spec.tsx +7 -3
- package/__tests__/features/provider/use-config-provider.base.spec.tsx +2 -1
- package/__tests__/features/use-app-manager/use-app-manager.base.spec.ts +1 -1
- package/__tests__/features/use-cache/use-cache.base.spec.ts +9 -6
- package/__tests__/features/use-cache/use-cache.helpers.ts +1 -1
- package/__tests__/features/use-cache/use-cache.typescript.spec.ts +111 -0
- package/__tests__/features/use-emitter/use-emitter.base.spec.ts +2 -2
- package/__tests__/features/use-fetch/use-fetch.base.spec.ts +103 -15
- package/__tests__/features/use-fetch/use-fetch.cancel.spec.ts +2 -2
- package/__tests__/features/use-fetch/use-fetch.clear-state.spec.ts +82 -0
- package/__tests__/features/use-fetch/use-fetch.concurrency.spec.ts +3 -3
- package/__tests__/features/use-fetch/use-fetch.debounce.spec.ts +5 -5
- package/__tests__/features/use-fetch/use-fetch.deduplication.spec.ts +1 -1
- package/__tests__/features/use-fetch/use-fetch.infinite.ts +1 -1
- package/__tests__/features/use-fetch/use-fetch.integration.spec.tsx +3 -3
- package/__tests__/features/use-fetch/use-fetch.keep-previous-data.spec.ts +215 -0
- package/__tests__/features/use-fetch/use-fetch.methods.spec.ts +34 -34
- package/__tests__/features/use-fetch/use-fetch.offline.spec.ts +3 -3
- package/__tests__/features/use-fetch/use-fetch.pagination.ts +1 -1
- package/__tests__/features/use-fetch/use-fetch.prefetch.spec.tsx +1 -1
- package/__tests__/features/use-fetch/use-fetch.refresh.spec.ts +41 -7
- package/__tests__/features/use-fetch/use-fetch.rerender.spec.tsx +2 -2
- package/__tests__/features/use-fetch/use-fetch.retry.ts +1 -1
- package/__tests__/features/use-fetch/use-fetch.revalidate.spec.ts +6 -4
- package/__tests__/features/use-fetch/use-fetch.suspense.spec.tsx +505 -0
- package/__tests__/features/use-fetch/use-fetch.typescript.spec.ts +179 -56
- package/__tests__/features/use-fetch/use-fetch.window.spec.ts +75 -3
- package/__tests__/features/use-listener/use-listener.base.spec.ts +3 -3
- package/__tests__/features/use-queue/use-queue.actions.spec.ts +1 -1
- package/__tests__/features/use-queue/use-queue.base.spec.ts +259 -1
- 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 +2 -2
- package/__tests__/features/use-submit/use-submit.cancel.spec.ts +1 -1
- package/__tests__/features/use-submit/use-submit.concurrency.spec.ts +1 -1
- package/__tests__/features/use-submit/use-submit.debounce.spec.ts +8 -8
- package/__tests__/features/use-submit/use-submit.methods.spec.ts +44 -46
- package/__tests__/features/use-submit/use-submit.optimistic.spec.ts +548 -0
- package/__tests__/features/use-submit/use-submit.queue.spec.ts +3 -3
- package/__tests__/features/use-submit/use-submit.retry.spec.ts +4 -4
- package/__tests__/features/use-submit/use-submit.revalidate.spec.ts +5 -5
- package/__tests__/features/use-submit/use-submit.submit.spec.ts +4 -3
- package/__tests__/features/use-submit/use-submit.typescript.spec.ts +325 -0
- package/__tests__/shared/isolation.tests.ts +1 -1
- package/__tests__/shared/response.tests.ts +5 -4
- package/__tests__/tsconfig.json +21 -2
- package/__tests__/utils/cache.utils.ts +6 -4
- package/__tests__/utils/emitter.utils.ts +3 -2
- package/__tests__/utils/listener.utils.ts +3 -2
- package/__tests__/utils/request.utils.ts +6 -6
- package/__tests__/utils/state.utils.ts +1 -1
- package/__tests__/utils/use-app-manager.utils.ts +1 -1
- package/__tests__/utils/use-cache.utils.ts +3 -2
- package/__tests__/utils/use-emitter.utils.ts +3 -2
- package/__tests__/utils/use-fetch.utils.ts +4 -3
- package/__tests__/utils/use-listener.utils.ts +3 -2
- package/__tests__/utils/use-queue.utils.ts +4 -2
- package/__tests__/utils/use-request-events.utils.ts +3 -2
- package/__tests__/utils/use-socket-state.utils.ts +3 -2
- package/__tests__/utils/use-submit.utils.ts +3 -2
- package/__tests__/utils/use-tracked-state.utils.ts +3 -2
- 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/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/__tests__/features/use-fetch/use-fetch.suspense.ts +0 -37
- package/__tests__/global.d.ts +0 -1
- package/__tests__/jest.setup.ts +0 -9
- package/dist/index.js +0 -1541
- package/dist/index.js.map +0 -7
- package/jest.config.ts +0 -6
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
/* eslint-disable react/jsx-fragments */
|
|
2
2
|
/* eslint-disable react/jsx-no-useless-fragment */
|
|
3
3
|
import { Fragment } from "react";
|
|
4
|
-
import { HttpAdapterExtraType
|
|
4
|
+
import type { HttpAdapterExtraType } from "@hyper-fetch/core";
|
|
5
|
+
import { Client } from "@hyper-fetch/core";
|
|
5
6
|
import { render, waitFor } from "@testing-library/react";
|
|
6
7
|
|
|
7
|
-
import { UseFetchReturnType
|
|
8
|
-
import {
|
|
8
|
+
import type { UseFetchReturnType } from "hooks/use-fetch";
|
|
9
|
+
import { useFetch } from "hooks/use-fetch";
|
|
10
|
+
import type { UseSubmitReturnType } from "hooks/use-submit";
|
|
11
|
+
import { useSubmit } from "hooks/use-submit";
|
|
9
12
|
import { Provider } from "provider";
|
|
10
13
|
|
|
11
14
|
describe("Provider [ Hydration ]", () => {
|
|
@@ -22,6 +25,7 @@ describe("Provider [ Hydration ]", () => {
|
|
|
22
25
|
cacheKey: request.cacheKey,
|
|
23
26
|
cache: true,
|
|
24
27
|
cacheTime: Infinity,
|
|
28
|
+
scope: null,
|
|
25
29
|
staleTime: 1000,
|
|
26
30
|
timestamp: Date.now(),
|
|
27
31
|
response: {
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { act, render, screen, waitFor } from "@testing-library/react";
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import type { ProviderValueType } from "provider";
|
|
4
|
+
import { Provider, useProvider } from "provider";
|
|
4
5
|
|
|
5
6
|
describe("useProvider [ Base ]", () => {
|
|
6
7
|
const text = "test";
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { act } from "@testing-library/react";
|
|
2
|
-
import { CacheValueType } from "@hyper-fetch/core";
|
|
2
|
+
import type { CacheValueType } from "@hyper-fetch/core";
|
|
3
3
|
import { createHttpMockingServer } from "@hyper-fetch/testing";
|
|
4
4
|
|
|
5
5
|
import { client, createRequest } from "../../utils";
|
|
@@ -26,11 +26,14 @@ describe("useCache [ Base ]", () => {
|
|
|
26
26
|
addedTimestamp: +new Date(),
|
|
27
27
|
triggerTimestamp: +new Date(),
|
|
28
28
|
cacheKey: request.cacheKey,
|
|
29
|
+
scope: request.scope,
|
|
29
30
|
isCanceled: false,
|
|
30
31
|
isOffline: false,
|
|
32
|
+
willRetry: false,
|
|
31
33
|
staleTime: request.staleTime,
|
|
32
34
|
version: request.client.cache.version,
|
|
33
35
|
cacheTime: Infinity,
|
|
36
|
+
cached: true,
|
|
34
37
|
};
|
|
35
38
|
|
|
36
39
|
beforeAll(() => {
|
|
@@ -46,7 +49,7 @@ describe("useCache [ Base ]", () => {
|
|
|
46
49
|
});
|
|
47
50
|
|
|
48
51
|
beforeEach(() => {
|
|
49
|
-
|
|
52
|
+
vi.resetModules();
|
|
50
53
|
client.clear();
|
|
51
54
|
request = createRequest();
|
|
52
55
|
});
|
|
@@ -81,7 +84,7 @@ describe("useCache [ Base ]", () => {
|
|
|
81
84
|
expect(response.result.current.retries).toBe(0);
|
|
82
85
|
});
|
|
83
86
|
it("should allow to invalidate by Request", async () => {
|
|
84
|
-
const spy =
|
|
87
|
+
const spy = vi.spyOn(client.cache, "invalidate");
|
|
85
88
|
|
|
86
89
|
const { result } = renderUseCache(request);
|
|
87
90
|
|
|
@@ -93,7 +96,7 @@ describe("useCache [ Base ]", () => {
|
|
|
93
96
|
expect(spy).toHaveBeenCalledWith(request);
|
|
94
97
|
});
|
|
95
98
|
it("should allow to invalidate by RegExp", async () => {
|
|
96
|
-
const spy =
|
|
99
|
+
const spy = vi.spyOn(client.cache, "invalidate");
|
|
97
100
|
|
|
98
101
|
const { result } = renderUseCache(request);
|
|
99
102
|
|
|
@@ -105,7 +108,7 @@ describe("useCache [ Base ]", () => {
|
|
|
105
108
|
expect(spy).toHaveBeenCalledWith(new RegExp(request.cacheKey));
|
|
106
109
|
});
|
|
107
110
|
it("should allow to invalidate by cacheKey", async () => {
|
|
108
|
-
const spy =
|
|
111
|
+
const spy = vi.spyOn(client.cache, "invalidate");
|
|
109
112
|
|
|
110
113
|
const { result } = renderUseCache(request);
|
|
111
114
|
|
|
@@ -117,7 +120,7 @@ describe("useCache [ Base ]", () => {
|
|
|
117
120
|
expect(spy).toHaveBeenCalledWith(request.cacheKey);
|
|
118
121
|
});
|
|
119
122
|
it("should allow to invalidate by default key", async () => {
|
|
120
|
-
const spy =
|
|
123
|
+
const spy = vi.spyOn(client.cache, "invalidate");
|
|
121
124
|
|
|
122
125
|
const { result } = renderUseCache(request);
|
|
123
126
|
|
|
@@ -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
|
+
});
|
|
@@ -15,8 +15,8 @@ describe("useEmitter [ Base ]", () => {
|
|
|
15
15
|
emitter = socket.createEmitter<{ name: string; age: number }>()({ topic: "test/:testId" });
|
|
16
16
|
await waitForConnection(socket);
|
|
17
17
|
|
|
18
|
-
|
|
19
|
-
|
|
18
|
+
vi.resetModules();
|
|
19
|
+
vi.resetAllMocks();
|
|
20
20
|
});
|
|
21
21
|
|
|
22
22
|
afterEach(() => {
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { act, waitFor } from "@testing-library/react";
|
|
2
|
-
import { AdapterInstance, ResponseType
|
|
2
|
+
import type { AdapterInstance, ResponseType } from "@hyper-fetch/core";
|
|
3
|
+
import { xhrExtra } from "@hyper-fetch/core";
|
|
3
4
|
import { createHttpMockingServer, sleep } from "@hyper-fetch/testing";
|
|
4
5
|
|
|
5
6
|
import { createRequest, renderUseFetch, createCacheData, client } from "../../utils";
|
|
@@ -23,7 +24,7 @@ describe("useFetch [ Base ]", () => {
|
|
|
23
24
|
});
|
|
24
25
|
|
|
25
26
|
beforeEach(() => {
|
|
26
|
-
|
|
27
|
+
vi.resetModules();
|
|
27
28
|
request = createRequest();
|
|
28
29
|
client.clear();
|
|
29
30
|
});
|
|
@@ -130,7 +131,7 @@ describe("useFetch [ Base ]", () => {
|
|
|
130
131
|
// Todo
|
|
131
132
|
});
|
|
132
133
|
it("should make only one request", async () => {
|
|
133
|
-
const spy =
|
|
134
|
+
const spy = vi.spyOn(client.adapter, "fetch");
|
|
134
135
|
await testClientIsolation(client);
|
|
135
136
|
const mock = mockRequest(request);
|
|
136
137
|
const view = renderUseFetch(request);
|
|
@@ -152,8 +153,12 @@ describe("useFetch [ Base ]", () => {
|
|
|
152
153
|
});
|
|
153
154
|
it("should map the data on deps change", async () => {
|
|
154
155
|
mockRequest(request);
|
|
155
|
-
const request1 = request
|
|
156
|
-
|
|
156
|
+
const request1 = request
|
|
157
|
+
.setCacheKey("request1")
|
|
158
|
+
.setResponseMapper((response) => ({ ...response, data: 1 }) as typeof response);
|
|
159
|
+
const request2 = request
|
|
160
|
+
.setCacheKey("request2")
|
|
161
|
+
.setResponseMapper((response) => ({ ...response, data: 2 }) as typeof response);
|
|
157
162
|
|
|
158
163
|
const { result, rerender } = renderUseFetch(request1);
|
|
159
164
|
|
|
@@ -197,22 +202,28 @@ describe("useFetch [ Base ]", () => {
|
|
|
197
202
|
});
|
|
198
203
|
it("should map response data", async () => {
|
|
199
204
|
const mappedData = { test: 1, test2: 2, test3: 3 };
|
|
200
|
-
const mappedRequest = request.setResponseMapper(
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
205
|
+
const mappedRequest = request.setResponseMapper(
|
|
206
|
+
(response) =>
|
|
207
|
+
({
|
|
208
|
+
...response,
|
|
209
|
+
data: mappedData,
|
|
210
|
+
}) as typeof response,
|
|
211
|
+
);
|
|
204
212
|
mockRequest(mappedRequest);
|
|
205
213
|
const view = renderUseFetch(mappedRequest);
|
|
206
214
|
|
|
207
215
|
await testSuccessState(mappedData, view);
|
|
208
216
|
});
|
|
209
217
|
it("should map async response data", async () => {
|
|
210
|
-
const spy =
|
|
218
|
+
const spy = vi.fn();
|
|
211
219
|
const mappedData = { test: 1, test2: 2, test3: 3 };
|
|
212
|
-
const mappedRequest = request.setResponseMapper(
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
220
|
+
const mappedRequest = request.setResponseMapper(
|
|
221
|
+
async (response) =>
|
|
222
|
+
({
|
|
223
|
+
...response,
|
|
224
|
+
data: mappedData,
|
|
225
|
+
}) as typeof response,
|
|
226
|
+
);
|
|
216
227
|
mockRequest(mappedRequest);
|
|
217
228
|
const view = renderUseFetch(mappedRequest, { disabled: true });
|
|
218
229
|
|
|
@@ -260,7 +271,7 @@ describe("useFetch [ Base ]", () => {
|
|
|
260
271
|
describe("when dependencies change", () => {
|
|
261
272
|
// Solves Issue #22
|
|
262
273
|
it("should fetch data when disabled prop changes", async () => {
|
|
263
|
-
const spy =
|
|
274
|
+
const spy = vi.fn();
|
|
264
275
|
await testClientIsolation(client);
|
|
265
276
|
const mock = mockRequest(request);
|
|
266
277
|
const view = renderUseFetch(request, { disabled: true });
|
|
@@ -281,4 +292,81 @@ describe("useFetch [ Base ]", () => {
|
|
|
281
292
|
// Todo
|
|
282
293
|
});
|
|
283
294
|
});
|
|
295
|
+
|
|
296
|
+
// GitHub Issue #126
|
|
297
|
+
describe("when query parameters change", () => {
|
|
298
|
+
it("should reset loading to true when query params change and no cache exists for new key", async () => {
|
|
299
|
+
await testClientIsolation(client);
|
|
300
|
+
|
|
301
|
+
const paginatedRequest = createRequest({ endpoint: "/items" });
|
|
302
|
+
|
|
303
|
+
const page1Request = paginatedRequest.setQueryParams({ page: "1" } as any);
|
|
304
|
+
mockRequest(page1Request, { data: { items: [1, 2, 3] } });
|
|
305
|
+
|
|
306
|
+
const view = renderUseFetch(page1Request);
|
|
307
|
+
|
|
308
|
+
await waitFor(() => {
|
|
309
|
+
expect(view.result.current.data).toStrictEqual({ items: [1, 2, 3] });
|
|
310
|
+
expect(view.result.current.loading).toBe(false);
|
|
311
|
+
});
|
|
312
|
+
|
|
313
|
+
const page2Request = paginatedRequest.setQueryParams({ page: "2" } as any);
|
|
314
|
+
mockRequest(page2Request, { data: { items: [4, 5, 6] }, delay: 100 });
|
|
315
|
+
|
|
316
|
+
view.rerender({ request: page2Request });
|
|
317
|
+
|
|
318
|
+
await waitFor(() => {
|
|
319
|
+
expect(view.result.current.loading).toBe(true);
|
|
320
|
+
});
|
|
321
|
+
|
|
322
|
+
await waitFor(() => {
|
|
323
|
+
expect(view.result.current.data).toStrictEqual({ items: [4, 5, 6] });
|
|
324
|
+
expect(view.result.current.loading).toBe(false);
|
|
325
|
+
});
|
|
326
|
+
});
|
|
327
|
+
|
|
328
|
+
it("should reset loading to true on each subsequent query param change", async () => {
|
|
329
|
+
await testClientIsolation(client);
|
|
330
|
+
|
|
331
|
+
const paginatedRequest = createRequest({ endpoint: "/posts" });
|
|
332
|
+
|
|
333
|
+
const page1 = paginatedRequest.setQueryParams({ page: "1" } as any);
|
|
334
|
+
mockRequest(page1, { data: { page: 1 } });
|
|
335
|
+
|
|
336
|
+
const view = renderUseFetch(page1);
|
|
337
|
+
|
|
338
|
+
await waitFor(() => {
|
|
339
|
+
expect(view.result.current.data).toStrictEqual({ page: 1 });
|
|
340
|
+
expect(view.result.current.loading).toBe(false);
|
|
341
|
+
});
|
|
342
|
+
|
|
343
|
+
const page2 = paginatedRequest.setQueryParams({ page: "2" } as any);
|
|
344
|
+
mockRequest(page2, { data: { page: 2 }, delay: 100 });
|
|
345
|
+
|
|
346
|
+
view.rerender({ request: page2 });
|
|
347
|
+
|
|
348
|
+
await waitFor(() => {
|
|
349
|
+
expect(view.result.current.loading).toBe(true);
|
|
350
|
+
});
|
|
351
|
+
|
|
352
|
+
await waitFor(() => {
|
|
353
|
+
expect(view.result.current.data).toStrictEqual({ page: 2 });
|
|
354
|
+
expect(view.result.current.loading).toBe(false);
|
|
355
|
+
});
|
|
356
|
+
|
|
357
|
+
const page3 = paginatedRequest.setQueryParams({ page: "3" } as any);
|
|
358
|
+
mockRequest(page3, { data: { page: 3 }, delay: 100 });
|
|
359
|
+
|
|
360
|
+
view.rerender({ request: page3 });
|
|
361
|
+
|
|
362
|
+
await waitFor(() => {
|
|
363
|
+
expect(view.result.current.loading).toBe(true);
|
|
364
|
+
});
|
|
365
|
+
|
|
366
|
+
await waitFor(() => {
|
|
367
|
+
expect(view.result.current.data).toStrictEqual({ page: 3 });
|
|
368
|
+
expect(view.result.current.loading).toBe(false);
|
|
369
|
+
});
|
|
370
|
+
});
|
|
371
|
+
});
|
|
284
372
|
});
|
|
@@ -23,7 +23,7 @@ describe("useFetch [ Cancel ]", () => {
|
|
|
23
23
|
});
|
|
24
24
|
|
|
25
25
|
beforeEach(() => {
|
|
26
|
-
|
|
26
|
+
vi.resetModules();
|
|
27
27
|
request = createRequest({ cacheKey: "test", queryKey: "testQueue", cancelable: true });
|
|
28
28
|
client.clear();
|
|
29
29
|
});
|
|
@@ -58,7 +58,7 @@ describe("useFetch [ Cancel ]", () => {
|
|
|
58
58
|
await testErrorState(getErrorMessage("abort"), dedupeResponse);
|
|
59
59
|
});
|
|
60
60
|
it("should cancel previous requests when dependencies change", async () => {
|
|
61
|
-
const spy =
|
|
61
|
+
const spy = vi.fn();
|
|
62
62
|
|
|
63
63
|
mockRequest(request, { delay: 50 });
|
|
64
64
|
const response = renderUseFetch(request, { dependencies: [{}] });
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { act, waitFor } from "@testing-library/react";
|
|
2
|
+
import { createHttpMockingServer } from "@hyper-fetch/testing";
|
|
3
|
+
|
|
4
|
+
import { client, createRequest, renderUseFetch } from "../../utils";
|
|
5
|
+
|
|
6
|
+
const { resetMocks, startServer, stopServer, mockRequest } = createHttpMockingServer();
|
|
7
|
+
|
|
8
|
+
describe("useFetch [ clearState ]", () => {
|
|
9
|
+
let request = createRequest();
|
|
10
|
+
|
|
11
|
+
beforeAll(() => {
|
|
12
|
+
startServer();
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
afterEach(() => {
|
|
16
|
+
resetMocks();
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
afterAll(() => {
|
|
20
|
+
stopServer();
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
beforeEach(() => {
|
|
24
|
+
vi.resetModules();
|
|
25
|
+
request = createRequest();
|
|
26
|
+
client.clear();
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
describe("when clearState is called", () => {
|
|
30
|
+
it("should reset all state fields to initial values after successful fetch", async () => {
|
|
31
|
+
const mock = mockRequest(request, { data: { name: "test" } });
|
|
32
|
+
const view = renderUseFetch(request);
|
|
33
|
+
|
|
34
|
+
await waitFor(() => {
|
|
35
|
+
expect(view.result.current.data).toStrictEqual(mock);
|
|
36
|
+
expect(view.result.current.success).toBeTrue();
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
act(() => {
|
|
40
|
+
view.result.current.clearState();
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
await waitFor(() => {
|
|
44
|
+
expect(view.result.current.data).toBeNull();
|
|
45
|
+
expect(view.result.current.error).toBeNull();
|
|
46
|
+
expect(view.result.current.status).toBeNull();
|
|
47
|
+
expect(view.result.current.success).toBeFalse();
|
|
48
|
+
expect(view.result.current.loading).toBeFalse();
|
|
49
|
+
expect(view.result.current.retries).toBe(0);
|
|
50
|
+
expect(view.result.current.responseTimestamp).toBeNull();
|
|
51
|
+
expect(view.result.current.requestTimestamp).toBeNull();
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it("should reset all state fields after error response", async () => {
|
|
56
|
+
mockRequest(request, { status: 400 });
|
|
57
|
+
const view = renderUseFetch(request);
|
|
58
|
+
|
|
59
|
+
await waitFor(() => {
|
|
60
|
+
expect(view.result.current.error).toBeDefined();
|
|
61
|
+
expect(view.result.current.success).toBeFalse();
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
act(() => {
|
|
65
|
+
view.result.current.clearState();
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
await waitFor(() => {
|
|
69
|
+
expect(view.result.current.data).toBeNull();
|
|
70
|
+
expect(view.result.current.error).toBeNull();
|
|
71
|
+
expect(view.result.current.status).toBeNull();
|
|
72
|
+
expect(view.result.current.success).toBeFalse();
|
|
73
|
+
expect(view.result.current.loading).toBeFalse();
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it("should be a callable function", () => {
|
|
78
|
+
const view = renderUseFetch(request);
|
|
79
|
+
expect(view.result.current.clearState).toBeFunction();
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
});
|
|
@@ -22,7 +22,7 @@ describe("useFetch [ Concurrency ]", () => {
|
|
|
22
22
|
});
|
|
23
23
|
|
|
24
24
|
beforeEach(() => {
|
|
25
|
-
|
|
25
|
+
vi.resetModules();
|
|
26
26
|
request = createRequest();
|
|
27
27
|
client.clear();
|
|
28
28
|
});
|
|
@@ -30,8 +30,8 @@ describe("useFetch [ Concurrency ]", () => {
|
|
|
30
30
|
describe("given multiple rendered hooks", () => {
|
|
31
31
|
describe("when used the same non-dedupe requests", () => {
|
|
32
32
|
it("should allow to trigger request for each hook", async () => {
|
|
33
|
-
const spyOne =
|
|
34
|
-
const spyTwo =
|
|
33
|
+
const spyOne = vi.fn();
|
|
34
|
+
const spyTwo = vi.fn();
|
|
35
35
|
|
|
36
36
|
const mock = mockRequest(request);
|
|
37
37
|
const responseOne = renderUseFetch(request.setQueryKey("1"));
|
|
@@ -22,7 +22,7 @@ describe("useFetch [ Bounce ]", () => {
|
|
|
22
22
|
});
|
|
23
23
|
|
|
24
24
|
beforeEach(() => {
|
|
25
|
-
|
|
25
|
+
vi.resetModules();
|
|
26
26
|
request = createRequest();
|
|
27
27
|
client.clear();
|
|
28
28
|
});
|
|
@@ -30,7 +30,7 @@ describe("useFetch [ Bounce ]", () => {
|
|
|
30
30
|
describe("given debounce is active", () => {
|
|
31
31
|
describe("when request is about to change", () => {
|
|
32
32
|
it("should not debounce initial request", async () => {
|
|
33
|
-
const spy =
|
|
33
|
+
const spy = vi.fn();
|
|
34
34
|
mockRequest(request);
|
|
35
35
|
const response = renderUseFetch(request, hookDebounceOptions);
|
|
36
36
|
|
|
@@ -42,7 +42,7 @@ describe("useFetch [ Bounce ]", () => {
|
|
|
42
42
|
expect(spy).toHaveBeenCalledTimes(1);
|
|
43
43
|
});
|
|
44
44
|
it("should debounce multiple request triggers by 100ms", async () => {
|
|
45
|
-
const spy =
|
|
45
|
+
const spy = vi.fn();
|
|
46
46
|
mockRequest(request, { delay: 0 });
|
|
47
47
|
const response = renderUseFetch(request, { ...hookDebounceOptions, dependencies: [{ test: 10 }] });
|
|
48
48
|
|
|
@@ -91,7 +91,7 @@ describe("useFetch [ Bounce ]", () => {
|
|
|
91
91
|
describe("given throttle is active", () => {
|
|
92
92
|
describe("when request is about to change", () => {
|
|
93
93
|
it("should not throttle initial request", async () => {
|
|
94
|
-
const spy =
|
|
94
|
+
const spy = vi.fn();
|
|
95
95
|
mockRequest(request);
|
|
96
96
|
const response = renderUseFetch(request, hookThrottleOptions);
|
|
97
97
|
|
|
@@ -103,7 +103,7 @@ describe("useFetch [ Bounce ]", () => {
|
|
|
103
103
|
expect(spy).toHaveBeenCalledTimes(1);
|
|
104
104
|
});
|
|
105
105
|
it("should throttle multiple request triggers by 100ms", async () => {
|
|
106
|
-
const spy =
|
|
106
|
+
const spy = vi.fn();
|
|
107
107
|
mockRequest(request, { delay: 0 });
|
|
108
108
|
const response = renderUseFetch(request, { ...hookThrottleOptions, dependencies: [{ test: 10 }] });
|
|
109
109
|
|
|
@@ -27,7 +27,7 @@ describe("useFetch [ Integration ]", () => {
|
|
|
27
27
|
});
|
|
28
28
|
|
|
29
29
|
beforeEach(() => {
|
|
30
|
-
|
|
30
|
+
vi.resetModules();
|
|
31
31
|
request = createRequest({ endpoint: `/users/${Date.now()}` });
|
|
32
32
|
request.client.clear();
|
|
33
33
|
});
|
|
@@ -75,7 +75,7 @@ describe("useFetch [ Integration ]", () => {
|
|
|
75
75
|
});
|
|
76
76
|
});
|
|
77
77
|
it("should fetch once in StrictMode", async () => {
|
|
78
|
-
const spy =
|
|
78
|
+
const spy = vi.fn();
|
|
79
79
|
|
|
80
80
|
const Page = () => {
|
|
81
81
|
const { data, onFinished } = useFetch(request, {
|
|
@@ -106,7 +106,7 @@ describe("useFetch [ Integration ]", () => {
|
|
|
106
106
|
});
|
|
107
107
|
});
|
|
108
108
|
it("should fetch sequence in StrictMode", async () => {
|
|
109
|
-
const spy =
|
|
109
|
+
const spy = vi.fn();
|
|
110
110
|
|
|
111
111
|
const Page = () => {
|
|
112
112
|
const [params, setParams] = useState({});
|