-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgetStartTimeStamp.spec.ts
64 lines (56 loc) · 1.78 KB
/
getStartTimeStamp.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
import { startOfDay, startOfWeek } from 'date-fns'
import * as path from 'path'
import { getStartTimeStamp } from './getStartTimeStamp'
const now = new Date('2022-09-26T09:49:13.236Z') // was: 40 is now 39
const dataFolder = (name: string) =>
path.join(process.cwd(), 'test-data', 'getStartTimeStamp', name)
describe('getStartTimeStamp()', () => {
it('should return the latest timestamp based on the Strava data', async () =>
expect(
await getStartTimeStamp({
dataFolder: dataFolder('multiple-weeks'),
now,
}),
).toEqual(1664178000))
it('should return midnight of today if there is no previous data', async () => {
const midnightOfToday = startOfDay(now).getTime()
expect(
await getStartTimeStamp({
dataFolder: dataFolder('blank'),
now,
}),
).toEqual(midnightOfToday)
})
it('should return midnight of today if the JSON does not contain a timestamp', async () => {
const midnightOfToday = startOfDay(now).getTime()
expect(
await getStartTimeStamp({
dataFolder: dataFolder('invalid-json'),
now,
}),
).toEqual(midnightOfToday)
})
it('should return start of the week if the week folder exists, but has not data', async () => {
const startOfTheWeek = startOfWeek(now).getTime()
expect(
await getStartTimeStamp({
dataFolder: dataFolder('no-json-in-week-folder'),
now,
}),
).toEqual(startOfTheWeek)
})
it('should return the timestamp from last week, if the current week folder is empty', async () =>
expect(
await getStartTimeStamp({
dataFolder: dataFolder('current-week-empty'),
now,
}),
).toEqual(1664148498))
it('should not go back beyond week 1', async () =>
expect(
getStartTimeStamp({
dataFolder: dataFolder('new-year'),
now,
}),
).rejects.toThrow(/Strava challenge does not run in Winter!/))
})