@hyper-fetch/react 7.5.3 → 8.0.1
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
|
@@ -0,0 +1,548 @@
|
|
|
1
|
+
import { act, waitFor } from "@testing-library/react";
|
|
2
|
+
import { createHttpMockingServer } from "@hyper-fetch/testing";
|
|
3
|
+
|
|
4
|
+
import { testSuccessState, testErrorState } from "../../shared";
|
|
5
|
+
import { client, createRequest, renderUseSubmit } from "../../utils";
|
|
6
|
+
|
|
7
|
+
const { resetMocks, startServer, stopServer, mockRequest } = createHttpMockingServer();
|
|
8
|
+
|
|
9
|
+
describe("useSubmit [ Optimistic ]", () => {
|
|
10
|
+
let request = createRequest({ method: "POST" });
|
|
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({ method: "POST" });
|
|
27
|
+
client.clear();
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
// ============================
|
|
31
|
+
// Happy paths
|
|
32
|
+
// ============================
|
|
33
|
+
|
|
34
|
+
describe("happy path", () => {
|
|
35
|
+
it("should call optimistic callback before sending request and pass mutationContext to onSubmitSuccess", async () => {
|
|
36
|
+
const optimisticSpy = vi.fn(() => ({
|
|
37
|
+
context: { snapshot: "old-data" },
|
|
38
|
+
}));
|
|
39
|
+
const successSpy = vi.fn();
|
|
40
|
+
|
|
41
|
+
const optimisticRequest = request.setOptimistic(optimisticSpy);
|
|
42
|
+
const mock = mockRequest(optimisticRequest);
|
|
43
|
+
const response = renderUseSubmit(optimisticRequest);
|
|
44
|
+
|
|
45
|
+
act(() => {
|
|
46
|
+
response.result.current.onSubmitSuccess(successSpy);
|
|
47
|
+
response.result.current.submit();
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
await testSuccessState(mock, response);
|
|
51
|
+
expect(optimisticSpy).toHaveBeenCalledTimes(1);
|
|
52
|
+
expect(successSpy).toHaveBeenCalledTimes(1);
|
|
53
|
+
expect(successSpy.mock.calls[0][0].mutationContext).toStrictEqual({ snapshot: "old-data" });
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it("should invalidate listed requests on success", async () => {
|
|
57
|
+
const listRequest = client.createRequest()({ endpoint: "/list" });
|
|
58
|
+
const invalidateSpy = vi.spyOn(client.cache, "invalidate");
|
|
59
|
+
|
|
60
|
+
const optimisticRequest = request.setOptimistic(() => ({
|
|
61
|
+
context: { done: true },
|
|
62
|
+
invalidate: [listRequest],
|
|
63
|
+
}));
|
|
64
|
+
|
|
65
|
+
const mock = mockRequest(optimisticRequest);
|
|
66
|
+
const response = renderUseSubmit(optimisticRequest);
|
|
67
|
+
|
|
68
|
+
act(() => {
|
|
69
|
+
response.result.current.submit();
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
await testSuccessState(mock, response);
|
|
73
|
+
expect(invalidateSpy).toHaveBeenCalledWith(listRequest);
|
|
74
|
+
invalidateSpy.mockRestore();
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it("should pass mutationContext to onSubmitFinished on success", async () => {
|
|
78
|
+
const finishedSpy = vi.fn();
|
|
79
|
+
const optimisticRequest = request.setOptimistic(() => ({
|
|
80
|
+
context: { prevValue: 42 },
|
|
81
|
+
}));
|
|
82
|
+
|
|
83
|
+
const mock = mockRequest(optimisticRequest);
|
|
84
|
+
const response = renderUseSubmit(optimisticRequest);
|
|
85
|
+
|
|
86
|
+
act(() => {
|
|
87
|
+
response.result.current.onSubmitFinished(finishedSpy);
|
|
88
|
+
response.result.current.submit();
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
await testSuccessState(mock, response);
|
|
92
|
+
expect(finishedSpy).toHaveBeenCalledTimes(1);
|
|
93
|
+
expect(finishedSpy.mock.calls[0][0].mutationContext).toStrictEqual({ prevValue: 42 });
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
// ============================
|
|
98
|
+
// Failure / rollback
|
|
99
|
+
// ============================
|
|
100
|
+
|
|
101
|
+
describe("failure / rollback", () => {
|
|
102
|
+
it("should call rollback on network error and pass mutationContext to onSubmitError", async () => {
|
|
103
|
+
const rollbackSpy = vi.fn();
|
|
104
|
+
const errorSpy = vi.fn();
|
|
105
|
+
const optimisticRequest = request.setOptimistic(() => ({
|
|
106
|
+
context: { snapshot: "before" },
|
|
107
|
+
rollback: rollbackSpy,
|
|
108
|
+
}));
|
|
109
|
+
|
|
110
|
+
const mock = mockRequest(optimisticRequest, { status: 400 });
|
|
111
|
+
const response = renderUseSubmit(optimisticRequest);
|
|
112
|
+
|
|
113
|
+
act(() => {
|
|
114
|
+
response.result.current.onSubmitError(errorSpy);
|
|
115
|
+
response.result.current.submit();
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
await testErrorState(mock, response);
|
|
119
|
+
expect(rollbackSpy).toHaveBeenCalledTimes(1);
|
|
120
|
+
expect(errorSpy).toHaveBeenCalledTimes(1);
|
|
121
|
+
expect(errorSpy.mock.calls[0][0].mutationContext).toStrictEqual({ snapshot: "before" });
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it("should still fire onSubmitError when rollback throws", async () => {
|
|
125
|
+
const errorSpy = vi.fn();
|
|
126
|
+
const optimisticRequest = request.setOptimistic(() => ({
|
|
127
|
+
context: { val: true },
|
|
128
|
+
rollback: () => {
|
|
129
|
+
throw new Error("Rollback failed");
|
|
130
|
+
},
|
|
131
|
+
}));
|
|
132
|
+
|
|
133
|
+
const mock = mockRequest(optimisticRequest, { status: 400 });
|
|
134
|
+
const response = renderUseSubmit(optimisticRequest);
|
|
135
|
+
|
|
136
|
+
act(() => {
|
|
137
|
+
response.result.current.onSubmitError(errorSpy);
|
|
138
|
+
response.result.current.submit();
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
await testErrorState(mock, response);
|
|
142
|
+
expect(errorSpy).toHaveBeenCalledTimes(1);
|
|
143
|
+
expect(errorSpy.mock.calls[0][0].mutationContext).toStrictEqual({ val: true });
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
// ============================
|
|
148
|
+
// Async optimistic callback
|
|
149
|
+
// ============================
|
|
150
|
+
|
|
151
|
+
describe("async optimistic callback", () => {
|
|
152
|
+
it("should wait for async optimistic callback before sending request", async () => {
|
|
153
|
+
const callOrder: string[] = [];
|
|
154
|
+
|
|
155
|
+
const optimisticRequest = request.setOptimistic(async () => {
|
|
156
|
+
callOrder.push("optimistic");
|
|
157
|
+
await new Promise<void>((r) => {
|
|
158
|
+
setTimeout(r, 50);
|
|
159
|
+
});
|
|
160
|
+
return { context: { async: true } };
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
const mock = mockRequest(optimisticRequest);
|
|
164
|
+
const successSpy = vi.fn(() => {
|
|
165
|
+
callOrder.push("success");
|
|
166
|
+
});
|
|
167
|
+
const response = renderUseSubmit(optimisticRequest);
|
|
168
|
+
|
|
169
|
+
act(() => {
|
|
170
|
+
response.result.current.onSubmitSuccess(successSpy);
|
|
171
|
+
response.result.current.submit();
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
await testSuccessState(mock, response);
|
|
175
|
+
expect(callOrder).toStrictEqual(["optimistic", "success"]);
|
|
176
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
177
|
+
expect((successSpy.mock.calls as any)[0][0]?.mutationContext).toStrictEqual({ async: true });
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
it("should not send request when async optimistic callback rejects", async () => {
|
|
181
|
+
const addSpy = vi.spyOn(client.submitDispatcher, "add");
|
|
182
|
+
|
|
183
|
+
const optimisticRequest = request.setOptimistic(async () => {
|
|
184
|
+
throw new Error("Async onMutate failed");
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
const response = renderUseSubmit(optimisticRequest);
|
|
188
|
+
|
|
189
|
+
let submitResult: any;
|
|
190
|
+
await act(async () => {
|
|
191
|
+
submitResult = await response.result.current.submit();
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
expect(submitResult.error).toBeDefined();
|
|
195
|
+
expect(submitResult.data).toBeNull();
|
|
196
|
+
expect(addSpy).not.toHaveBeenCalled();
|
|
197
|
+
addSpy.mockRestore();
|
|
198
|
+
});
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
// ============================
|
|
202
|
+
// Partial returns
|
|
203
|
+
// ============================
|
|
204
|
+
|
|
205
|
+
describe("partial returns from callback", () => {
|
|
206
|
+
it("should work with empty return object — mutationContext is undefined", async () => {
|
|
207
|
+
const successSpy = vi.fn();
|
|
208
|
+
const optimisticRequest = request.setOptimistic(() => ({}));
|
|
209
|
+
const mock = mockRequest(optimisticRequest);
|
|
210
|
+
const response = renderUseSubmit(optimisticRequest);
|
|
211
|
+
|
|
212
|
+
act(() => {
|
|
213
|
+
response.result.current.onSubmitSuccess(successSpy);
|
|
214
|
+
response.result.current.submit();
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
await testSuccessState(mock, response);
|
|
218
|
+
expect(successSpy).toHaveBeenCalledTimes(1);
|
|
219
|
+
expect(successSpy.mock.calls[0][0].mutationContext).toBeUndefined();
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
it("should work with only context returned (no rollback, no invalidate)", async () => {
|
|
223
|
+
const successSpy = vi.fn();
|
|
224
|
+
const optimisticRequest = request.setOptimistic(() => ({
|
|
225
|
+
context: { partial: true },
|
|
226
|
+
}));
|
|
227
|
+
const mock = mockRequest(optimisticRequest);
|
|
228
|
+
const response = renderUseSubmit(optimisticRequest);
|
|
229
|
+
|
|
230
|
+
act(() => {
|
|
231
|
+
response.result.current.onSubmitSuccess(successSpy);
|
|
232
|
+
response.result.current.submit();
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
await testSuccessState(mock, response);
|
|
236
|
+
expect(successSpy.mock.calls[0][0].mutationContext).toStrictEqual({ partial: true });
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
it("should work with only rollback returned (no context)", async () => {
|
|
240
|
+
const rollbackSpy = vi.fn();
|
|
241
|
+
const errorSpy = vi.fn();
|
|
242
|
+
const optimisticRequest = request.setOptimistic(() => ({
|
|
243
|
+
rollback: rollbackSpy,
|
|
244
|
+
}));
|
|
245
|
+
const mock = mockRequest(optimisticRequest, { status: 400 });
|
|
246
|
+
const response = renderUseSubmit(optimisticRequest);
|
|
247
|
+
|
|
248
|
+
act(() => {
|
|
249
|
+
response.result.current.onSubmitError(errorSpy);
|
|
250
|
+
response.result.current.submit();
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
await testErrorState(mock, response);
|
|
254
|
+
expect(rollbackSpy).toHaveBeenCalledTimes(1);
|
|
255
|
+
expect(errorSpy.mock.calls[0][0].mutationContext).toBeUndefined();
|
|
256
|
+
});
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
// ============================
|
|
260
|
+
// Retry interaction
|
|
261
|
+
// ============================
|
|
262
|
+
|
|
263
|
+
describe("retry interaction", () => {
|
|
264
|
+
it("should run optimistic callback once before first attempt (not on retries)", async () => {
|
|
265
|
+
const optimisticSpy = vi.fn(() => ({
|
|
266
|
+
context: { snapshot: "data" },
|
|
267
|
+
rollback: vi.fn(),
|
|
268
|
+
}));
|
|
269
|
+
|
|
270
|
+
const optimisticRequest = request.setOptimistic(optimisticSpy).setRetry(1).setRetryTime(50);
|
|
271
|
+
mockRequest(optimisticRequest, { status: 400 });
|
|
272
|
+
const response = renderUseSubmit(optimisticRequest);
|
|
273
|
+
|
|
274
|
+
act(() => {
|
|
275
|
+
response.result.current.submit();
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
await waitFor(() => {
|
|
279
|
+
expect(response.result.current.error).toBeDefined();
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
expect(optimisticSpy).toHaveBeenCalledTimes(1);
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
it("should call rollback only on final failure after retries exhausted", async () => {
|
|
286
|
+
const rollbackSpy = vi.fn();
|
|
287
|
+
const errorSpy = vi.fn();
|
|
288
|
+
|
|
289
|
+
const optimisticRequest = request
|
|
290
|
+
.setOptimistic(() => ({
|
|
291
|
+
context: { snapshot: "v1" },
|
|
292
|
+
rollback: rollbackSpy,
|
|
293
|
+
}))
|
|
294
|
+
.setRetry(1)
|
|
295
|
+
.setRetryTime(50);
|
|
296
|
+
|
|
297
|
+
mockRequest(optimisticRequest, { status: 400 });
|
|
298
|
+
const response = renderUseSubmit(optimisticRequest);
|
|
299
|
+
|
|
300
|
+
act(() => {
|
|
301
|
+
response.result.current.onSubmitError(errorSpy);
|
|
302
|
+
response.result.current.submit();
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
await waitFor(() => {
|
|
306
|
+
expect(errorSpy).toHaveBeenCalledTimes(2);
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
expect(rollbackSpy).toHaveBeenCalledTimes(1);
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
it("should not call rollback when retry succeeds after initial failure", async () => {
|
|
313
|
+
const rollbackSpy = vi.fn();
|
|
314
|
+
const successSpy = vi.fn();
|
|
315
|
+
|
|
316
|
+
const optimisticRequest = request
|
|
317
|
+
.setOptimistic(() => ({
|
|
318
|
+
context: { snapshot: "v1" },
|
|
319
|
+
rollback: rollbackSpy,
|
|
320
|
+
}))
|
|
321
|
+
.setRetry(1)
|
|
322
|
+
.setRetryTime(50);
|
|
323
|
+
|
|
324
|
+
const errorMock = mockRequest(optimisticRequest, { status: 400 });
|
|
325
|
+
const response = renderUseSubmit(optimisticRequest);
|
|
326
|
+
|
|
327
|
+
act(() => {
|
|
328
|
+
response.result.current.onSubmitSuccess(successSpy);
|
|
329
|
+
response.result.current.submit();
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
await testErrorState(errorMock, response);
|
|
333
|
+
const successMock = mockRequest(optimisticRequest);
|
|
334
|
+
|
|
335
|
+
await testSuccessState(successMock, response);
|
|
336
|
+
expect(rollbackSpy).not.toHaveBeenCalled();
|
|
337
|
+
expect(successSpy).toHaveBeenCalledTimes(1);
|
|
338
|
+
expect(successSpy.mock.calls[0][0].mutationContext).toStrictEqual({ snapshot: "v1" });
|
|
339
|
+
});
|
|
340
|
+
});
|
|
341
|
+
|
|
342
|
+
// ============================
|
|
343
|
+
// Disabled
|
|
344
|
+
// ============================
|
|
345
|
+
|
|
346
|
+
describe("disabled", () => {
|
|
347
|
+
it("should not run optimistic callback when disabled is true", async () => {
|
|
348
|
+
const optimisticSpy = vi.fn(() => ({ context: { test: true } }));
|
|
349
|
+
const optimisticRequest = request.setOptimistic(optimisticSpy);
|
|
350
|
+
mockRequest(optimisticRequest);
|
|
351
|
+
|
|
352
|
+
const response = renderUseSubmit(optimisticRequest, { disabled: true });
|
|
353
|
+
|
|
354
|
+
await act(async () => {
|
|
355
|
+
await response.result.current.submit();
|
|
356
|
+
});
|
|
357
|
+
|
|
358
|
+
expect(optimisticSpy).not.toHaveBeenCalled();
|
|
359
|
+
});
|
|
360
|
+
});
|
|
361
|
+
|
|
362
|
+
// ============================
|
|
363
|
+
// Bounce guard
|
|
364
|
+
// ============================
|
|
365
|
+
|
|
366
|
+
describe("bounce with optimistic", () => {
|
|
367
|
+
it("should call optimistic callback when bounced request actually fires", async () => {
|
|
368
|
+
const optimisticSpy = vi.fn(() => ({ context: { test: true } }));
|
|
369
|
+
const successSpy = vi.fn();
|
|
370
|
+
const optimisticRequest = request.setOptimistic(optimisticSpy);
|
|
371
|
+
const mock = mockRequest(optimisticRequest);
|
|
372
|
+
|
|
373
|
+
const response = renderUseSubmit(optimisticRequest, { bounce: true, bounceTime: 50 });
|
|
374
|
+
|
|
375
|
+
act(() => {
|
|
376
|
+
response.result.current.onSubmitSuccess(successSpy);
|
|
377
|
+
response.result.current.submit();
|
|
378
|
+
});
|
|
379
|
+
|
|
380
|
+
await testSuccessState(mock, response);
|
|
381
|
+
expect(optimisticSpy).toHaveBeenCalledTimes(1);
|
|
382
|
+
expect(successSpy.mock.calls[0][0].mutationContext).toStrictEqual({ test: true });
|
|
383
|
+
});
|
|
384
|
+
});
|
|
385
|
+
|
|
386
|
+
// ============================
|
|
387
|
+
// Callback ordering
|
|
388
|
+
// ============================
|
|
389
|
+
|
|
390
|
+
describe("callback ordering", () => {
|
|
391
|
+
it("should call in order: rollback → onSubmitError → onSubmitFinished on failure", async () => {
|
|
392
|
+
const callOrder: string[] = [];
|
|
393
|
+
|
|
394
|
+
const optimisticRequest = request.setOptimistic(() => ({
|
|
395
|
+
context: { snapshot: "data" },
|
|
396
|
+
rollback: () => callOrder.push("rollback"),
|
|
397
|
+
}));
|
|
398
|
+
|
|
399
|
+
const mock = mockRequest(optimisticRequest, { status: 400 });
|
|
400
|
+
const response = renderUseSubmit(optimisticRequest);
|
|
401
|
+
|
|
402
|
+
act(() => {
|
|
403
|
+
response.result.current.onSubmitError(() => {
|
|
404
|
+
callOrder.push("onSubmitError");
|
|
405
|
+
});
|
|
406
|
+
response.result.current.onSubmitFinished(() => {
|
|
407
|
+
callOrder.push("onSubmitFinished");
|
|
408
|
+
});
|
|
409
|
+
response.result.current.submit();
|
|
410
|
+
});
|
|
411
|
+
|
|
412
|
+
await testErrorState(mock, response);
|
|
413
|
+
expect(callOrder).toStrictEqual(["rollback", "onSubmitError", "onSubmitFinished"]);
|
|
414
|
+
});
|
|
415
|
+
|
|
416
|
+
it("should call in order: invalidate → onSubmitSuccess → onSubmitFinished on success", async () => {
|
|
417
|
+
const callOrder: string[] = [];
|
|
418
|
+
const listRequest = client.createRequest()({ endpoint: "/list" });
|
|
419
|
+
const invalidateSpy = vi.spyOn(client.cache, "invalidate").mockImplementation(() => {
|
|
420
|
+
callOrder.push("invalidate");
|
|
421
|
+
});
|
|
422
|
+
|
|
423
|
+
const optimisticRequest = request.setOptimistic(() => ({
|
|
424
|
+
context: { snapshot: "data" },
|
|
425
|
+
invalidate: [listRequest],
|
|
426
|
+
}));
|
|
427
|
+
|
|
428
|
+
const mock = mockRequest(optimisticRequest);
|
|
429
|
+
const response = renderUseSubmit(optimisticRequest);
|
|
430
|
+
|
|
431
|
+
act(() => {
|
|
432
|
+
response.result.current.onSubmitSuccess(() => {
|
|
433
|
+
callOrder.push("onSubmitSuccess");
|
|
434
|
+
});
|
|
435
|
+
response.result.current.onSubmitFinished(() => {
|
|
436
|
+
callOrder.push("onSubmitFinished");
|
|
437
|
+
});
|
|
438
|
+
response.result.current.submit();
|
|
439
|
+
});
|
|
440
|
+
|
|
441
|
+
await testSuccessState(mock, response);
|
|
442
|
+
expect(callOrder).toStrictEqual(["invalidate", "onSubmitSuccess", "onSubmitFinished"]);
|
|
443
|
+
invalidateSpy.mockRestore();
|
|
444
|
+
});
|
|
445
|
+
});
|
|
446
|
+
|
|
447
|
+
// ============================
|
|
448
|
+
// onMutate throws
|
|
449
|
+
// ============================
|
|
450
|
+
|
|
451
|
+
describe("onMutate throws", () => {
|
|
452
|
+
it("should resolve submit promise with error when optimistic callback throws", async () => {
|
|
453
|
+
const addSpy = vi.spyOn(client.submitDispatcher, "add");
|
|
454
|
+
|
|
455
|
+
const optimisticRequest = request.setOptimistic(() => {
|
|
456
|
+
throw new Error("onMutate failed");
|
|
457
|
+
});
|
|
458
|
+
|
|
459
|
+
const response = renderUseSubmit(optimisticRequest);
|
|
460
|
+
|
|
461
|
+
let submitResult: any;
|
|
462
|
+
await act(async () => {
|
|
463
|
+
submitResult = await response.result.current.submit();
|
|
464
|
+
});
|
|
465
|
+
|
|
466
|
+
expect(submitResult.error).toBeDefined();
|
|
467
|
+
expect(submitResult.error.message).toContain("Optimistic callback");
|
|
468
|
+
expect(submitResult.data).toBeNull();
|
|
469
|
+
expect(addSpy).not.toHaveBeenCalled();
|
|
470
|
+
addSpy.mockRestore();
|
|
471
|
+
});
|
|
472
|
+
});
|
|
473
|
+
|
|
474
|
+
// ============================
|
|
475
|
+
// Regression (no optimistic)
|
|
476
|
+
// ============================
|
|
477
|
+
|
|
478
|
+
describe("regression (no optimistic)", () => {
|
|
479
|
+
it("should work identically when no setOptimistic is used", async () => {
|
|
480
|
+
const successSpy = vi.fn();
|
|
481
|
+
const mock = mockRequest(request);
|
|
482
|
+
const response = renderUseSubmit(request);
|
|
483
|
+
|
|
484
|
+
act(() => {
|
|
485
|
+
response.result.current.onSubmitSuccess(successSpy);
|
|
486
|
+
response.result.current.submit();
|
|
487
|
+
});
|
|
488
|
+
|
|
489
|
+
await testSuccessState(mock, response);
|
|
490
|
+
expect(successSpy).toHaveBeenCalledTimes(1);
|
|
491
|
+
expect(successSpy.mock.calls[0][0].mutationContext).toBeUndefined();
|
|
492
|
+
});
|
|
493
|
+
|
|
494
|
+
it("should pass undefined mutationContext in all callbacks when no optimistic is set", async () => {
|
|
495
|
+
const successSpy = vi.fn();
|
|
496
|
+
const finishedSpy = vi.fn();
|
|
497
|
+
const mock = mockRequest(request);
|
|
498
|
+
const response = renderUseSubmit(request);
|
|
499
|
+
|
|
500
|
+
act(() => {
|
|
501
|
+
response.result.current.onSubmitSuccess(successSpy);
|
|
502
|
+
response.result.current.onSubmitFinished(finishedSpy);
|
|
503
|
+
response.result.current.submit();
|
|
504
|
+
});
|
|
505
|
+
|
|
506
|
+
await testSuccessState(mock, response);
|
|
507
|
+
expect(successSpy.mock.calls[0][0].mutationContext).toBeUndefined();
|
|
508
|
+
expect(finishedSpy.mock.calls[0][0].mutationContext).toBeUndefined();
|
|
509
|
+
});
|
|
510
|
+
});
|
|
511
|
+
|
|
512
|
+
// ============================
|
|
513
|
+
// Concurrency
|
|
514
|
+
// ============================
|
|
515
|
+
|
|
516
|
+
describe("concurrency", () => {
|
|
517
|
+
it("should handle two concurrent submits with independent mutationContext", async () => {
|
|
518
|
+
const request1 = createRequest({ method: "POST" }).setOptimistic(() => ({
|
|
519
|
+
context: { id: "submit-1" },
|
|
520
|
+
}));
|
|
521
|
+
const request2 = createRequest({ method: "POST" }).setOptimistic(() => ({
|
|
522
|
+
context: { id: "submit-2" },
|
|
523
|
+
}));
|
|
524
|
+
|
|
525
|
+
const mock1 = mockRequest(request1);
|
|
526
|
+
const mock2 = mockRequest(request2);
|
|
527
|
+
|
|
528
|
+
const successSpy1 = vi.fn();
|
|
529
|
+
const successSpy2 = vi.fn();
|
|
530
|
+
|
|
531
|
+
const response1 = renderUseSubmit(request1);
|
|
532
|
+
const response2 = renderUseSubmit(request2);
|
|
533
|
+
|
|
534
|
+
act(() => {
|
|
535
|
+
response1.result.current.onSubmitSuccess(successSpy1);
|
|
536
|
+
response2.result.current.onSubmitSuccess(successSpy2);
|
|
537
|
+
response1.result.current.submit();
|
|
538
|
+
response2.result.current.submit();
|
|
539
|
+
});
|
|
540
|
+
|
|
541
|
+
await testSuccessState(mock1, response1);
|
|
542
|
+
await testSuccessState(mock2, response2);
|
|
543
|
+
|
|
544
|
+
expect(successSpy1.mock.calls[0][0].mutationContext).toStrictEqual({ id: "submit-1" });
|
|
545
|
+
expect(successSpy2.mock.calls[0][0].mutationContext).toStrictEqual({ id: "submit-2" });
|
|
546
|
+
});
|
|
547
|
+
});
|
|
548
|
+
});
|
|
@@ -7,7 +7,7 @@ import { client, createRequest, renderUseSubmit, waitForRender } from "../../uti
|
|
|
7
7
|
const { resetMocks, startServer, stopServer, mockRequest } = createHttpMockingServer();
|
|
8
8
|
|
|
9
9
|
describe("useSubmit [ Queue ]", () => {
|
|
10
|
-
let request = createRequest
|
|
10
|
+
let request = createRequest({ method: "POST", queued: true });
|
|
11
11
|
|
|
12
12
|
beforeAll(() => {
|
|
13
13
|
startServer();
|
|
@@ -22,7 +22,7 @@ describe("useSubmit [ Queue ]", () => {
|
|
|
22
22
|
});
|
|
23
23
|
|
|
24
24
|
beforeEach(() => {
|
|
25
|
-
|
|
25
|
+
vi.resetModules();
|
|
26
26
|
client.clear();
|
|
27
27
|
request = createRequest({ method: "POST", queued: true });
|
|
28
28
|
});
|
|
@@ -31,7 +31,7 @@ describe("useSubmit [ Queue ]", () => {
|
|
|
31
31
|
describe("when submitting request", () => {
|
|
32
32
|
it("should send requests one by one", async () => {
|
|
33
33
|
let count = 1;
|
|
34
|
-
const spy =
|
|
34
|
+
const spy = vi.fn();
|
|
35
35
|
mockRequest(request, { data: count });
|
|
36
36
|
const response = renderUseSubmit(request);
|
|
37
37
|
|
|
@@ -21,14 +21,14 @@ describe("useSubmit [ Retry ]", () => {
|
|
|
21
21
|
});
|
|
22
22
|
|
|
23
23
|
beforeEach(() => {
|
|
24
|
-
|
|
24
|
+
vi.resetModules();
|
|
25
25
|
client.clear();
|
|
26
26
|
request = createRequest({ method: "POST" });
|
|
27
27
|
});
|
|
28
28
|
|
|
29
29
|
describe("when request retry attribute is set to false", () => {
|
|
30
30
|
it("should not retry request on failure", async () => {
|
|
31
|
-
const spy =
|
|
31
|
+
const spy = vi.fn();
|
|
32
32
|
mockRequest(request, { status: 400, delay: 5 });
|
|
33
33
|
const response = renderUseSubmit(request.setRetry(0).setRetryTime(0));
|
|
34
34
|
|
|
@@ -44,7 +44,7 @@ describe("useSubmit [ Retry ]", () => {
|
|
|
44
44
|
});
|
|
45
45
|
describe("when request retry attribute is set to true", () => {
|
|
46
46
|
it("should retry request once", async () => {
|
|
47
|
-
const spy =
|
|
47
|
+
const spy = vi.fn();
|
|
48
48
|
mockRequest(request, { status: 400, delay: 5 });
|
|
49
49
|
const response = renderUseSubmit(request.setRetry(1).setRetryTime(10));
|
|
50
50
|
|
|
@@ -58,7 +58,7 @@ describe("useSubmit [ Retry ]", () => {
|
|
|
58
58
|
expect(spy).toHaveBeenCalledTimes(2);
|
|
59
59
|
});
|
|
60
60
|
it("should retry request twice", async () => {
|
|
61
|
-
const spy =
|
|
61
|
+
const spy = vi.fn();
|
|
62
62
|
mockRequest(request, { status: 400, delay: 5 });
|
|
63
63
|
const response = renderUseSubmit(request.setRetry(2).setRetryTime(10));
|
|
64
64
|
|
|
@@ -24,7 +24,7 @@ describe("useFetch [ refetch ]", () => {
|
|
|
24
24
|
});
|
|
25
25
|
|
|
26
26
|
beforeEach(() => {
|
|
27
|
-
|
|
27
|
+
vi.resetModules();
|
|
28
28
|
client.clear();
|
|
29
29
|
requestSubmit = createRequest({ method: "POST" });
|
|
30
30
|
requestFetch = createRequest({ endpoint: "fetch-test" });
|
|
@@ -50,7 +50,7 @@ describe("useFetch [ refetch ]", () => {
|
|
|
50
50
|
await testSuccessState(fetchMock, responseFetch);
|
|
51
51
|
});
|
|
52
52
|
it("should allow to refetch by Request", async () => {
|
|
53
|
-
const spy =
|
|
53
|
+
const spy = vi.spyOn(client.cache, "invalidate");
|
|
54
54
|
|
|
55
55
|
renderUseSubmit(requestSubmit);
|
|
56
56
|
|
|
@@ -62,7 +62,7 @@ describe("useFetch [ refetch ]", () => {
|
|
|
62
62
|
expect(spy).toHaveBeenCalledWith(requestSubmit);
|
|
63
63
|
});
|
|
64
64
|
it("should allow to refetch by RegExp", async () => {
|
|
65
|
-
const spy =
|
|
65
|
+
const spy = vi.spyOn(client.cache, "invalidate");
|
|
66
66
|
|
|
67
67
|
renderUseSubmit(requestSubmit);
|
|
68
68
|
|
|
@@ -74,7 +74,7 @@ describe("useFetch [ refetch ]", () => {
|
|
|
74
74
|
expect(spy).toHaveBeenCalledWith(new RegExp(requestSubmit.cacheKey));
|
|
75
75
|
});
|
|
76
76
|
it("should allow to refetch by cacheKey", async () => {
|
|
77
|
-
const spy =
|
|
77
|
+
const spy = vi.spyOn(client.cache, "invalidate");
|
|
78
78
|
|
|
79
79
|
renderUseSubmit(requestSubmit);
|
|
80
80
|
|
|
@@ -86,7 +86,7 @@ describe("useFetch [ refetch ]", () => {
|
|
|
86
86
|
expect(spy).toHaveBeenCalledWith(requestSubmit.cacheKey);
|
|
87
87
|
});
|
|
88
88
|
it("should allow to refetch without key", async () => {
|
|
89
|
-
const spy =
|
|
89
|
+
const spy = vi.spyOn(client.cache, "invalidate");
|
|
90
90
|
|
|
91
91
|
const { result } = renderUseSubmit(requestSubmit);
|
|
92
92
|
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { ExtendRequest, RequestSendType
|
|
1
|
+
import type { ExtendRequest, RequestSendType } from "@hyper-fetch/core";
|
|
2
|
+
import { xhrExtra } from "@hyper-fetch/core";
|
|
2
3
|
import { act } from "@testing-library/react";
|
|
3
4
|
import { createHttpMockingServer } from "@hyper-fetch/testing";
|
|
4
5
|
|
|
@@ -24,7 +25,7 @@ describe("useSubmit [ Base ]", () => {
|
|
|
24
25
|
});
|
|
25
26
|
|
|
26
27
|
beforeEach(() => {
|
|
27
|
-
|
|
28
|
+
vi.resetModules();
|
|
28
29
|
client.clear();
|
|
29
30
|
request = createRequest({ method: "POST" });
|
|
30
31
|
});
|
|
@@ -50,7 +51,7 @@ describe("useSubmit [ Base ]", () => {
|
|
|
50
51
|
});
|
|
51
52
|
});
|
|
52
53
|
it("should call onBeforeSent", async () => {
|
|
53
|
-
const spy =
|
|
54
|
+
const spy = vi.fn();
|
|
54
55
|
mockRequest(request);
|
|
55
56
|
const response = renderUseSubmit(request);
|
|
56
57
|
|