Skip to content

Commit

Permalink
fix(test): use luxon to convert date to local timezone
Browse files Browse the repository at this point in the history
  • Loading branch information
lonelydev committed Oct 18, 2024
1 parent 3883039 commit 218a543
Showing 1 changed file with 48 additions and 117 deletions.
165 changes: 48 additions & 117 deletions test/OnCallPaymentCalculator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,35 @@ import { OnCallPaymentsCalculator } from "../src/OnCallPaymentsCalculator";
import { OnCallPeriod } from '../src/OnCallPeriod';
import { OnCallUser } from '../src/OnCallUser';
import { convertTimezone } from '../src/DateUtilities';
import { DateTime } from "luxon";

var runtimeEnvTimezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
var runtimeEnvLocale = Intl.DateTimeFormat().resolvedOptions().locale;

describe('should calculate the payment for an on call user', () => {
describe('understanding luxon', () => {
test('should be able to use luxon to convert date to local timezone', () => {
const luxonDate = DateTime.fromISO('2023-10-01T12:00:00+01:00', { zone: 'Europe/London' });
const timezone = luxonDate.zoneName;
const localDate = luxonDate.setZone(runtimeEnvTimezone);
const offset = luxonDate.offset;
expect(offset).toBe(60);
expect(timezone).toBe('Europe/London');
expect(localDate.toISO()).toBe('2023-10-01T12:00:00.000+01:00');
});
});

describe('should calculate the payment for an on call user', () => {
test('- when person continues to be on-call from end of Month to 12th of subsequent month', () => {
const since = new Date('2024-07-31T23:00:00+01:00');
const until = new Date('2024-08-12T10:00:00+01:00');
const sinceInEnvTimezone = convertTimezone(since, runtimeEnvTimezone, runtimeEnvLocale);
const untilInEnvTimezone = convertTimezone(until, runtimeEnvTimezone, runtimeEnvLocale);
const luxonSince = DateTime.fromISO('2024-07-31T23:00:00+01:00', { zone: 'Europe/London' });
const luxonUntil = DateTime.fromISO('2024-08-12T10:00:00+01:00', { zone: 'Europe/London' });
const since = luxonSince.toJSDate();
const until = luxonUntil.toJSDate();

const onCallUser = new OnCallUser(
'1',
'John Doe',
[
new OnCallPeriod(sinceInEnvTimezone, untilInEnvTimezone)
new OnCallPeriod(since, until)
]
);

Expand All @@ -32,8 +44,10 @@ describe('should calculate the payment for an on call user', () => {
});

test('- when person starts to be on-call from start of Month 10am to 12th of that month', () => {
const since = new Date('2024-08-01T10:00:00+01:00');
const until = new Date('2024-08-12T10:00:00+01:00');
const luxonSince = DateTime.fromISO('2024-08-01T10:00:00+01:00', { zone: 'Europe/London' });
const luxonUntil = DateTime.fromISO('2024-08-12T10:00:00+01:00', { zone: 'Europe/London' });
const since = luxonSince.toJSDate();
const until = luxonUntil.toJSDate();
const onCallUser = new OnCallUser(
'1',
'John Doe',
Expand All @@ -51,13 +65,9 @@ describe('should calculate the payment for an on call user', () => {
});

test('- when person starts to be on-call from 28th of August 10am to end of August', () => {
const since = convertTimezone(
new Date('2024-08-28T10:00:00+01:00'),
runtimeEnvTimezone,
runtimeEnvLocale);
const until = convertTimezone(new Date('2024-08-31T23:59:59+01:00'),
runtimeEnvTimezone,
runtimeEnvLocale);

const since = DateTime.fromISO('2024-08-28T10:00:00+01:00', { zone: 'Europe/London' }).toJSDate();
const until = DateTime.fromISO('2024-08-31T23:59:59+01:00', { zone: 'Europe/London' }).toJSDate();
const onCallUser = new OnCallUser(
'1',
'John Doe',
Expand All @@ -75,8 +85,8 @@ describe('should calculate the payment for an on call user', () => {
});

test('- when person starts to be on-call from 28th of Month 10am to 2nd of next month', () => {
const since = convertTimezone(new Date('2024-08-28T10:00:00+01:00'), runtimeEnvTimezone, runtimeEnvLocale);
const until = convertTimezone(new Date('2024-09-02T10:00:00+01:00'), runtimeEnvTimezone, runtimeEnvLocale);
const since = DateTime.fromISO('2024-08-28T10:00:00+01:00', { zone: 'Europe/London' }).toJSDate();
const until = DateTime.fromISO('2024-09-02T10:00:00+01:00', { zone: 'Europe/London' }).toJSDate();
const onCallUser = new OnCallUser(
'1',
'John Doe',
Expand All @@ -102,28 +112,12 @@ describe('should calculate the payment for an on call user', () => {
'YW Oncall',
[
new OnCallPeriod(
convertTimezone(
new Date('2024-08-01T00:00:00+01:00'),
runtimeEnvTimezone,
runtimeEnvLocale
),
convertTimezone(
new Date('2024-08-06T10:00:00+01:00'),
runtimeEnvTimezone,
runtimeEnvLocale
)
DateTime.fromISO('2024-08-01T00:00:00+01:00', { zone: 'Europe/London' }).toJSDate(),
DateTime.fromISO('2024-08-06T10:00:00+01:00', { zone: 'Europe/London' }).toJSDate()
),
new OnCallPeriod(
convertTimezone(
new Date('2024-08-28T10:00:00+01:00'),
runtimeEnvTimezone,
runtimeEnvLocale
),
convertTimezone(
new Date('2024-09-01T00:00:00+01:00'),
runtimeEnvTimezone,
runtimeEnvLocale
)
DateTime.fromISO('2024-08-28T10:00:00+01:00', { zone: 'Europe/London' }).toJSDate(),
DateTime.fromISO('2024-09-01T00:00:00+01:00', { zone: 'Europe/London' }).toJSDate()
)
]
),
Expand All @@ -132,28 +126,13 @@ describe('should calculate the payment for an on call user', () => {
'SK Oncall',
[
new OnCallPeriod(
convertTimezone(
new Date('2024-08-06T10:00:00+01:00'),
runtimeEnvTimezone,
runtimeEnvLocale
),
convertTimezone(
new Date('2024-08-15T10:00:00+01:00'),
runtimeEnvTimezone,
runtimeEnvLocale
DateTime.fromISO('2024-08-06T10:00:00+01:00', { zone: 'Europe/London' }).toJSDate(),
DateTime.fromISO('2024-08-15T10:00:00+01:00', { zone: 'Europe/London' }).toJSDate()
)
),
,
new OnCallPeriod(
convertTimezone(
new Date('2024-08-16T10:00:00+01:00'),
runtimeEnvTimezone,
runtimeEnvLocale
),
convertTimezone(
new Date('2024-08-21T10:00:00+01:00'),
runtimeEnvTimezone,
runtimeEnvLocale
)
DateTime.fromISO('2024-08-16T10:00:00+01:00', { zone: 'Europe/London' }).toJSDate(),
DateTime.fromISO('2024-08-21T10:00:00+01:00', { zone: 'Europe/London' }).toJSDate()
)
]
),
Expand All @@ -162,16 +141,8 @@ describe('should calculate the payment for an on call user', () => {
'EG Oncall',
[
new OnCallPeriod(
convertTimezone(
new Date('2024-08-15T00:00:00+01:00'),
runtimeEnvTimezone,
runtimeEnvLocale
),
convertTimezone(
new Date('2024-08-16T10:00:00+01:00'),
runtimeEnvTimezone,
runtimeEnvLocale
)
DateTime.fromISO('2024-08-15T00:00:00+01:00', { zone: 'Europe/London' }).toJSDate(),
DateTime.fromISO('2024-08-16T10:00:00+01:00', { zone: 'Europe/London' }).toJSDate()
)
]
),
Expand All @@ -180,16 +151,8 @@ describe('should calculate the payment for an on call user', () => {
'CE Oncall',
[
new OnCallPeriod(
convertTimezone(
new Date('2024-08-21T10:00:00+01:00'),
runtimeEnvTimezone,
runtimeEnvLocale
),
convertTimezone(
new Date('2024-08-28T10:00:00+01:00'),
runtimeEnvTimezone,
runtimeEnvLocale
)
DateTime.fromISO('2024-08-21T10:00:00+01:00', { zone: 'Europe/London' }).toJSDate(),
DateTime.fromISO('2024-08-28T10:00:00+01:00', { zone: 'Europe/London' }).toJSDate()
)
]
)
Expand All @@ -213,28 +176,12 @@ describe('should be able to audit the payment for an on call user', () => {
'YW Oncall',
[
new OnCallPeriod(
convertTimezone(
new Date('2024-08-01T00:00:00+01:00'),
runtimeEnvTimezone,
runtimeEnvLocale
),
convertTimezone(
new Date('2024-08-06T10:00:00+01:00'),
runtimeEnvTimezone,
runtimeEnvLocale
)
DateTime.fromISO('2024-08-01T00:00:00+01:00', { zone: 'Europe/London' }).toJSDate(),
DateTime.fromISO('2024-08-06T10:00:00+01:00', { zone: 'Europe/London' }).toJSDate()
),
new OnCallPeriod(
convertTimezone(
new Date('2024-08-28T10:00:00+01:00'),
runtimeEnvTimezone,
runtimeEnvLocale
),
convertTimezone(
new Date('2024-09-01T00:00:00+01:00'),
runtimeEnvTimezone,
runtimeEnvLocale
)
DateTime.fromISO('2024-08-28T10:00:00+01:00', { zone: 'Europe/London' }).toJSDate(),
DateTime.fromISO('2024-09-01T00:00:00+01:00', { zone: 'Europe/London' }).toJSDate()
)
]
),
Expand All @@ -243,28 +190,12 @@ describe('should be able to audit the payment for an on call user', () => {
'SK Oncall',
[
new OnCallPeriod(
convertTimezone(
new Date('2024-08-06T10:00:00+01:00'),
runtimeEnvTimezone,
runtimeEnvLocale
),
convertTimezone(
new Date('2024-08-15T10:00:00+01:00'),
runtimeEnvTimezone,
runtimeEnvLocale
)
DateTime.fromISO('2024-08-06T10:00:00+01:00', { zone: 'Europe/London' }).toJSDate(),
DateTime.fromISO('2024-08-15T10:00:00+01:00', { zone: 'Europe/London' }).toJSDate()
),
new OnCallPeriod(
convertTimezone(
new Date('2024-08-16T10:00:00+01:00'),
runtimeEnvTimezone,
runtimeEnvLocale
),
convertTimezone(
new Date('2024-08-21T10:00:00+01:00'),
runtimeEnvTimezone,
runtimeEnvLocale
)
DateTime.fromISO('2024-08-16T10:00:00+01:00', { zone: 'Europe/London' }).toJSDate(),
DateTime.fromISO('2024-08-21T10:00:00+01:00', { zone: 'Europe/London' }).toJSDate()
)
]
)
Expand Down

0 comments on commit 218a543

Please sign in to comment.