@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.
Files changed (212) hide show
  1. package/.eslintrc.cjs +12 -0
  2. package/__tests__/features/helpers/use-request-events/use-request-events.base.spec.ts +4 -4
  3. package/__tests__/features/helpers/use-request-events/use-request-events.utils.spec.ts +3 -3
  4. package/__tests__/features/helpers/use-socket-state/use-socket-state.base.spec.ts +44 -6
  5. package/__tests__/features/helpers/use-tracked-state/use-tacked-state.actions.spec.ts +177 -9
  6. package/__tests__/features/helpers/use-tracked-state/use-tracked-state.base.spec.ts +47 -8
  7. package/__tests__/features/helpers/use-tracked-state/use-tracked-state.utils.spec.ts +6 -6
  8. package/__tests__/features/provider/provider.hydration.spec.tsx +7 -3
  9. package/__tests__/features/provider/use-config-provider.base.spec.tsx +2 -1
  10. package/__tests__/features/use-app-manager/use-app-manager.base.spec.ts +1 -1
  11. package/__tests__/features/use-cache/use-cache.base.spec.ts +9 -6
  12. package/__tests__/features/use-cache/use-cache.helpers.ts +1 -1
  13. package/__tests__/features/use-cache/use-cache.typescript.spec.ts +111 -0
  14. package/__tests__/features/use-emitter/use-emitter.base.spec.ts +2 -2
  15. package/__tests__/features/use-fetch/use-fetch.base.spec.ts +103 -15
  16. package/__tests__/features/use-fetch/use-fetch.cancel.spec.ts +2 -2
  17. package/__tests__/features/use-fetch/use-fetch.clear-state.spec.ts +82 -0
  18. package/__tests__/features/use-fetch/use-fetch.concurrency.spec.ts +3 -3
  19. package/__tests__/features/use-fetch/use-fetch.debounce.spec.ts +5 -5
  20. package/__tests__/features/use-fetch/use-fetch.deduplication.spec.ts +1 -1
  21. package/__tests__/features/use-fetch/use-fetch.infinite.ts +1 -1
  22. package/__tests__/features/use-fetch/use-fetch.integration.spec.tsx +3 -3
  23. package/__tests__/features/use-fetch/use-fetch.keep-previous-data.spec.ts +215 -0
  24. package/__tests__/features/use-fetch/use-fetch.methods.spec.ts +34 -34
  25. package/__tests__/features/use-fetch/use-fetch.offline.spec.ts +3 -3
  26. package/__tests__/features/use-fetch/use-fetch.pagination.ts +1 -1
  27. package/__tests__/features/use-fetch/use-fetch.prefetch.spec.tsx +1 -1
  28. package/__tests__/features/use-fetch/use-fetch.refresh.spec.ts +41 -7
  29. package/__tests__/features/use-fetch/use-fetch.rerender.spec.tsx +2 -2
  30. package/__tests__/features/use-fetch/use-fetch.retry.ts +1 -1
  31. package/__tests__/features/use-fetch/use-fetch.revalidate.spec.ts +6 -4
  32. package/__tests__/features/use-fetch/use-fetch.suspense.spec.tsx +505 -0
  33. package/__tests__/features/use-fetch/use-fetch.typescript.spec.ts +179 -56
  34. package/__tests__/features/use-fetch/use-fetch.window.spec.ts +75 -3
  35. package/__tests__/features/use-listener/use-listener.base.spec.ts +3 -3
  36. package/__tests__/features/use-queue/use-queue.actions.spec.ts +1 -1
  37. package/__tests__/features/use-queue/use-queue.base.spec.ts +259 -1
  38. package/__tests__/features/use-queue/use-queue.resolve.spec.ts +173 -0
  39. package/__tests__/features/use-stream/use-stream.base.spec.ts +481 -0
  40. package/__tests__/features/use-submit/use-submit.base.spec.ts +2 -2
  41. package/__tests__/features/use-submit/use-submit.cancel.spec.ts +1 -1
  42. package/__tests__/features/use-submit/use-submit.concurrency.spec.ts +1 -1
  43. package/__tests__/features/use-submit/use-submit.debounce.spec.ts +8 -8
  44. package/__tests__/features/use-submit/use-submit.methods.spec.ts +44 -46
  45. package/__tests__/features/use-submit/use-submit.optimistic.spec.ts +548 -0
  46. package/__tests__/features/use-submit/use-submit.queue.spec.ts +3 -3
  47. package/__tests__/features/use-submit/use-submit.retry.spec.ts +4 -4
  48. package/__tests__/features/use-submit/use-submit.revalidate.spec.ts +5 -5
  49. package/__tests__/features/use-submit/use-submit.submit.spec.ts +4 -3
  50. package/__tests__/features/use-submit/use-submit.typescript.spec.ts +325 -0
  51. package/__tests__/shared/isolation.tests.ts +1 -1
  52. package/__tests__/shared/response.tests.ts +5 -4
  53. package/__tests__/tsconfig.json +21 -2
  54. package/__tests__/utils/cache.utils.ts +6 -4
  55. package/__tests__/utils/emitter.utils.ts +3 -2
  56. package/__tests__/utils/listener.utils.ts +3 -2
  57. package/__tests__/utils/request.utils.ts +6 -6
  58. package/__tests__/utils/state.utils.ts +1 -1
  59. package/__tests__/utils/use-app-manager.utils.ts +1 -1
  60. package/__tests__/utils/use-cache.utils.ts +3 -2
  61. package/__tests__/utils/use-emitter.utils.ts +3 -2
  62. package/__tests__/utils/use-fetch.utils.ts +4 -3
  63. package/__tests__/utils/use-listener.utils.ts +3 -2
  64. package/__tests__/utils/use-queue.utils.ts +4 -2
  65. package/__tests__/utils/use-request-events.utils.ts +3 -2
  66. package/__tests__/utils/use-socket-state.utils.ts +3 -2
  67. package/__tests__/utils/use-submit.utils.ts +3 -2
  68. package/__tests__/utils/use-tracked-state.utils.ts +3 -2
  69. package/__tests__/vitest.setup.ts +8 -0
  70. package/dist/helpers/index.d.ts +4 -0
  71. package/dist/helpers/index.d.ts.map +1 -0
  72. package/dist/helpers/use-request-events/index.d.ts +3 -0
  73. package/dist/helpers/use-request-events/index.d.ts.map +1 -0
  74. package/dist/helpers/use-request-events/use-request-events.hooks.d.ts +10 -0
  75. package/dist/helpers/use-request-events/use-request-events.hooks.d.ts.map +1 -0
  76. package/dist/helpers/use-request-events/use-request-events.types.d.ts +78 -0
  77. package/dist/helpers/use-request-events/use-request-events.types.d.ts.map +1 -0
  78. package/dist/helpers/use-socket-state/index.d.ts +4 -0
  79. package/dist/helpers/use-socket-state/index.d.ts.map +1 -0
  80. package/dist/helpers/use-socket-state/use-socket-state.constants.d.ts +4 -0
  81. package/dist/helpers/use-socket-state/use-socket-state.constants.d.ts.map +1 -0
  82. package/dist/helpers/use-socket-state/use-socket-state.hooks.d.ts +24 -0
  83. package/dist/helpers/use-socket-state/use-socket-state.hooks.d.ts.map +1 -0
  84. package/dist/helpers/use-socket-state/use-socket-state.types.d.ts +12 -0
  85. package/dist/helpers/use-socket-state/use-socket-state.types.d.ts.map +1 -0
  86. package/dist/helpers/use-tracked-state/index.d.ts +5 -0
  87. package/dist/helpers/use-tracked-state/index.d.ts.map +1 -0
  88. package/dist/helpers/use-tracked-state/use-tracked-state.constants.d.ts +3 -0
  89. package/dist/helpers/use-tracked-state/use-tracked-state.constants.d.ts.map +1 -0
  90. package/dist/helpers/use-tracked-state/use-tracked-state.hooks.d.ts +12 -0
  91. package/dist/helpers/use-tracked-state/use-tracked-state.hooks.d.ts.map +1 -0
  92. package/dist/helpers/use-tracked-state/use-tracked-state.types.d.ts +113 -0
  93. package/dist/helpers/use-tracked-state/use-tracked-state.types.d.ts.map +1 -0
  94. package/dist/helpers/use-tracked-state/use-tracked-state.utils.d.ts +29 -0
  95. package/dist/helpers/use-tracked-state/use-tracked-state.utils.d.ts.map +1 -0
  96. package/dist/hooks/use-app-manager/index.d.ts +3 -0
  97. package/dist/hooks/use-app-manager/index.d.ts.map +1 -0
  98. package/dist/hooks/use-app-manager/use-app-manager.hooks.d.ts +4 -0
  99. package/dist/hooks/use-app-manager/use-app-manager.hooks.d.ts.map +1 -0
  100. package/dist/hooks/use-app-manager/use-app-manager.types.d.ts +19 -0
  101. package/dist/hooks/use-app-manager/use-app-manager.types.d.ts.map +1 -0
  102. package/dist/hooks/use-cache/index.d.ts +4 -0
  103. package/dist/hooks/use-cache/index.d.ts.map +1 -0
  104. package/dist/hooks/use-cache/use-cache.constants.d.ts +6 -0
  105. package/dist/hooks/use-cache/use-cache.constants.d.ts.map +1 -0
  106. package/dist/hooks/use-cache/use-cache.hooks.d.ts +4 -0
  107. package/dist/hooks/use-cache/use-cache.hooks.d.ts.map +1 -0
  108. package/dist/hooks/use-cache/use-cache.types.d.ts +24 -0
  109. package/dist/hooks/use-cache/use-cache.types.d.ts.map +1 -0
  110. package/dist/hooks/use-emitter/index.d.ts +3 -0
  111. package/dist/hooks/use-emitter/index.d.ts.map +1 -0
  112. package/dist/hooks/use-emitter/use-emitter.hooks.d.ts +26 -0
  113. package/dist/hooks/use-emitter/use-emitter.hooks.d.ts.map +1 -0
  114. package/dist/hooks/use-emitter/use-emitter.types.d.ts +4 -0
  115. package/dist/hooks/use-emitter/use-emitter.types.d.ts.map +1 -0
  116. package/dist/hooks/use-fetch/index.d.ts +5 -0
  117. package/dist/hooks/use-fetch/index.d.ts.map +1 -0
  118. package/dist/hooks/use-fetch/use-fetch.constants.d.ts +8 -0
  119. package/dist/hooks/use-fetch/use-fetch.constants.d.ts.map +1 -0
  120. package/dist/hooks/use-fetch/use-fetch.hooks.d.ts +10 -0
  121. package/dist/hooks/use-fetch/use-fetch.hooks.d.ts.map +1 -0
  122. package/dist/hooks/use-fetch/use-fetch.types.d.ts +118 -0
  123. package/dist/hooks/use-fetch/use-fetch.types.d.ts.map +1 -0
  124. package/dist/hooks/use-fetch/use-fetch.utils.d.ts +2 -0
  125. package/dist/hooks/use-fetch/use-fetch.utils.d.ts.map +1 -0
  126. package/dist/hooks/use-listener/index.d.ts +3 -0
  127. package/dist/hooks/use-listener/index.d.ts.map +1 -0
  128. package/dist/hooks/use-listener/use-listener.hooks.d.ts +31 -0
  129. package/dist/hooks/use-listener/use-listener.hooks.d.ts.map +1 -0
  130. package/dist/hooks/use-listener/use-listener.types.d.ts +4 -0
  131. package/dist/hooks/use-listener/use-listener.types.d.ts.map +1 -0
  132. package/dist/hooks/use-queue/index.d.ts +4 -0
  133. package/dist/hooks/use-queue/index.d.ts.map +1 -0
  134. package/dist/hooks/use-queue/use-queue.constants.d.ts +4 -0
  135. package/dist/hooks/use-queue/use-queue.constants.d.ts.map +1 -0
  136. package/dist/hooks/use-queue/use-queue.hooks.d.ts +10 -0
  137. package/dist/hooks/use-queue/use-queue.hooks.d.ts.map +1 -0
  138. package/dist/hooks/use-queue/use-queue.types.d.ts +58 -0
  139. package/dist/hooks/use-queue/use-queue.types.d.ts.map +1 -0
  140. package/dist/hooks/use-stream/index.d.ts +3 -0
  141. package/dist/hooks/use-stream/index.d.ts.map +1 -0
  142. package/dist/hooks/use-stream/use-stream.hooks.d.ts +11 -0
  143. package/dist/hooks/use-stream/use-stream.hooks.d.ts.map +1 -0
  144. package/dist/hooks/use-stream/use-stream.types.d.ts +52 -0
  145. package/dist/hooks/use-stream/use-stream.types.d.ts.map +1 -0
  146. package/dist/hooks/use-submit/index.d.ts +4 -0
  147. package/dist/hooks/use-submit/index.d.ts.map +1 -0
  148. package/dist/hooks/use-submit/use-submit.constants.d.ts +8 -0
  149. package/dist/hooks/use-submit/use-submit.constants.d.ts.map +1 -0
  150. package/dist/hooks/use-submit/use-submit.hooks.d.ts +10 -0
  151. package/dist/hooks/use-submit/use-submit.hooks.d.ts.map +1 -0
  152. package/dist/hooks/use-submit/use-submit.types.d.ts +115 -0
  153. package/dist/hooks/use-submit/use-submit.types.d.ts.map +1 -0
  154. package/dist/index.d.ts +12 -733
  155. package/dist/index.d.ts.map +1 -0
  156. package/dist/index.mjs +1935 -0
  157. package/dist/index.mjs.map +1 -0
  158. package/dist/provider/index.d.ts +3 -0
  159. package/dist/provider/index.d.ts.map +1 -0
  160. package/dist/provider/provider.d.ts +13 -0
  161. package/dist/provider/provider.d.ts.map +1 -0
  162. package/dist/provider/provider.types.d.ts +31 -0
  163. package/dist/provider/provider.types.d.ts.map +1 -0
  164. package/dist/utils/bounce.utils.d.ts +8 -0
  165. package/dist/utils/bounce.utils.d.ts.map +1 -0
  166. package/dist/utils/deep-equal.utils.d.ts +14 -0
  167. package/dist/utils/deep-equal.utils.d.ts.map +1 -0
  168. package/dist/utils/index.d.ts +4 -0
  169. package/dist/utils/index.d.ts.map +1 -0
  170. package/dist/utils/tracked-proxy.utils.d.ts +7 -0
  171. package/dist/utils/tracked-proxy.utils.d.ts.map +1 -0
  172. package/package.json +21 -13
  173. package/src/helpers/use-request-events/use-request-events.hooks.ts +60 -14
  174. package/src/helpers/use-request-events/use-request-events.types.ts +19 -12
  175. package/src/helpers/use-socket-state/use-socket-state.constants.ts +2 -2
  176. package/src/helpers/use-socket-state/use-socket-state.hooks.ts +47 -13
  177. package/src/helpers/use-socket-state/use-socket-state.types.ts +1 -1
  178. package/src/helpers/use-tracked-state/use-tracked-state.constants.ts +1 -1
  179. package/src/helpers/use-tracked-state/use-tracked-state.hooks.ts +74 -22
  180. package/src/helpers/use-tracked-state/use-tracked-state.types.ts +7 -2
  181. package/src/helpers/use-tracked-state/use-tracked-state.utils.ts +39 -4
  182. package/src/hooks/use-app-manager/use-app-manager.hooks.ts +2 -2
  183. package/src/hooks/use-cache/use-cache.hooks.ts +33 -41
  184. package/src/hooks/use-cache/use-cache.types.ts +3 -3
  185. package/src/hooks/use-emitter/use-emitter.hooks.ts +17 -16
  186. package/src/hooks/use-fetch/use-fetch.constants.ts +5 -2
  187. package/src/hooks/use-fetch/use-fetch.hooks.ts +109 -51
  188. package/src/hooks/use-fetch/use-fetch.types.ts +18 -3
  189. package/src/hooks/use-listener/use-listener.hooks.ts +19 -27
  190. package/src/hooks/use-queue/use-queue.constants.ts +2 -2
  191. package/src/hooks/use-queue/use-queue.hooks.ts +25 -6
  192. package/src/hooks/use-queue/use-queue.types.ts +8 -2
  193. package/src/hooks/use-stream/index.ts +2 -0
  194. package/src/hooks/use-stream/use-stream.hooks.ts +164 -0
  195. package/src/hooks/use-stream/use-stream.types.ts +53 -0
  196. package/src/hooks/use-submit/use-submit.constants.ts +2 -2
  197. package/src/hooks/use-submit/use-submit.hooks.ts +43 -48
  198. package/src/hooks/use-submit/use-submit.types.ts +9 -9
  199. package/src/index.ts +1 -0
  200. package/src/provider/provider.tsx +1 -1
  201. package/src/provider/provider.types.ts +8 -8
  202. package/src/utils/index.ts +1 -0
  203. package/src/utils/tracked-proxy.utils.ts +19 -0
  204. package/tsconfig.json +1 -1
  205. package/vite.config.ts +45 -0
  206. package/.eslintrc.json +0 -11
  207. package/__tests__/features/use-fetch/use-fetch.suspense.ts +0 -37
  208. package/__tests__/global.d.ts +0 -1
  209. package/__tests__/jest.setup.ts +0 -9
  210. package/dist/index.js +0 -1541
  211. package/dist/index.js.map +0 -7
  212. package/jest.config.ts +0 -6
