@applicaster/zapp-react-native-utils 15.0.0-rc.94 → 15.0.0-rc.96

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.
@@ -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
+ });