@hyper-fetch/react 7.5.1 → 7.5.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (70) hide show
  1. package/__tests__/features/helpers/use-request-events/use-request-events.base.spec.ts +115 -0
  2. package/__tests__/features/helpers/use-request-events/use-request-events.utils.spec.ts +69 -0
  3. package/__tests__/features/helpers/use-socket-state/use-socket-state.base.spec.ts +175 -0
  4. package/__tests__/features/helpers/use-tracked-state/use-tacked-state.actions.spec.ts +382 -0
  5. package/__tests__/features/helpers/use-tracked-state/use-tracked-state.base.spec.ts +527 -0
  6. package/__tests__/features/helpers/use-tracked-state/use-tracked-state.utils.spec.ts +260 -0
  7. package/__tests__/features/provider/provider.hydration.spec.tsx +161 -0
  8. package/__tests__/features/provider/use-config-provider.base.spec.tsx +68 -0
  9. package/__tests__/features/use-app-manager/use-app-manager.base.spec.ts +85 -0
  10. package/__tests__/features/use-cache/use-cache.base.spec.ts +133 -0
  11. package/__tests__/features/use-cache/use-cache.helpers.ts +40 -0
  12. package/__tests__/features/use-emitter/use-emitter.base.spec.ts +124 -0
  13. package/__tests__/features/use-fetch/use-fetch.base.spec.ts +284 -0
  14. package/__tests__/features/use-fetch/use-fetch.cancel.spec.ts +83 -0
  15. package/__tests__/features/use-fetch/use-fetch.concurrency.spec.ts +91 -0
  16. package/__tests__/features/use-fetch/use-fetch.debounce.spec.ts +153 -0
  17. package/__tests__/features/use-fetch/use-fetch.deduplication.spec.ts +80 -0
  18. package/__tests__/features/use-fetch/use-fetch.infinite.ts +39 -0
  19. package/__tests__/features/use-fetch/use-fetch.integration.spec.tsx +172 -0
  20. package/__tests__/features/use-fetch/use-fetch.methods.spec.ts +233 -0
  21. package/__tests__/features/use-fetch/use-fetch.offline.spec.ts +103 -0
  22. package/__tests__/features/use-fetch/use-fetch.pagination.ts +36 -0
  23. package/__tests__/features/use-fetch/use-fetch.prefetch.spec.tsx +73 -0
  24. package/__tests__/features/use-fetch/use-fetch.refresh.spec.ts +153 -0
  25. package/__tests__/features/use-fetch/use-fetch.rerender.spec.tsx +151 -0
  26. package/__tests__/features/use-fetch/use-fetch.retry.ts +38 -0
  27. package/__tests__/features/use-fetch/use-fetch.revalidate.spec.ts +235 -0
  28. package/__tests__/features/use-fetch/use-fetch.suspense.ts +37 -0
  29. package/__tests__/features/use-fetch/use-fetch.typescript.spec.ts +59 -0
  30. package/__tests__/features/use-fetch/use-fetch.utils.spec.ts +26 -0
  31. package/__tests__/features/use-fetch/use-fetch.window.spec.ts +59 -0
  32. package/__tests__/features/use-listener/use-listener.base.spec.ts +57 -0
  33. package/__tests__/features/use-queue/use-queue.actions.spec.ts +238 -0
  34. package/__tests__/features/use-queue/use-queue.base.spec.ts +109 -0
  35. package/__tests__/features/use-submit/use-submit.base.spec.ts +76 -0
  36. package/__tests__/features/use-submit/use-submit.cancel.spec.ts +46 -0
  37. package/__tests__/features/use-submit/use-submit.concurrency.spec.ts +62 -0
  38. package/__tests__/features/use-submit/use-submit.debounce.spec.ts +320 -0
  39. package/__tests__/features/use-submit/use-submit.methods.spec.ts +278 -0
  40. package/__tests__/features/use-submit/use-submit.queue.spec.ts +84 -0
  41. package/__tests__/features/use-submit/use-submit.retry.spec.ts +92 -0
  42. package/__tests__/features/use-submit/use-submit.revalidate.spec.ts +99 -0
  43. package/__tests__/features/use-submit/use-submit.submit.spec.ts +233 -0
  44. package/__tests__/global.d.ts +1 -0
  45. package/__tests__/jest.setup.ts +9 -0
  46. package/__tests__/shared/index.ts +2 -0
  47. package/__tests__/shared/isolation.tests.ts +12 -0
  48. package/__tests__/shared/response.tests.ts +127 -0
  49. package/__tests__/tsconfig.json +8 -0
  50. package/__tests__/utils/cache.utils.ts +53 -0
  51. package/__tests__/utils/client.utils.ts +3 -0
  52. package/__tests__/utils/emitter.utils.ts +7 -0
  53. package/__tests__/utils/helpers.utils.ts +9 -0
  54. package/__tests__/utils/index.ts +14 -0
  55. package/__tests__/utils/listener.utils.ts +10 -0
  56. package/__tests__/utils/request.utils.ts +12 -0
  57. package/__tests__/utils/state.utils.ts +7 -0
  58. package/__tests__/utils/use-app-manager.utils.ts +10 -0
  59. package/__tests__/utils/use-cache.utils.ts +11 -0
  60. package/__tests__/utils/use-emitter.utils.ts +11 -0
  61. package/__tests__/utils/use-fetch.utils.ts +11 -0
  62. package/__tests__/utils/use-listener.utils.ts +11 -0
  63. package/__tests__/utils/use-queue.utils.ts +68 -0
  64. package/__tests__/utils/use-request-events.utils.ts +42 -0
  65. package/__tests__/utils/use-socket-state.utils.ts +11 -0
  66. package/__tests__/utils/use-submit.utils.ts +11 -0
  67. package/__tests__/utils/use-tracked-state.utils.ts +26 -0
  68. package/jest.config.ts +6 -0
  69. package/package.json +1 -1
  70. package/project.json +4 -0
