@hyper-fetch/react 7.5.2 → 8.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.cjs +12 -0
- package/__tests__/features/helpers/use-request-events/use-request-events.base.spec.ts +115 -0
- package/__tests__/features/helpers/use-request-events/use-request-events.utils.spec.ts +69 -0
- package/__tests__/features/helpers/use-socket-state/use-socket-state.base.spec.ts +213 -0
- package/__tests__/features/helpers/use-tracked-state/use-tacked-state.actions.spec.ts +550 -0
- package/__tests__/features/helpers/use-tracked-state/use-tracked-state.base.spec.ts +566 -0
- package/__tests__/features/helpers/use-tracked-state/use-tracked-state.utils.spec.ts +260 -0
- package/__tests__/features/provider/provider.hydration.spec.tsx +165 -0
- package/__tests__/features/provider/use-config-provider.base.spec.tsx +69 -0
- package/__tests__/features/use-app-manager/use-app-manager.base.spec.ts +85 -0
- package/__tests__/features/use-cache/use-cache.base.spec.ts +136 -0
- package/__tests__/features/use-cache/use-cache.helpers.ts +40 -0
- package/__tests__/features/use-cache/use-cache.typescript.spec.ts +111 -0
- package/__tests__/features/use-emitter/use-emitter.base.spec.ts +124 -0
- package/__tests__/features/use-fetch/use-fetch.base.spec.ts +372 -0
- package/__tests__/features/use-fetch/use-fetch.cancel.spec.ts +83 -0
- package/__tests__/features/use-fetch/use-fetch.clear-state.spec.ts +82 -0
- package/__tests__/features/use-fetch/use-fetch.concurrency.spec.ts +91 -0
- package/__tests__/features/use-fetch/use-fetch.debounce.spec.ts +153 -0
- package/__tests__/features/use-fetch/use-fetch.deduplication.spec.ts +80 -0
- package/__tests__/features/use-fetch/use-fetch.infinite.ts +39 -0
- package/__tests__/features/use-fetch/use-fetch.integration.spec.tsx +172 -0
- package/__tests__/features/use-fetch/use-fetch.keep-previous-data.spec.ts +215 -0
- package/__tests__/features/use-fetch/use-fetch.methods.spec.ts +233 -0
- package/__tests__/features/use-fetch/use-fetch.offline.spec.ts +103 -0
- package/__tests__/features/use-fetch/use-fetch.pagination.ts +36 -0
- package/__tests__/features/use-fetch/use-fetch.prefetch.spec.tsx +73 -0
- package/__tests__/features/use-fetch/use-fetch.refresh.spec.ts +187 -0
- package/__tests__/features/use-fetch/use-fetch.rerender.spec.tsx +151 -0
- package/__tests__/features/use-fetch/use-fetch.retry.ts +38 -0
- package/__tests__/features/use-fetch/use-fetch.revalidate.spec.ts +237 -0
- package/__tests__/features/use-fetch/use-fetch.suspense.spec.tsx +505 -0
- package/__tests__/features/use-fetch/use-fetch.typescript.spec.ts +182 -0
- package/__tests__/features/use-fetch/use-fetch.utils.spec.ts +26 -0
- package/__tests__/features/use-fetch/use-fetch.window.spec.ts +131 -0
- package/__tests__/features/use-listener/use-listener.base.spec.ts +57 -0
- package/__tests__/features/use-queue/use-queue.actions.spec.ts +238 -0
- package/__tests__/features/use-queue/use-queue.base.spec.ts +367 -0
- package/__tests__/features/use-queue/use-queue.resolve.spec.ts +173 -0
- package/__tests__/features/use-stream/use-stream.base.spec.ts +481 -0
- package/__tests__/features/use-submit/use-submit.base.spec.ts +76 -0
- package/__tests__/features/use-submit/use-submit.cancel.spec.ts +46 -0
- package/__tests__/features/use-submit/use-submit.concurrency.spec.ts +62 -0
- package/__tests__/features/use-submit/use-submit.debounce.spec.ts +320 -0
- package/__tests__/features/use-submit/use-submit.methods.spec.ts +276 -0
- package/__tests__/features/use-submit/use-submit.optimistic.spec.ts +548 -0
- package/__tests__/features/use-submit/use-submit.queue.spec.ts +84 -0
- package/__tests__/features/use-submit/use-submit.retry.spec.ts +92 -0
- package/__tests__/features/use-submit/use-submit.revalidate.spec.ts +99 -0
- package/__tests__/features/use-submit/use-submit.submit.spec.ts +234 -0
- package/__tests__/features/use-submit/use-submit.typescript.spec.ts +325 -0
- package/__tests__/shared/index.ts +2 -0
- package/__tests__/shared/isolation.tests.ts +12 -0
- package/__tests__/shared/response.tests.ts +128 -0
- package/__tests__/tsconfig.json +27 -0
- package/__tests__/utils/cache.utils.ts +55 -0
- package/__tests__/utils/client.utils.ts +3 -0
- package/__tests__/utils/emitter.utils.ts +8 -0
- package/__tests__/utils/helpers.utils.ts +9 -0
- package/__tests__/utils/index.ts +14 -0
- package/__tests__/utils/listener.utils.ts +11 -0
- package/__tests__/utils/request.utils.ts +12 -0
- package/__tests__/utils/state.utils.ts +7 -0
- package/__tests__/utils/use-app-manager.utils.ts +10 -0
- package/__tests__/utils/use-cache.utils.ts +12 -0
- package/__tests__/utils/use-emitter.utils.ts +12 -0
- package/__tests__/utils/use-fetch.utils.ts +12 -0
- package/__tests__/utils/use-listener.utils.ts +12 -0
- package/__tests__/utils/use-queue.utils.ts +70 -0
- package/__tests__/utils/use-request-events.utils.ts +43 -0
- package/__tests__/utils/use-socket-state.utils.ts +12 -0
- package/__tests__/utils/use-submit.utils.ts +12 -0
- package/__tests__/utils/use-tracked-state.utils.ts +27 -0
- package/__tests__/vitest.setup.ts +8 -0
- package/dist/helpers/index.d.ts +4 -0
- package/dist/helpers/index.d.ts.map +1 -0
- package/dist/helpers/use-request-events/index.d.ts +3 -0
- package/dist/helpers/use-request-events/index.d.ts.map +1 -0
- package/dist/helpers/use-request-events/use-request-events.hooks.d.ts +10 -0
- package/dist/helpers/use-request-events/use-request-events.hooks.d.ts.map +1 -0
- package/dist/helpers/use-request-events/use-request-events.types.d.ts +78 -0
- package/dist/helpers/use-request-events/use-request-events.types.d.ts.map +1 -0
- package/dist/helpers/use-socket-state/index.d.ts +4 -0
- package/dist/helpers/use-socket-state/index.d.ts.map +1 -0
- package/dist/helpers/use-socket-state/use-socket-state.constants.d.ts +4 -0
- package/dist/helpers/use-socket-state/use-socket-state.constants.d.ts.map +1 -0
- package/dist/helpers/use-socket-state/use-socket-state.hooks.d.ts +24 -0
- package/dist/helpers/use-socket-state/use-socket-state.hooks.d.ts.map +1 -0
- package/dist/helpers/use-socket-state/use-socket-state.types.d.ts +12 -0
- package/dist/helpers/use-socket-state/use-socket-state.types.d.ts.map +1 -0
- package/dist/helpers/use-tracked-state/index.d.ts +5 -0
- package/dist/helpers/use-tracked-state/index.d.ts.map +1 -0
- package/dist/helpers/use-tracked-state/use-tracked-state.constants.d.ts +3 -0
- package/dist/helpers/use-tracked-state/use-tracked-state.constants.d.ts.map +1 -0
- package/dist/helpers/use-tracked-state/use-tracked-state.hooks.d.ts +12 -0
- package/dist/helpers/use-tracked-state/use-tracked-state.hooks.d.ts.map +1 -0
- package/dist/helpers/use-tracked-state/use-tracked-state.types.d.ts +113 -0
- package/dist/helpers/use-tracked-state/use-tracked-state.types.d.ts.map +1 -0
- package/dist/helpers/use-tracked-state/use-tracked-state.utils.d.ts +29 -0
- package/dist/helpers/use-tracked-state/use-tracked-state.utils.d.ts.map +1 -0
- package/dist/hooks/use-app-manager/index.d.ts +3 -0
- package/dist/hooks/use-app-manager/index.d.ts.map +1 -0
- package/dist/hooks/use-app-manager/use-app-manager.hooks.d.ts +4 -0
- package/dist/hooks/use-app-manager/use-app-manager.hooks.d.ts.map +1 -0
- package/dist/hooks/use-app-manager/use-app-manager.types.d.ts +19 -0
- package/dist/hooks/use-app-manager/use-app-manager.types.d.ts.map +1 -0
- package/dist/hooks/use-cache/index.d.ts +4 -0
- package/dist/hooks/use-cache/index.d.ts.map +1 -0
- package/dist/hooks/use-cache/use-cache.constants.d.ts +6 -0
- package/dist/hooks/use-cache/use-cache.constants.d.ts.map +1 -0
- package/dist/hooks/use-cache/use-cache.hooks.d.ts +4 -0
- package/dist/hooks/use-cache/use-cache.hooks.d.ts.map +1 -0
- package/dist/hooks/use-cache/use-cache.types.d.ts +24 -0
- package/dist/hooks/use-cache/use-cache.types.d.ts.map +1 -0
- package/dist/hooks/use-emitter/index.d.ts +3 -0
- package/dist/hooks/use-emitter/index.d.ts.map +1 -0
- package/dist/hooks/use-emitter/use-emitter.hooks.d.ts +26 -0
- package/dist/hooks/use-emitter/use-emitter.hooks.d.ts.map +1 -0
- package/dist/hooks/use-emitter/use-emitter.types.d.ts +4 -0
- package/dist/hooks/use-emitter/use-emitter.types.d.ts.map +1 -0
- package/dist/hooks/use-fetch/index.d.ts +5 -0
- package/dist/hooks/use-fetch/index.d.ts.map +1 -0
- package/dist/hooks/use-fetch/use-fetch.constants.d.ts +8 -0
- package/dist/hooks/use-fetch/use-fetch.constants.d.ts.map +1 -0
- package/dist/hooks/use-fetch/use-fetch.hooks.d.ts +10 -0
- package/dist/hooks/use-fetch/use-fetch.hooks.d.ts.map +1 -0
- package/dist/hooks/use-fetch/use-fetch.types.d.ts +118 -0
- package/dist/hooks/use-fetch/use-fetch.types.d.ts.map +1 -0
- package/dist/hooks/use-fetch/use-fetch.utils.d.ts +2 -0
- package/dist/hooks/use-fetch/use-fetch.utils.d.ts.map +1 -0
- package/dist/hooks/use-listener/index.d.ts +3 -0
- package/dist/hooks/use-listener/index.d.ts.map +1 -0
- package/dist/hooks/use-listener/use-listener.hooks.d.ts +31 -0
- package/dist/hooks/use-listener/use-listener.hooks.d.ts.map +1 -0
- package/dist/hooks/use-listener/use-listener.types.d.ts +4 -0
- package/dist/hooks/use-listener/use-listener.types.d.ts.map +1 -0
- package/dist/hooks/use-queue/index.d.ts +4 -0
- package/dist/hooks/use-queue/index.d.ts.map +1 -0
- package/dist/hooks/use-queue/use-queue.constants.d.ts +4 -0
- package/dist/hooks/use-queue/use-queue.constants.d.ts.map +1 -0
- package/dist/hooks/use-queue/use-queue.hooks.d.ts +10 -0
- package/dist/hooks/use-queue/use-queue.hooks.d.ts.map +1 -0
- package/dist/hooks/use-queue/use-queue.types.d.ts +58 -0
- package/dist/hooks/use-queue/use-queue.types.d.ts.map +1 -0
- package/dist/hooks/use-stream/index.d.ts +3 -0
- package/dist/hooks/use-stream/index.d.ts.map +1 -0
- package/dist/hooks/use-stream/use-stream.hooks.d.ts +11 -0
- package/dist/hooks/use-stream/use-stream.hooks.d.ts.map +1 -0
- package/dist/hooks/use-stream/use-stream.types.d.ts +52 -0
- package/dist/hooks/use-stream/use-stream.types.d.ts.map +1 -0
- package/dist/hooks/use-submit/index.d.ts +4 -0
- package/dist/hooks/use-submit/index.d.ts.map +1 -0
- package/dist/hooks/use-submit/use-submit.constants.d.ts +8 -0
- package/dist/hooks/use-submit/use-submit.constants.d.ts.map +1 -0
- package/dist/hooks/use-submit/use-submit.hooks.d.ts +10 -0
- package/dist/hooks/use-submit/use-submit.hooks.d.ts.map +1 -0
- package/dist/hooks/use-submit/use-submit.types.d.ts +115 -0
- package/dist/hooks/use-submit/use-submit.types.d.ts.map +1 -0
- package/dist/index.d.ts +12 -733
- package/dist/index.d.ts.map +1 -0
- package/dist/index.mjs +1935 -0
- package/dist/index.mjs.map +1 -0
- package/dist/provider/index.d.ts +3 -0
- package/dist/provider/index.d.ts.map +1 -0
- package/dist/provider/provider.d.ts +13 -0
- package/dist/provider/provider.d.ts.map +1 -0
- package/dist/provider/provider.types.d.ts +31 -0
- package/dist/provider/provider.types.d.ts.map +1 -0
- package/dist/utils/bounce.utils.d.ts +8 -0
- package/dist/utils/bounce.utils.d.ts.map +1 -0
- package/dist/utils/deep-equal.utils.d.ts +14 -0
- package/dist/utils/deep-equal.utils.d.ts.map +1 -0
- package/dist/utils/index.d.ts +4 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/tracked-proxy.utils.d.ts +7 -0
- package/dist/utils/tracked-proxy.utils.d.ts.map +1 -0
- package/package.json +21 -13
- package/project.json +4 -0
- package/src/helpers/use-request-events/use-request-events.hooks.ts +60 -14
- package/src/helpers/use-request-events/use-request-events.types.ts +19 -12
- package/src/helpers/use-socket-state/use-socket-state.constants.ts +2 -2
- package/src/helpers/use-socket-state/use-socket-state.hooks.ts +47 -13
- package/src/helpers/use-socket-state/use-socket-state.types.ts +1 -1
- package/src/helpers/use-tracked-state/use-tracked-state.constants.ts +1 -1
- package/src/helpers/use-tracked-state/use-tracked-state.hooks.ts +74 -22
- package/src/helpers/use-tracked-state/use-tracked-state.types.ts +7 -2
- package/src/helpers/use-tracked-state/use-tracked-state.utils.ts +39 -4
- package/src/hooks/use-app-manager/use-app-manager.hooks.ts +2 -2
- package/src/hooks/use-cache/use-cache.hooks.ts +33 -41
- package/src/hooks/use-cache/use-cache.types.ts +3 -3
- package/src/hooks/use-emitter/use-emitter.hooks.ts +17 -16
- package/src/hooks/use-fetch/use-fetch.constants.ts +5 -2
- package/src/hooks/use-fetch/use-fetch.hooks.ts +109 -51
- package/src/hooks/use-fetch/use-fetch.types.ts +18 -3
- package/src/hooks/use-listener/use-listener.hooks.ts +19 -27
- package/src/hooks/use-queue/use-queue.constants.ts +2 -2
- package/src/hooks/use-queue/use-queue.hooks.ts +25 -6
- package/src/hooks/use-queue/use-queue.types.ts +8 -2
- package/src/hooks/use-stream/index.ts +2 -0
- package/src/hooks/use-stream/use-stream.hooks.ts +164 -0
- package/src/hooks/use-stream/use-stream.types.ts +53 -0
- package/src/hooks/use-submit/use-submit.constants.ts +2 -2
- package/src/hooks/use-submit/use-submit.hooks.ts +43 -48
- package/src/hooks/use-submit/use-submit.types.ts +9 -9
- package/src/index.ts +1 -0
- package/src/provider/provider.tsx +1 -1
- package/src/provider/provider.types.ts +8 -8
- package/src/utils/index.ts +1 -0
- package/src/utils/tracked-proxy.utils.ts +19 -0
- package/tsconfig.json +1 -1
- package/vite.config.ts +45 -0
- package/.eslintrc.json +0 -11
- package/dist/index.js +0 -1541
- package/dist/index.js.map +0 -7
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
import { act, waitFor } from "@testing-library/react";
|
|
2
|
+
import { createHttpMockingServer } from "@hyper-fetch/testing";
|
|
3
|
+
|
|
4
|
+
import { testErrorState, testSuccessState } from "../../shared";
|
|
5
|
+
import { client, createRequest, renderUseFetch } from "../../utils";
|
|
6
|
+
|
|
7
|
+
const { resetMocks, startServer, stopServer, mockRequest } = createHttpMockingServer();
|
|
8
|
+
|
|
9
|
+
describe("useFetch [ Methods ]", () => {
|
|
10
|
+
let request = createRequest();
|
|
11
|
+
|
|
12
|
+
beforeAll(() => {
|
|
13
|
+
startServer();
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
afterEach(() => {
|
|
17
|
+
resetMocks();
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
afterAll(() => {
|
|
21
|
+
stopServer();
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
beforeEach(() => {
|
|
25
|
+
vi.resetModules();
|
|
26
|
+
request = createRequest();
|
|
27
|
+
client.clear();
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
describe("given hook is mounted", () => {
|
|
31
|
+
describe("when processing request", () => {
|
|
32
|
+
it("should trigger onRequestStart helper", async () => {
|
|
33
|
+
const spy = vi.fn();
|
|
34
|
+
const mock = mockRequest(request);
|
|
35
|
+
const response = renderUseFetch(request);
|
|
36
|
+
|
|
37
|
+
act(() => {
|
|
38
|
+
response.result.current.onRequestStart(spy);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
await testSuccessState(mock, response);
|
|
42
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
43
|
+
});
|
|
44
|
+
it("should trigger onResponseStart helper", async () => {
|
|
45
|
+
const spy = vi.fn();
|
|
46
|
+
const mock = mockRequest(request);
|
|
47
|
+
const response = renderUseFetch(request);
|
|
48
|
+
|
|
49
|
+
act(() => {
|
|
50
|
+
response.result.current.onResponseStart(spy);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
await testSuccessState(mock, response);
|
|
54
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
55
|
+
});
|
|
56
|
+
it("should trigger onDownloadProgress helper", async () => {
|
|
57
|
+
const spy = vi.fn();
|
|
58
|
+
const mock = mockRequest(request);
|
|
59
|
+
const response = renderUseFetch(request);
|
|
60
|
+
|
|
61
|
+
act(() => {
|
|
62
|
+
response.result.current.onDownloadProgress(spy);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
await testSuccessState(mock, response);
|
|
66
|
+
expect(spy.mock.calls.length).toBeGreaterThanOrEqual(1);
|
|
67
|
+
});
|
|
68
|
+
it("should trigger onUploadProgress helper", async () => {
|
|
69
|
+
const spy = vi.fn();
|
|
70
|
+
const mock = mockRequest(request);
|
|
71
|
+
const response = renderUseFetch(request);
|
|
72
|
+
|
|
73
|
+
act(() => {
|
|
74
|
+
response.result.current.onUploadProgress(spy);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
await testSuccessState(mock, response);
|
|
78
|
+
expect(spy.mock.calls.length).toBeGreaterThanOrEqual(1);
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
describe("when getting the response", () => {
|
|
82
|
+
it("should trigger onSuccess helper", async () => {
|
|
83
|
+
const spy = vi.fn();
|
|
84
|
+
const unusedSpy = vi.fn();
|
|
85
|
+
const mock = mockRequest(request);
|
|
86
|
+
const response = renderUseFetch(request);
|
|
87
|
+
|
|
88
|
+
act(() => {
|
|
89
|
+
response.result.current.onSuccess(spy);
|
|
90
|
+
response.result.current.onError(unusedSpy);
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
await testSuccessState(mock, response);
|
|
94
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
95
|
+
expect(unusedSpy).toHaveBeenCalledTimes(0);
|
|
96
|
+
});
|
|
97
|
+
it("should trigger onError helper", async () => {
|
|
98
|
+
const spy = vi.fn();
|
|
99
|
+
const unusedSpy = vi.fn();
|
|
100
|
+
const mock = mockRequest(request, { status: 400 });
|
|
101
|
+
const response = renderUseFetch(request);
|
|
102
|
+
|
|
103
|
+
act(() => {
|
|
104
|
+
response.result.current.onError(spy);
|
|
105
|
+
response.result.current.onSuccess(unusedSpy);
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
await testErrorState(mock, response);
|
|
109
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
110
|
+
expect(unusedSpy).toHaveBeenCalledTimes(0);
|
|
111
|
+
});
|
|
112
|
+
it("should trigger onFinished helper on success", async () => {
|
|
113
|
+
const spy = vi.fn();
|
|
114
|
+
const mock = mockRequest(request);
|
|
115
|
+
const response = renderUseFetch(request);
|
|
116
|
+
|
|
117
|
+
act(() => {
|
|
118
|
+
response.result.current.onFinished(spy);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
await testSuccessState(mock, response);
|
|
122
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
123
|
+
});
|
|
124
|
+
it("should trigger onFinished helper on error", async () => {
|
|
125
|
+
const spy = vi.fn();
|
|
126
|
+
const mock = mockRequest(request, { status: 400 });
|
|
127
|
+
const response = renderUseFetch(request);
|
|
128
|
+
|
|
129
|
+
act(() => {
|
|
130
|
+
response.result.current.onFinished(spy);
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
await testErrorState(mock, response);
|
|
134
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
135
|
+
});
|
|
136
|
+
});
|
|
137
|
+
describe("when getting internal error", () => {
|
|
138
|
+
it("should trigger onOfflineError helper", async () => {
|
|
139
|
+
const spy = vi.fn();
|
|
140
|
+
mockRequest(request, { status: 400 });
|
|
141
|
+
const response = renderUseFetch(request.setOffline(true));
|
|
142
|
+
|
|
143
|
+
act(() => {
|
|
144
|
+
response.result.current.onOfflineError(spy);
|
|
145
|
+
client.appManager.setOnline(false);
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
await waitFor(() => {
|
|
149
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
150
|
+
});
|
|
151
|
+
});
|
|
152
|
+
it("should trigger methods on retry", async () => {
|
|
153
|
+
const spy1 = vi.fn();
|
|
154
|
+
const spy2 = vi.fn();
|
|
155
|
+
const spy3 = vi.fn();
|
|
156
|
+
const spy4 = vi.fn();
|
|
157
|
+
const spy5 = vi.fn();
|
|
158
|
+
const spy6 = vi.fn();
|
|
159
|
+
const spy7 = vi.fn();
|
|
160
|
+
const spy8 = vi.fn();
|
|
161
|
+
|
|
162
|
+
const errorMock = mockRequest(request, { status: 400 });
|
|
163
|
+
const response = renderUseFetch(request.setRetry(1).setRetryTime(100));
|
|
164
|
+
|
|
165
|
+
act(() => {
|
|
166
|
+
response.result.current.onRequestStart(spy1);
|
|
167
|
+
response.result.current.onResponseStart(spy2);
|
|
168
|
+
response.result.current.onDownloadProgress(spy3);
|
|
169
|
+
response.result.current.onUploadProgress(spy4);
|
|
170
|
+
response.result.current.onSuccess(spy5);
|
|
171
|
+
response.result.current.onError(spy6);
|
|
172
|
+
response.result.current.onFinished(spy7);
|
|
173
|
+
response.result.current.onOfflineError(spy8);
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
await testErrorState(errorMock, response);
|
|
177
|
+
const successMock = mockRequest(request);
|
|
178
|
+
await testSuccessState(successMock, response);
|
|
179
|
+
|
|
180
|
+
expect(spy1).toHaveBeenCalledTimes(2);
|
|
181
|
+
expect(spy2).toHaveBeenCalledTimes(2);
|
|
182
|
+
expect(spy3.mock.calls.length).toBeGreaterThanOrEqual(2);
|
|
183
|
+
expect(spy4.mock.calls.length).toBeGreaterThanOrEqual(2);
|
|
184
|
+
expect(spy5).toHaveBeenCalledTimes(1);
|
|
185
|
+
expect(spy6).toHaveBeenCalledTimes(1);
|
|
186
|
+
expect(spy7).toHaveBeenCalledTimes(2);
|
|
187
|
+
expect(spy8).toHaveBeenCalledTimes(0);
|
|
188
|
+
});
|
|
189
|
+
it("should trigger methods on coming back online", async () => {
|
|
190
|
+
const spy1 = vi.fn();
|
|
191
|
+
const spy2 = vi.fn();
|
|
192
|
+
const spy3 = vi.fn();
|
|
193
|
+
const spy4 = vi.fn();
|
|
194
|
+
const spy5 = vi.fn();
|
|
195
|
+
const spy6 = vi.fn();
|
|
196
|
+
const spy7 = vi.fn();
|
|
197
|
+
const spy8 = vi.fn();
|
|
198
|
+
|
|
199
|
+
const errorMock = mockRequest(request, { status: 400 });
|
|
200
|
+
const response = renderUseFetch(request);
|
|
201
|
+
|
|
202
|
+
act(() => {
|
|
203
|
+
response.result.current.onRequestStart(spy1);
|
|
204
|
+
response.result.current.onResponseStart(spy2);
|
|
205
|
+
response.result.current.onDownloadProgress(spy3);
|
|
206
|
+
response.result.current.onUploadProgress(spy4);
|
|
207
|
+
response.result.current.onSuccess(spy5);
|
|
208
|
+
response.result.current.onError(spy6);
|
|
209
|
+
response.result.current.onFinished(spy7);
|
|
210
|
+
response.result.current.onOfflineError(spy8);
|
|
211
|
+
client.appManager.setOnline(false);
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
await testErrorState(errorMock, response);
|
|
215
|
+
const successMock = mockRequest(request);
|
|
216
|
+
|
|
217
|
+
act(() => {
|
|
218
|
+
client.appManager.setOnline(true);
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
await testSuccessState(successMock, response);
|
|
222
|
+
expect(spy1).toHaveBeenCalledTimes(2);
|
|
223
|
+
expect(spy2).toHaveBeenCalledTimes(2);
|
|
224
|
+
expect(spy3.mock.calls.length).toBeGreaterThanOrEqual(2);
|
|
225
|
+
expect(spy4.mock.calls.length).toBeGreaterThanOrEqual(2);
|
|
226
|
+
expect(spy5).toHaveBeenCalledTimes(1);
|
|
227
|
+
expect(spy6).toHaveBeenCalledTimes(0);
|
|
228
|
+
expect(spy7).toHaveBeenCalledTimes(2);
|
|
229
|
+
expect(spy8).toHaveBeenCalledTimes(1);
|
|
230
|
+
});
|
|
231
|
+
});
|
|
232
|
+
});
|
|
233
|
+
});
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { waitFor, act } from "@testing-library/react";
|
|
2
|
+
import { createHttpMockingServer } from "@hyper-fetch/testing";
|
|
3
|
+
|
|
4
|
+
import { testErrorState, testSuccessState } from "../../shared";
|
|
5
|
+
import { client, createRequest, renderUseFetch, waitForRender } from "../../utils";
|
|
6
|
+
|
|
7
|
+
const { resetMocks, startServer, stopServer, mockRequest } = createHttpMockingServer();
|
|
8
|
+
|
|
9
|
+
describe("useFetch [ Offline ]", () => {
|
|
10
|
+
let request = createRequest({ offline: true });
|
|
11
|
+
|
|
12
|
+
beforeAll(() => {
|
|
13
|
+
startServer();
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
afterEach(() => {
|
|
17
|
+
resetMocks();
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
afterAll(() => {
|
|
21
|
+
stopServer();
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
beforeEach(() => {
|
|
25
|
+
vi.resetModules();
|
|
26
|
+
request = createRequest({ offline: true });
|
|
27
|
+
client.clear();
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
describe("when application is offline", () => {
|
|
31
|
+
it("should not refetch on offline", async () => {
|
|
32
|
+
const mock = mockRequest(request);
|
|
33
|
+
const response = renderUseFetch(request);
|
|
34
|
+
|
|
35
|
+
await testSuccessState(mock, response);
|
|
36
|
+
|
|
37
|
+
const spy = vi.spyOn(client.adapter, "fetch");
|
|
38
|
+
|
|
39
|
+
act(() => {
|
|
40
|
+
client.appManager.setOnline(false);
|
|
41
|
+
response.result.current.refetch();
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
expect(spy).toHaveBeenCalledTimes(0);
|
|
45
|
+
});
|
|
46
|
+
it("should finish request when coming back online", async () => {
|
|
47
|
+
client.appManager.setOnline(false);
|
|
48
|
+
|
|
49
|
+
const mock = mockRequest(request);
|
|
50
|
+
const response = renderUseFetch(request);
|
|
51
|
+
|
|
52
|
+
act(() => {
|
|
53
|
+
client.appManager.setOnline(true);
|
|
54
|
+
});
|
|
55
|
+
await testSuccessState(mock, response);
|
|
56
|
+
});
|
|
57
|
+
it("should refetch when coming back online", async () => {
|
|
58
|
+
const spy = vi.fn();
|
|
59
|
+
const mock = mockRequest(request);
|
|
60
|
+
const response = renderUseFetch(request, { refetchOnReconnect: true });
|
|
61
|
+
await testSuccessState(mock, response);
|
|
62
|
+
|
|
63
|
+
act(() => {
|
|
64
|
+
response.result.current.onRequestStart(spy);
|
|
65
|
+
client.appManager.setOnline(false);
|
|
66
|
+
client.appManager.setOnline(true);
|
|
67
|
+
});
|
|
68
|
+
await waitFor(() => {
|
|
69
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
describe("when request offline attribute is set to true", () => {
|
|
74
|
+
it("should not emit offline error until request is finished", async () => {
|
|
75
|
+
const mock = mockRequest(request);
|
|
76
|
+
const response = renderUseFetch(request);
|
|
77
|
+
await waitForRender();
|
|
78
|
+
act(() => {
|
|
79
|
+
client.appManager.setOnline(false);
|
|
80
|
+
});
|
|
81
|
+
await waitForRender();
|
|
82
|
+
act(() => {
|
|
83
|
+
client.appManager.setOnline(true);
|
|
84
|
+
});
|
|
85
|
+
await testSuccessState(mock, response);
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
describe("when request offline attribute is set to false", () => {
|
|
89
|
+
it("should emit offline error until request is finished", async () => {
|
|
90
|
+
const mock = mockRequest(request, { status: 400 });
|
|
91
|
+
const response = renderUseFetch(request.setOffline(false));
|
|
92
|
+
await waitForRender();
|
|
93
|
+
act(() => {
|
|
94
|
+
client.appManager.setOnline(false);
|
|
95
|
+
});
|
|
96
|
+
await waitForRender();
|
|
97
|
+
act(() => {
|
|
98
|
+
client.appManager.setOnline(true);
|
|
99
|
+
});
|
|
100
|
+
await testErrorState(mock, response);
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
});
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { createHttpMockingServer } from "@hyper-fetch/testing";
|
|
2
|
+
|
|
3
|
+
import { client } from "../../utils";
|
|
4
|
+
|
|
5
|
+
const { resetMocks, startServer, stopServer } = createHttpMockingServer();
|
|
6
|
+
|
|
7
|
+
describe("useFetch [ Pagination ]", () => {
|
|
8
|
+
beforeAll(() => {
|
|
9
|
+
startServer();
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
afterEach(() => {
|
|
13
|
+
resetMocks();
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
afterAll(() => {
|
|
17
|
+
stopServer();
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
beforeEach(() => {
|
|
21
|
+
vi.resetModules();
|
|
22
|
+
client.clear();
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
describe("when request query params are changed", () => {
|
|
26
|
+
it("should allow to see previous results until new page is loaded", async () => {
|
|
27
|
+
// TODO
|
|
28
|
+
});
|
|
29
|
+
it("should not override previous results by initialData", async () => {
|
|
30
|
+
// TODO
|
|
31
|
+
});
|
|
32
|
+
it("should cache pages on change", async () => {
|
|
33
|
+
// TODO
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
});
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { render, screen, waitFor } from "@testing-library/react";
|
|
2
|
+
import { createHttpMockingServer } from "@hyper-fetch/testing";
|
|
3
|
+
|
|
4
|
+
import { useFetch } from "hooks/use-fetch";
|
|
5
|
+
import { client, createRequest } from "../../utils";
|
|
6
|
+
|
|
7
|
+
const { resetMocks, startServer, stopServer, mockRequest } = createHttpMockingServer();
|
|
8
|
+
|
|
9
|
+
describe("useFetch [ Prefetch ]", () => {
|
|
10
|
+
let request = createRequest<any, undefined>();
|
|
11
|
+
let mock = mockRequest(request);
|
|
12
|
+
|
|
13
|
+
beforeAll(() => {
|
|
14
|
+
startServer();
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
afterEach(() => {
|
|
18
|
+
resetMocks();
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
afterAll(() => {
|
|
22
|
+
stopServer();
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
beforeEach(() => {
|
|
26
|
+
vi.resetModules();
|
|
27
|
+
client.clear();
|
|
28
|
+
mock = mockRequest(request);
|
|
29
|
+
request = createRequest();
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
describe("when request is triggered while mounting page", () => {
|
|
33
|
+
it("should pre-fetch data", async () => {
|
|
34
|
+
await request.send({});
|
|
35
|
+
|
|
36
|
+
mockRequest(request, { data: { wrongData: 123 } });
|
|
37
|
+
|
|
38
|
+
const Page = () => {
|
|
39
|
+
const { data } = useFetch(request, { revalidate: false });
|
|
40
|
+
return <div>{JSON.stringify(data)}</div>;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
render(<Page />);
|
|
44
|
+
|
|
45
|
+
await waitFor(async () => {
|
|
46
|
+
const successData = await screen.findByText(JSON.stringify(mock));
|
|
47
|
+
expect(successData).toBeInTheDocument();
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
it("should not show error when pre-fetching is failed and fetch again", async () => {
|
|
51
|
+
const errorMock = mockRequest(request, { status: 400 });
|
|
52
|
+
|
|
53
|
+
await request.send({});
|
|
54
|
+
|
|
55
|
+
const successMock = mockRequest(request);
|
|
56
|
+
|
|
57
|
+
const Page = () => {
|
|
58
|
+
const { data } = useFetch(request, { revalidate: false });
|
|
59
|
+
return <div>{JSON.stringify(data)}</div>;
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
render(<Page />);
|
|
63
|
+
|
|
64
|
+
const errorData = await screen.queryByText(JSON.stringify(errorMock));
|
|
65
|
+
expect(errorData).not.toBeInTheDocument();
|
|
66
|
+
|
|
67
|
+
await waitFor(async () => {
|
|
68
|
+
const successData = await screen.findByText(JSON.stringify(successMock));
|
|
69
|
+
expect(successData).toBeInTheDocument();
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
});
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
import { act } from "@testing-library/react";
|
|
2
|
+
import { createHttpMockingServer } from "@hyper-fetch/testing";
|
|
3
|
+
|
|
4
|
+
import { client, createRequest, renderUseFetch, waitForRender } from "../../utils";
|
|
5
|
+
|
|
6
|
+
const { resetMocks, startServer, stopServer, mockRequest } = createHttpMockingServer();
|
|
7
|
+
|
|
8
|
+
describe("useFetch [ Refreshing ]", () => {
|
|
9
|
+
const hookOptions = {
|
|
10
|
+
refresh: true,
|
|
11
|
+
refreshTime: 100,
|
|
12
|
+
refetchBlurred: true,
|
|
13
|
+
refetchOnReconnect: true,
|
|
14
|
+
refetchOnBlur: true,
|
|
15
|
+
refetchOnFocus: true,
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
let request = createRequest();
|
|
19
|
+
|
|
20
|
+
beforeAll(() => {
|
|
21
|
+
startServer();
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
afterEach(() => {
|
|
25
|
+
resetMocks();
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
afterAll(() => {
|
|
29
|
+
stopServer();
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
beforeEach(() => {
|
|
33
|
+
vi.resetModules();
|
|
34
|
+
request = createRequest();
|
|
35
|
+
client.clear();
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("should refetch data after refresh time of 200ms", async () => {
|
|
39
|
+
const spy = vi.fn();
|
|
40
|
+
mockRequest(request);
|
|
41
|
+
const { result } = renderUseFetch(request, hookOptions);
|
|
42
|
+
|
|
43
|
+
act(() => {
|
|
44
|
+
result.current.onRequestStart(spy);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
await waitForRender();
|
|
48
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
49
|
+
await waitForRender(hookOptions.refreshTime * 1.5);
|
|
50
|
+
|
|
51
|
+
expect(spy).toHaveBeenCalledTimes(2);
|
|
52
|
+
});
|
|
53
|
+
it("should refresh blurred tab", async () => {
|
|
54
|
+
const spy = vi.fn();
|
|
55
|
+
mockRequest(request);
|
|
56
|
+
const { result } = renderUseFetch(request, { ...hookOptions, refetchOnBlur: false });
|
|
57
|
+
|
|
58
|
+
act(() => {
|
|
59
|
+
result.current.onRequestStart(spy);
|
|
60
|
+
client.appManager.setFocused(false);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
await waitForRender();
|
|
64
|
+
await waitForRender(hookOptions.refreshTime * 1.5);
|
|
65
|
+
expect(spy).toHaveBeenCalledTimes(2);
|
|
66
|
+
});
|
|
67
|
+
it("should not refresh blurred tab", async () => {
|
|
68
|
+
const spy = vi.fn();
|
|
69
|
+
mockRequest(request);
|
|
70
|
+
const { result } = renderUseFetch(request, { ...hookOptions, refetchOnBlur: false, refetchBlurred: false });
|
|
71
|
+
|
|
72
|
+
act(() => {
|
|
73
|
+
result.current.onRequestStart(spy);
|
|
74
|
+
client.appManager.setFocused(false);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
await waitForRender();
|
|
78
|
+
await waitForRender(hookOptions.refreshTime * 1.5);
|
|
79
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
80
|
+
});
|
|
81
|
+
it("should postpone refresh when invalidation is triggered during countdown", async () => {
|
|
82
|
+
// TODO
|
|
83
|
+
});
|
|
84
|
+
it("should postpone refresh when dependencies change during countdown", async () => {
|
|
85
|
+
// TODO
|
|
86
|
+
});
|
|
87
|
+
it("should stop refreshing when value is changing from true to false", async () => {
|
|
88
|
+
const spy = vi.fn();
|
|
89
|
+
mockRequest(request);
|
|
90
|
+
const { result, rerender } = renderUseFetch(request, hookOptions);
|
|
91
|
+
|
|
92
|
+
act(() => {
|
|
93
|
+
result.current.onRequestStart(spy);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
await waitForRender();
|
|
97
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
98
|
+
await waitForRender(hookOptions.refreshTime * 1.5);
|
|
99
|
+
|
|
100
|
+
expect(spy).toHaveBeenCalledTimes(2);
|
|
101
|
+
|
|
102
|
+
act(() => {
|
|
103
|
+
rerender({ refresh: false });
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
await waitForRender(hookOptions.refreshTime * 1.5);
|
|
107
|
+
|
|
108
|
+
expect(spy).toHaveBeenCalledTimes(2);
|
|
109
|
+
});
|
|
110
|
+
it("should refetch when tab is focused and refetchOnFocus is true", async () => {
|
|
111
|
+
const spy = vi.fn();
|
|
112
|
+
mockRequest(request);
|
|
113
|
+
const { result } = renderUseFetch(request, { ...hookOptions });
|
|
114
|
+
|
|
115
|
+
act(() => {
|
|
116
|
+
result.current.onRequestStart(spy);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
await waitForRender();
|
|
120
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
121
|
+
|
|
122
|
+
// Simulate tab focus
|
|
123
|
+
act(() => {
|
|
124
|
+
client.appManager.setFocused(true);
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
await waitForRender();
|
|
128
|
+
expect(spy).toHaveBeenCalledTimes(2);
|
|
129
|
+
});
|
|
130
|
+
it("should not refresh when disabled is true", async () => {
|
|
131
|
+
const spy = vi.fn();
|
|
132
|
+
mockRequest(request);
|
|
133
|
+
const { result } = renderUseFetch(request, { ...hookOptions, disabled: true });
|
|
134
|
+
|
|
135
|
+
act(() => {
|
|
136
|
+
result.current.onRequestStart(spy);
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
await waitForRender();
|
|
140
|
+
await waitForRender(hookOptions.refreshTime * 1.5);
|
|
141
|
+
|
|
142
|
+
expect(spy).not.toHaveBeenCalled();
|
|
143
|
+
});
|
|
144
|
+
it("should stop refreshing when disabled changes from false to true", async () => {
|
|
145
|
+
const spy = vi.fn();
|
|
146
|
+
mockRequest(request);
|
|
147
|
+
const { result, rerender } = renderUseFetch(request, hookOptions);
|
|
148
|
+
|
|
149
|
+
act(() => {
|
|
150
|
+
result.current.onRequestStart(spy);
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
await waitForRender();
|
|
154
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
155
|
+
|
|
156
|
+
act(() => {
|
|
157
|
+
rerender({ disabled: true });
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
await waitForRender(hookOptions.refreshTime * 1.5);
|
|
161
|
+
|
|
162
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
163
|
+
});
|
|
164
|
+
it("should not refetch when tab is focused and refetchOnFocus is false", async () => {
|
|
165
|
+
const spy = vi.fn();
|
|
166
|
+
mockRequest(request);
|
|
167
|
+
const { result } = renderUseFetch(request, {
|
|
168
|
+
...hookOptions,
|
|
169
|
+
refetchOnFocus: false,
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
act(() => {
|
|
173
|
+
result.current.onRequestStart(spy);
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
await waitForRender();
|
|
177
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
178
|
+
|
|
179
|
+
// Simulate tab focus
|
|
180
|
+
act(() => {
|
|
181
|
+
client.appManager.setFocused(true);
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
await waitForRender();
|
|
185
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
186
|
+
});
|
|
187
|
+
});
|