-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathindex.test.ts
More file actions
113 lines (90 loc) · 5.12 KB
/
index.test.ts
File metadata and controls
113 lines (90 loc) · 5.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import dayjs from 'dayjs';
import formatDateTime, { DateTimeFormat } from '../';
describe('formatDateTime', () => {
const testDate = new Date('2023-05-15T14:30:45');
test('should correctly format date using standard format', () => {
expect(formatDateTime(testDate, DateTimeFormat.STANDARD)).toBe('2023-05-15 14:30:45');
});
test('should correctly format years', () => {
expect(formatDateTime(testDate, DateTimeFormat.YEAR)).toBe('2023');
expect(formatDateTime(testDate, DateTimeFormat.YEAR_SHORT)).toBe('23');
});
test('should correctly format months', () => {
expect(formatDateTime(testDate, DateTimeFormat.MONTH)).toBe('05');
expect(formatDateTime(testDate, DateTimeFormat.MONTH_SHORT)).toBe('5');
expect(formatDateTime(testDate, DateTimeFormat.MONTH_NAME)).toMatch('May');
expect(formatDateTime(testDate, DateTimeFormat.MONTH_NAME_SHORT)).toMatch('May');
});
test('should correctly format dates', () => {
expect(formatDateTime(testDate, DateTimeFormat.DAY)).toBe('15');
expect(formatDateTime(testDate, DateTimeFormat.DAY_SHORT)).toBe('15');
expect(formatDateTime(testDate, DateTimeFormat.WEEKDAY)).toMatch('Monday');
expect(formatDateTime(testDate, DateTimeFormat.WEEKDAY_SHORT)).toMatch('Mon');
});
test('should correctly format times', () => {
expect(formatDateTime(testDate, DateTimeFormat.HOUR)).toBe('14');
expect(formatDateTime(testDate, DateTimeFormat.HOUR_SHORT)).toBe('14');
expect(formatDateTime(testDate, DateTimeFormat.HOUR_12)).toBe('02');
expect(formatDateTime(testDate, DateTimeFormat.HOUR_12_SHORT)).toBe('2');
expect(formatDateTime(testDate, DateTimeFormat.MINUTE)).toBe('30');
expect(formatDateTime(testDate, DateTimeFormat.MINUTE_SHORT)).toBe('30');
expect(formatDateTime(testDate, DateTimeFormat.SECOND)).toBe('45');
expect(formatDateTime(testDate, DateTimeFormat.SECOND_SHORT)).toBe('45');
});
test('should correctly format timezones', () => {
const timezoneResult = formatDateTime(testDate, DateTimeFormat.TIMEZONE);
expect(typeof timezoneResult).toBe('string');
expect(timezoneResult).toMatch(/[+-]\d{2}:\d{2}|Z/);
const timezoneISOResult = formatDateTime(testDate, DateTimeFormat.TIMEZONE_ISO);
expect(typeof timezoneISOResult).toBe('string');
expect(timezoneISOResult).toMatch(/[+-]\d{4}|Z/);
});
// Test common combined formats
test('should correctly format common combined formats', () => {
expect(formatDateTime(testDate, DateTimeFormat.DATE)).toBe('2023-05-15');
expect(formatDateTime(testDate, DateTimeFormat.TIME)).toBe('14:30:45');
const time12Result = formatDateTime(testDate, DateTimeFormat.TIME_12);
expect(time12Result).toMatch(/02:30:45 (AM|PM)/);
expect(formatDateTime(testDate, DateTimeFormat.DATE_TIME)).toBe('2023-05-15 14:30');
const dateTime12Result = formatDateTime(testDate, DateTimeFormat.DATE_TIME_12);
expect(dateTime12Result).toMatch(/2023-05-15 02:30 (AM|PM)/);
const isoResult = formatDateTime(testDate, DateTimeFormat.ISO);
expect(isoResult).toMatch(/2023-05-15T14:30:45[+-]\d{2}:\d{2}|Z/);
const fullDatetimeISOResult = formatDateTime(testDate, DateTimeFormat.FULL_DATETIME_ISO);
expect(fullDatetimeISOResult).toMatch(/2023-05-15T14:30:45[+-]\d{2}:\d{2}|Z/);
});
test('should handle different date input types', () => {
// String input
expect(formatDateTime('2023-05-15T14:30:45Z', DateTimeFormat.DATE)).toBe('2023-05-15');
// Timestamp input
const timestamp = testDate.getTime();
expect(formatDateTime(timestamp, DateTimeFormat.DATE)).toBe('2023-05-15');
// Dayjs object input
const dayjsObj = dayjs('2023-05-15T14:30:45Z');
expect(formatDateTime(dayjsObj, DateTimeFormat.DATE)).toBe('2023-05-15');
});
test('should handle invalid format', () => {
const result = formatDateTime(testDate, 'INVALID_FORMAT');
expect(result).toBeInstanceOf(dayjs);
});
// Test edge cases
test('should handle edge case dates', () => {
// Year boundaries
const yearMin = new Date('0001-01-01T00:00:00');
expect(formatDateTime(yearMin, DateTimeFormat.YEAR)).toBe('0001');
expect(formatDateTime(yearMin, DateTimeFormat.DATE)).toBe('0001-01-01');
expect(formatDateTime(yearMin, DateTimeFormat.DATE_TIME_12)).toBe('0001-01-01 12:00 AM');
expect(formatDateTime(yearMin, DateTimeFormat.STANDARD)).toBe('0001-01-01 00:00:00');
const yearMax = new Date('9999-12-31T23:59:59');
expect(formatDateTime(yearMax, DateTimeFormat.YEAR)).toBe('9999');
// Leap year
const leapYear = new Date('2024-02-29T12:00:00');
expect(formatDateTime(leapYear, DateTimeFormat.DATE)).toBe('2024-02-29');
});
// Test invalid input
test('should handle invalid date input', () => {
const invalidDate = 'not-a-date';
const result = formatDateTime(invalidDate, DateTimeFormat.STANDARD);
expect(result).toBe('Invalid Date');
});
});