-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdate.spec.ts
148 lines (117 loc) · 5.29 KB
/
date.spec.ts
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import dayjs from 'dayjs';
import {
rangeSimpleDate,
simpleDate,
simpleDateTime,
formatSecondToString,
formatTotalSeconds,
millisecondToSeconds, dayToMillisecond, hourToMillisecond, minuteToMillisecond
} from './date';
describe('simpleDateTime', () => {
it('should return a formatted date time string with year truncated for current year', () => {
const currentYear = dayjs().year();
const inputDate = `${currentYear}-03-20T10:02:00`;
expect(simpleDateTime(inputDate)).toBe('03/20 10:02');
});
});
describe('simpleDate', () => {
it('should return a formatted date string with year truncated for current year', () => {
const currentYear = dayjs().year();
const inputDate = `${currentYear}-03-20T00:00:00.000Z`;
expect(simpleDate(inputDate)).toBe('03/20');
});
});
describe('rangeSimpleDate', () => {
const currentYear = dayjs().year();
const lastYear = currentYear - 1;
const nextYear = currentYear + 1;
// 動態生成的日期
const inputStartDate = `${currentYear}-03-20`;
const inputEndDate = `${currentYear}-04-20`;
const inputNextYearDate = `${nextYear}-08-15`;
const inputLastYearDate = `${lastYear}-10-13`;
it('should return a formatted date string with year truncated for same date and current year', () => {
const testDate = `${currentYear}-10-13`;
expect(rangeSimpleDate(testDate, testDate)).toBe('10/13');
});
it('should return a formatted date string with year for different dates across years', () => {
expect(rangeSimpleDate(inputLastYearDate, `${currentYear}-10-14`)).toBe(`${lastYear}/10/13 - ${currentYear}/10/14`);
});
it('should return a formatted date string with year truncated for different dates in the same current year', () => {
expect(rangeSimpleDate(inputStartDate, inputEndDate)).toBe('03/20 - 04/20');
});
it('should return a formatted date string with year for the same date but not current year', () => {
expect(rangeSimpleDate(inputLastYearDate, inputLastYearDate)).toBe(`${lastYear}/10/13`);
});
it('should return a formatted date string for a range from last year to current year', () => {
expect(rangeSimpleDate(inputLastYearDate, `${lastYear}-08-15`)).toBe(`${lastYear}/10/13 - 08/15`);
});
it('should return a formatted date string for a range from current year to next year', () => {
expect(rangeSimpleDate(inputStartDate, inputNextYearDate)).toBe(`${currentYear}/03/20 - ${nextYear}/08/15`);
});
it('should return a formatted date string for only end date in last year', () => {
expect(rangeSimpleDate('', inputLastYearDate)).toBe(`${lastYear}/10/13`);
});
it('should return an empty string for null dates', () => {
expect(rangeSimpleDate(null, null)).toBe('');
});
it('should return an empty string for undefined dates', () => {
expect(rangeSimpleDate(undefined, undefined)).toBe('');
});
it('should return an empty string for 0000-00-00 dates', () => {
expect(rangeSimpleDate('0000-00-00', '0000-00-00')).toBe('');
});
});
describe('formatTotalSeconds', () => {
it('should return a formatted 121 second', () => {
expect(formatTotalSeconds(121)).toEqual({days: 0, hours: 0, minutes: 2, seconds: 1});
});
it('should return a formatted 36000 second to HH:mm:ss', () => {
expect(formatTotalSeconds(36000)).toEqual({days: 0, hours: 10, minutes: 0, seconds: 0});
});
it('should return a formatted 104400 second not isCountDay', () => {
expect(formatTotalSeconds(104400)).toEqual({days: 0, hours: 29, minutes: 0, seconds: 0});
});
it('should return a formatted 104400 second isCountDay', () => {
expect(formatTotalSeconds(104400, true)).toEqual({days: 1, hours: 5, minutes: 0, seconds: 0});
});
});
describe('formatSecondToString', () => {
it('should return a formatted 121 second to HH:mm:ss', () => {
expect(formatSecondToString(121)).toBe('00:02:01');
});
it('should return a formatted 121 second to mm:ss (isVisibleMinimumUnitOnly)', () => {
expect(formatSecondToString(121, true)).toBe('02:01');
expect(formatSecondToString(120, true)).toBe('02:00');
expect(formatSecondToString(36001, true)).toBe('10:00:01');
});
it('should return a formatted 36000 second to HH:mm:ss', () => {
expect(formatSecondToString(36000)).toBe('10:00:00');
});
it('should return a formatted 104400 second to HH:mm:ss', () => {
expect(formatSecondToString(104400)).toBe('29:00:00');
});
});
describe('millisecondToSeconds', () => {
it('should 3000 millisecond to 3 sec', () => {
expect(millisecondToSeconds(3000)).toEqual(3);
});
it('should 3500 millisecond to 3 sec', () => {
expect(millisecondToSeconds(3500)).toEqual(3.5);
});
});
describe('minuteToMillisecond', () => {
it('should 1 minute to 60000 sec', () => {
expect(minuteToMillisecond(1)).toEqual(60000);
});
});
describe('hourToMillisecond', () => {
it('should 1 hour to 3600000 sec', () => {
expect(hourToMillisecond(1)).toEqual(3600000);
});
});
describe('dayToMillisecond', () => {
it('should 1 millisecond to 86400000 millisecond', () => {
expect(dayToMillisecond(1)).toEqual(86400000);
});
});