@applicaster/zapp-react-native-utils 15.0.0-alpha.8680244503 → 15.0.0-alpha.9102777840

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 (113) hide show
  1. package/README.md +0 -6
  2. package/adsUtils/__tests__/createVMAP.test.ts +419 -0
  3. package/analyticsUtils/analyticsMapper.ts +10 -2
  4. package/appDataUtils/__tests__/urlScheme.test.ts +678 -0
  5. package/appUtils/HooksManager/__tests__/__snapshots__/hooksManager.test.js.snap +0 -188
  6. package/appUtils/HooksManager/__tests__/hooksManager.test.js +16 -2
  7. package/appUtils/RiverFocusManager/{index.js → index.ts} +25 -18
  8. package/appUtils/accessibilityManager/__tests__/utils.test.ts +360 -0
  9. package/appUtils/accessibilityManager/utils.ts +25 -5
  10. package/appUtils/contextKeysManager/__tests__/getKeys/failure.test.ts +7 -2
  11. package/appUtils/contextKeysManager/__tests__/getKeys/success.test.ts +48 -0
  12. package/appUtils/contextKeysManager/contextResolver.ts +51 -22
  13. package/appUtils/contextKeysManager/index.ts +65 -10
  14. package/appUtils/focusManager/__tests__/__snapshots__/focusManager.test.js.snap +3 -0
  15. package/appUtils/focusManager/index.ios.ts +43 -4
  16. package/appUtils/focusManager/treeDataStructure/Tree/__tests__/Tree.test.js +46 -0
  17. package/appUtils/focusManager/treeDataStructure/Tree/index.js +18 -18
  18. package/appUtils/focusManagerAux/utils/index.ios.ts +122 -0
  19. package/appUtils/focusManagerAux/utils/index.ts +3 -5
  20. package/appUtils/focusManagerAux/utils/utils.ios.ts +202 -3
  21. package/appUtils/playerManager/playerNative.ts +2 -1
  22. package/cloudEventsUtils/__tests__/index.test.ts +529 -0
  23. package/cloudEventsUtils/index.ts +65 -1
  24. package/configurationUtils/__tests__/imageSrcFromMediaItem.test.ts +38 -0
  25. package/configurationUtils/index.ts +17 -11
  26. package/dateUtils/__tests__/dayjs.test.ts +330 -0
  27. package/enumUtils/__tests__/getEnumKeyByEnumValue.test.ts +207 -0
  28. package/errorUtils/__tests__/GeneralError.test.ts +97 -0
  29. package/errorUtils/__tests__/HttpStatusCode.test.ts +344 -0
  30. package/errorUtils/__tests__/MissingPluginError.test.ts +113 -0
  31. package/errorUtils/__tests__/NetworkError.test.ts +202 -0
  32. package/errorUtils/__tests__/getParsedResponse.test.ts +188 -0
  33. package/errorUtils/__tests__/invariant.test.ts +112 -0
  34. package/focusManager/aux/index.ts +1 -1
  35. package/headersUtils/__tests__/headersUtils.test.js +11 -1
  36. package/headersUtils/index.ts +2 -1
  37. package/manifestUtils/defaultManifestConfigurations/player.js +40 -10
  38. package/manifestUtils/platformIsTV.js +13 -0
  39. package/navigationUtils/index.ts +15 -5
  40. package/numberUtils/__tests__/toNumber.test.ts +27 -0
  41. package/numberUtils/__tests__/toPositiveNumber.test.ts +193 -0
  42. package/numberUtils/index.ts +23 -1
  43. package/package.json +4 -4
  44. package/reactHooks/analytics/__tests__/useSendAnalyticsOnPress.test.ts +537 -0
  45. package/reactHooks/app/__tests__/useAppState.test.ts +1 -1
  46. package/reactHooks/autoscrolling/__tests__/useTrackCurrentAutoScrollingElement.test.ts +1 -1
  47. package/reactHooks/autoscrolling/__tests__/useTrackedView.test.tsx +1 -2
  48. package/reactHooks/cell-click/__tests__/index.test.js +1 -3
  49. package/reactHooks/configuration/__tests__/index.test.tsx +1 -1
  50. package/reactHooks/connection/__tests__/index.test.js +1 -1
  51. package/reactHooks/dev/__tests__/useReRenderLog.test.ts +188 -0
  52. package/reactHooks/device/useIsTablet.tsx +14 -19
  53. package/reactHooks/events/index.ts +20 -0
  54. package/reactHooks/feed/__tests__/useBatchLoading.test.tsx +32 -23
  55. package/reactHooks/feed/__tests__/useBuildPipesUrl.test.tsx +19 -19
  56. package/reactHooks/feed/__tests__/useEntryScreenId.test.tsx +1 -1
  57. package/reactHooks/feed/__tests__/useFeedLoader.test.tsx +42 -30
  58. package/reactHooks/feed/__tests__/{useInflatedUrl.test.ts → useInflatedUrl.test.tsx} +62 -7
  59. package/reactHooks/feed/index.ts +0 -2
  60. package/reactHooks/feed/useInflatedUrl.ts +43 -17
  61. package/reactHooks/hookModal/hooks/useHookModalScreenData.ts +12 -8
  62. package/reactHooks/index.ts +2 -0
  63. package/reactHooks/layout/__tests__/index.test.tsx +1 -1
  64. package/reactHooks/layout/__tests__/useLayoutVersion.test.tsx +1 -1
  65. package/reactHooks/navigation/__tests__/index.test.tsx +40 -9
  66. package/reactHooks/navigation/index.ts +19 -4
  67. package/reactHooks/navigation/useRoute.ts +3 -1
  68. package/reactHooks/player/__tests__/useAutoSeek._test.tsx +1 -1
  69. package/reactHooks/player/__tests__/useTapSeek._test.ts +1 -1
  70. package/reactHooks/resolvers/__tests__/useCellResolver.test.tsx +1 -1
  71. package/reactHooks/resolvers/__tests__/useComponentResolver.test.tsx +1 -1
  72. package/reactHooks/screen/__tests__/useCurrentScreenData.test.tsx +2 -2
  73. package/reactHooks/screen/__tests__/useScreenBackgroundColor.test.tsx +1 -1
  74. package/reactHooks/screen/__tests__/useScreenData.test.tsx +1 -1
  75. package/reactHooks/screen/__tests__/useTargetScreenData.test.tsx +2 -2
  76. package/reactHooks/state/__tests__/useComponentScreenState.test.ts +246 -0
  77. package/reactHooks/state/index.ts +2 -0
  78. package/reactHooks/state/useComponentScreenState.ts +45 -0
  79. package/reactHooks/state/useRefWithInitialValue.ts +10 -0
  80. package/reactHooks/ui/__tests__/useFadeOutWhenBlurred.test.ts +580 -0
  81. package/reactHooks/utils/__tests__/index.test.js +1 -1
  82. package/rectUtils/__tests__/index.test.ts +549 -0
  83. package/rectUtils/index.ts +2 -2
  84. package/refreshUtils/RefreshCoordinator/__tests__/refreshCoordinator.test.ts +161 -0
  85. package/refreshUtils/RefreshCoordinator/index.ts +216 -0
  86. package/refreshUtils/RefreshCoordinator/utils/__tests__/getDataRefreshConfig.test.ts +104 -0
  87. package/refreshUtils/RefreshCoordinator/utils/index.ts +29 -0
  88. package/screenPickerUtils/__tests__/index.test.ts +333 -0
  89. package/screenPickerUtils/index.ts +5 -0
  90. package/screenState/__tests__/index.test.ts +1 -1
  91. package/screenUtils/index.ts +3 -0
  92. package/searchUtils/const.ts +7 -0
  93. package/searchUtils/index.ts +3 -0
  94. package/services/storageServiceSync.web.ts +1 -1
  95. package/stringUtils/index.ts +1 -1
  96. package/testUtils/index.tsx +1 -1
  97. package/time/__tests__/BackgroundTimer.test.ts +156 -0
  98. package/time/__tests__/Timer.test.ts +236 -0
  99. package/typeGuards/__tests__/isString.test.ts +21 -0
  100. package/typeGuards/index.ts +4 -0
  101. package/utils/__tests__/clone.test.ts +158 -0
  102. package/utils/__tests__/mergeRight.test.ts +48 -0
  103. package/utils/__tests__/path.test.ts +7 -0
  104. package/utils/clone.ts +7 -0
  105. package/utils/index.ts +12 -1
  106. package/utils/mergeRight.ts +5 -0
  107. package/utils/path.ts +6 -3
  108. package/utils/pathOr.ts +5 -1
  109. package/zappFrameworkUtils/HookCallback/callbackNavigationAction.ts +19 -5
  110. package/zappFrameworkUtils/HookCallback/hookCallbackManifestExtensions.config.js +1 -1
  111. package/reactHooks/componentsMap/index.ts +0 -55
  112. package/reactHooks/feed/__tests__/useFeedRefresh.test.tsx +0 -75
  113. package/reactHooks/feed/useFeedRefresh.tsx +0 -65
