/
date.test.ts
500 lines (440 loc) · 15.1 KB
/
date.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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
import { isNull } from "lodash"
import moment, { Moment } from "moment-timezone"
import {
dateRange,
exhibitionStatus,
dateTimeRange,
singleDate,
singleDateWithDay,
singleDateTime,
singleTime,
formattedOpeningHours,
formattedStartDateTime,
datesAreSameDay,
timeRange,
} from "lib/date"
describe("date formatting", () => {
describe("timeRange", () => {
it("includes only one am or pm if within same am/pm", () => {
const period = timeRange(
"2022-12-30T08:00:00+00:00",
"2022-12-30T10:00:00+00:00",
"UTC"
)
expect(period).toBe("8:00 – 10:00am UTC")
})
it("includes both am and pm if not within the same am/pm", () => {
const period = timeRange(
"2021-12-05T08:00:00+00:00",
"2022-12-30T17:00:00+00:00",
"UTC"
)
expect(period).toBe("8:00am – 5:00pm UTC")
})
it("includes updated time with a specific timezone", () => {
const period = timeRange(
"2021-12-05T08:00:00+00:00",
"2022-12-30T17:00:00+00:00",
"America/New_York"
)
expect(period).toBe("3:00 – 12:00pm EST")
})
})
describe("datesAreSameDay", () => {
it("returns true if dates are the same day", () => {
const period = datesAreSameDay(
"2022-12-30T20:00:00+00:00",
"2022-12-30T17:00:00+00:00",
"UTC"
)
expect(period).toBe(true)
})
it("returns false if dates are not the same day", () => {
const period = datesAreSameDay(
"2021-12-05T20:00:00+00:00",
"2022-12-30T17:00:00+00:00",
"UTC"
)
expect(period).toBe(false)
})
it("returns true if dates are the same day with specific timezone", () => {
const period = datesAreSameDay(
"2021-12-05T20:00:00+00:00",
"2022-12-30T17:00:00+00:00",
"America/New_York"
)
expect(period).toBe(false)
})
it("returns false if dates are not the same day with specific timezone", () => {
const period = datesAreSameDay(
"2021-12-05T20:00:00+00:00",
"2022-12-30T17:00:00+00:00",
"America/New_York"
)
expect(period).toBe(false)
})
})
describe("formattedStartDateTime", () => {
const realNow = Date.now
beforeEach(() => {
Date.now = () => new Date("2018-01-30T03:24:00") as any
})
afterEach(() => {
Date.now = realNow
})
it("includes 'Starts' when event starts in the future", () => {
const period = formattedStartDateTime(
"2045-12-05T20:00:00+00:00",
"2050-12-30T17:00:00+00:00",
null,
"UTC"
)
expect(period).toBe("Starts Dec 5, 2045 at 8:00pm UTC")
})
it("includes 'Ends' when event is running and terminates in the future", () => {
const period = formattedStartDateTime(
"2017-12-05T20:00:00+00:00",
"2045-12-30T17:00:00+00:00",
null,
"UTC"
)
expect(period).toBe("Ends Dec 30, 2045 at 5:00pm UTC")
})
it("includes 'Ended on date' when event ended in the past and is now closed", () => {
const period = formattedStartDateTime(
"2016-12-05T20:00:00+00:00",
"2016-12-30T17:00:00+00:00",
null,
"UTC"
)
expect(period).toBe("Ended Dec 30, 2016")
})
it("includes 'Starts' when event starts in the future (2)", () => {
const period = formattedStartDateTime(
"2045-12-05T20:00:00+00:00",
"2050-12-30T17:00:00+00:00",
null,
"UTC"
)
expect(period).toBe("Starts Dec 5, 2045 at 8:00pm UTC")
})
it("includes 'Live' string when auction has started but live sale has not", () => {
const startAt = "2012-12-05T20:00:00+00:00"
const liveStartAt = "2045-12-05T20:00:00+00:00"
const endAt = "2045-12-05T20:00:00+00:00"
const date = formattedStartDateTime(startAt, endAt, liveStartAt, "UTC")
expect(date).toEqual("Live Dec 5, 2045 at 8:00pm UTC")
})
it("includes 'In progress' string when auction is live", () => {
const startAt = "2012-12-05T20:00:00+00:00"
const liveStartAt = "2012-12-05T20:00:00+00:00"
const endAt = "2045-12-05T20:00:00+00:00"
const date = formattedStartDateTime(startAt, endAt, liveStartAt, "UTC")
expect(date).toEqual("In progress")
})
})
describe("formattedOpeningHours", () => {
const realNow = Date.now
beforeEach(() => {
Date.now = () => new Date("2018-01-30T03:24:00") as any
})
afterEach(() => {
Date.now = realNow
})
it("includes 'Opens' when event opens in the future", () => {
const period = formattedOpeningHours(
"2045-12-05T20:00:00+00:00",
"2046-12-30T17:00:00+00:00",
"UTC"
)
expect(period).toBe("Opens Dec 5, 2045 at 8:00pm UTC")
})
it("includes 'Closes' when event is running and closes in the future", () => {
const period = formattedOpeningHours(
"2017-12-05T20:00:00+00:00",
"2045-12-30T17:00:00+00:00",
"UTC"
)
expect(period).toBe("Closes Dec 30, 2045 at 5:00pm UTC")
})
it("includes 'Closed' when event ended in the past and is closed", () => {
const period = formattedOpeningHours(
"2016-12-05T20:00:00+00:00",
"2016-12-30T17:00:00+00:00",
"UTC"
)
expect(period).toBe("Closed")
})
})
describe("singleTime", () => {
it("includes hour using default UTC timezone", () => {
const period = singleTime("2018-12-05T20:00:00+00:00", "UTC")
expect(period).toBe("8:00pm UTC")
})
it("also includes hour using specific timezone", () => {
const period = singleTime("2018-12-05T20:00:00+00:00", "America/New_York")
expect(period).toBe("3:00pm EST")
})
})
describe("singleDateTime", () => {
const realNow = Date.now
beforeEach(() => {
Date.now = () => new Date("2018-01-30T03:24:00") as any
})
afterEach(() => {
Date.now = realNow
})
it("includes only the month day and time for dates in the current year", () => {
const period = singleDateTime("2018-12-05T20:00:00+00:00", "UTC")
expect(period).toBe("Dec 5 at 8:00pm UTC")
})
it("also includes the year for dates in a different year from now", () => {
const period = singleDateTime("2021-12-05T20:00:00+00:00", "UTC")
expect(period).toBe("Dec 5, 2021 at 8:00pm UTC")
})
it("returns the dates with a specific timezone", () => {
const period = singleDateTime(
"2021-12-05T20:00:00+00:00",
"America/Costa_Rica"
)
expect(period).toBe("Dec 5, 2021 at 2:00pm CST")
})
})
describe("singleDate", () => {
const realNow = Date.now
beforeEach(() => {
Date.now = () => new Date("2018-01-30T03:24:00") as any
})
afterEach(() => {
Date.now = realNow
})
it("includes single date with month day when in current year", () => {
const period = singleDate("2018-12-05T20:00:00+00:00", "UTC")
expect(period).toBe("Dec 5")
})
it("also includes the year when not in current year", () => {
const period = singleDate("2021-12-05T20:00:00+00:00", "UTC")
expect(period).toBe("Dec 5, 2021")
})
})
describe("singleDateWithDay", () => {
const realNow = Date.now
beforeEach(() => {
Date.now = () => new Date("2018-01-30T03:24:00") as any
})
afterEach(() => {
Date.now = realNow
})
it("includes single date with only day of week and month day when in current year", () => {
const period = singleDateWithDay("2018-12-05T20:00:00+00:00", "UTC")
expect(period).toBe("Wed, Dec 5")
})
it("also includes the year when not in current year", () => {
const period = singleDateWithDay("2021-12-05T20:00:00+00:00", "UTC")
expect(period).toBe("Sun, Dec 5, 2021")
})
})
describe("dateTimeRange", () => {
const realNow = Date.now
beforeEach(() => {
Date.now = () => new Date("2018-01-30T03:24:00") as any
})
afterEach(() => {
Date.now = realNow
})
describe("if displayDayOfWeek param is false (the default), does not include day of week", () => {
it("includes month day, hour, and both years when years are different", () => {
const period = dateTimeRange(
"2021-12-05T20:00:00+00:00",
"2022-12-30T17:00:00+00:00",
"UTC"
)
expect(period).toBe(
"Dec 5, 2021 at 8:00pm – Dec 30, 2022 at 5:00pm UTC"
)
})
it("also includes both years when the years are not the present year", () => {
const period = dateTimeRange(
"2040-12-05T20:00:00+00:00",
"2040-12-30T17:00:00+00:00",
"UTC"
)
expect(period).toBe(
"Dec 5, 2040 at 8:00pm – Dec 30, 2040 at 5:00pm UTC"
)
})
it("does not include year when the years are the present year", () => {
const period = dateTimeRange(
"2018-12-05T20:00:00+00:00",
"2018-12-30T17:00:00+00:00",
"UTC"
)
expect(period).toBe("Dec 5 at 8:00pm – Dec 30 at 5:00pm UTC")
})
it("includes the year when both dates are the same day but not the current year", () => {
const period = dateTimeRange(
"2019-12-05T20:00:00+00:00",
"2019-12-05T22:00:00+00:00",
"UTC"
)
expect(period).toBe("Dec 5, 2019 from 8:00 – 10:00pm UTC")
})
it("does not include year when both dates are same day and the current year", () => {
const period = dateTimeRange(
"2018-12-05T20:00:00+00:00",
"2018-12-05T22:00:00+00:00",
"UTC"
)
expect(period).toBe("Dec 5 from 8:00 – 10:00pm UTC")
})
})
describe("if displayDayOfWeek param is true includes day of week", () => {
it("includes month day, hour, and both years when years are different", () => {
const period = dateTimeRange(
"2021-12-05T20:00:00+00:00",
"2022-12-30T17:00:00+00:00",
"UTC",
true
)
expect(period).toBe(
"Sun, Dec 5, 2021 at 8:00pm – Fri, Dec 30, 2022 at 5:00pm UTC"
)
})
it("also includes both years when the years are not the present year", () => {
const period = dateTimeRange(
"2020-12-05T20:00:00+00:00",
"2020-12-30T17:00:00+00:00",
"UTC",
true
)
expect(period).toBe(
"Sat, Dec 5, 2020 at 8:00pm – Wed, Dec 30, 2020 at 5:00pm UTC"
)
})
it("does not include year when the years are the present year", () => {
const period = dateTimeRange(
"2018-12-05T20:00:00+00:00",
"2018-12-30T17:00:00+00:00",
"UTC",
true
)
expect(period).toBe("Wed, Dec 5 at 8:00pm – Sun, Dec 30 at 5:00pm UTC")
})
it("includes the year when both dates are the same day but not the current year", () => {
const period = dateTimeRange(
"2019-12-05T20:00:00+00:00",
"2019-12-05T22:00:00+00:00",
"UTC",
true
)
expect(period).toBe("Thu, Dec 5, 2019 from 8:00 – 10:00pm UTC")
})
it("does not include the year when the dates are the same day and the same year as the present year", () => {
const period = dateTimeRange(
"2018-12-05T20:00:00+00:00",
"2018-12-05T22:00:00+00:00",
"UTC",
true
)
expect(period).toBe("Wed, Dec 5 from 8:00 – 10:00pm UTC")
})
it("returns the dates with a specific timezone", () => {
const period = dateTimeRange(
"2018-12-05T20:00:00+00:00",
"2018-12-05T22:00:00+00:00",
"America/Costa_Rica",
true
)
expect(period).toBe("Wed, Dec 5 from 2:00 – 4:00pm CST")
})
})
})
describe("dateRange", () => {
it("includes month day and both years when years are different years", () => {
const period = dateRange("2011-01-01", "2014-04-19", "UTC")
expect(period).toBe("Jan 1, 2011 – Apr 19, 2014")
})
it("includes month twice when years are different even if the same month", () => {
const period = dateRange("2011-01-01", "2014-01-04", "UTC")
expect(period).toBe("Jan 1, 2011 – Jan 4, 2014")
})
it("only includes the year once if the dates have the same year", () => {
const period = dateRange("2011-01-01", "2011-04-19", "UTC")
expect(period).toBe("Jan 1 – Apr 19, 2011")
})
it("include only one month if dates have same month and same year", () => {
const period = dateRange("2011-01-01", "2011-01-19", "UTC")
expect(period).toBe("Jan 1 – 19, 2011")
})
it("does not include the year if both years are the same as the present year", () => {
const period = dateRange(
moment.tz("UTC").format("YYYY-01-01"),
moment.tz("UTC").format("YYYY-01-19"),
"UTC"
)
expect(period).toBe("Jan 1 – 19")
})
})
describe("exhibitionStatus", () => {
let today: Moment = null as any
beforeEach(() => {
today = moment()
})
describe("before an exhibition opens", () => {
let future: Moment = null as any
beforeEach(() => {
future = today.clone().add(1, "M")
})
it("states that an exhibition opens today", () => {
const status = exhibitionStatus(today, future, "UTC")
expect(status).toBe("Opening today")
})
it("states that an exhibition opens tomorrow", () => {
const status = exhibitionStatus(today.add(1, "d"), future, "UTC")
expect(status).toBe("Opening tomorrow")
})
it("states that an exhibition opens in a few days", () => {
for (let days = 2; days <= 5; days++) {
const status = exhibitionStatus(
today.clone().add(days, "d"),
future,
"UTC"
)
expect(status).toBe(`Opening in ${days} days`)
}
})
it("returns nothing when it opens in more than a few days", () => {
const status = exhibitionStatus(today.add(6, "d"), future, "UTC")
expect(isNull(status)).toBe(true)
})
})
describe("before an exhibition closes", () => {
let past: Moment = null as any
beforeEach(() => {
past = today.clone().subtract(1, "M")
})
it("states that an exhibition will close today", () => {
const status = exhibitionStatus(past, today, "UTC")
expect(status).toBe("Closing today")
})
it("states that an exhibition will close tomorrow", () => {
const status = exhibitionStatus(past, today.add(1, "d"), "UTC")
expect(status).toBe("Closing tomorrow")
})
it("states that an exhibition is about to close in a few days", () => {
for (let days = 2; days <= 5; days++) {
const status = exhibitionStatus(
past,
today.clone().add(days, "d"),
"UTC"
)
expect(status).toBe(`Closing in ${days} days`)
}
})
it("returns nothing when it closes in more than a few days", () => {
const status = exhibitionStatus(past, today.add(6, "d"), "UTC")
expect(isNull(status)).toBe(true)
})
})
})
})