@@ -0,0 +1,215 @@
1
+ import { waitFor } from "@testing-library/react";
2
+ import { createHttpMockingServer } from "@hyper-fetch/testing";
3
+
4
+ import { createRequest, renderUseFetch, client } from "../../utils";
5
+
6
+ const { resetMocks, startServer, stopServer, mockRequest } = createHttpMockingServer();
7
+
8
+ describe("useFetch [ keepPreviousData ]", () => {
9
+ beforeAll(() => {
10
+ startServer();
11
+ });
12
+
13
+ afterEach(() => {
14
+ resetMocks();
15
+ });
16
+
17
+ afterAll(() => {
18
+ stopServer();
19
+ });
20
+
21
+ beforeEach(() => {
22
+ vi.resetModules();
23
+ client.clear();
24
+ });
25
+
26
+ describe('when keepPreviousData is "auto" (default)', () => {
27
+ it("should clear state when URL params change (different resource)", async () => {
28
+ const request = createRequest({ endpoint: "/users/:userId" });
29
+
30
+ const requestA = request.setParams({ userId: "1" });
31
+ mockRequest(requestA, { data: { name: "Alice" } });
32
+
33
+ const view = renderUseFetch(requestA, { keepPreviousData: "auto" });
34
+
35
+ await waitFor(() => {
36
+ expect(view.result.current.data).toStrictEqual({ name: "Alice" });
37
+ });
38
+
39
+ const requestB = request.setParams({ userId: "2" });
40
+ mockRequest(requestB, { data: { name: "Bob" }, delay: 100 });
41
+
42
+ view.rerender({ request: requestB });
43
+
44
+ await waitFor(() => {
45
+ expect(view.result.current.data).toBeNull();
46
+ expect(view.result.current.loading).toBe(true);
47
+ });
48
+
49
+ await waitFor(() => {
50
+ expect(view.result.current.data).toStrictEqual({ name: "Bob" });
51
+ });
52
+ });
53
+
54
+ it("should preserve state when only query params change (pagination)", async () => {
55
+ const request = createRequest({ endpoint: "/items" });
56
+
57
+ const requestPage1 = request.setQueryParams({ page: "1" } as any);
58
+ mockRequest(requestPage1, { data: { items: [1, 2, 3] } });
59
+
60
+ const view = renderUseFetch(requestPage1, { keepPreviousData: "auto" });
61
+
62
+ await waitFor(() => {
63
+ expect(view.result.current.data).toStrictEqual({ items: [1, 2, 3] });
64
+ });
65
+
66
+ const requestPage2 = request.setQueryParams({ page: "2" } as any);
67
+ mockRequest(requestPage2, { data: { items: [4, 5, 6] }, delay: 100 });
68
+
69
+ view.rerender({ request: requestPage2 });
70
+
71
+ // Should preserve previous data while loading
72
+ expect(view.result.current.data).toStrictEqual({ items: [1, 2, 3] });
73
+
74
+ await waitFor(() => {
75
+ expect(view.result.current.data).toStrictEqual({ items: [4, 5, 6] });
76
+ });
77
+ });
78
+ });
79
+
80
+ describe('when keepPreviousData is "preserve"', () => {
81
+ it("should keep previous data even when URL params change", async () => {
82
+ const request = createRequest({ endpoint: "/users/:userId" });
83
+
84
+ const requestA = request.setParams({ userId: "1" });
85
+ mockRequest(requestA, { data: { name: "Alice" } });
86
+
87
+ const view = renderUseFetch(requestA, { keepPreviousData: "preserve" });
88
+
89
+ await waitFor(() => {
90
+ expect(view.result.current.data).toStrictEqual({ name: "Alice" });
91
+ });
92
+
93
+ const requestB = request.setParams({ userId: "2" });
94
+ mockRequest(requestB, { data: { name: "Bob" }, delay: 100 });
95
+
96
+ view.rerender({ request: requestB });
97
+
98
+ // Should preserve previous data
99
+ expect(view.result.current.data).toStrictEqual({ name: "Alice" });
100
+
101
+ await waitFor(() => {
102
+ expect(view.result.current.data).toStrictEqual({ name: "Bob" });
103
+ });
104
+ });
105
+
106
+ it("should keep previous data when query params change", async () => {
107
+ const request = createRequest({ endpoint: "/items" });
108
+
109
+ const requestPage1 = request.setQueryParams({ page: "1" } as any);
110
+ mockRequest(requestPage1, { data: { items: [1] } });
111
+
112
+ const view = renderUseFetch(requestPage1, { keepPreviousData: "preserve" });
113
+
114
+ await waitFor(() => {
115
+ expect(view.result.current.data).toStrictEqual({ items: [1] });
116
+ });
117
+
118
+ const requestPage2 = request.setQueryParams({ page: "2" } as any);
119
+ mockRequest(requestPage2, { data: { items: [2] }, delay: 100 });
120
+
121
+ view.rerender({ request: requestPage2 });
122
+
123
+ expect(view.result.current.data).toStrictEqual({ items: [1] });
124
+
125
+ await waitFor(() => {
126
+ expect(view.result.current.data).toStrictEqual({ items: [2] });
127
+ });
128
+ });
129
+ });
130
+
131
+ describe('when keepPreviousData is "clean"', () => {
132
+ it("should clear state when URL params change", async () => {
133
+ const request = createRequest({ endpoint: "/users/:userId" });
134
+
135
+ const requestA = request.setParams({ userId: "1" });
136
+ mockRequest(requestA, { data: { name: "Alice" } });
137
+
138
+ const view = renderUseFetch(requestA, { keepPreviousData: "clean" });
139
+
140
+ await waitFor(() => {
141
+ expect(view.result.current.data).toStrictEqual({ name: "Alice" });
142
+ });
143
+
144
+ const requestB = request.setParams({ userId: "2" });
145
+ mockRequest(requestB, { data: { name: "Bob" }, delay: 100 });
146
+
147
+ view.rerender({ request: requestB });
148
+
149
+ await waitFor(() => {
150
+ expect(view.result.current.data).toBeNull();
151
+ expect(view.result.current.loading).toBe(true);
152
+ });
153
+
154
+ await waitFor(() => {
155
+ expect(view.result.current.data).toStrictEqual({ name: "Bob" });
156
+ });
157
+ });
158
+
159
+ it("should clear state when query params change", async () => {
160
+ const request = createRequest({ endpoint: "/items" });
161
+
162
+ const requestPage1 = request.setQueryParams({ page: "1" } as any);
163
+ mockRequest(requestPage1, { data: { items: [1] } });
164
+
165
+ const view = renderUseFetch(requestPage1, { keepPreviousData: "clean" });
166
+
167
+ await waitFor(() => {
168
+ expect(view.result.current.data).toStrictEqual({ items: [1] });
169
+ });
170
+
171
+ const requestPage2 = request.setQueryParams({ page: "2" } as any);
172
+ mockRequest(requestPage2, { data: { items: [2] }, delay: 100 });
173
+
174
+ view.rerender({ request: requestPage2 });
175
+
176
+ await waitFor(() => {
177
+ expect(view.result.current.data).toBeNull();
178
+ expect(view.result.current.loading).toBe(true);
179
+ });
180
+
181
+ await waitFor(() => {
182
+ expect(view.result.current.data).toStrictEqual({ items: [2] });
183
+ });
184
+ });
185
+ });
186
+
187
+ describe("default behavior (no option specified)", () => {
188
+ it('should default to "auto" mode', async () => {
189
+ const request = createRequest({ endpoint: "/users/:userId" });
190
+
191
+ const requestA = request.setParams({ userId: "1" });
192
+ mockRequest(requestA, { data: { name: "Alice" } });
193
+
194
+ const view = renderUseFetch(requestA);
195
+
196
+ await waitFor(() => {
197
+ expect(view.result.current.data).toStrictEqual({ name: "Alice" });
198
+ });
199
+
200
+ const requestB = request.setParams({ userId: "2" });
201
+ mockRequest(requestB, { data: { name: "Bob" }, delay: 100 });
202
+
203
+ view.rerender({ request: requestB });
204
+
205
+ // Auto mode should clear for param changes
206
+ await waitFor(() => {
207
+ expect(view.result.current.data).toBeNull();
208
+ });
209
+
210
+ await waitFor(() => {
211
+ expect(view.result.current.data).toStrictEqual({ name: "Bob" });
212
+ });
213
+ });
214
+ });
215
+ });
@@ -22,7 +22,7 @@ describe("useFetch [ Methods ]", () => {
22
22
  });
