-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrange.test.ts
More file actions
143 lines (125 loc) · 4.71 KB
/
Copy pathrange.test.ts
File metadata and controls
143 lines (125 loc) · 4.71 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
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
import MockDate from 'mockdate';
import dayjs from 'dayjs';
import dayjsRange from '../src';
beforeEach(() => {
MockDate.set(new Date());
});
afterEach(() => {
MockDate.reset();
});
it('range isValidRange', () => {
const startDate = '2021-07-15 19:01:00';
const endDate = '2021-07-16 10:00:00';
expect(dayjsRange(startDate, null).isValidRange()).toBe(false);
expect(dayjsRange(null, startDate).isValidRange()).toBe(false);
expect(dayjsRange(startDate, endDate).isValidRange()).toBe(true);
expect(dayjsRange(endDate, startDate).isValidRange()).toBe(true);
});
it('range isEqual', () => {
const startDate = '2021-07-15 19:01:00';
const endDate = '2021-07-16 19:00:00';
const otherDate = '2021-07-17 00:00:00';
expect(
dayjsRange(startDate, endDate).isEqual(dayjsRange(startDate, endDate)),
).toBe(true);
expect(
dayjsRange(startDate, endDate).isEqual(dayjsRange(endDate, startDate)),
).toBe(true);
expect(
dayjsRange(endDate, startDate).isEqual(dayjsRange(startDate, otherDate)),
).toBe(false);
});
it('range isOverlap', () => {
const startDate = '2021-07-15 19:01:00';
const endDate = '2021-07-16 19:00:00';
const otherDate = '2021-07-17 00:00:00';
const otherDate2 = '2021-07-19 00:00:00';
expect(
dayjsRange(startDate, otherDate).isOverlap(dayjsRange(endDate, otherDate2)),
).toBe(true);
expect(
dayjsRange(endDate, otherDate2).isOverlap(dayjsRange(startDate, otherDate)),
).toBe(true);
expect(
dayjsRange(startDate, endDate).isOverlap(dayjsRange(startDate, otherDate)),
).toBe(true);
expect(
dayjsRange(startDate, otherDate).isOverlap(dayjsRange(startDate, endDate)),
).toBe(true);
expect(
dayjsRange(startDate, otherDate2).isOverlap(dayjsRange(endDate, otherDate)),
).toBe(true);
expect(
dayjsRange(endDate, otherDate).isOverlap(dayjsRange(startDate, otherDate2)),
).toBe(true);
expect(
dayjsRange(startDate, endDate).isOverlap(dayjsRange(startDate, endDate)),
).toBe(true);
expect(
dayjsRange(startDate, endDate).isOverlap(dayjsRange(otherDate, otherDate2)),
).toBe(false);
expect(
dayjsRange(otherDate, otherDate2).isOverlap(dayjsRange(startDate, endDate)),
).toBe(false);
expect(
dayjsRange(startDate, endDate).isOverlap(dayjsRange(endDate, otherDate)),
).toBe(false);
expect(
dayjsRange(endDate, otherDate).isOverlap(dayjsRange(startDate, endDate)),
).toBe(false);
expect(
dayjsRange(startDate, startDate).isOverlap(dayjsRange(startDate, endDate)),
).toBe(false);
expect(
dayjsRange(startDate, endDate).isOverlap(dayjsRange(startDate, startDate)),
).toBe(false);
expect(
dayjsRange(endDate, endDate).isOverlap(dayjsRange(startDate, endDate)),
).toBe(false);
expect(
dayjsRange(startDate, endDate).isOverlap(dayjsRange(endDate, endDate)),
).toBe(false);
expect(dayjsRange(endDate, otherDate).isOverlap()).toBe(false);
expect(
dayjsRange(startDate, endDate).isOverlap(dayjsRange(startDate, null)),
).toBe(false);
expect(
dayjsRange(startDate, endDate).isOverlap(dayjsRange(endDate, endDate)),
).toBe(false);
});
it('range get date', () => {
const startDate = '2021-07-15 19:01:00';
const endDate = '2021-07-16 19:00:00';
expect(dayjsRange(startDate, endDate).startDate).toEqual(dayjs(startDate));
expect(dayjsRange(startDate, endDate).endDate).toEqual(dayjs(endDate));
});
it('range add', () => {
const startDate = '2021-07-15 19:01:00';
const endDate = '2021-07-17 19:00:00';
const rangeBasic = dayjsRange(startDate, endDate);
const rangeAddStart = rangeBasic.addStartRange(1, 'd');
const rangeAddEnd = rangeBasic.addEndRange(1, 'd');
expect(rangeAddStart.startDate).toEqual(dayjs(startDate).add(1, 'd'));
expect(rangeAddEnd.endDate).toEqual(dayjs(endDate).add(1, 'd'));
expect(rangeBasic.startDate).toEqual(dayjs(startDate));
expect(rangeBasic.endDate).toEqual(dayjs(endDate));
});
it('range clone', () => {
const startDate = '2021-07-15 19:01:00';
const endDate = '2021-07-16 10:00:00';
const originalRange = dayjsRange(startDate, endDate);
const cloneRange = originalRange.clone();
expect(originalRange.isEqual(cloneRange)).toBe(true);
expect(originalRange.addStartRange(1, 'd').isEqual(cloneRange)).toBe(false);
});
it('range subtract', () => {
const startDate = '2021-07-15 19:01:00';
const endDate = '2021-07-17 19:00:00';
const rangeBasic = dayjsRange(startDate, endDate);
const rangeSubStart = rangeBasic.subtractStartRange(1, 'd');
const rangeSubEnd = rangeBasic.subtractEndRange(1, 'd');
expect(rangeSubStart.startDate).toEqual(dayjs(startDate).subtract(1, 'd'));
expect(rangeSubEnd.endDate).toEqual(dayjs(endDate).subtract(1, 'd'));
expect(rangeBasic.startDate).toEqual(dayjs(startDate));
expect(rangeBasic.endDate).toEqual(dayjs(endDate));
});