@applicaster/zapp-react-native-utils 15.0.0-alpha.2004137882 → 15.0.0-alpha.2060204268
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.
- package/adsUtils/__tests__/createVMAP.test.ts +419 -0
- package/analyticsUtils/analyticsMapper.ts +10 -2
- package/appDataUtils/__tests__/urlScheme.test.ts +678 -0
- package/appUtils/HooksManager/__tests__/__snapshots__/hooksManager.test.js.snap +0 -188
- package/appUtils/HooksManager/__tests__/hooksManager.test.js +16 -2
- package/appUtils/RiverFocusManager/{index.js → index.ts} +25 -18
- package/appUtils/contextKeysManager/__tests__/getKeys/failure.test.ts +7 -2
- package/appUtils/contextKeysManager/__tests__/getKeys/success.test.ts +48 -0
- package/appUtils/contextKeysManager/contextResolver.ts +51 -22
- package/appUtils/contextKeysManager/index.ts +65 -10
- package/appUtils/focusManager/__tests__/__snapshots__/focusManager.test.js.snap +3 -0
- package/appUtils/focusManager/index.ios.ts +35 -2
- package/appUtils/focusManagerAux/utils/index.ios.ts +122 -0
- package/appUtils/focusManagerAux/utils/utils.ios.ts +55 -1
- package/cloudEventsUtils/__tests__/index.test.ts +529 -0
- package/cloudEventsUtils/index.ts +65 -1
- package/configurationUtils/__tests__/imageSrcFromMediaItem.test.ts +38 -0
- package/configurationUtils/index.ts +17 -11
- package/dateUtils/__tests__/dayjs.test.ts +330 -0
- package/enumUtils/__tests__/getEnumKeyByEnumValue.test.ts +207 -0
- package/errorUtils/__tests__/GeneralError.test.ts +97 -0
- package/errorUtils/__tests__/HttpStatusCode.test.ts +344 -0
- package/errorUtils/__tests__/MissingPluginError.test.ts +113 -0
- package/errorUtils/__tests__/NetworkError.test.ts +202 -0
- package/errorUtils/__tests__/getParsedResponse.test.ts +188 -0
- package/errorUtils/__tests__/invariant.test.ts +112 -0
- package/headersUtils/__tests__/headersUtils.test.js +11 -1
- package/headersUtils/index.ts +2 -1
- package/manifestUtils/defaultManifestConfigurations/player.js +50 -10
- package/manifestUtils/platformIsTV.js +13 -0
- package/navigationUtils/index.ts +15 -5
- package/numberUtils/__tests__/toNumber.test.ts +12 -0
- package/numberUtils/__tests__/toPositiveNumber.test.ts +165 -0
- package/numberUtils/index.ts +19 -1
- package/package.json +4 -4
- package/reactHooks/analytics/__tests__/useSendAnalyticsOnPress.test.ts +537 -0
- package/reactHooks/app/__tests__/useAppState.test.ts +1 -1
- package/reactHooks/autoscrolling/__tests__/useTrackCurrentAutoScrollingElement.test.ts +1 -1
- package/reactHooks/autoscrolling/__tests__/useTrackedView.test.tsx +1 -2
- package/reactHooks/cell-click/__tests__/index.test.js +1 -3
- package/reactHooks/configuration/__tests__/index.test.tsx +1 -1
- package/reactHooks/connection/__tests__/index.test.js +1 -1
- package/reactHooks/dev/__tests__/useReRenderLog.test.ts +188 -0
- package/reactHooks/device/useIsTablet.tsx +14 -19
- package/reactHooks/events/index.ts +20 -0
- package/reactHooks/feed/__tests__/useBatchLoading.test.tsx +32 -23
- package/reactHooks/feed/__tests__/useBuildPipesUrl.test.tsx +19 -19
- package/reactHooks/feed/__tests__/useEntryScreenId.test.tsx +1 -1
- package/reactHooks/feed/__tests__/useFeedLoader.test.tsx +42 -30
- package/reactHooks/feed/__tests__/useFeedRefresh.test.tsx +1 -1
- package/reactHooks/feed/__tests__/useInflatedUrl.test.tsx +1 -1
- package/reactHooks/hookModal/hooks/useHookModalScreenData.ts +12 -8
- package/reactHooks/index.ts +2 -0
- package/reactHooks/layout/__tests__/index.test.tsx +1 -1
- package/reactHooks/layout/__tests__/useLayoutVersion.test.tsx +1 -1
- package/reactHooks/navigation/__tests__/index.test.tsx +40 -9
- package/reactHooks/navigation/index.ts +19 -4
- package/reactHooks/navigation/useRoute.ts +3 -1
- package/reactHooks/player/__tests__/useAutoSeek._test.tsx +1 -1
- package/reactHooks/player/__tests__/useTapSeek._test.ts +1 -1
- package/reactHooks/resolvers/__tests__/useCellResolver.test.tsx +1 -1
- package/reactHooks/resolvers/__tests__/useComponentResolver.test.tsx +1 -1
- package/reactHooks/screen/__tests__/useCurrentScreenData.test.tsx +2 -2
- package/reactHooks/screen/__tests__/useScreenBackgroundColor.test.tsx +1 -1
- package/reactHooks/screen/__tests__/useScreenData.test.tsx +1 -1
- package/reactHooks/screen/__tests__/useTargetScreenData.test.tsx +2 -2
- package/reactHooks/state/useRefWithInitialValue.ts +10 -0
- package/reactHooks/ui/__tests__/useFadeOutWhenBlurred.test.ts +580 -0
- package/reactHooks/utils/__tests__/index.test.js +1 -1
- package/rectUtils/__tests__/index.test.ts +549 -0
- package/rectUtils/index.ts +2 -2
- package/screenPickerUtils/__tests__/index.test.ts +333 -0
- package/screenState/__tests__/index.test.ts +1 -1
- package/screenUtils/index.ts +3 -0
- package/searchUtils/const.ts +7 -0
- package/searchUtils/index.ts +3 -0
- package/services/storageServiceSync.web.ts +1 -1
- package/stringUtils/index.ts +1 -1
- package/testUtils/index.tsx +1 -1
- package/time/__tests__/BackgroundTimer.test.ts +156 -0
- package/time/__tests__/Timer.test.ts +236 -0
- package/typeGuards/__tests__/isString.test.ts +21 -0
- package/typeGuards/index.ts +4 -0
- package/utils/__tests__/mergeRight.test.ts +48 -0
- package/utils/index.ts +3 -0
- package/utils/mergeRight.ts +5 -0
- package/reactHooks/componentsMap/index.ts +0 -55
|
@@ -3,6 +3,41 @@ import { RequestBuilder } from "@applicaster/zapp-pipes-v2-client";
|
|
|
3
3
|
import { sessionStorage } from "@applicaster/zapp-react-native-bridge/ZappStorage/SessionStorage";
|
|
4
4
|
import { getAppUrlScheme } from "@applicaster/zapp-react-native-utils/appDataUtils";
|
|
5
5
|
|
|
6
|
+
/**
|
|
7
|
+
* CloudEvents v1.0 compliant event object
|
|
8
|
+
* @see https://github.com/cloudevents/spec/blob/v1.0/spec.md
|
|
9
|
+
*/
|
|
10
|
+
export interface CloudEvent<T = any> {
|
|
11
|
+
/** CloudEvents specification version (always "1.0") */
|
|
12
|
+
specversion: "1.0";
|
|
13
|
+
/** Type of event (e.g., "user.action", "commerce.purchase") */
|
|
14
|
+
type: string;
|
|
15
|
+
/** Identifier for the entity that produced the event */
|
|
16
|
+
source: string;
|
|
17
|
+
/** Unique identifier for the event */
|
|
18
|
+
id: string;
|
|
19
|
+
/** Subject of the event in the context of the event producer */
|
|
20
|
+
subject: string;
|
|
21
|
+
/** Timestamp of when the event occurred (ISO 8601 format) */
|
|
22
|
+
time: string;
|
|
23
|
+
/** Content type of the data attribute (always "application/json") */
|
|
24
|
+
datacontenttype: "application/json";
|
|
25
|
+
/** Event payload */
|
|
26
|
+
data: T;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Parameters required to create a cloud event
|
|
31
|
+
*/
|
|
32
|
+
export interface CreateCloudEventParams<T = any> {
|
|
33
|
+
/** Type of event (e.g., "user.action", "commerce.purchase") */
|
|
34
|
+
type: string;
|
|
35
|
+
/** Subject of the event in the context of the event producer */
|
|
36
|
+
subject: string;
|
|
37
|
+
/** Event payload */
|
|
38
|
+
data: T;
|
|
39
|
+
}
|
|
40
|
+
|
|
6
41
|
let source: string | null = null;
|
|
7
42
|
|
|
8
43
|
const addSource = async () => {
|
|
@@ -26,7 +61,36 @@ const addSource = async () => {
|
|
|
26
61
|
return source;
|
|
27
62
|
};
|
|
28
63
|
|
|
29
|
-
|
|
64
|
+
/**
|
|
65
|
+
* Resets the cached CloudEvents source.
|
|
66
|
+
*
|
|
67
|
+
* @internal
|
|
68
|
+
* @testonly This function is intended for test use only and will only work in test environments.
|
|
69
|
+
*
|
|
70
|
+
* @remarks
|
|
71
|
+
* This function clears the cached source value, forcing the next call to `createCloudEvent`
|
|
72
|
+
* to regenerate the source from app metadata. It only operates when NODE_ENV is set to "test"
|
|
73
|
+
* to prevent accidental misuse in production code.
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* ```typescript
|
|
77
|
+
* // In Jest tests
|
|
78
|
+
* beforeEach(() => {
|
|
79
|
+
* resetCloudEventsSource();
|
|
80
|
+
* });
|
|
81
|
+
* ```
|
|
82
|
+
*/
|
|
83
|
+
export const resetCloudEventsSource = (): void => {
|
|
84
|
+
if (process.env.NODE_ENV === "test") {
|
|
85
|
+
source = null;
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
export const createCloudEvent = async <T = any>({
|
|
90
|
+
type,
|
|
91
|
+
subject,
|
|
92
|
+
data,
|
|
93
|
+
}: CreateCloudEventParams<T>): Promise<CloudEvent<T>> => {
|
|
30
94
|
return {
|
|
31
95
|
specversion: "1.0",
|
|
32
96
|
type,
|
|
@@ -31,4 +31,42 @@ describe("imageSrcFromMediaItem", () => {
|
|
|
31
31
|
|
|
32
32
|
expect(imageSrcFromMediaItem(badEntry, ["image_base"])).toBeUndefined();
|
|
33
33
|
});
|
|
34
|
+
|
|
35
|
+
it("returns undefined when fallback is false and key is not found", () => {
|
|
36
|
+
const result = imageSrcFromMediaItem(entry as ZappEntry, [
|
|
37
|
+
"does_not_exist",
|
|
38
|
+
false,
|
|
39
|
+
]);
|
|
40
|
+
|
|
41
|
+
expect(result).toBeUndefined();
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it("returns src when fallback is false and key is found", () => {
|
|
45
|
+
const result = imageSrcFromMediaItem(entry as ZappEntry, [
|
|
46
|
+
"logo_thumbnail",
|
|
47
|
+
false,
|
|
48
|
+
]);
|
|
49
|
+
|
|
50
|
+
expect(result).toEqual(entry.media_group[1].media_item[0].src);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it("returns image_base as fallback when fallback is explicitly true and key is not found", () => {
|
|
54
|
+
const result = imageSrcFromMediaItem(entry as ZappEntry, [
|
|
55
|
+
"does_not_exist",
|
|
56
|
+
true,
|
|
57
|
+
]);
|
|
58
|
+
|
|
59
|
+
const fallback = entry.media_group[0].media_item[0];
|
|
60
|
+
expect(result).toEqual(fallback.src);
|
|
61
|
+
expect(fallback.key).toBe("image_base");
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it("returns src when fallback is explicitly true and key is found", () => {
|
|
65
|
+
const result = imageSrcFromMediaItem(entry as ZappEntry, [
|
|
66
|
+
"logo_thumbnail",
|
|
67
|
+
true,
|
|
68
|
+
]);
|
|
69
|
+
|
|
70
|
+
expect(result).toEqual(entry.media_group[1].media_item[0].src);
|
|
71
|
+
});
|
|
34
72
|
});
|
|
@@ -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 -
|
|
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:
|
|
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
|
-
|
|
189
|
-
|
|
190
|
-
|
|
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
|
-
|
|
194
|
-
|
|
195
|
-
|
|
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
|
+
});
|