@@ -155,9 +155,9 @@ export function getMediaItems(entry: ZappEntry): Option<ZappMediaItem[]> {
155
155
 
156
156
  /**
157
157
  * Retrieves the "src" value from a media item in the entry's media group,
158
- * based on a provided key, with fallback logic.
158
+ * based on a provided key, with optional fallback logic.
159
159
  *
160
- * Fallback order:
160
+ * Fallback order (when enabled):
161
161
  * 1. Attempts to find a media item with the specified key (or "image_base" if none provided).
162
162
  * 2. If not found, attempts to find a media item with the key "image_base".
163
163
  * 3. If still not found, falls back to the first available media item.
@@ -166,15 +166,19 @@ export function getMediaItems(entry: ZappEntry): Option<ZappMediaItem[]> {
166
166
  * since empty URIs are invalid in some platforms (e.g., React Native).
167
167
  *
168
168
  * @param {ZappEntry} entry - The entry object containing a media group.
169
- * @param {string[] | unknown} arg - A single-element array containing the key to look up, or any unknown value.
169
+ * @param {string[] | unknown} arg - Can be an array or any other value (treated as empty array).
170
+ * When an array:
171
+ * - First element: The key to look up. If omitted or undefined, defaults to "image_base".
172
+ * - Second element: Boolean to enable/disable fallback logic. If omitted or undefined, defaults to true.
170
173
  * @returns {?string} The "src" URI from the matched media item, or undefined if not found or empty.
171
174
  */
172
175
  export function imageSrcFromMediaItem(
173
176
  entry: ZappEntry,
174
177
  arg: string[] | unknown
175
178
  ): Option<string> {
176
- const args: unknown = R.unless(Array.isArray, Array)(arg || []);
179
+ const args: any = R.unless(Array.isArray, Array)(arg || []);
177
180
  const imageKey: string = args?.[0] || "image_base"; // always a single key in this function
181
+ const fallback: boolean = args?.[1] !== false;
178
182
 
179
183
  const mediaItems = getMediaItems(entry);
180
184
 
@@ -185,14 +189,16 @@ export function imageSrcFromMediaItem(
185
189
  // Try to find the item with the given key
186
190
  let foundItem = mediaItems.find((item) => item.key === imageKey);
187
191
 
188
- // If not found and key was not "image_base", try to find "image_base"
189
- if (!foundItem && imageKey !== "image_base") {
190
- foundItem = mediaItems.find((item) => item.key === "image_base");
191
- }
192
+ if (fallback) {
193
+ // If not found and key was not "image_base", try to find "image_base"
194
+ if (!foundItem && imageKey !== "image_base") {
195
+ foundItem = mediaItems.find((item) => item.key === "image_base");
196
+ }
192
197
 
193
- // If still not found, default to first item
194
- if (!foundItem) {
195
- foundItem = mediaItems[0];
198
+ // If still not found, default to first item
199
+ if (!foundItem) {
200
+ foundItem = mediaItems[0];
201
+ }
196
202
  }
197
203
 
198
204
  const src = foundItem?.src;
@@ -0,0 +1,330 @@
1
+ import { dayjs } from "../index";
2
+ import customParseFormat from "dayjs/plugin/customParseFormat";
3
+
4
+ dayjs.extend(customParseFormat);
5
+
6
+ describe("dayjs", () => {
7
+ describe("basic functionality", () => {
8
+ it("should be defined and be a function", () => {
9
+ expect(dayjs).toBeDefined();
10
+ expect(typeof dayjs).toBe("function");
11
+ });
12
+
13
+ it("should create dayjs object from date string", () => {
14
+ const date = dayjs("2023-01-01");
15
+
16
+ expect(date.isValid()).toBe(true);
17
+ expect(date.year()).toBe(2023);
18
+ expect(date.month()).toBe(0); // January is 0
19
+ expect(date.date()).toBe(1);
20
+ });
21
+
22
+ it("should create dayjs object from timestamp", () => {
23
+ const timestamp = 1672531200000; // 2023-01-01 00:00:00 UTC
24
+ const date = dayjs(timestamp);
25
+
26
+ expect(date.isValid()).toBe(true);
27
+ expect(date.valueOf()).toBe(timestamp);
28
+ });
29
+
30
+ it("should get current date when called without arguments", () => {
31
+ const now = dayjs();
32
+
33
+ expect(now.isValid()).toBe(true);
34
+ expect(now.year()).toBeGreaterThan(2020);
35
+ });
36
+ });
37
+
38
+ describe("utc plugin", () => {
39
+ it("should support UTC conversion", () => {
40
+ const date = dayjs("2023-01-01T12:00:00");
41
+ const utcDate = date.utc();
42
+
43
+ expect(utcDate).toBeDefined();
44
+ expect(utcDate.isValid()).toBe(true);
45
+ });
46
+
47
+ it("should create UTC date", () => {
48
+ const utcDate = dayjs.utc("2023-01-01");
49
+
50
+ expect(utcDate.isValid()).toBe(true);
51
+ expect(utcDate.utcOffset()).toBe(0);
52
+ });
53
+
54
+ it("should handle UTC offset correctly", () => {
55
+ const date = dayjs.utc();
56
+
57
+ expect(date.utcOffset()).toBe(0);
58
+ });
59
+ });
60
+
61
+ describe("timezone plugin", () => {
62
+ it("should support timezone conversion", () => {
63
+ const date = dayjs("2023-01-01T12:00:00");
64
+ const tzDate = date.tz("America/New_York");
65
+
66
+ expect(tzDate).toBeDefined();
67
+ expect(tzDate.isValid()).toBe(true);
68
+ });
69
+
70
+ it("should create date in specific timezone", () => {
71
+ const date = dayjs.tz("2023-01-01", "America/New_York");
72
+
73
+ expect(date.isValid()).toBe(true);
74
+ });
75
+
76
+ it("should handle timezone method", () => {
77
+ const date = dayjs();
78
+
79
+ // Just verify the method exists and doesn't throw
80
+ expect(() => date.tz("UTC")).not.toThrow();
81
+ });
82
+ });
83
+
84
+ describe("isoWeek plugin", () => {
85
+ it("should get ISO week of year", () => {
86
+ const date = dayjs("2023-01-01");
87
+ const isoWeek = date.isoWeek();
88
+
89
+ expect(typeof isoWeek).toBe("number");
90
+ expect(isoWeek).toBeGreaterThan(0);
91
+ expect(isoWeek).toBeLessThanOrEqual(53);
92
+ });
93
+
94
+ it("should get ISO weekday", () => {
95
+ const date = dayjs("2023-01-02"); // Monday
96
+ const isoWeekday = date.isoWeekday();
97
+
98
+ expect(isoWeekday).toBe(1); // Monday is 1 in ISO weekday
99
+ });
100
+
101
+ it("should set ISO week", () => {
102
+ const date = dayjs("2023-01-01");
103
+ const newDate = date.isoWeek(10);
104
+
105
+ expect(newDate.isoWeek()).toBe(10);
106
+ });
107
+ });
108
+
109
+ describe("duration plugin", () => {
110
+ it("should create duration from milliseconds", () => {
111
+ const duration = dayjs.duration(1000);
112
+
113
+ expect(duration.asSeconds()).toBe(1);
114
+ });
115
+
116
+ it("should create duration with object", () => {
117
+ const duration = dayjs.duration({
118
+ hours: 1,
119
+ minutes: 30,
120
+ });
121
+
122
+ expect(duration.asMinutes()).toBe(90);
123
+ });
124
+
125
+ it("should format duration", () => {
126
+ const duration = dayjs.duration(3661, "seconds");
127
+
128
+ expect(duration.hours()).toBe(1);
129
+ expect(duration.minutes()).toBe(1);
130
+ expect(duration.seconds()).toBe(1);
131
+ });
132
+
133
+ it("should add duration to date", () => {
134
+ const date = dayjs("2023-01-01");
135
+ const duration = dayjs.duration(1, "day");
136
+ const newDate = date.add(duration);
137
+
138
+ expect(newDate.date()).toBe(2);
139
+ });
140
+
141
+ it("should subtract duration from date", () => {
142
+ const date = dayjs("2023-01-02");
143
+ const duration = dayjs.duration(1, "day");
144
+ const newDate = date.subtract(duration);
145
+
146
+ expect(newDate.date()).toBe(1);
147
+ });
148
+ });
149
+
150
+ describe("relativeTime plugin", () => {
151
+ it("should get time from now", () => {
152
+ const pastDate = dayjs().subtract(1, "hour");
153
+ const relative = pastDate.fromNow();
154
+
155
+ expect(typeof relative).toBe("string");
156
+ expect(relative).toContain("hour");
157
+ });
158
+
159
+ it("should get time to now", () => {
160
+ const futureDate = dayjs().add(1, "hour");
161
+ const relative = futureDate.toNow();
162
+
163
+ expect(typeof relative).toBe("string");
164
+ expect(relative).toContain("hour");
165
+ });
166
+
167
+ it("should get time from specific date", () => {
168
+ const date1 = dayjs("2023-01-01");
169
+ const date2 = dayjs("2023-01-02");
170
+ const relative = date1.from(date2);
171
+
172
+ expect(typeof relative).toBe("string");
173
+ expect(relative).toContain("day");
174
+ });
175
+
176
+ it("should get time to specific date", () => {
177
+ const date1 = dayjs("2023-01-01");
178
+ const date2 = dayjs("2023-01-02");
179
+ const relative = date2.to(date1);
180
+
181
+ expect(typeof relative).toBe("string");
182
+ expect(relative).toContain("day");
183
+ });
184
+
185
+ it("should handle relative time for minutes", () => {
186
+ const pastDate = dayjs().subtract(30, "minutes");
187
+ const relative = pastDate.fromNow();
188
+
189
+ expect(relative).toContain("minute");
190
+ });
191
+
192
+ it("should handle relative time for days", () => {
193
+ const pastDate = dayjs().subtract(5, "days");
194
+ const relative = pastDate.fromNow();
195
+
196
+ expect(relative).toContain("day");
197
+ });
198
+
199
+ it("should handle relative time for months", () => {
200
+ const pastDate = dayjs().subtract(2, "months");
201
+ const relative = pastDate.fromNow();
202
+
203
+ expect(relative).toContain("month");
204
+ });
205
+
206
+ it("should handle relative time for years", () => {
207
+ const pastDate = dayjs().subtract(2, "years");
208
+ const relative = pastDate.fromNow();
209
+
210
+ expect(relative).toContain("year");
211
+ });
212
+ });
213
+
214
+ describe("custom relativeTime thresholds", () => {
215
+ it("should respect custom threshold configuration", () => {
216
+ // Our custom config has 59 minutes threshold for 'mm'
217
+ const date = dayjs().subtract(45, "minutes");
218
+ const relative = date.fromNow();
219
+
220
+ // Should show minutes, not hours
221
+ expect(relative).toContain("minute");
222
+ });
223
+
224
+ it("should handle edge case at day threshold", () => {
225
+ const date = dayjs().subtract(29, "days");
226
+ const relative = date.fromNow();
227
+
228
+ // With custom threshold of 29 days, should still show days
229
+ expect(relative).toContain("day");
230
+ });
231
+ });
232
+
233
+ describe("date manipulation", () => {
234
+ it("should add time units", () => {
235
+ const date = dayjs("2023-01-01");
236
+
237
+ expect(date.add(1, "day").date()).toBe(2);
238
+ expect(date.add(1, "month").month()).toBe(1);
239
+ expect(date.add(1, "year").year()).toBe(2024);
240
+ });
241
+
242
+ it("should subtract time units", () => {
243
+ const date = dayjs("2023-01-02");
244
+
245
+ expect(date.subtract(1, "day").date()).toBe(1);
246
+ expect(date.subtract(1, "month").month()).toBe(11); // December of previous year
247
+ expect(date.subtract(1, "year").year()).toBe(2022);
248
+ });
249
+
250
+ it("should start of time unit", () => {
251
+ const date = dayjs("2023-06-15T14:30:45");
252
+
253
+ expect(date.startOf("day").hour()).toBe(0);
254
+ expect(date.startOf("month").date()).toBe(1);
255
+ expect(date.startOf("year").month()).toBe(0);
256
+ });
257
+
258
+ it("should end of time unit", () => {
259
+ const date = dayjs("2023-06-15T14:30:45");
260
+
261
+ expect(date.endOf("day").hour()).toBe(23);
262
+ expect(date.endOf("month").date()).toBe(30); // June has 30 days
263
+ expect(date.endOf("year").month()).toBe(11); // December
264
+ });
265
+ });
266
+
267
+ describe("formatting", () => {
268
+ it("should format date", () => {
269
+ const date = dayjs("2023-01-01T12:30:45");
270
+
271
+ expect(date.format("YYYY-MM-DD")).toBe("2023-01-01");
272
+ expect(date.format("YYYY/MM/DD HH:mm:ss")).toContain("2023/01/01");
273
+ });
274
+
275
+ it("should convert to ISO string", () => {
276
+ const date = dayjs("2023-01-01");
277
+ const iso = date.toISOString();
278
+
279
+ expect(iso).toContain("2023-01-01");
280
+ expect(iso).toContain("T");
281
+ expect(iso).toContain("Z");
282
+ });
283
+
284
+ it("should convert to Date object", () => {
285
+ const dayjsDate = dayjs("2023-01-01");
286
+ const jsDate = dayjsDate.toDate();
287
+
288
+ expect(jsDate).toBeInstanceOf(Date);
289
+ expect(jsDate.getFullYear()).toBe(2023);
290
+ });
291
+ });
292
+
293
+ describe("comparison", () => {
294
+ it("should check if date is before another", () => {
295
+ const date1 = dayjs("2023-01-01");
296
+ const date2 = dayjs("2023-01-02");
297
+
298
+ expect(date1.isBefore(date2)).toBe(true);
299
+ expect(date2.isBefore(date1)).toBe(false);
300
+ });
301
+
302
+ it("should check if date is after another", () => {
303
+ const date1 = dayjs("2023-01-01");
304
+ const date2 = dayjs("2023-01-02");
305
+
306
+ expect(date2.isAfter(date1)).toBe(true);
307
+ expect(date1.isAfter(date2)).toBe(false);
308
+ });
309
+
310
+ it("should check if date is same as another", () => {
311
+ const date1 = dayjs("2023-01-01");
312
+ const date2 = dayjs("2023-01-01");
313
+
314
+ expect(date1.isSame(date2)).toBe(true);
315
+ });
316
+ });
317
+
318
+ describe("validation", () => {
319
+ it("should validate valid dates", () => {
320
+ expect(dayjs("2023-01-01").isValid()).toBe(true);
321
+ expect(dayjs(new Date()).isValid()).toBe(true);
322
+ expect(dayjs(Date.now()).isValid()).toBe(true);
323
+ });
324
+
325
+ it("should detect invalid dates", () => {
326
+ expect(dayjs("invalid").isValid()).toBe(false);
327
+ expect(dayjs("2023-13-01", "YYYY-MM-DD", true).isValid()).toBe(false);
328
+ });
329
+ });
330
+ });
@@ -0,0 +1,207 @@
1
+ import { getEnumKeyByEnumValue } from "../index";
2
+
3
+ describe("getEnumKeyByEnumValue", () => {
4
+ enum TestEnum {
5
+ First = "FIRST",
6
+ Second = "SECOND",
7
+ Third = "THIRD",
8
+ }
9
+
10
+ enum NumericEnum {
11
+ Zero = 0,
12
+ One = 1,
13
+ Two = 2,
14
+ Three = 3,
15
+ }
16
+
17
+ enum MixedEnum {
18
+ StringValue = "string",
19
+ NumericValue = 42,
20
+ BooleanValue = "true",
21
+ }
22
+
23
+ describe("string enums", () => {
24
+ it("should return key for valid enum value", () => {
25
+ expect(getEnumKeyByEnumValue(TestEnum, "FIRST")).toBe("First");
26
+ expect(getEnumKeyByEnumValue(TestEnum, "SECOND")).toBe("Second");
27
+ expect(getEnumKeyByEnumValue(TestEnum, "THIRD")).toBe("Third");
28
+ });
29
+
30
+ it("should return null for invalid enum value", () => {
31
+ expect(getEnumKeyByEnumValue(TestEnum, "INVALID")).toBeNull();
32
+ expect(getEnumKeyByEnumValue(TestEnum, "")).toBeNull();
33
+ expect(getEnumKeyByEnumValue(TestEnum, "first")).toBeNull();
34
+ });
35
+
36
+ it("should be case-sensitive", () => {
37
+ expect(getEnumKeyByEnumValue(TestEnum, "first")).toBeNull();
38
+ expect(getEnumKeyByEnumValue(TestEnum, "FIRST")).toBe("First");
39
+ });
40
+ });
41
+
42
+ describe("numeric enums", () => {
43
+ it("should return key for valid numeric enum value", () => {
44
+ expect(getEnumKeyByEnumValue(NumericEnum, 0)).toBe("Zero");
45
+ expect(getEnumKeyByEnumValue(NumericEnum, 1)).toBe("One");
46
+ expect(getEnumKeyByEnumValue(NumericEnum, 2)).toBe("Two");
47
+ expect(getEnumKeyByEnumValue(NumericEnum, 3)).toBe("Three");
48
+ });
49
+
50
+ it("should return null for invalid numeric value", () => {
51
+ expect(getEnumKeyByEnumValue(NumericEnum, 4)).toBeNull();
52
+ expect(getEnumKeyByEnumValue(NumericEnum, -1)).toBeNull();
53
+ expect(getEnumKeyByEnumValue(NumericEnum, 999)).toBeNull();
54
+ });
55
+
56
+ it("should handle numeric strings", () => {
57
+ // When passing string "0", it won't match numeric enum value 0
58
+ expect(getEnumKeyByEnumValue(NumericEnum, "0")).toBeNull();
59
+ });
60
+ });
61
+
62
+ describe("mixed enums", () => {
63
+ it("should handle string values in mixed enum", () => {
64
+ expect(getEnumKeyByEnumValue(MixedEnum, "string")).toBe("StringValue");
65
+ });
66
+
67
+ it("should handle numeric values in mixed enum", () => {
68
+ expect(getEnumKeyByEnumValue(MixedEnum, 42)).toBe("NumericValue");
69
+ });
70
+
71
+ it("should handle boolean-like string values", () => {
72
+ expect(getEnumKeyByEnumValue(MixedEnum, "true")).toBe("BooleanValue");
73
+ });
74
+
75
+ it("should not match boolean to string 'true'", () => {
76
+ expect(getEnumKeyByEnumValue(MixedEnum, true)).toBeNull();
77
+ });
78
+ });
79
+
80
+ describe("edge cases", () => {
81
+ it("should return null for null value", () => {
82
+ expect(getEnumKeyByEnumValue(TestEnum, null)).toBeNull();
83
+ });
84
+
85
+ it("should return null for undefined value", () => {
86
+ expect(getEnumKeyByEnumValue(TestEnum, undefined)).toBeNull();
87
+ });
88
+
89
+ it("should handle empty enum", () => {
90
+ const EmptyEnum = {};
91
+ expect(getEnumKeyByEnumValue(EmptyEnum, "anything")).toBeNull();
92
+ });
93
+
94
+ it("should handle object values", () => {
95
+ expect(getEnumKeyByEnumValue(TestEnum, {})).toBeNull();
96
+ });
97
+
98
+ it("should handle array values", () => {
99
+ expect(getEnumKeyByEnumValue(TestEnum, [])).toBeNull();
100
+ });
101
+
102
+ it("should return first matching key if multiple keys have same value", () => {
103
+ enum DuplicateEnum {
104
+ First = "SAME",
105
+ Second = "SAME",
106
+ }
107
+
108
+ const result = getEnumKeyByEnumValue(DuplicateEnum, "SAME");
109
+ expect(result).toBeTruthy();
110
+ expect(["First", "Second"]).toContain(result);
111
+ });
112
+ });
113
+
114
+ describe("type checking", () => {
115
+ it("should work with const enum-like objects", () => {
116
+ const ConstLikeEnum = {
117
+ KEY_ONE: "value1",
118
+ KEY_TWO: "value2",
119
+ } as const;
120
+
121
+ expect(getEnumKeyByEnumValue(ConstLikeEnum, "value1")).toBe("KEY_ONE");
122
+ expect(getEnumKeyByEnumValue(ConstLikeEnum, "value2")).toBe("KEY_TWO");
123
+ });
124
+
125
+ it("should work with plain objects", () => {
126
+ const plainObject = {
127
+ key1: "val1",
128
+ key2: "val2",
129
+ key3: 123,
130
+ };
131
+
132
+ expect(getEnumKeyByEnumValue(plainObject, "val1")).toBe("key1");
133
+ expect(getEnumKeyByEnumValue(plainObject, 123)).toBe("key3");
134
+ });
135
+
136
+ it("should return correct TypeScript type", () => {
137
+ const result = getEnumKeyByEnumValue(TestEnum, "FIRST");
138
+
139
+ // Result should be keyof TestEnum | null
140
+ if (result !== null) {
141
+ expect(typeof result).toBe("string");
142
+ }
143
+ });
144
+ });
145
+
146
+ describe("special values", () => {
147
+ enum SpecialEnum {
148
+ EmptyString = "",
149
+ Zero = 0,
150
+ False = "false",
151
+ Null = "null",
152
+ }
153
+
154
+ it("should handle empty string value", () => {
155
+ expect(getEnumKeyByEnumValue(SpecialEnum, "")).toBe("EmptyString");
156
+ });
157
+
158
+ it("should handle zero value", () => {
159
+ expect(getEnumKeyByEnumValue(SpecialEnum, 0)).toBe("Zero");
160
+ });
161
+
162
+ it("should handle false-like string", () => {
163
+ expect(getEnumKeyByEnumValue(SpecialEnum, "false")).toBe("False");
164
+ });
165
+
166
+ it("should handle null-like string", () => {
167
+ expect(getEnumKeyByEnumValue(SpecialEnum, "null")).toBe("Null");
168
+ });
169
+ });
170
+
171
+ describe("performance", () => {
172
+ it("should handle large enums efficiently", () => {
173
+ const largeEnum: Record<string, string> = {};
174
+
175
+ for (let i = 0; i < 1000; i++) {
176
+ largeEnum[`Key${i}`] = `Value${i}`;
177
+ }
178
+
179
+ expect(getEnumKeyByEnumValue(largeEnum, "Value500")).toBe("Key500");
180
+ expect(getEnumKeyByEnumValue(largeEnum, "Value999")).toBe("Key999");
181
+ expect(getEnumKeyByEnumValue(largeEnum, "NonExistent")).toBeNull();
182
+ });
183
+ });
184
+
185
+ describe("reverse lookup", () => {
186
+ enum ReverseEnum {
187
+ A = "ValueA",
188
+ B = "ValueB",
189
+ }
190
+
191
+ it("should provide reverse lookup functionality", () => {
192
+ // Forward: get value from key
193
+ expect(ReverseEnum.A).toBe("ValueA");
194
+
195
+ // Reverse: get key from value
196
+ expect(getEnumKeyByEnumValue(ReverseEnum, "ValueA")).toBe("A");
197
+ });
198
+
199
+ it("should be consistent with enum access", () => {
200
+ const key = getEnumKeyByEnumValue(TestEnum, "SECOND");
201
+
202
+ if (key) {
203
+ expect(TestEnum[key]).toBe("SECOND");
204
+ }
205
+ });
206
+ });
207
+ });
@@ -0,0 +1,97 @@
1
+ import { GeneralError } from "../GeneralError";
2
+
3
+ describe("GeneralError", () => {
4
+ it("should create error with message only", () => {
5
+ const message = "Something went wrong";
6
+ const error = new GeneralError(message);
7
+
8
+ expect(error).toBeInstanceOf(Error);
9
+ expect(error).toBeInstanceOf(GeneralError);
10
+ expect(error.message).toBe(message);
11
+ expect(error.statusCode).toBeNull();
12
+ expect(error.localizationKey).toBeNull();
13
+ });
14
+
15
+ it("should create error with message and status code", () => {
16
+ const message = "Not found";
17
+ const statusCode = 404;
18
+ const error = new GeneralError(message, statusCode);
19
+
20
+ expect(error.message).toBe(message);
21
+ expect(error.statusCode).toBe(statusCode);
22
+ expect(error.localizationKey).toBeNull();
23
+ });
24
+
25
+ it("should create error with all parameters", () => {
26
+ const message = "Forbidden";
27
+ const statusCode = 403;
28
+ const localizationKey = "error.forbidden";
29
+ const error = new GeneralError(message, statusCode, localizationKey);
30
+
31
+ expect(error.message).toBe(message);
32
+ expect(error.statusCode).toBe(statusCode);
33
+ expect(error.localizationKey).toBe(localizationKey);
34
+ });
35
+
36
+ it("should create error with null status code explicitly", () => {
37
+ const message = "Error without status";
38
+ const error = new GeneralError(message, null, "error.general");
39
+
40
+ expect(error.message).toBe(message);
41
+ expect(error.statusCode).toBeNull();
42
+ expect(error.localizationKey).toBe("error.general");
43
+ });
44
+
45
+ it("should create error with null localization key explicitly", () => {
46
+ const message = "Server error";
47
+ const statusCode = 500;
48
+ const error = new GeneralError(message, statusCode, null);
49
+
50
+ expect(error.message).toBe(message);
51
+ expect(error.statusCode).toBe(statusCode);
52
+ expect(error.localizationKey).toBeNull();
53
+ });
54
+
55
+ it("should have error name", () => {
56
+ const error = new GeneralError("Test error");
57
+
58
+ expect(error.name).toBe("Error");
59
+ });
60
+
61
+ it("should be throwable and catchable", () => {
62
+ const message = "Test throw";
63
+ const statusCode = 400;
64
+
65
+ expect(() => {
66
+ throw new GeneralError(message, statusCode);
67
+ }).toThrow(GeneralError);
68
+
69
+ try {
70
+ throw new GeneralError(message, statusCode);
71
+ } catch (error) {
72
+ expect(error).toBeInstanceOf(GeneralError);
73
+ expect((error as GeneralError).message).toBe(message);
74
+ expect((error as GeneralError).statusCode).toBe(statusCode);
75
+ }
76
+ });
77
+
78
+ it("should maintain error stack trace", () => {
79
+ const error = new GeneralError("Stack trace test");
80
+
81
+ expect(error.stack).toBeDefined();
82
+ expect(error.stack).toContain("Stack trace test");
83
+ });
84
+
85
+ it("should allow status code to be 0", () => {
86
+ const error = new GeneralError("Zero status", 0);
87
+
88
+ expect(error.statusCode).toBe(0);
89
+ });
90
+
91
+ it("should allow empty string message", () => {
92
+ const error = new GeneralError("");
93
+
94
+ expect(error.message).toBe("");
95
+ expect(error.statusCode).toBeNull();
96
+ });
97
+ });