@@ -0,0 +1,260 @@
1
+ import { createClient } from "@hyper-fetch/core";
2
+
3
+ import { getValidCacheData, getInitialState, initialState } from "helpers";
4
+ import { isEmpty, isEqual } from "utils";
5
+
6
+ describe("useTrackedState [ Utils ]", () => {
7
+ describe("when isEmpty gets triggered", () => {
8
+ it("should return true for empty array or object", async () => {
9
+ expect(isEmpty([])).toBe(true);
10
+ expect(isEmpty({})).toBe(true);
11
+ });
12
+
13
+ it("should return false", async () => {
14
+ expect(isEmpty(null)).toBe(false);
15
+ expect(isEmpty(new Date())).toBe(false);
16
+ expect(isEmpty(NaN)).toBe(false);
17
+ expect(isEmpty("")).toBe(false);
18
+ expect(isEmpty(false)).toBe(false);
19
+ expect(isEmpty(undefined)).toBe(false);
20
+ expect(isEmpty(0)).toBe(false);
21
+ expect(isEmpty(true)).toBe(false);
22
+ expect(isEmpty(123)).toBe(false);
23
+ expect(isEmpty("123")).toBe(false);
24
+ expect(isEmpty([1, 2, 3])).toBe(false);
25
+ expect(isEmpty({ something: 1 })).toBe(false);
26
+ });
27
+ });
28
+
29
+ describe("when isEqual gets triggered", () => {
30
+ it("should return false for non equal values", async () => {
31
+ expect(isEqual(true, false)).toBe(false);
32
+ expect(isEqual(null, {})).toBe(false);
33
+ expect(isEqual(new Date(), {})).toBe(false);
34
+ expect(isEqual(new Date(), null)).toBe(false);
35
+ expect(isEqual(NaN, {})).toBe(false);
36
+ expect(isEqual(NaN, null)).toBe(false);
37
+ expect(isEqual("1", 1)).toBe(false);
38
+ expect(isEqual(0, -1)).toBe(false);
39
+ expect(isEqual("2", "3")).toBe(false);
40
+ expect(isEqual([], [1])).toBe(false);
41
+ expect(isEqual([1], [2])).toBe(false);
42
+ expect(isEqual(["1"], [1])).toBe(false);
43
+ expect(isEqual([null], [NaN])).toBe(false);
44
+ expect(isEqual([null], [new Date()])).toBe(false);
45
+ expect(isEqual([null], [{}])).toBe(false);
46
+ expect(isEqual([new Date()], [{}])).toBe(false);
47
+ expect(isEqual([NaN], [{}])).toBe(false);
48
+ expect(isEqual([{ someKey: 1 }], [{ someKey: 2 }])).toBe(false);
49
+ expect(isEqual([{ someKey: "1" }], [{ someKey: 1 }])).toBe(false);
50
+ expect(isEqual([{ someKey: null }], [{ someKey: {} }])).toBe(false);
51
+ expect(isEqual([{ someKey: null }], [{ someKey: NaN }])).toBe(false);
52
+ expect(isEqual([{ someKey: null }], [{ someKey: new Date() }])).toBe(false);
53
+ expect(isEqual([{ someKey: {} }], [{ someKey: new Date() }])).toBe(false);
54
+ expect(isEqual([{ someKey: {} }], [{ someKey: NaN }])).toBe(false);
55
+ expect(isEqual({ someKey: 1 }, { otherKey: 1 })).toBe(false);
56
+ expect(isEqual({ someKey: 1 }, { someKey: 1, otherKey: 1 })).toBe(false);
57
+ expect(isEqual({ someKey: 1 }, { someKey: 2 })).toBe(false);
58
+ expect(isEqual({ someKey: "1" }, { someKey: 1 })).toBe(false);
59
+ expect(isEqual({ someKey: null }, { someKey: {} })).toBe(false);
60
+ expect(isEqual({ someKey: null }, { someKey: NaN })).toBe(false);
61
+ expect(isEqual({ someKey: null }, { someKey: new Date() })).toBe(false);
62
+ expect(isEqual({ someKey: {} }, { someKey: new Date() })).toBe(false);
63
+ expect(isEqual({ someKey: {} }, { someKey: NaN })).toBe(false);
64
+ expect(isEqual({ someKey: 1 }, { otherKey: 1 })).toBe(false);
65
+ expect(isEqual({ someKey: 1 }, { someKey: 1, otherKey: 1 })).toBe(false);
66
+ });
67
+
68
+ it("should return true for equal arguments", async () => {
69
+ const date = new Date();
70
+
71
+ expect(isEqual(true, true)).toBe(true);
72
+ expect(isEqual(false, false)).toBe(true);
73
+ expect(isEqual(undefined, undefined)).toBe(true);
74
+ expect(isEqual(null, null)).toBe(true);
75
+ expect(isEqual(date, date)).toBe(true);
76
+ expect(isEqual(date, date)).toBe(true);
77
+ expect(isEqual(NaN, NaN)).toBe(true);
78
+ expect(isEqual("1", "1")).toBe(true);
79
+ expect(isEqual(0, 0)).toBe(true);
80
+ expect(isEqual(1, 1)).toBe(true);
81
+ expect(isEqual("2", "2")).toBe(true);
82
+ expect(isEqual([], [])).toBe(true);
83
+ expect(isEqual([1], [1])).toBe(true);
84
+ expect(isEqual(["1"], ["1"])).toBe(true);
85
+ expect(isEqual([NaN], [NaN])).toBe(true);
86
+ expect(isEqual([date], [date])).toBe(true);
87
+ expect(isEqual([null], [null])).toBe(true);
88
+ expect(isEqual([{}], [{}])).toBe(true);
89
+ expect(isEqual([{ someKey: 1 }], [{ someKey: 1 }])).toBe(true);
90
+ expect(isEqual([{ someKey: "1" }], [{ someKey: "1" }])).toBe(true);
91
+ expect(isEqual([{ someKey: null }], [{ someKey: null }])).toBe(true);
92
+ expect(isEqual([{ someKey: NaN }], [{ someKey: NaN }])).toBe(true);
93
+ expect(isEqual([{ someKey: date }], [{ someKey: date }])).toBe(true);
94
+ expect(isEqual([{ someKey: {} }], [{ someKey: {} }])).toBe(true);
95
+ expect(isEqual({ someKey: 1, otherKey: 1 }, { someKey: 1, otherKey: 1 })).toBe(true);
96
+ expect(isEqual({ someKey: "1" }, { someKey: "1" })).toBe(true);
97
+ expect(isEqual({ someKey: {} }, { someKey: {} })).toBe(true);
98
+ expect(isEqual({ someKey: NaN }, { someKey: NaN })).toBe(true);
99
+ expect(isEqual({ someKey: null }, { someKey: null })).toBe(true);
100
+ expect(isEqual({ someKey: date }, { someKey: date })).toBe(true);
101
+ expect(isEqual({}, {})).toBe(true);
102
+ expect(isEqual([null], [null])).toBe(true);
103
+ });
104
+
105
+ it("should return false when comparison throws an error", async () => {
106
+ // Create an object with a getter that throws
107
+ const throwingObj = {
108
+ get evil() {
109
+ throw new Error("Comparison error");
110
+ },
111
+ };
112
+
113
+ const obj1 = { prop: throwingObj };
114
+ const obj2 = { prop: { key: 2 } };
115
+
116
+ // Should return false when comparison throws
117
+ expect(isEqual(obj1, obj2)).toBe(false);
118
+ });
119
+ });
120
+
121
+ describe("when getValidCacheData gets triggered", () => {
122
+ const mockRequest = {
123
+ staleTime: 1000,
124
+ client: {
125
+ cache: {
126
+ version: 1,
127
+ },
128
+ },
129
+ cacheKey: "test-key",
130
+ cacheTime: 2000,
131
+ } as any;
132
+
133
+ it("should return initialResponse data when cache is stale", () => {
134
+ const initialResponse = {
135
+ data: "test-data",
136
+ status: 200,
137
+ success: true,
138
+ requestTimestamp: new Date().getTime(),
139
+ responseTimestamp: new Date().getTime(),
140
+ };
141
+
142
+ const result = getValidCacheData(mockRequest, initialResponse, null);
143
+
144
+ expect(result).toEqual(
145
+ expect.objectContaining({
146
+ data: "test-data",
147
+ error: null,
148
+ status: 200,
149
+ success: true,
150
+ extra: null,
151
+ staleTime: 1000,
152
+ version: 1,
153
+ cacheKey: "test-key",
154
+ cacheTime: 2000,
155
+ }),
156
+ );
157
+ });
158
+
159
+ it("should return null when no cache data and no initialResponse", () => {
160
+ const result = getValidCacheData(mockRequest, null, null);
161
+ expect(result).toBeNull();
162
+ });
163
+
164
+ it("should return cache data when not stale", () => {
165
+ const currentTime = new Date().getTime();
166
+ const cacheData = {
167
+ data: "cached-data",
168
+ error: null,
169
+ status: 200,
170
+ success: true,
171
+ extra: null,
172
+ responseTimestamp: currentTime, // Recent timestamp within stale time
173
+ } as any;
174
+
175
+ const result = getValidCacheData(mockRequest, null, cacheData);
176
+ expect(result).toBe(cacheData);
177
+ });
178
+ });
179
+
180
+ describe("when getInitialState handles async responseMapper", () => {
181
+ it("should return initialState when responseMapper returns a Promise", () => {
182
+ const mockRequest = {
183
+ client: {
184
+ cache: { get: jest.fn().mockReturnValue(null) },
185
+ defaultExtra: null,
186
+ },
187
+ queryKey: "test",
188
+ cacheKey: "test",
189
+ unstable_responseMapper: () => Promise.resolve({ data: "test" }),
190
+ } as any;
191
+
192
+ const mockDispatcher = {
193
+ getQueue: jest.fn().mockReturnValue({ stopped: false }),
194
+ hasRunningRequests: jest.fn().mockReturnValue(false),
195
+ } as any;
196
+
197
+ const result = getInitialState({
198
+ initialResponse: { data: "test" },
199
+ dispatcher: mockDispatcher,
200
+ request: mockRequest,
201
+ disabled: false,
202
+ revalidate: false,
203
+ });
204
+
205
+ expect(result).toEqual(initialState);
206
+ });
207
+ });
208
+
209
+ describe("when getInitialState handles sync responseMapper", () => {
210
+ it("should apply sync responseMapper to cache data", () => {
211
+ const currentTime = new Date().getTime();
212
+ const cacheData = {
213
+ data: "original-data",
214
+ error: null,
215
+ status: 200,
216
+ success: true,
217
+ extra: null,
218
+ responseTimestamp: currentTime,
219
+ retries: 0,
220
+ };
221
+
222
+ const client = createClient({ url: "http://localhost:3000" });
223
+ const mockRequest = client.createRequest()({ endpoint: "/test" });
224
+ mockRequest.unstable_responseMapper = (data: any) => ({
225
+ ...data,
226
+ data: `mapped-${data.data}`,
227
+ });
228
+
229
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
230
+ // @ts-ignore
231
+ client.cache = {
232
+ get: jest.fn().mockReturnValue(cacheData),
233
+ version: "1",
234
+ };
235
+
236
+ const mockDispatcher = {
237
+ getQueue: jest.fn().mockReturnValue({ stopped: false }),
238
+ hasRunningRequests: jest.fn().mockReturnValue(false),
239
+ } as any;
240
+
241
+ const result = getInitialState({
242
+ initialResponse: null,
243
+ dispatcher: mockDispatcher,
244
+ request: mockRequest,
245
+ disabled: false,
246
+ revalidate: false,
247
+ });
248
+
249
+ expect(result).toEqual(
250
+ expect.objectContaining({
251
+ data: "mapped-original-data",
252
+ error: null,
253
+ status: 200,
254
+ success: true,
255
+ loading: false,
256
+ }),
257
+ );
258
+ });
259
+ });
260
+ });
@@ -0,0 +1,161 @@
1
+ /* eslint-disable react/jsx-fragments */
2
+ /* eslint-disable react/jsx-no-useless-fragment */
3
+ import { Fragment } from "react";
4
+ import { HttpAdapterExtraType, Client } from "@hyper-fetch/core";
5
+ import { render, waitFor } from "@testing-library/react";
6
+
7
+ import { UseFetchReturnType, useFetch } from "hooks/use-fetch";
8
+ import { UseSubmitReturnType, useSubmit } from "hooks/use-submit";
9
+ import { Provider } from "provider";
10
+
11
+ describe("Provider [ Hydration ]", () => {
12
+ const data = [1, 2, 3, 4, 5];
13
+ const extra: HttpAdapterExtraType = { headers: { "x-test": "test" } };
14
+ let client = new Client({
15
+ url: "http://localhost:3000",
16
+ });
17
+ let request = client.createRequest<{ response: any }>()({ endpoint: "/shared-endpoint" });
18
+
19
+ const hydrate = () => {
20
+ client.hydrate([
21
+ {
22
+ cacheKey: request.cacheKey,
23
+ cache: true,
24
+ cacheTime: Infinity,
25
+ staleTime: 1000,
26
+ timestamp: Date.now(),
27
+ response: {
28
+ data,
29
+ error: null,
30
+ status: 200,
31
+ extra,
32
+ success: true,
33
+ responseTimestamp: Date.now(),
34
+ requestTimestamp: Date.now(),
35
+ },
36
+ hydrated: true,
37
+ override: true,
38
+ },
39
+ ]);
40
+ };
41
+
42
+ const Page = () => {
43
+ return <Fragment />;
44
+ };
45
+
46
+ const App = ({ children }: { children?: React.ReactNode }) => {
47
+ return (
48
+ <Provider
49
+ config={{
50
+ useFetchConfig: {
51
+ disabled: true,
52
+ },
53
+ }}
54
+ >
55
+ <Page />
56
+ {children}
57
+ </Provider>
58
+ );
59
+ };
60
+
61
+ beforeEach(() => {
62
+ client.clear();
63
+ client = new Client({
64
+ url: "http://localhost:3000",
65
+ });
66
+ request = client.createRequest<{ response: any }>()({ endpoint: "/shared-endpoint" });
67
+ });
68
+
69
+ describe("given app is rendered on the Server", () => {
70
+ describe("when hydration data is passed down to the Provider", () => {
71
+ it("should hydrate the data for useFetch hook", async () => {
72
+ let useFetchResults: UseFetchReturnType<typeof request> | undefined;
73
+
74
+ hydrate();
75
+ const Children = () => {
76
+ useFetchResults = useFetch(request, { revalidate: false, disabled: true });
77
+ return <Fragment />;
78
+ };
79
+ render(
80
+ <App>
81
+ <Children />
82
+ </App>,
83
+ { hydrate: true },
84
+ );
85
+
86
+ await waitFor(() => {
87
+ expect(useFetchResults?.data).toStrictEqual(data);
88
+ expect(useFetchResults?.status).toBe(200);
89
+ expect(useFetchResults?.error).toBe(null);
90
+ expect(useFetchResults?.extra).toBe(extra);
91
+ expect(useFetchResults?.success).toBe(true);
92
+ });
93
+ });
94
+ it("should hydrate the data for useSubmit hook", async () => {
95
+ let useSubmitResults: UseSubmitReturnType<typeof request> | undefined;
96
+
97
+ hydrate();
98
+ const Children = () => {
99
+ useSubmitResults = useSubmit(request);
100
+ return <Fragment />;
101
+ };
102
+ render(
103
+ <App>
104
+ <Children />
105
+ </App>,
106
+ { hydrate: true },
107
+ );
108
+ await waitFor(() => {
109
+ expect(useSubmitResults?.data).toStrictEqual(data);
110
+ expect(useSubmitResults?.status).toBe(200);
111
+ expect(useSubmitResults?.error).toBe(null);
112
+ expect(useSubmitResults?.extra).toBe(extra);
113
+ });
114
+ });
115
+ });
116
+ });
117
+
118
+ describe("given app is rendered on the Client", () => {
119
+ describe("when hydration data is passed down to the Provider", () => {
120
+ it("should hydrate the data for useFetch hook", async () => {
121
+ let useFetchResults: UseFetchReturnType<typeof request> | undefined;
122
+ hydrate();
123
+ const Children = () => {
124
+ useFetchResults = useFetch(request);
125
+ return <Fragment />;
126
+ };
127
+ render(
128
+ <App>
129
+ <Children />
130
+ </App>,
131
+ );
132
+ await waitFor(() => {
133
+ expect(useFetchResults?.data).toStrictEqual(data);
134
+ expect(useFetchResults?.status).toBe(200);
135
+ expect(useFetchResults?.error).toBe(null);
136
+ expect(useFetchResults?.extra).toBe(extra);
137
+ expect(useFetchResults?.success).toBe(true);
138
+ });
139
+ });
140
+ it("should hydrate the data for useSubmit hook", async () => {
141
+ let useSubmitResults: UseSubmitReturnType<typeof request> | undefined;
142
+ hydrate();
143
+ const Children = () => {
144
+ useSubmitResults = useSubmit(request);
145
+ return <Fragment />;
146
+ };
147
+ render(
148
+ <App>
149
+ <Children />
150
+ </App>,
151
+ );
152
+ await waitFor(() => {
153
+ expect(useSubmitResults?.data).toStrictEqual(data);
154
+ expect(useSubmitResults?.status).toBe(200);
155
+ expect(useSubmitResults?.error).toBe(null);
156
+ expect(useSubmitResults?.extra).toBe(extra);
157
+ });
158
+ });
159
+ });
160
+ });
161
+ });
@@ -0,0 +1,68 @@
1
+ import { act, render, screen, waitFor } from "@testing-library/react";
2
+
3
+ import { Provider, ProviderValueType, useProvider } from "provider";
4
+
5
+ describe("useProvider [ Base ]", () => {
6
+ const text = "test";
7
+ let values: ProviderValueType | undefined;
8
+
9
+ const Page = () => {
10
+ values = useProvider();
11
+ return <div>{text}</div>;
12
+ };
13
+ const App = () => {
14
+ return (
15
+ <Provider>
16
+ <Page />
17
+ </Provider>
18
+ );
19
+ };
20
+
21
+ beforeEach(() => {
22
+ values = undefined;
23
+ });
24
+
25
+ describe("given app is wrapped with Provider", () => {
26
+ describe("when configuration values are passed to the provider", () => {
27
+ it("should receive configuration with hook", async () => {
28
+ render(<App />);
29
+ await waitFor(() => {
30
+ expect(values).toBeDefined();
31
+ });
32
+ });
33
+ it("should allow to modify received values", async () => {
34
+ const customConfig = { useFetchConfig: { bounceTime: 99999 } };
35
+ render(<App />);
36
+
37
+ act(() => {
38
+ values?.setConfig(customConfig);
39
+ });
40
+
41
+ await waitFor(() => {
42
+ expect(values?.config).toStrictEqual(customConfig);
43
+ });
44
+ });
45
+ });
46
+ });
47
+
48
+ describe("given app is not wrapped with provider", () => {
49
+ describe("when app gets rendered", () => {
50
+ it("should allow to render", async () => {
51
+ render(<Page />);
52
+ expect(screen.getByText(text)).toBeInTheDocument();
53
+ });
54
+
55
+ it("should have default setConfig that does nothing", async () => {
56
+ render(<Page />);
57
+
58
+ act(() => {
59
+ values?.setConfig({ useCacheConfig: { dependencyTracking: false } });
60
+ });
61
+
62
+ await waitFor(() => {
63
+ expect(values?.config).toStrictEqual({});
64
+ });
65
+ });
66
+ });
67
+ });
68
+ });
@@ -0,0 +1,85 @@
1
+ import { act } from "@testing-library/react";
2
+ import { createHttpMockingServer } from "@hyper-fetch/testing";
3
+
4
+ import { client, renderUseAppManager } from "../../utils";
5
+
6
+ const { resetMocks, startServer, stopServer } = createHttpMockingServer();
7
+
8
+ describe("useAppManager [ Base ]", () => {
9
+ beforeAll(() => {
10
+ startServer();
11
+ });
12
+
13
+ afterEach(() => {
14
+ resetMocks();
15
+ });
16
+
17
+ afterAll(() => {
18
+ stopServer();
19
+ });
20
+
21
+ beforeEach(() => {
22
+ jest.resetModules();
23
+ client.clear();
24
+ });
25
+
26
+ describe("given app is initializing", () => {
27
+ describe("when hook gets rendered", () => {
28
+ it("should initialize in online mode", async () => {
29
+ const { result } = renderUseAppManager(client);
30
+ expect(result.current.isOnline).toBeTrue();
31
+ });
32
+
33
+ it("should initialize in focused mode", async () => {
34
+ const { result } = renderUseAppManager(client);
35
+ expect(result.current.isFocused).toBeTrue();
36
+ });
37
+ });
38
+ });
39
+ describe("given app online status change", () => {
40
+ describe("when turning offline", () => {
41
+ it("should change isOnline state to false", async () => {
42
+ const { result } = renderUseAppManager(client);
43
+
44
+ act(() => {
45
+ result.current.setOnline(false);
46
+ });
47
+ expect(result.current.isOnline).toBeFalse();
48
+ });
49
+ });
50
+ describe("when turning online", () => {
51
+ it("should change isOnline state to true", async () => {
52
+ const { result } = renderUseAppManager(client);
53
+
54
+ act(() => {
55
+ result.current.setOnline(false);
56
+ result.current.setOnline(true);
57
+ });
58
+ expect(result.current.isOnline).toBeTrue();
59
+ });
60
+ });
61
+ });
62
+ describe("given app focus status change", () => {
63
+ describe("when turning blur", () => {
64
+ it("should change isFocused state to false", async () => {
65
+ const { result } = renderUseAppManager(client);
66
+
67
+ act(() => {
68
+ result.current.setFocused(false);
69
+ });
70
+ expect(result.current.isFocused).toBeFalse();
71
+ });
72
+ });
73
+ describe("when turning focused", () => {
74
+ it("should change isFocused state to true", async () => {
75
+ const { result } = renderUseAppManager(client);
76
+
77
+ act(() => {
78
+ result.current.setFocused(false);
79
+ result.current.setFocused(true);
80
+ });
81
+ expect(result.current.isFocused).toBeTrue();
82
+ });
83
+ });
84
+ });
85
+ });
@@ -0,0 +1,133 @@
1
+ import { act } from "@testing-library/react";
2
+ import { CacheValueType } from "@hyper-fetch/core";
3
+ import { createHttpMockingServer } from "@hyper-fetch/testing";
4
+
5
+ import { client, createRequest } from "../../utils";
6
+ import { renderUseCache } from "../../utils/use-cache.utils";
7
+ import { testInitialState, testSuccessState } from "../../shared";
8
+
9
+ const { resetMocks, startServer, stopServer } = createHttpMockingServer();
10
+
11
+ describe("useCache [ Base ]", () => {
12
+ let request = createRequest();
13
+ const cacheData: CacheValueType = {
14
+ data: { test: 1 },
15
+ error: null,
16
+ status: 200,
17
+ success: true,
18
+ extra: {
19
+ headers: {
20
+ "content-type": "application/json",
21
+ },
22
+ },
23
+ retries: 0,
24
+ requestTimestamp: +new Date(),
25
+ responseTimestamp: +new Date(),
26
+ addedTimestamp: +new Date(),
27
+ triggerTimestamp: +new Date(),
28
+ cacheKey: request.cacheKey,
29
+ isCanceled: false,
30
+ isOffline: false,
31
+ staleTime: request.staleTime,
32
+ version: request.client.cache.version,
33
+ cacheTime: Infinity,
34
+ };
35
+
36
+ beforeAll(() => {
37
+ startServer();
38
+ });
39
+
40
+ afterEach(() => {
41
+ resetMocks();
42
+ });
43
+
44
+ afterAll(() => {
45
+ stopServer();
46
+ });
47
+
48
+ beforeEach(() => {
49
+ jest.resetModules();
50
+ client.clear();
51
+ request = createRequest();
52
+ });
53
+
54
+ describe("given hook is mounting", () => {
55
+ describe("when cache data read is pending", () => {
56
+ it("should initialize with non-loading state", async () => {
57
+ const { result } = renderUseCache(request);
58
+
59
+ expect(result.current.loading).toBeFalse();
60
+ });
61
+ });
62
+ });
63
+ describe("given cache is empty", () => {
64
+ describe("when reading the state", () => {
65
+ it("should return empty state", async () => {
66
+ const response = renderUseCache(request);
67
+
68
+ testInitialState(response, false);
69
+ });
70
+ });
71
+ });
72
+ describe("given cache is present", () => {
73
+ describe("when reading the state", () => {
74
+ it("should return state", async () => {
75
+ client.cache.set(request, cacheData);
76
+ const response = renderUseCache(request);
77
+
78
+ await testSuccessState(cacheData.data, response);
79
+ expect(+(response.result.current?.responseTimestamp || 0)).toBe(cacheData.responseTimestamp);
80
+ expect(+(response.result.current?.requestTimestamp || 0)).toBe(cacheData.requestTimestamp);
81
+ expect(response.result.current.retries).toBe(0);
82
+ });
83
+ it("should allow to invalidate by Request", async () => {
84
+ const spy = jest.spyOn(client.cache, "invalidate");
85
+
86
+ const { result } = renderUseCache(request);
87
+
88
+ act(() => {
89
+ result.current.invalidate(request);
90
+ });
91
+
92
+ expect(spy).toHaveBeenCalledTimes(1);
93
+ expect(spy).toHaveBeenCalledWith(request);
94
+ });
95
+ it("should allow to invalidate by RegExp", async () => {
96
+ const spy = jest.spyOn(client.cache, "invalidate");
97
+
98
+ const { result } = renderUseCache(request);
99
+
100
+ act(() => {
101
+ result.current.invalidate(new RegExp(request.cacheKey));
102
+ });
103
+
104
+ expect(spy).toHaveBeenCalledTimes(1);
105
+ expect(spy).toHaveBeenCalledWith(new RegExp(request.cacheKey));
106
+ });
107
+ it("should allow to invalidate by cacheKey", async () => {
108
+ const spy = jest.spyOn(client.cache, "invalidate");
109
+
110
+ const { result } = renderUseCache(request);
111
+
112
+ act(() => {
113
+ result.current.invalidate(request.cacheKey);
114
+ });
115
+
116
+ expect(spy).toHaveBeenCalledTimes(1);
117
+ expect(spy).toHaveBeenCalledWith(request.cacheKey);
118
+ });
119
+ it("should allow to invalidate by default key", async () => {
120
+ const spy = jest.spyOn(client.cache, "invalidate");
121
+
122
+ const { result } = renderUseCache(request);
123
+
124
+ act(() => {
125
+ result.current.invalidate();
126
+ });
127
+
128
+ expect(spy).toHaveBeenCalledTimes(1);
129
+ expect(spy).toHaveBeenCalledWith(request.cacheKey);
130
+ });
131
+ });
132
+ });
133
+ });