23
23
 
24
24
  beforeEach(() => {
25
- jest.resetModules();
25
+ vi.resetModules();
26
26
  request = createRequest();
27
27
  client.clear();
28
28
  });
@@ -30,7 +30,7 @@ describe("useFetch [ Methods ]", () => {
30
30
  describe("given hook is mounted", () => {
31
31
  describe("when processing request", () => {
32
32
  it("should trigger onRequestStart helper", async () => {
33
- const spy = jest.fn();
33
+ const spy = vi.fn();
34
34
  const mock = mockRequest(request);
35
35
  const response = renderUseFetch(request);
36
36
 
@@ -42,7 +42,7 @@ describe("useFetch [ Methods ]", () => {
42
42
  expect(spy).toHaveBeenCalledTimes(1);
43
43
  });
44
44
  it("should trigger onResponseStart helper", async () => {
45
- const spy = jest.fn();
45
+ const spy = vi.fn();
46
46
  const mock = mockRequest(request);
47
47
  const response = renderUseFetch(request);
48
48
 
@@ -54,7 +54,7 @@ describe("useFetch [ Methods ]", () => {
54
54
  expect(spy).toHaveBeenCalledTimes(1);
55
55
  });
56
56
  it("should trigger onDownloadProgress helper", async () => {
57
- const spy = jest.fn();
57
+ const spy = vi.fn();
58
58
  const mock = mockRequest(request);
59
59
  const response = renderUseFetch(request);
60
60
 
@@ -63,10 +63,10 @@ describe("useFetch [ Methods ]", () => {
63
63
  });
64
64
 
65
65
  await testSuccessState(mock, response);
66
- expect(spy).toHaveBeenCalledTimes(3);
66
+ expect(spy.mock.calls.length).toBeGreaterThanOrEqual(1);
67
67
  });
68
68
  it("should trigger onUploadProgress helper", async () => {
69
- const spy = jest.fn();
69
+ const spy = vi.fn();
70
70
  const mock = mockRequest(request);
71
71
  const response = renderUseFetch(request);
72
72
 
@@ -75,13 +75,13 @@ describe("useFetch [ Methods ]", () => {
75
75
  });
76
76
 
77
77
  await testSuccessState(mock, response);
78
- expect(spy).toHaveBeenCalledTimes(3);
78
+ expect(spy.mock.calls.length).toBeGreaterThanOrEqual(1);
79
79
  });
80
80
  });
81
81
  describe("when getting the response", () => {
82
82
  it("should trigger onSuccess helper", async () => {
83
- const spy = jest.fn();
84
- const unusedSpy = jest.fn();
83
+ const spy = vi.fn();
84
+ const unusedSpy = vi.fn();
85
85
  const mock = mockRequest(request);
86
86
  const response = renderUseFetch(request);
87
87
 
@@ -95,8 +95,8 @@ describe("useFetch [ Methods ]", () => {
95
95
  expect(unusedSpy).toHaveBeenCalledTimes(0);
96
96
  });
97
97
  it("should trigger onError helper", async () => {
98
- const spy = jest.fn();
99
- const unusedSpy = jest.fn();
98
+ const spy = vi.fn();
99
+ const unusedSpy = vi.fn();
100
100
  const mock = mockRequest(request, { status: 400 });
101
101
  const response = renderUseFetch(request);
102
102
 
@@ -110,7 +110,7 @@ describe("useFetch [ Methods ]", () => {
110
110
  expect(unusedSpy).toHaveBeenCalledTimes(0);
111
111
  });
112
112
  it("should trigger onFinished helper on success", async () => {
113
- const spy = jest.fn();
113
+ const spy = vi.fn();
114
114
  const mock = mockRequest(request);
115
115
  const response = renderUseFetch(request);
116
116
 
@@ -122,7 +122,7 @@ describe("useFetch [ Methods ]", () => {
122
122
  expect(spy).toHaveBeenCalledTimes(1);
123
123
  });
124
124
  it("should trigger onFinished helper on error", async () => {
125
- const spy = jest.fn();
125
+ const spy = vi.fn();
126
126
  const mock = mockRequest(request, { status: 400 });
127
127
  const response = renderUseFetch(request);
128
128
 
@@ -136,7 +136,7 @@ describe("useFetch [ Methods ]", () => {
136
136
  });
137
137
  describe("when getting internal error", () => {
138
138
  it("should trigger onOfflineError helper", async () => {
139
- const spy = jest.fn();
139
+ const spy = vi.fn();
140
140
  mockRequest(request, { status: 400 });
141
141
  const response = renderUseFetch(request.setOffline(true));
142
142
 
@@ -150,14 +150,14 @@ describe("useFetch [ Methods ]", () => {
150
150
  });
151
151
  });
152
152
  it("should trigger methods on retry", async () => {
153
- const spy1 = jest.fn();
154
- const spy2 = jest.fn();
155
- const spy3 = jest.fn();
156
- const spy4 = jest.fn();
157
- const spy5 = jest.fn();
158
- const spy6 = jest.fn();
159
- const spy7 = jest.fn();
160
- const spy8 = jest.fn();
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
161
 
162
162
  const errorMock = mockRequest(request, { status: 400 });
163
163
  const response = renderUseFetch(request.setRetry(1).setRetryTime(100));
@@ -179,22 +179,22 @@ describe("useFetch [ Methods ]", () => {
179
179
 
180
180
  expect(spy1).toHaveBeenCalledTimes(2);
181
181
  expect(spy2).toHaveBeenCalledTimes(2);
182
- expect(spy3).toHaveBeenCalledTimes(6);
183
- expect(spy4).toHaveBeenCalledTimes(6);
182
+ expect(spy3.mock.calls.length).toBeGreaterThanOrEqual(2);
183
+ expect(spy4.mock.calls.length).toBeGreaterThanOrEqual(2);
184
184
  expect(spy5).toHaveBeenCalledTimes(1);
185
185
  expect(spy6).toHaveBeenCalledTimes(1);
186
186
  expect(spy7).toHaveBeenCalledTimes(2);
187
187
  expect(spy8).toHaveBeenCalledTimes(0);
188
188
  });
189
189
  it("should trigger methods on coming back online", async () => {
190
- const spy1 = jest.fn();
191
- const spy2 = jest.fn();
192
- const spy3 = jest.fn();
193
- const spy4 = jest.fn();
194
- const spy5 = jest.fn();
195
- const spy6 = jest.fn();
196
- const spy7 = jest.fn();
197
- const spy8 = jest.fn();
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
198
 
199
199
  const errorMock = mockRequest(request, { status: 400 });
200
200
  const response = renderUseFetch(request);
@@ -221,8 +221,8 @@ describe("useFetch [ Methods ]", () => {
221
221
  await testSuccessState(successMock, response);
222
222
  expect(spy1).toHaveBeenCalledTimes(2);
223
223
  expect(spy2).toHaveBeenCalledTimes(2);
224
- expect(spy3).toHaveBeenCalledTimes(6);
225
- expect(spy4).toHaveBeenCalledTimes(6);
224
+ expect(spy3.mock.calls.length).toBeGreaterThanOrEqual(2);
225
+ expect(spy4.mock.calls.length).toBeGreaterThanOrEqual(2);
226
226
  expect(spy5).toHaveBeenCalledTimes(1);
227
227
  expect(spy6).toHaveBeenCalledTimes(0);
228
228
  expect(spy7).toHaveBeenCalledTimes(2);
@@ -22,7 +22,7 @@ describe("useFetch [ Offline ]", () => {
22
22
  });
23
23
 
24
24
  beforeEach(() => {
25
- jest.resetModules();
25
+ vi.resetModules();
26
26
  request = createRequest({ offline: true });
27
27
  client.clear();
28
28
  });
@@ -34,7 +34,7 @@ describe("useFetch [ Offline ]", () => {
34
34
 
35
35
  await testSuccessState(mock, response);
36
36
 
37
- const spy = jest.spyOn(client.adapter, "fetch");
37
+ const spy = vi.spyOn(client.adapter, "fetch");
38
38
 
39
39
  act(() => {
40
40
  client.appManager.setOnline(false);
@@ -55,7 +55,7 @@ describe("useFetch [ Offline ]", () => {
55
55
  await testSuccessState(mock, response);
56
56
  });
57
57
  it("should refetch when coming back online", async () => {
58
- const spy = jest.fn();
58
+ const spy = vi.fn();
59
59
  const mock = mockRequest(request);
60
60
  const response = renderUseFetch(request, { refetchOnReconnect: true });
61
61
  await testSuccessState(mock, response);
@@ -18,7 +18,7 @@ describe("useFetch [ Pagination ]", () => {
18
18
  });
19
19
 
20
20
  beforeEach(() => {
21
- jest.resetModules();
21
+ vi.resetModules();
22
22
  client.clear();
23
23
  });
24
24
 
@@ -23,7 +23,7 @@ describe("useFetch [ Prefetch ]", () => {
23
23
  });
24
24
 
25
25
  beforeEach(() => {
26
- jest.resetModules();
26
+ vi.resetModules();
27
27
  client.clear();
28
28
  mock = mockRequest(request);
29
29
  request = createRequest();
@@ -30,13 +30,13 @@ describe("useFetch [ Refreshing ]", () => {
30
30
  });
31
31
 
32
32
  beforeEach(() => {
33
- jest.resetModules();
33
+ vi.resetModules();
34
34
  request = createRequest();
35
35
  client.clear();
36
36
  });
37
37
 
38
38
  it("should refetch data after refresh time of 200ms", async () => {
39
- const spy = jest.fn();
39
+ const spy = vi.fn();
40
40
  mockRequest(request);
41
41
  const { result } = renderUseFetch(request, hookOptions);
42
42
 
@@ -51,7 +51,7 @@ describe("useFetch [ Refreshing ]", () => {
51
51
  expect(spy).toHaveBeenCalledTimes(2);
52
52
  });
53
53
  it("should refresh blurred tab", async () => {
54
- const spy = jest.fn();
54
+ const spy = vi.fn();
55
55
  mockRequest(request);
56
56
  const { result } = renderUseFetch(request, { ...hookOptions, refetchOnBlur: false });
57
57
 
@@ -65,7 +65,7 @@ describe("useFetch [ Refreshing ]", () => {
65
65
  expect(spy).toHaveBeenCalledTimes(2);
66
66
  });
67
67
  it("should not refresh blurred tab", async () => {
68
- const spy = jest.fn();
68
+ const spy = vi.fn();
69
69
  mockRequest(request);
70
70
  const { result } = renderUseFetch(request, { ...hookOptions, refetchOnBlur: false, refetchBlurred: false });
71
71
 
@@ -85,7 +85,7 @@ describe("useFetch [ Refreshing ]", () => {
85
85
  // TODO
86
86
  });
87
87
  it("should stop refreshing when value is changing from true to false", async () => {
88
- const spy = jest.fn();
88
+ const spy = vi.fn();
89
89
  mockRequest(request);
90
90
  const { result, rerender } = renderUseFetch(request, hookOptions);
91
91
 
@@ -108,7 +108,7 @@ describe("useFetch [ Refreshing ]", () => {
108
108
  expect(spy).toHaveBeenCalledTimes(2);
109
109
  });
110
110
  it("should refetch when tab is focused and refetchOnFocus is true", async () => {
111
- const spy = jest.fn();
111
+ const spy = vi.fn();
112
112
  mockRequest(request);
113
113
  const { result } = renderUseFetch(request, { ...hookOptions });
114
114
 
@@ -127,8 +127,42 @@ describe("useFetch [ Refreshing ]", () => {
127
127
  await waitForRender();
128
128
  expect(spy).toHaveBeenCalledTimes(2);
129
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
+ });
130
164
  it("should not refetch when tab is focused and refetchOnFocus is false", async () => {
131
- const spy = jest.fn();
165
+ const spy = vi.fn();
132
166
  mockRequest(request);
133
167
  const { result } = renderUseFetch(request, {
134
168
  ...hookOptions,
@@ -44,7 +44,7 @@ describe("useFetch [ Rerender ]", () => {
44
44
  });
45
45
 
46
46
  beforeEach(() => {
47
- jest.resetModules();
47
+ vi.resetModules();
48
48
  rerenders = 0;
49
49
  request = createRequest();
50
50
  client.clear();
@@ -99,7 +99,7 @@ describe("useFetch [ Rerender ]", () => {
99
99
  useDidMount(() => {
100
100
  setResponseTimestamp(new Date());
101
101
  setTimeout(() => {
102
- setError(new Error("test"), true);
102
+ setError(new Error("test"));
103
103
  }, fetchTime / 2);
104
104
  });
105
105
 
@@ -16,7 +16,7 @@ describe("useFetch [ Retry ]", () => {
16
16
  });
17
17
 
18
18
  beforeEach(() => {
19
- jest.resetModules();
19
+ vi.resetModules();
20
20
  });
21
21
 
22
22
  describe("when request retry attribute is set to false", () => {
@@ -26,7 +26,7 @@ describe("useFetch [ refetch ]", () => {
26
26
  });
27
27
 
28
28
  beforeEach(() => {
29
- jest.resetModules();
29
+ vi.resetModules();
30
30
  request = createRequest();
31
31
  client.clear();
32
32
  mock = mockRequest(request, {
@@ -35,7 +35,7 @@ describe("useFetch [ refetch ]", () => {
35
35
  });
36
36
 
37
37
  it("should allow to prevent invalidation on mount with cache data", async () => {
38
- const spy = jest.fn();
38
+ const spy = vi.fn();
39
39
  const customMock = { something: "123" };
40
40
  client.cache.set(request, {
41
41
  data: customMock,
@@ -50,6 +50,7 @@ describe("useFetch [ refetch ]", () => {
50
50
  responseTimestamp: +new Date(),
51
51
  isCanceled: false,
52
52
  isOffline: false,
53
+ willRetry: false,
53
54
  });
54
55
 
55
56
  const response = renderUseFetch(request, { revalidate: false });
@@ -62,7 +63,7 @@ describe("useFetch [ refetch ]", () => {
62
63
  expect(spy).toHaveBeenCalledTimes(0);
63
64
  });
64
65
  it("should allow invalidation on mount", async () => {
65
- const spy = jest.fn();
66
+ const spy = vi.fn();
66
67
  const response = renderUseFetch(request, { revalidate: false });
67
68
 
68
69
  act(() => {
@@ -91,6 +92,7 @@ describe("useFetch [ refetch ]", () => {
91
92
  responseTimestamp: +new Date(),
92
93
  isCanceled: false,
93
94
  isOffline: false,
95
+ willRetry: false,
94
96
  });
95
97
 
96
98
  const response = renderUseFetch(request, { revalidate: true });
@@ -193,7 +195,7 @@ describe("useFetch [ refetch ]", () => {
193
195
  });
194
196
  });
195
197
  it("should not refetch while toggling query", async () => {
196
- const spy = jest.fn();
198
+ const spy = vi.fn();
197
199
 
198
200
  const revalidateRequest = createRequest({ endpoint: "123-revalidate" });
199
201
  const revalidateMock = mockRequest(revalidateRequest, { data: { something: 123 } });