Skip to content

Commit

Permalink
Fixed test
Browse files Browse the repository at this point in the history
  • Loading branch information
apata committed Aug 27, 2024
1 parent 9e42bff commit 11ca19b
Showing 1 changed file with 133 additions and 23 deletions.
156 changes: 133 additions & 23 deletions assets/js/dashboard/util/date.test.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,142 @@
/** @format */

import { formatISO, nowForSite, yesterday } from './date'
import { formatISO, nowForSite, shiftMonths, yesterday } from './date'

jest.useFakeTimers()

/* prettier-ignore */
const dstChangeOverDayEstonia = [
['2024-03-30T21:00:00.000Z', '2024-03-30', '2024-03-29', '7200'],
['2024-03-30T22:00:00.000Z', '2024-03-31', '2024-03-30', '7200'], // start of 31st March, 00:00
['2024-03-30T23:00:00.000Z', '2024-03-31', '2024-03-30', '7200'],
['2024-03-31T00:00:00.000Z', '2024-03-31', '2024-03-30', '7200'],
['2024-03-31T01:00:00.000Z', '2024-03-31', '2024-03-30', '7200'],
['2024-03-31T02:00:00.000Z', '2024-03-31', '2024-03-30', '7200'],
['2024-03-31T03:00:00.000Z', '2024-03-31', '2024-03-30', '10800'],
['2024-03-31T04:00:00.000Z', '2024-03-31', '2024-03-30', '10800'],
// system time today yesterday today-2mo today+2mo today-12mo offset
[ '2024-03-30T21:00:00.000Z', '2024-03-30', '2024-03-29', '2024-01-30', '2024-05-30', '2023-03-30', '7200' ],
[ '2024-03-30T22:00:00.000Z', '2024-03-31', '2024-03-30', '2024-01-31', '2024-05-31', '2023-03-31', '7200' ],
[ '2024-03-30T23:00:00.000Z', '2024-03-31', '2024-03-30', '2024-01-31', '2024-05-31', '2023-03-31', '7200' ],
[ '2024-03-31T00:00:00.000Z', '2024-03-31', '2024-03-30', '2024-01-31', '2024-05-31', '2023-03-31', '7200' ],
[ '2024-03-31T01:00:00.000Z', '2024-03-31', '2024-03-30', '2024-01-31', '2024-05-31', '2023-03-31', '7200' ],
[ '2024-03-31T02:00:00.000Z', '2024-03-31', '2024-03-30', '2024-01-31', '2024-05-31', '2023-03-31', '7200' ],
[ '2024-03-31T03:00:00.000Z', '2024-03-31', '2024-03-30', '2024-01-31', '2024-05-31', '2023-03-31', '10800' ],
[ '2024-03-31T04:00:00.000Z', '2024-03-31', '2024-03-30', '2024-01-31', '2024-05-31', '2023-03-31', '10800' ],
// ...
['2024-03-31T20:00:00.000Z', '2024-03-31', '2024-03-30', '10800'],
['2024-03-31T21:00:00.000Z', '2024-03-31', '2024-03-30', '10800'],
['2024-03-31T22:00:00.000Z', '2024-04-01', '2024-03-31', '10800'], // start of 1st of April, 00:00
['2024-03-31T23:00:00.000Z', '2024-04-01', '2024-03-31', '10800']
[ '2024-03-31T20:00:00.000Z', '2024-03-31', '2024-03-30', '2024-01-31', '2024-05-31', '2023-03-31', '10800' ],
[ '2024-03-31T21:00:00.000Z', '2024-04-01', '2024-03-31', '2024-02-01', '2024-06-01', '2023-04-01', '10800' ],
[ '2024-03-31T22:00:00.000Z', '2024-04-01', '2024-03-31', '2024-02-01', '2024-06-01', '2023-04-01', '10800' ],
[ '2024-03-31T23:00:00.000Z', '2024-04-01', '2024-03-31', '2024-02-01', '2024-06-01', '2023-04-01', '10800' ]
]

