@hyper-fetch/core 7.5.2 → 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 (54) hide show
  1. package/__tests__/features/adapter/adapter.base.spec.ts +387 -0
  2. package/__tests__/features/adapter/adapter.bindings.spec.ts +1023 -0
  3. package/__tests__/features/adapter/adapter.browser.spec.ts +103 -0
  4. package/__tests__/features/adapter/adapter.server.spec.ts +177 -0
  5. package/__tests__/features/adapter/adapter.utils.spec.ts +165 -0
  6. package/__tests__/features/cache/cache.base.spec.ts +358 -0
  7. package/__tests__/features/cache/cache.events.spec.ts +215 -0
  8. package/__tests__/features/cache/cache.garbage-collector.spec.ts +172 -0
  9. package/__tests__/features/cache/cache.lazy-storage.spec.ts +129 -0
  10. package/__tests__/features/cache/cache.utils.spec.ts +119 -0
  11. package/__tests__/features/client/client.auth.spec.ts +125 -0
  12. package/__tests__/features/client/client.base.spec.ts +149 -0
  13. package/__tests__/features/client/client.hydration.spec.ts +171 -0
  14. package/__tests__/features/client/client.interceptor.spec.ts +225 -0
  15. package/__tests__/features/client/client.methods.spec.ts +188 -0
  16. package/__tests__/features/client/client.middleware.spec.ts +118 -0
  17. package/__tests__/features/client/client.utils.spec.ts +292 -0
  18. package/__tests__/features/dispatcher/dispatcher.base.spec.ts +123 -0
  19. package/__tests__/features/dispatcher/dispatcher.events.spec.ts +304 -0
  20. package/__tests__/features/dispatcher/dispatcher.queue.spec.ts +462 -0
  21. package/__tests__/features/dispatcher/dispatcher.requests.spec.ts +241 -0
  22. package/__tests__/features/dispatcher/dispatcher.utils.spec.ts +125 -0
  23. package/__tests__/features/managers/app/app.manager.base.spec.ts +235 -0
  24. package/__tests__/features/managers/app/app.manager.ssr.spec.ts +40 -0
  25. package/__tests__/features/managers/logger/logger.base.spec.ts +107 -0
  26. package/__tests__/features/managers/logger/logger.utils.spec.ts +100 -0
  27. package/__tests__/features/managers/request/request.manager.base.spec.ts +222 -0
  28. package/__tests__/features/managers/request/request.manager.events.spec.ts +280 -0
  29. package/__tests__/features/mocker/mocker.base.spec.ts +567 -0
  30. package/__tests__/features/plugin/plugin.base.spec.ts +255 -0
  31. package/__tests__/features/request/request.base.spec.ts +199 -0
  32. package/__tests__/features/request/request.cloning.spec.ts +47 -0
  33. package/__tests__/features/request/request.sending.spec.ts +272 -0
  34. package/__tests__/features/request/request.setters.spec.ts +157 -0
  35. package/__tests__/features/request/request.typescript.spec.ts +423 -0
  36. package/__tests__/features/request/request.utils.spec.ts +77 -0
  37. package/__tests__/features/sdk/sdk.base.spec.ts +130 -0
  38. package/__tests__/global.d.ts +1 -0
  39. package/__tests__/jest.setup.ts +13 -0
  40. package/__tests__/shared/callbacks.shared.ts +9 -0
  41. package/__tests__/shared/index.ts +2 -0
  42. package/__tests__/shared/progress.shared.ts +35 -0
  43. package/__tests__/tsconfig.json +13 -0
  44. package/__tests__/utils/adapter.utils.ts +38 -0
  45. package/__tests__/utils/cache.utils.ts +19 -0
  46. package/__tests__/utils/callbacks.utils.ts +47 -0
  47. package/__tests__/utils/dispatcher.utils.ts +6 -0
  48. package/__tests__/utils/index.ts +4 -0
  49. package/dist/index.js +12 -1
  50. package/dist/index.js.map +2 -2
  51. package/jest.config.ts +6 -0
  52. package/package.json +1 -1
  53. package/project.json +4 -0
  54. package/src/http-adapter/http-adapter.server.ts +15 -1
