-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.ts
411 lines (365 loc) · 14.8 KB
/
test.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
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
import { LocalDate, LocalWeek, LocalMonth, Quarter } from "./index";
// I don't know how to test LocalDate.fromDateInTz.
// If you figure out how to correctly mock date.protoType.toLocaleDateString()
// please add that in yourself :)
describe("LocalDate", () => {
it("should have correct Y/M/D", () => {
const date = new LocalDate(2014, 6, 14);
expect(date.year).toEqual(2014);
expect(date.month).toEqual(6);
expect(date.day).toEqual(14);
});
it("should handle extra days", () => {
const date = new LocalDate(2019, 12, 32); // December has 31 days
expect(date.year).toEqual(2020);
expect(date.month).toEqual(1);
expect(date.day).toEqual(1);
});
it("should handle negative days", () => {
const date = new LocalDate(2019, 1, -1); // two days before the first
expect(date.year).toEqual(2018);
expect(date.month).toEqual(12);
expect(date.day).toEqual(30);
});
describe("weekday", () => {
it("should treat monday as the 0th", () => {
const monday = new LocalDate(2019, 8, 19);
expect(monday.weekday).toEqual(0);
});
it("should treat sunday as the 6th", () => {
const sunday = new LocalDate(2019, 8, 18);
expect(sunday.weekday).toEqual(6);
});
});
it("should convert to date correctly", () => {
const date = new LocalDate(2019, 8, 19);
// months are zero-indexed in js!
expect(date.toDate()).toEqual(new Date(2019, 7, 19));
});
it("should handle adding days across years", () => {
const date = new LocalDate(2019, 12, 30).plusDays(3);
expect(date.year).toEqual(2020);
expect(date.month).toEqual(1);
expect(date.day).toEqual(2);
});
it("should handle subtracting days across months", () => {
const date = new LocalDate(2019, 9, 6).minusDays(8);
expect(date.year).toEqual(2019);
expect(date.month).toEqual(8);
expect(date.day).toEqual(29);
});
describe("range", () => {
test("should go small to large", () => {
const start = new LocalDate(2020, 2, 25);
const end = new LocalDate(2020, 3, 1);
expect(start.range(end)).toEqual([
new LocalDate(2020, 2, 25),
new LocalDate(2020, 2, 26),
new LocalDate(2020, 2, 27),
new LocalDate(2020, 2, 28),
new LocalDate(2020, 2, 29),
new LocalDate(2020, 3, 1),
]);
});
test("should go large to small", () => {
const start = new LocalDate(2020, 1, 4);
const end = new LocalDate(2019, 12, 28);
expect(start.range(end)).toEqual([
new LocalDate(2020, 1, 4),
new LocalDate(2020, 1, 3),
new LocalDate(2020, 1, 2),
new LocalDate(2020, 1, 1),
new LocalDate(2019, 12, 31),
new LocalDate(2019, 12, 30),
new LocalDate(2019, 12, 29),
new LocalDate(2019, 12, 28),
]);
});
test("should return single date if inputs are the same", () => {
const date = new LocalDate(2019, 8, 18);
expect(date.range(date)).toEqual([date]);
});
});
it("should convert from datestring effectively", () => {
const date = LocalDate.fromDateString("2019-06-04");
expect(date.year).toEqual(2019);
expect(date.month).toEqual(6);
expect(date.day).toEqual(4);
});
it("should convert from date correctly", () => {
// months are zero-indexed in js!
const date = LocalDate.fromDate(new Date(2021, 7, 18));
expect(date.year).toEqual(2021);
expect(date.month).toEqual(8);
expect(date.day).toEqual(18);
});
it("should convert to month", () => {
const month = new LocalDate(2020, 4, 24).toLocalMonth();
expect(month.year).toEqual(2020);
expect(month.month).toEqual(4);
expect(month.first.day).toEqual(1);
});
it("should convert to week", () => {
const thursday = new LocalDate(2019, 8, 29);
const week = thursday.toLocalWeek();
expect(week.monday).toEqual(thursday.minusDays(3));
expect(week.sunday).toEqual(thursday.plusDays(3));
});
describe("equals", () => {
it("should return true on equal days", () => {
const date = new LocalDate(2019, 8, 5);
const sameDate = new LocalDate(2019, 8, 6).minusDays(1);
expect(date.equals(sameDate)).toBe(true);
});
it("should return false on different days", () => {
const date = new LocalDate(2019, 8, 5);
const sameDate = new LocalDate(2018, 8, 5);
expect(date.equals(sameDate)).toBe(false);
});
});
describe("isBefore", () => {
it("should return true with tomorrow", () => {
expect(
new LocalDate(2019, 8, 5).isBefore(new LocalDate(2019, 8, 6))
).toBe(true);
});
it("should return false with today", () => {
const date = new LocalDate(2019, 8, 5);
expect(date.isBefore(date)).toBe(false);
});
it("should return false with last year", () => {
expect(
new LocalDate(2019, 8, 5).isBefore(new LocalDate(2018, 8, 5))
).toBe(false);
});
});
describe("after", () => {
it("should return false with tomorrow", () => {
expect(
new LocalDate(2019, 8, 5).isAfter(new LocalDate(2019, 8, 6))
).toBe(false);
});
it("should return false with today", () => {
const date = new LocalDate(2019, 8, 5);
expect(date.isAfter(date)).toBe(false);
});
it("should return true with last year", () => {
expect(
new LocalDate(2019, 8, 5).isAfter(new LocalDate(2018, 8, 5))
).toBe(true);
});
});
describe("toString", () => {
it("should format to string", () => {
const date = new LocalDate(2019, 12, 25);
expect(date.toString()).toEqual("2019-12-25");
});
it("should pad with zeros on single digit days and months", () => {
const date = new LocalDate(2019, 4, 5);
expect(date.toString()).toEqual("2019-04-05");
});
});
describe("toQuarter", () => {
it("should correctly assign quarters at date boundaries", () => {
const jan1 = new LocalDate(2019, 1, 1);
expect(jan1.toQuarter()).toEqual(new Quarter(2019, 1));
const mar31 = new LocalDate(2019, 3, 31);
expect(mar31.toQuarter()).toEqual(new Quarter(2019, 1));
const apr1 = new LocalDate(2019, 4, 1);
expect(apr1.toQuarter()).toEqual(new Quarter(2019, 2));
const jun30 = new LocalDate(2019, 6, 30);
expect(jun30.toQuarter()).toEqual(new Quarter(2019, 2));
const jul1 = new LocalDate(2019, 7, 1);
expect(jul1.toQuarter()).toEqual(new Quarter(2019, 3));
const sep30 = new LocalDate(2019, 9, 30);
expect(sep30.toQuarter()).toEqual(new Quarter(2019, 3));
const oct1 = new LocalDate(2019, 10, 1);
expect(oct1.toQuarter()).toEqual(new Quarter(2019, 4));
const dec31 = new LocalDate(2019, 12, 31);
expect(dec31.toQuarter()).toEqual(new Quarter(2019, 4));
});
});
});
describe("LocalWeek", () => {
it("should have correct monday and sunday when given a monday", () => {
const monday = new LocalDate(2019, 8, 26);
const week = new LocalWeek(monday);
expect(week.monday).toEqual(monday);
expect(week.sunday).toEqual(monday.plusDays(6));
});
it("should have correct monday and sunday when given a sunday", () => {
const sunday = new LocalDate(2019, 8, 25);
const week = new LocalWeek(sunday);
expect(week.monday).toEqual(sunday.minusDays(6));
expect(week.sunday).toEqual(sunday);
});
it("should have correct monday and sunday when given a midweek date", () => {
const friday = new LocalDate(2019, 8, 30);
const week = new LocalWeek(friday);
expect(week.monday).toEqual(friday.minusDays(4));
expect(week.sunday).toEqual(friday.plusDays(2));
});
it("should add weeks", () => {
const week = new LocalWeek(new LocalDate(2019, 8, 26));
expect(week.plusWeeks(4).monday).toEqual(week.monday.plusDays(28));
});
it("should subtract weeks", () => {
const week = new LocalWeek(new LocalDate(2019, 8, 26));
expect(week.minusWeeks(2).monday).toEqual(week.monday.minusDays(14));
});
it("should convert to days", () => {
const monday = new LocalDate(2019, 7, 29);
const week = new LocalWeek(monday);
expect(week.toDays()).toEqual([
new LocalDate(2019, 7, 29),
new LocalDate(2019, 7, 30),
new LocalDate(2019, 7, 31),
new LocalDate(2019, 8, 1),
new LocalDate(2019, 8, 2),
new LocalDate(2019, 8, 3),
new LocalDate(2019, 8, 4),
]);
});
describe("toString", () => {
it("should format to string", () => {
const monday = new LocalDate(2019, 7, 29);
const week = new LocalWeek(monday);
expect(week.toString()).toEqual("2019-07-29--2019-08-04");
});
});
});
describe("LocalMonth", () => {
it("should have correct year and month", () => {
const month = new LocalMonth(2018, 8);
expect(month.year).toEqual(2018);
expect(month.month).toEqual(8);
});
it("should handle weird months", () => {
const over = new LocalMonth(2018, 15);
expect(over.year).toEqual(2019);
expect(over.month).toEqual(3);
const under = new LocalMonth(2018, -3);
expect(under.year).toEqual(2017);
expect(under.month).toEqual(9);
});
it("should return the correct date for the first", () => {
const first = new LocalMonth(2018, 8).first;
expect(first.year).toEqual(2018);
expect(first.month).toEqual(8);
expect(first.day).toEqual(1);
});
describe("plusMonths", () => {
it("should add across years", () => {
const month = new LocalMonth(2018, 8).plusMonths(5);
expect(month.year).toEqual(2019);
expect(month.month).toEqual(1);
});
it("should subtract across years", () => {
const month = new LocalMonth(2018, 8).minusMonths(8);
expect(month.year).toEqual(2017);
expect(month.month).toEqual(12);
});
});
describe("isBefore", () => {
it("should return true with next month", () => {
expect(
new LocalMonth(2019, 8).isBefore(new LocalMonth(2019, 9))
).toBe(true);
});
it("should return false with this month", () => {
const month = new LocalMonth(2019, 8);
expect(month.isBefore(month)).toBe(false);
});
it("should return false with last year", () => {
expect(
new LocalMonth(2019, 8).isBefore(new LocalMonth(2018, 8))
).toBe(false);
});
});
describe("after", () => {
it("should return false with next month", () => {
expect(
new LocalMonth(2019, 8).isAfter(new LocalMonth(2019, 9))
).toBe(false);
});
it("should return false with this month", () => {
const month = new LocalMonth(2019, 8);
expect(month.isAfter(month)).toBe(false);
});
it("should return true with last year", () => {
expect(
new LocalMonth(2019, 8).isAfter(new LocalMonth(2018, 8))
).toBe(true);
});
});
describe("numberOfDays", () => {
it("should have correct number", () => {
expect(new LocalMonth(2018, 8).numberOfDays()).toEqual(31);
expect(new LocalMonth(2018, 6).numberOfDays()).toEqual(30);
expect(new LocalMonth(2018, 2).numberOfDays()).toEqual(28);
});
it("should know about leap years", () => {
expect(new LocalMonth(2016, 2).numberOfDays()).toEqual(29);
expect(new LocalMonth(2000, 2).numberOfDays()).toEqual(29);
expect(new LocalMonth(2100, 2).numberOfDays()).toEqual(28); // yes, this won't be a leap year
});
});
it("should start on the correct weekday", () => {
expect(new LocalMonth(2019, 8).weekdayStart()).toEqual(3);
expect(new LocalMonth(2019, 7).weekdayStart()).toEqual(0);
expect(new LocalMonth(2019, 12).weekdayStart()).toEqual(6);
});
it("should convert from localDate", () => {
const month = LocalMonth.fromLocalDate(new LocalDate(2019, 8, 17));
expect(month.year).toEqual(2019);
expect(month.month).toEqual(8);
expect(month.first.day).toEqual(1);
});
it("should convert to weeks", () => {
expect(new LocalMonth(2019, 8).toWeeks()).toEqual([
new LocalDate(2019, 7, 29).toLocalWeek(),
new LocalDate(2019, 8, 5).toLocalWeek(),
new LocalDate(2019, 8, 12).toLocalWeek(),
new LocalDate(2019, 8, 19).toLocalWeek(),
new LocalDate(2019, 8, 26).toLocalWeek(),
]);
});
it("should list for year", () => {
expect(LocalMonth.listForYear(2014)).toEqual([
new LocalMonth(2014, 1),
new LocalMonth(2014, 2),
new LocalMonth(2014, 3),
new LocalMonth(2014, 4),
new LocalMonth(2014, 5),
new LocalMonth(2014, 6),
new LocalMonth(2014, 7),
new LocalMonth(2014, 8),
new LocalMonth(2014, 9),
new LocalMonth(2014, 10),
new LocalMonth(2014, 11),
new LocalMonth(2014, 12),
]);
});
});
describe("Quarter", () => {
it("should have correct starts", () => {
const q1 = new Quarter(2021, 1);
expect(q1.start).toEqual(new LocalDate(2021, 1, 1));
const q2 = new Quarter(2021, 2);
expect(q2.start).toEqual(new LocalDate(2021, 4, 1));
const q3 = new Quarter(2021, 3);
expect(q3.start).toEqual(new LocalDate(2021, 7, 1));
const q4 = new Quarter(2021, 4);
expect(q4.start).toEqual(new LocalDate(2021, 10, 1));
});
it("should have correct ends", () => {
const q1 = new Quarter(2021, 1);
expect(q1.end).toEqual(new LocalDate(2021, 3, 31));
const q2 = new Quarter(2021, 2);
expect(q2.end).toEqual(new LocalDate(2021, 6, 30));
const q3 = new Quarter(2021, 3);
expect(q3.end).toEqual(new LocalDate(2021, 9, 30));
const q4 = new Quarter(2021, 4);
expect(q4.end).toEqual(new LocalDate(2021, 12, 31));
});
});