it.each(dstChangeOverDayEstonia)(
'at system time %s in Estonia, today is %s and yesterday is %s',
(unixTime, expectedToday, expectedYesterday, offset) => {
jest.setSystemTime(new Date(unixTime))
expect({
today: formatISO(nowForSite({ offset })),
yesterday: formatISO(yesterday({ offset }))
}).toEqual({ today: expectedToday, yesterday: expectedYesterday })
}
)
/* prettier-ignore */
const dstChangeOverDayGermany = [
// system time today yesterday today-2mo today+2mo today-12mo offset
[ '2024-03-30T21:00:00.000Z', '2024-03-30', '2024-03-29', '2024-01-30', '2024-05-30', '2023-03-30', '3600' ],
[ '2024-03-30T22:00:00.000Z', '2024-03-30', '2024-03-29', '2024-01-30', '2024-05-30', '2023-03-30', '3600' ],
[ '2024-03-30T23:00:00.000Z', '2024-03-31', '2024-03-30', '2024-01-31', '2024-05-31', '2023-03-31', '3600' ],
[ '2024-03-31T00:00:00.000Z', '2024-03-31', '2024-03-30', '2024-01-31', '2024-05-31', '2023-03-31', '3600' ],
[ '2024-03-31T01:00:00.000Z', '2024-03-31', '2024-03-30', '2024-01-31', '2024-05-31', '2023-03-31', '3600' ],
[ '2024-03-31T02:00:00.000Z', '2024-03-31', '2024-03-30', '2024-01-31', '2024-05-31', '2023-03-31', '3600' ],
[ '2024-03-31T03:00:00.000Z', '2024-03-31', '2024-03-30', '2024-01-31', '2024-05-31', '2023-03-31', '7200' ],
[ '2024-03-31T04:00:00.000Z', '2024-03-31', '2024-03-30', '2024-01-31', '2024-05-31', '2023-03-31', '7200' ],
// ...
[ '2024-03-31T20:00:00.000Z', '2024-03-31', '2024-03-30', '2024-01-31', '2024-05-31', '2023-03-31', '7200' ],
[ '2024-03-31T21:00:00.000Z', '2024-03-31', '2024-03-30', '2024-01-31', '2024-05-31', '2023-03-31', '7200' ],
[ '2024-03-31T22:00:00.000Z', '2024-04-01', '2024-03-31', '2024-02-01', '2024-06-01', '2023-04-01', '7200' ],
[ '2024-03-31T23:00:00.000Z', '2024-04-01', '2024-03-31', '2024-02-01', '2024-06-01', '2023-04-01', '7200' ]
]

const sets = [
['Europe/Tallinn', dstChangeOverDayEstonia],
['Europe/Berlin', dstChangeOverDayGermany]
] as const

for (const [timezone, suite] of sets) {
describe(`${timezone} (relying on offset)`, () => {
it.each(suite)(
'handles system time %s, today is %s and yesterday is %s',
(
unixTime,
expectedToday,
expectedYesterday,
expectedTwoMonthsBack,
expectedTwoMonthsAhead,
expectedOneYearBack,
offset
) => {
jest.setSystemTime(new Date(unixTime))
expect({
today: formatISO(nowForSite({ offset })),
yesterday: formatISO(yesterday({ offset })),
twoMonthsBack: formatISO(shiftMonths(nowForSite({ offset }), -2)),
twoMonthsAhead: formatISO(shiftMonths(nowForSite({ offset }), 2)),
oneYearBack: formatISO(shiftMonths(nowForSite({ offset }), -12))
}).toEqual({
today: expectedToday,
yesterday: expectedYesterday,
twoMonthsBack: expectedTwoMonthsBack,
twoMonthsAhead: expectedTwoMonthsAhead,
oneYearBack: expectedOneYearBack
})
}
)
})
}

for (const [timezone, suite] of sets) {
describe(`${timezone} alternative`, () => {
it.each(suite)(
'at system time %s, today is %s and yesterday is %s',
(
unixTime,
expectedToday,
expectedYesterday,
expectedTwoMonthsBack,
expectedTwoMonthsAhead,
expectedOneYearBack
) => {
jest.setSystemTime(new Date(unixTime))

const alternativeFormatISO = (
date: Date,
{ timezone }: { timezone: string }
): string =>
// Canada has a convenient format that looks like "YYYY-MM-DD <time>"
Intl.DateTimeFormat('en-CA', {
timeZone: timezone
})
.format(date)
.split(' ')
.shift()!

const alternativeYesterday = () => {
const d = new Date()
d.setDate(d.getDate() - 1)
return d
}

const alternativeShiftMonths = (date: Date, months: number): Date => {
const d = new Date(date)
d.setMonth(d.getMonth() + months)
return d
}

expect({
today: alternativeFormatISO(new Date(), { timezone }),
yesterday: alternativeFormatISO(alternativeYesterday(), {
timezone
}),
twoMonthsBack: alternativeFormatISO(alternativeShiftMonths(new Date(), -2), {
timezone
}),
twoMonthsAhead: alternativeFormatISO(alternativeShiftMonths(new Date(), 2), {
timezone
}),
oneYearBack: alternativeFormatISO(alternativeShiftMonths(new Date(), -12), {
timezone
})
}).toEqual({
today: expectedToday,
yesterday: expectedYesterday,
twoMonthsBack: expectedTwoMonthsBack,
twoMonthsAhead: expectedTwoMonthsAhead,
oneYearBack: expectedOneYearBack
})
}
)
})
}

0 comments on commit 11ca19b

Please sign in to comment.