@@ -0,0 +1,125 @@
1
+ import { createHttpMockingServer } from "@hyper-fetch/testing";
2
+
3
+ import {
4
+ canRetryRequest,
5
+ QueueItemType,
6
+ DispatcherMode,
7
+ getDispatcherChangeByKey,
8
+ getDispatcherDrainedByKey,
9
+ getDispatcherStatusByKey,
10
+ getIsEqualTimestamp,
11
+ getRequestType,
12
+ } from "dispatcher";
13
+ import { createDispatcher, createAdapter } from "../../utils";
14
+ import { Client } from "client";
15
+
16
+ const { resetMocks, startServer, stopServer, mockRequest } = createHttpMockingServer();
17
+
18
+ describe("Dispatcher [ Utils ]", () => {
19
+ const adapterSpy = jest.fn();
20
+
21
+ let adapter = createAdapter({ callback: adapterSpy });
22
+ let client = new Client({ url: "shared-base-url" }).setAdapter(adapter);
23
+ let dispatcher = createDispatcher(client);
24
+
25
+ beforeAll(() => {
26
+ startServer();
27
+ });
28
+
29
+ beforeEach(() => {
30
+ adapter = createAdapter({ callback: adapterSpy });
31
+ client = new Client({ url: "shared-base-url" }).setAdapter(adapter);
32
+ dispatcher = createDispatcher(client);
33
+ resetMocks();
34
+ jest.resetAllMocks();
35
+ });
36
+
37
+ afterAll(() => {
38
+ stopServer();
39
+ });
40
+
41
+ describe("When using request counting methods", () => {
42
+ it("should increment request count", async () => {
43
+ const queryKey = "test";
44
+ expect(dispatcher.getQueueRequestCount(queryKey)).toBe(0);
45
+ dispatcher.incrementQueueRequestCount(queryKey);
46
+ expect(dispatcher.getQueueRequestCount(queryKey)).toBe(1);
47
+ });
48
+ });
49
+ describe("When using getRequest method", () => {
50
+ it("should give stored request", async () => {
51
+ const request = client.createRequest()({ endpoint: "shared-base-endpoint" });
52
+ mockRequest(request);
53
+
54
+ const requestId = dispatcher.add(request);
55
+ const storedRequest = dispatcher.getRequest(request.queryKey, requestId);
56
+ expect(storedRequest).toBeDefined();
57
+ });
58
+ it("should not return request from empty store", async () => {
59
+ const request = client.createRequest()({ endpoint: "shared-base-endpoint" });
60
+ mockRequest(request);
61
+
62
+ const storedRequest = dispatcher.getRequest(request.queryKey, "test");
63
+ expect(storedRequest).not.toBeDefined();
64
+ });
65
+ });
66
+ describe("When using clear methods", () => {
67
+ it("should clear request from queue", async () => {
68
+ const request = client.createRequest()({ endpoint: "shared-base-endpoint" });
69
+ mockRequest(request);
70
+
71
+ dispatcher.stop(request.queryKey);
72
+ dispatcher.add(request);
73
+
74
+ expect(dispatcher.getQueue(request.queryKey).requests).toHaveLength(1);
75
+
76
+ dispatcher.clearQueue(request.queryKey);
77
+
78
+ expect(dispatcher.getQueue(request.queryKey).requests).toHaveLength(0);
79
+ });
80
+ });
81
+ describe("When using getIsEqualTimestamp util", () => {
82
+ it("should return true when timestamps are equal", async () => {
83
+ expect(getIsEqualTimestamp(+new Date(), 0, +new Date())).toBeTrue();
84
+ expect(getIsEqualTimestamp(+new Date(), 10, +new Date() + 5)).toBeTrue();
85
+ });
86
+ it("should return false when timestamps aren't equal", async () => {
87
+ expect(getIsEqualTimestamp(+new Date(), 0, +new Date() + 5)).toBeFalse();
88
+ expect(getIsEqualTimestamp(+new Date(), 10, +new Date() + 15)).toBeFalse();
89
+ expect(getIsEqualTimestamp(+new Date(), 10)).toBeFalse();
90
+ });
91
+ });
92
+ describe("When using canRetryRequest util", () => {
93
+ it("should return true if retry is possible", async () => {
94
+ expect(canRetryRequest(0, 1)).toBeTrue();
95
+ expect(canRetryRequest(0, 2)).toBeTrue();
96
+ expect(canRetryRequest(5, 10)).toBeTrue();
97
+ });
98
+ it("should return false on success statuses", async () => {
99
+ expect(canRetryRequest(1, 0)).toBeFalse();
100
+ expect(canRetryRequest(2, 1)).toBeFalse();
101
+ expect(canRetryRequest(5, 5)).toBeFalse();
102
+ });
103
+ });
104
+ describe("When using event get key utils", () => {
105
+ it("should return true if retry is possible", async () => {
106
+ expect(getDispatcherDrainedByKey("test")).toBe(`test-drained-event`);
107
+ expect(getDispatcherStatusByKey("test")).toBe(`test-status-event`);
108
+ expect(getDispatcherChangeByKey("test")).toBe(`test-change-event`);
109
+ });
110
+ });
111
+ describe("When using getRequestType util", () => {
112
+ it("should return deduplicated type", async () => {
113
+ const request = client.createRequest()({ endpoint: "shared-base-endpoint", deduplicate: true });
114
+ const duplicated: QueueItemType<typeof request> = dispatcher.createStorageItem(request);
115
+ const type = getRequestType(request, duplicated);
116
+ expect(type).toBe(DispatcherMode.DEDUPLICATED);
117
+ });
118
+ it("should return cancelable type", async () => {
119
+ const request = client.createRequest()({ endpoint: "shared-base-endpoint", cancelable: true });
120
+ const duplicated: QueueItemType<typeof request> = dispatcher.createStorageItem(request);
121
+ const type = getRequestType(request, duplicated);
122
+ expect(type).toBe(DispatcherMode.PREVIOUS_CANCELED);
123
+ });
124
+ });
125
+ });
@@ -0,0 +1,235 @@
1
+ import { waitFor } from "@testing-library/dom";
2
+ import { createHttpMockingServer } from "@hyper-fetch/testing";
3
+
4
+ import { AppManager, appManagerInitialOptions, hasDocument, hasWindow } from "managers";
5
+ import { Client } from "client";
6
+
7
+ const { resetMocks, startServer, stopServer } = createHttpMockingServer();
8
+
9
+ describe("AppManager [ Base ]", () => {
10
+ let client = new Client({ url: "shared-base-url" });
11
+
12
+ beforeAll(() => {
13
+ startServer();
14
+ });
15
+
16
+ beforeEach(() => {
17
+ resetMocks();
18
+ jest.resetAllMocks();
19
+ client = new Client({ url: "shared-base-url" });
20
+ });
21
+
22
+ afterAll(() => {
23
+ stopServer();
24
+ });
25
+
26
+ describe("When app manager is initialized", () => {
27
+ it("should initialize with isFocused set to true", async () => {
28
+ expect(client.appManager.isFocused).toBeTrue();
29
+ });
30
+ it("should initialize with isOnline set to true", async () => {
31
+ expect(client.appManager.isOnline).toBeTrue();
32
+ });
33
+ it("should initialize with custom isFocused", async () => {
34
+ const firstManager = new AppManager({ initiallyFocused: false });
35
+ const secondManager = new AppManager({ initiallyFocused: async () => false });
36
+ const thirdManager = new AppManager({ initiallyFocused: undefined });
37
+
38
+ await waitFor(() => {
39
+ expect(firstManager.isFocused).toBeFalse();
40
+ expect(secondManager.isFocused).toBeFalse();
41
+ expect(thirdManager.isFocused).toBeTrue();
42
+ });
43
+ });
44
+ it("should initialize with custom isOnline", async () => {
45
+ const firstManager = new AppManager({ initiallyOnline: false });
46
+ const secondManager = new AppManager({ initiallyOnline: async () => false });
47
+ const thirdManager = new AppManager({ initiallyOnline: undefined });
48
+
49
+ await waitFor(() => {
50
+ expect(firstManager.isOnline).toBeFalse();
51
+ expect(secondManager.isOnline).toBeFalse();
52
+ expect(thirdManager.isOnline).toBeTrue();
53
+ });
54
+ });
55
+ });
56
+ describe("When using app manager methods", () => {
57
+ it("should allow to change app focus state", async () => {
58
+ const focusSpy = jest.fn();
59
+ const blurSpy = jest.fn();
60
+ client.appManager.events.onFocus(focusSpy);
61
+ client.appManager.events.onBlur(blurSpy);
62
+ client.appManager.setFocused(false);
63
+ expect(client.appManager.isFocused).toBeFalse();
64
+ client.appManager.setFocused(true);
65
+ expect(client.appManager.isFocused).toBeTrue();
66
+
67
+ expect(focusSpy).toHaveBeenCalledTimes(1);
68
+ expect(blurSpy).toHaveBeenCalledTimes(1);
69
+ });
70
+ it("should allow to change app online state", async () => {
71
+ const onlineSpy = jest.fn();
72
+ const offlineSpy = jest.fn();
73
+ client.appManager.events.onOnline(onlineSpy);
74
+ client.appManager.events.onOffline(offlineSpy);
75
+ client.appManager.setOnline(false);
76
+ expect(client.appManager.isOnline).toBeFalse();
77
+ client.appManager.setOnline(true);
78
+ expect(client.appManager.isOnline).toBeTrue();
79
+
80
+ expect(onlineSpy).toHaveBeenCalledTimes(1);
81
+ expect(offlineSpy).toHaveBeenCalledTimes(1);
82
+ });
83
+ it("should properly cleanup event listeners when unmount functions are called", async () => {
84
+ const focusSpy = jest.fn();
85
+ const blurSpy = jest.fn();
86
+ const onlineSpy = jest.fn();
87
+ const offlineSpy = jest.fn();
88
+
89
+ // Setup listeners and store cleanup functions
90
+ const cleanupFocus = client.appManager.events.onFocus(focusSpy);
91
+ const cleanupBlur = client.appManager.events.onBlur(blurSpy);
92
+ const cleanupOnline = client.appManager.events.onOnline(onlineSpy);
93
+ const cleanupOffline = client.appManager.events.onOffline(offlineSpy);
94
+
95
+ // Trigger events once - all should fire
96
+ client.appManager.setFocused(false);
97
+ client.appManager.setFocused(true);
98
+ client.appManager.setOnline(false);
99
+ client.appManager.setOnline(true);
100
+
101
+ expect(focusSpy).toHaveBeenCalledTimes(1);
102
+ expect(blurSpy).toHaveBeenCalledTimes(1);
103
+ expect(onlineSpy).toHaveBeenCalledTimes(1);
104
+ expect(offlineSpy).toHaveBeenCalledTimes(1);
105
+
106
+ // Clean up all listeners
107
+ cleanupFocus();
108
+ cleanupBlur();
109
+ cleanupOnline();
110
+ cleanupOffline();
111
+
112
+ // Trigger events again - none should fire
113
+ client.appManager.setFocused(false);
114
+ client.appManager.setFocused(true);
115
+ client.appManager.setOnline(false);
116
+ client.appManager.setOnline(true);
117
+
118
+ // Counts should remain the same
119
+ expect(focusSpy).toHaveBeenCalledTimes(1);
120
+ expect(blurSpy).toHaveBeenCalledTimes(1);
121
+ expect(onlineSpy).toHaveBeenCalledTimes(1);
122
+ expect(offlineSpy).toHaveBeenCalledTimes(1);
123
+ });
124
+ });
125
+ describe("When using app manager utils", () => {
126
+ it("should allow to detect window", async () => {
127
+ expect(hasWindow()).toBeTrue();
128
+ });
129
+ it("should allow to detect document", async () => {
130
+ expect(hasDocument()).toBeTrue();
131
+ });
132
+ });
133
+ describe("When app manager is initialized with custom events", () => {
134
+ it("should use custom focus event handler", async () => {
135
+ const focusSpy = jest.fn();
136
+ const customFocusHandler = (setFocused: (isFocused: boolean) => void) => {
137
+ // Custom focus handler implementation
138
+ setFocused(true);
139
+ };
140
+
141
+ const manager = new AppManager({ focusEvent: customFocusHandler });
142
+ manager.events.onFocus(focusSpy);
143
+
144
+ // Trigger the custom focus handler
145
+ customFocusHandler(manager.setFocused);
146
+ expect(focusSpy).toHaveBeenCalledTimes(1);
147
+ });
148
+
149
+ it("should use custom online event handler", async () => {
150
+ const onlineSpy = jest.fn();
151
+ const customOnlineHandler = (setOnline: (isOnline: boolean) => void) => {
152
+ // Custom online handler implementation
153
+ setOnline(true);
154
+ };
155
+
156
+ const manager = new AppManager({ onlineEvent: customOnlineHandler });
157
+ manager.events.onOnline(onlineSpy);
158
+
159
+ // Trigger the custom online handler
160
+ customOnlineHandler(manager.setOnline);
161
+ expect(onlineSpy).toHaveBeenCalledTimes(1);
162
+ });
163
+
164
+ it("should handle state changes with custom handlers", async () => {
165
+ const focusSpy = jest.fn();
166
+ const blurSpy = jest.fn();
167
+
168
+ const customFocusHandler = (setFocused: (isFocused: boolean) => void) => {
169
+ // Test both focus and blur
170
+ setFocused(false);
171
+ setFocused(true);
172
+ };
173
+
174
+ const manager = new AppManager({ focusEvent: customFocusHandler });
175
+ manager.events.onFocus(focusSpy);
176
+ manager.events.onBlur(blurSpy);
177
+
178
+ // Trigger the custom focus handler
179
+ customFocusHandler(manager.setFocused);
180
+
181
+ expect(blurSpy).toHaveBeenCalledTimes(1);
182
+ expect(focusSpy).toHaveBeenCalledTimes(1);
183
+ expect(manager.isFocused).toBeTrue();
184
+ });
185
+ });
186
+
187
+ describe("When using app manager initial options", () => {
188
+ it("should use default event handlers when not specified", async () => {
189
+ const focusSpy = jest.fn();
190
+ const onlineSpy = jest.fn();
191
+
192
+ jest.spyOn(appManagerInitialOptions, "focusEvent").mockImplementation(focusSpy);
193
+ jest.spyOn(appManagerInitialOptions, "onlineEvent").mockImplementation(onlineSpy);
194
+
195
+ // eslint-disable-next-line no-new
196
+ new AppManager().initialize();
197
+
198
+ window.dispatchEvent(new Event("focus"));
199
+ window.dispatchEvent(new Event("online"));
200
+
201
+ expect(focusSpy).toHaveBeenCalledTimes(1);
202
+ expect(onlineSpy).toHaveBeenCalledTimes(1);
203
+ });
204
+ it("should use one default event handlers when not specified", async () => {
205
+ const focusSpy = jest.fn();
206
+ const onlineSpy = jest.fn();
207
+
208
+ jest.spyOn(appManagerInitialOptions, "onlineEvent").mockImplementation(onlineSpy);
209
+
210
+ // eslint-disable-next-line no-new
211
+ new AppManager({ focusEvent: focusSpy }).initialize();
212
+
213
+ window.dispatchEvent(new Event("focus"));
214
+ window.dispatchEvent(new Event("online"));
215
+
216
+ expect(focusSpy).toHaveBeenCalledTimes(1);
217
+ expect(onlineSpy).toHaveBeenCalledTimes(1);
218
+ });
219
+ it("should use other default event handlers when not specified", async () => {
220
+ const focusSpy = jest.fn();
221
+ const onlineSpy = jest.fn();
222
+
223
+ jest.spyOn(appManagerInitialOptions, "focusEvent").mockImplementation(focusSpy);
224
+
225
+ // eslint-disable-next-line no-new
226
+ new AppManager({ onlineEvent: onlineSpy }).initialize();
227
+
228
+ window.dispatchEvent(new Event("focus"));
229
+ window.dispatchEvent(new Event("online"));
230
+
231
+ expect(focusSpy).toHaveBeenCalledTimes(1);
232
+ expect(onlineSpy).toHaveBeenCalledTimes(1);
233
+ });
234
+ });
235
+ });
@@ -0,0 +1,40 @@
1
+ import { hasDocument, hasWindow } from "managers";
2
+
3
+ describe("AppManager [ SSR ]", () => {
4
+ beforeEach(() => {
5
+ jest.resetAllMocks();
6
+ });
7
+
8
+ describe("Given window is not available from beginning", () => {
9
+ describe("When app manager is initialized", () => {
10
+ it("should not throw without document", async () => {
11
+ jest.spyOn(window, "document", "get").mockImplementation(() => {
12
+ throw new Error();
13
+ });
14
+
15
+ expect(hasDocument).not.toThrow();
16
+ expect(hasDocument()).toBeFalse();
17
+ });
18
+ it("should not throw without window", async () => {
19
+ jest.spyOn(global, "window", "get").mockImplementation(() => {
20
+ throw new Error();
21
+ });
22
+
23
+ expect(hasWindow).not.toThrow();
24
+ expect(hasWindow()).toBeFalse();
25
+ });
26
+ it("should initialize and await for window to attach events", async () => {
27
+ // Todo
28
+ });
29
+ it("should emit events after hydration", async () => {
30
+ // Todo
31
+ });
32
+ it("should await to set initial isOnline value", async () => {
33
+ // Todo
34
+ });
35
+ it("should await to set initial isFocused value", async () => {
36
+ // Todo
37
+ });
38
+ });
39
+ });
40
+ });
@@ -0,0 +1,107 @@
1
+ import { Client } from "client";
2
+ import { LoggerManager } from "managers";
3
+
4
+ describe("Logger [ Base ]", () => {
5
+ let client = new Client({ url: "shared-base-url" }).setDebug(true);
6
+ let loggerManager = new LoggerManager();
7
+ let mockLogger: jest.Mock;
8
+
9
+ beforeEach(() => {
10
+ client = new Client({ url: "shared-base-url" }).setDebug(true);
11
+ loggerManager = new LoggerManager();
12
+ mockLogger = jest.fn();
13
+ loggerManager.logger = mockLogger;
14
+ });
15
+
16
+ describe("When using config methods", () => {
17
+ it("should allow to initialize logger with module name", async () => {
18
+ const logger = loggerManager.initialize(client, "Test Module");
19
+ expect(logger).toBeDefined();
20
+ });
21
+
22
+ it("should allow to set severity level", () => {
23
+ loggerManager.setSeverity("error");
24
+ expect(loggerManager.level).toBe("error");
25
+ });
26
+
27
+ it("should allow to set modules", () => {
28
+ const modules = ["Module1", "Module2"];
29
+ loggerManager.setModules(modules);
30
+ expect(loggerManager.modules).toEqual(modules);
31
+ });
32
+
33
+ it("should allow to clear modules by passing undefined", () => {
34
+ const modules = ["Module1", "Module2"];
35
+ loggerManager.setModules(modules);
36
+ loggerManager.setModules(undefined);
37
+ expect(loggerManager.modules).toBeUndefined();
38
+ });
39
+ });
40
+
41
+ describe("When filtering logs", () => {
42
+ it("should log if message level is higher than manager level", () => {
43
+ loggerManager.setSeverity("info");
44
+ const logData = { level: "error", type: "system", module: "Test", title: "Test", extra: {} } as const;
45
+
46
+ loggerManager.logger(logData);
47
+
48
+ expect(mockLogger).toHaveBeenCalledWith(logData);
49
+ });
50
+
51
+ it("should log if message level equals manager level", () => {
52
+ loggerManager.setSeverity("error");
53
+ const logData = { level: "error", type: "system", module: "Test", title: "Test", extra: {} } as const;
54
+
55
+ loggerManager.logger(logData);
56
+
57
+ expect(mockLogger).toHaveBeenCalledWith(logData);
58
+ });
59
+
60
+ it("should not log if message level is lower than manager level", () => {
61
+ loggerManager.setSeverity("error");
62
+ const logger = loggerManager.initialize(client, "Test Module");
63
+
64
+ logger.info({ type: "system", title: "Test", extra: {} });
65
+
66
+ expect(mockLogger).not.toHaveBeenCalled();
67
+ });
68
+
69
+ it("should not log if module is not in allowed modules", () => {
70
+ loggerManager.setModules(["Module1"]);
71
+ const logger = loggerManager.initialize(client, "Test Module");
72
+
73
+ logger.info({ type: "system", title: "Test", extra: {} });
74
+
75
+ expect(mockLogger).not.toHaveBeenCalled();
76
+ });
77
+
78
+ it("should log if module is in allowed modules", () => {
79
+ loggerManager.setModules(["Module1"]);
80
+ const logger = loggerManager.initialize(client, "Module1");
81
+ const logData = { level: "error", type: "system", module: "Module1", title: "Test", extra: {} } as const;
82
+
83
+ logger.error(logData);
84
+
85
+ expect(mockLogger).toHaveBeenCalledWith(logData);
86
+ });
87
+
88
+ it("should not log if module is in allowed modules", () => {
89
+ loggerManager.setModules(["Module1"]);
90
+ const logger = loggerManager.initialize(client, "Module2");
91
+ const logData = { level: "error", type: "system", module: "Module2", title: "Test", extra: {} } as const;
92
+
93
+ logger.error(logData);
94
+
95
+ expect(mockLogger).not.toHaveBeenCalled();
96
+ });
97
+
98
+ it("should log all modules if no modules filter is set", () => {
99
+ const logger = loggerManager.initialize(client, "Test Module");
100
+ const logData = { level: "error", type: "system", module: "Test Module", title: "Test", extra: {} } as const;
101
+
102
+ logger.error(logData);
103
+
104
+ expect(mockLogger).toHaveBeenCalledWith(logData);
105
+ });
106
+ });
107
+ });
@@ -0,0 +1,100 @@
1
+ import { createClient } from "client";
2
+ import { getTime, logger } from "managers";
3
+
4
+ describe("Logger [ Utils ]", () => {
5
+ const logSpy = jest.spyOn(console, "log");
6
+ const groupCollapsedSpy = jest.spyOn(console, "groupCollapsed");
7
+ const groupEndSpy = jest.spyOn(console, "groupEnd");
8
+
9
+ beforeEach(() => {
10
+ jest.clearAllMocks();
11
+ });
12
+
13
+ describe("Given using logger utils", () => {
14
+ describe("When getTime util gets triggered", () => {
15
+ it("should allow to format data string", () => {
16
+ const time = getTime();
17
+ expect(time).toBeString();
18
+ });
19
+ });
20
+ describe("When logger util gets triggered", () => {
21
+ it("should allow to log message without extra data", () => {
22
+ logger({
23
+ module: "Test",
24
+ type: "system",
25
+ level: "debug",
26
+ title: "Test",
27
+ extra: {},
28
+ });
29
+ expect(logSpy).toHaveBeenCalledTimes(1);
30
+ expect(groupCollapsedSpy).not.toHaveBeenCalled();
31
+ expect(groupEndSpy).not.toHaveBeenCalled();
32
+ });
33
+ it("should allow to log message with extra data", () => {
34
+ const extra = { data: [{ test: 1 }] };
35
+
36
+ logger({
37
+ module: "Test",
38
+ type: "system",
39
+ level: "debug",
40
+ title: "Test",
41
+ extra,
42
+ });
43
+ expect(logSpy).toHaveBeenCalledTimes(1);
44
+ expect(groupCollapsedSpy).toHaveBeenCalledTimes(1);
45
+ expect(groupEndSpy).toHaveBeenCalledTimes(1);
46
+ });
47
+ it("should handle request type logging correctly", () => {
48
+ const extra = {
49
+ request: createClient({ url: "https://test.com" }).createRequest()({ endpoint: "/api/test" }),
50
+ response: { data: "test" } as any,
51
+ requestId: "123",
52
+ };
53
+
54
+ logger({
55
+ module: "Test",
56
+ type: "request",
57
+ level: "debug",
58
+ title: "Test Request",
59
+ extra,
60
+ });
61
+
62
+ expect(logSpy).toHaveBeenCalledTimes(1);
63
+ expect(groupCollapsedSpy).toHaveBeenCalledTimes(1);
64
+ expect(groupEndSpy).toHaveBeenCalledTimes(1);
65
+ });
66
+ it("should handle response type logging correctly", () => {
67
+ const extra = {
68
+ request: createClient({ url: "https://test.com" }).createRequest()({ endpoint: "/api/test" }),
69
+ response: { data: "test" } as any,
70
+ requestId: "123",
71
+ };
72
+
73
+ logger({
74
+ module: "Test",
75
+ type: "response",
76
+ level: "debug",
77
+ title: "Test Response",
78
+ extra,
79
+ });
80
+
81
+ expect(logSpy).toHaveBeenCalledTimes(1);
82
+ expect(groupCollapsedSpy).toHaveBeenCalledTimes(1);
83
+ expect(groupEndSpy).toHaveBeenCalledTimes(1);
84
+ });
85
+ it("should handle no extra data", () => {
86
+ logger({
87
+ module: "Test",
88
+ type: "system",
89
+ level: "debug",
90
+ title: "Test Response",
91
+ extra: undefined as any,
92
+ });
93
+
94
+ expect(logSpy).toHaveBeenCalledTimes(1);
95
+ expect(groupCollapsedSpy).toHaveBeenCalledTimes(0);
96
+ expect(groupEndSpy).toHaveBeenCalledTimes(0);
97
+ });
98
+ });
99
+ });
100
+ });