-
-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathtime.spec.js
More file actions
289 lines (255 loc) · 10.8 KB
/
Copy pathtime.spec.js
File metadata and controls
289 lines (255 loc) · 10.8 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
const { Instant, ZonedDateTime } = require('./java.mock');
const time = require('../src/time');
jest.mock('../src/items', () => ({
Item: new Object() // eslint-disable-line no-new-object
}));
jest.mock('../src/osgi', () => ({
getService: () => ({
getTimeZone: () => ({
getId: () => 'Europe/Paris'
})
})
}));
describe('time.js', () => {
describe('toZDT', () => {
it('passes through if when is a time.ZonedDateTime', () => {
const zdt = time.ZonedDateTime.now();
expect(time.toZDT(zdt)).toBe(zdt);
});
it('converts if when is a time.Instant', () => {
const instant = time.Instant.now();
expect(time.toZDT(instant)).toStrictEqual(instant.atZone(time.ZoneId.systemDefault()));
});
it('delegates to parseString', () => {
const when = 'when';
expect(() => time.toZDT(when)).toThrow('Failed to parse string when: DateTimeParseException: Text cannot be parsed to a Duration: when, at index: 0');
});
// TODO: Add remaining possible cases for when
});
describe('toInstant', () => {
it('passes through if when is a time.Instant', () => {
const instant = time.Instant.now();
expect(time.toInstant(instant)).toBe(instant);
});
it('converts if when is a time.ZonedDateTime', () => {
const zdt = time.ZonedDateTime.now();
expect(time.toInstant(zdt)).toStrictEqual(zdt.toInstant());
});
it('converts if when is a string', () => {
const instant = time.Instant.now();
const string = instant.toString();
expect(time.toInstant(string)).toStrictEqual(instant);
});
it('converts if when is epoch millis', () => {
const instant = time.Instant.now();
const millis = instant.toEpochMilli();
expect(time.toInstant(millis)).toStrictEqual(instant);
});
// TODO: Add remaining possible cases for when
});
describe('parseString', () => {
const parseString = time._parseString;
describe('accepts ISO patterns without the "T" for Blockly', () => {
it.each([
['YYYY-MM-DD hh:mm:ss.f+hh:mm', '2016-03-18 12:38:23.561+01:00'],
['YYYY-MM-DD hh:mm:ssZ', '2022-12-24 18:30:35Z'],
['YYYY-MM-DD hh:mm:ss+hh:mm[timezone]', '2017-02-04 17:01:15.846+01:00[Europe/Paris]'],
['YYYY-MM-DD hh:mm', '2022-12-24 18:30'],
['YYYY-MM-DD hh:mm:ss', '2022-12-24 18:30:00'],
['YYYY-MM-DD hh:mm:ss.f', '2022-12-24 18:30:00.5363']
])('accepts pattern %s', (pattern, isoStr) => {
expect(parseString(isoStr)).toBeInstanceOf(time.ZonedDateTime);
});
});
});
describe('parseISO8601', () => {
const parseISO8601 = time._parseISO8601;
describe('parses ISO Date', () => {
it('accepts correct pattern YYYY-MM-DD', () => {
expect(parseISO8601('2022-12-24')).toBeInstanceOf(time.ZonedDateTime);
});
});
describe('parses ISO Time', () => {
it.each([
['hh:mm', '18:00'],
['hh:mm:ss', '18:00:00'],
['hh:mm:ss.f', '18:00:00.4656']
])('accepts correct pattern %s', (pattern, isoStr) => {
expect(parseISO8601(isoStr)).toBeInstanceOf(time.ZonedDateTime);
});
});
describe('parses ISO DateTime', () => {
it.each([
['YYYY-MM-DDThh:mm', '2022-12-24T18:30'],
['YYYY-MM-DDThh:mm:ss', '2022-12-24T18:30:00'],
['YYYY-MM-DDThh:mm:ss.f', '2022-12-24T18:30:00.5363']
])('accepts correct pattern %s', (pattern, isoStr) => {
expect(parseISO8601(isoStr)).toBeInstanceOf(time.ZonedDateTime);
});
});
describe('parses ISO DateTime with zone offset and timezone', () => {
it.each([
['YYYY-MM-DDThh:mm:ss.f+HH:mm[SYSTEM]', '2016-03-18T12:38:23.561+01:00[SYSTEM]'],
['YYYY-MM-DDThh:mm:ssZ', '2022-12-24T18:30:35Z'],
['YYYY-MM-DDThh:mm:ss+HH:mm[timezone]', '2017-02-04T17:01:15.846+01:00[Europe/Paris]'],
['YYYY-MM-DDThh:mm:ss+HH:mm[timezone]', '2016-03-18T06:38:23.561-05:00[UTC-05:00]']
])('accepts correct pattern %s', (pattern, isoStr) => {
expect(parseISO8601(isoStr)).toBeInstanceOf(time.ZonedDateTime);
});
});
describe('parses ISO DateTime with zone offset and without time zone and defaults to SYSTEM timezone', () => {
it.each([
['YYYY-MM-DDThh:mm:ss.f+HH:mm', '2016-03-18T12:38:23.561+01:00'],
['YYYY-MM-DDThh:mm:ss.f-HH:mm', '2016-03-18T12:38:23.56-04:30'],
['YYYY-MM-DDThh:mm:ss.f+HHmm', '2016-03-18T12:38:23.561+0100'],
['YYYY-MM-DDThh:mm:ss.f-HHmm', '2016-03-18T12:38:23.561-0430']
])('accepts correct pattern %s', (pattern, isoStr) => {
const zdt = parseISO8601(isoStr);
expect(zdt).toBeInstanceOf(time.ZonedDateTime);
expect(zdt.zone().id()).toBe(time.ZoneId.systemDefault().id());
});
});
describe('rejects wrong ISO pattern', () => {
it.each([
['hh:mm:ss,f', '18:00:00,4654'],
['hh:mm:s', '18:30:0'],
['hh:m', '18:3'],
['h', '6'],
['YYYY-MM-DDhh:mm:ss', '2022-12-2418:30:00'],
['YYYY-MM-DDThh', '2022-12-24T18']
])('%s', (pattern, isoStr) => {
expect(parseISO8601(isoStr)).toBe(null);
});
});
});
describe('javaInstantToJsInstant', () => {
it('returns JS-Joda Instant', () => {
expect(time.javaInstantToJsInstant(new Instant())).toBeInstanceOf(time.Instant);
});
});
describe('javaZDTToJsZDT', () => {
it('returns JS-Joda ZonedDateTime', () => {
expect(time.javaZDTToJsZDT(new ZonedDateTime())).toBeInstanceOf(time.ZonedDateTime);
});
});
describe('ZonedDateTime', () => {
describe('polyfilled method', () => {
it('toToday works', () => {
const oldZdt = time.ZonedDateTime.parse('2005-01-21T02:13:35Z');
const newZdt = oldZdt.toToday();
expect(newZdt.toLocalDate().toString()).toBe(time.LocalDate.now().toString());
});
describe('isBeforeTime and isAfterTime', () => {
const zdt1 = time.toZDT('14:30');
const zdt2 = time.toZDT('16:30');
it('returns true if before/after', () => {
expect(zdt1.isBeforeTime(zdt2)).toBe(true);
expect(zdt2.isAfterTime(zdt1)).toBe(true);
});
it('returns false if not before/after', () => {
expect(zdt2.isBeforeTime(zdt1)).toBe(false);
expect(zdt1.isBeforeTime(zdt1)).toBe(false);
expect(zdt1.isAfterTime(zdt2)).toBe(false);
expect(zdt1.isAfterTime(zdt1)).toBe(false);
});
});
describe('isBeforeDate and isAfterDate', () => {
const zdt1 = time.toZDT('2022-12-01');
const zdt2 = time.toZDT('2022-12-06');
it('returns true if before/after', () => {
expect(zdt1.isBeforeDate(zdt2)).toBe(true);
expect(zdt2.isAfterDate(zdt1)).toBe(true);
});
it('returns false if not before/after', () => {
expect(zdt2.isBeforeDate(zdt1)).toBe(false);
expect(zdt1.isBeforeDate(zdt1)).toBe(false);
expect(zdt1.isAfterDate(zdt2)).toBe(false);
expect(zdt1.isAfterDate(zdt1)).toBe(false);
});
});
describe('isBeforeDateTime and isAfterDateTime', () => {
const zdt1 = time.toZDT('2022-12-01T14:30Z');
const zdt2 = time.toZDT('2022-12-01T16:30Z');
const zdt3 = time.toZDT('2022-12-02T16:30Z');
it('returns true if before/after', () => {
expect(zdt1.isBeforeDateTime(zdt2)).toBe(true);
expect(zdt2.isBeforeDateTime(zdt3)).toBe(true);
expect(zdt2.isAfterDateTime(zdt1)).toBe(true);
expect(zdt3.isAfterDateTime(zdt2)).toBe(true);
});
it('returns false if not before/after', () => {
expect(zdt2.isBeforeDateTime(zdt1)).toBe(false);
expect(zdt3.isBeforeDateTime(zdt2)).toBe(false);
expect(zdt1.isBeforeDateTime(zdt1)).toBe(false);
expect(zdt1.isAfterDateTime(zdt2)).toBe(false);
expect(zdt2.isAfterDateTime(zdt3)).toBe(false);
expect(zdt1.isAfterDateTime(zdt1)).toBe(false);
});
});
describe('isBetweenTimes', () => {
const zdt1 = time.toZDT('14:30');
const zdt2 = time.toZDT('16:30');
const zdt3 = time.toZDT('18:30');
const zdt4 = time.toZDT('0:30');
describe('it returns true if is between', () => {
it('without spanning over midnight', () => {
expect(zdt2.isBetweenTimes(zdt1, zdt3)).toBe(true);
});
it('with spanning over midnight', () => {
expect(zdt3.isBetweenTimes(zdt2, zdt4)).toBe(true);
});
});
describe('it returns false if is not between', () => {
it('without spanning over midnight', () => {
expect(zdt1.isBetweenTimes(zdt2, zdt3)).toBe(false);
expect(zdt3.isBetweenTimes(zdt1, zdt2)).toBe(false);
});
it('with spanning over midnight', () => {
expect(zdt1.isBetweenTimes(zdt2, zdt4)).toBe(false);
});
});
});
describe('isBetweenDates', () => {
const date1 = time.toZDT('2022-12-01');
const date2 = time.toZDT('2022-12-24');
const date3 = time.toZDT('2022-12-26');
it('returns true if is between', () => {
expect(date2.isBetweenDates(date1, date3)).toBe(true);
});
it('returns false is is not between', () => {
expect(date1.isBetweenDates(date2, date3)).toBe(false);
expect(date3.isBetweenDates(date1, date2)).toBe(false);
});
});
describe('isBetweenDateTimes', () => {
const dateTime1 = time.toZDT('2022-12-01T14:30Z');
const dateTime2 = time.toZDT('2022-12-01T18:30Z');
const dateTime3 = time.toZDT('2022-12-24T18:30Z');
it('returns true if is between', () => {
expect(dateTime2.isBetweenDateTimes(dateTime1, dateTime3)).toBe(true);
});
it('returns false if is not between', () => {
expect(dateTime1.isBetweenDateTimes(dateTime1, dateTime2)).toBe(false);
expect(dateTime3.isBetweenDateTimes(dateTime1, dateTime2)).toBe(false);
});
});
describe('isClose', () => {
const now = time.ZonedDateTime.now();
const zdt = time.ZonedDateTime.now().plusHours(6);
it('returns true if the difference between this and time.ZonedDateTime is within time.Duration', () => {
const duration = time.Duration.ofHours(12);
expect(now.isClose(zdt, duration)).toBe(true);
});
it('returns false if the difference between this and time.ZonedDateTime is not within time.Duration', () => {
const duration = time.Duration.ofHours(3);
expect(now.isClose(zdt, duration)).toBe(false);
});
});
it('getMillisFromNow works', () => {
const now = time.ZonedDateTime.now().plusSeconds(10);
expect(now.getMillisFromNow()).toBeGreaterThan(9000);
expect(now.getMillisFromNow()).toBeLessThan(11000);
});
});
});
});