Skip to content

Commit

Permalink
fix(tests): use environment timezone and locale in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lonelydev committed Oct 18, 2024
1 parent 38676ac commit ecabb55
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 26 deletions.
10 changes: 4 additions & 6 deletions test/DateUtilities.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { convertTimezone, toLocaTzIsoStringWithOffset } from '../src/DateUtiliti
import {describe, expect, test} from '@jest/globals';

describe('DateUtilities.toLocaTzIsoStringWithOffset', () => {
const environmentTimezone = Intl.DateTimeFormat().resolvedOptions().timeZone;

test('should convert UTC date to local ISO string with timezone offset', () => {
const dateFromISOStringZ = new Date('2023-10-01T12:00:00Z');
const localizedTzIsoStringWithOffset = toLocaTzIsoStringWithOffset(dateFromISOStringZ);

const environmentTimezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
const testDateConvertedToEnvTz = convertTimezone(dateFromISOStringZ, environmentTimezone, "en-US");
const expectedLocalISOTime = toLocaTzIsoStringWithOffset(testDateConvertedToEnvTz);
expect(localizedTzIsoStringWithOffset).toBe(expectedLocalISOTime);
Expand All @@ -15,17 +15,15 @@ describe('DateUtilities.toLocaTzIsoStringWithOffset', () => {
test('should handle dates with positive timezone offsets', () => {
const date = new Date('2023-10-01T12:00:00+02:00');
const result = toLocaTzIsoStringWithOffset(date);
const localTimezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
const localTzDate = convertTimezone(date, localTimezone, "en-US");
const localTzDate = convertTimezone(date, environmentTimezone, "en-US");
const expectedLocalISOTime = toLocaTzIsoStringWithOffset(localTzDate);
expect(result).toBe(expectedLocalISOTime);
});

test('should handle dates with negative timezone offsets', () => {
const date = new Date('2023-10-01T12:00:00-05:00');
const result = toLocaTzIsoStringWithOffset(date);
const localTimezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
const localTzDate = convertTimezone(date, localTimezone, "en-US");
const localTzDate = convertTimezone(date, environmentTimezone, "en-US");
const expectedLocalISOTime = toLocaTzIsoStringWithOffset(localTzDate);
expect(result).toBe(expectedLocalISOTime);
});
Expand Down
108 changes: 88 additions & 20 deletions test/OnCallPaymentCalculator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,28 @@ import { convertTimezone } from '../src/DateUtilities';
describe('should calculate the payment for an on call user', () => {

var runtimeEnvTimezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
var runtimeEnvLocale = "en-US";

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:00Z');
const since = new Date('2024-07-31T23:00:00+01:00');
const until = new Date('2024-08-12T10:00:00+01:00');
const testDateConvertedToEnvTz = convertTimezone(since, runtimeEnvTimezone, "en-US");
const sinceInEnvTimezone = convertTimezone(since, runtimeEnvTimezone, runtimeEnvLocale);
const untilInEnvTimezone = convertTimezone(until, runtimeEnvTimezone, runtimeEnvLocale);

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

const calculator = new OnCallPaymentsCalculator();
expect(onCallUser.onCallPeriods).toBeDefined();
expect(onCallUser.onCallPeriods.length).toBe(1);
expect(onCallUser.onCallPeriods[0].since).toEqual(since);
expect(onCallUser.onCallPeriods[0].until).toEqual(until);
expect(onCallUser.onCallPeriods[0].numberOfOOhWeekDays).toBe(5);
expect(onCallUser.onCallPeriods[0].numberOfOOhWeekDays).toBe(6);
expect(onCallUser.onCallPeriods[0].numberOfOohWeekends).toBe(6);
expect(calculator.calculateOnCallPayment(onCallUser)).toBe(700);
expect(calculator.calculateOnCallPayment(onCallUser)).toBe(750);
});

test('- when person starts to be on-call from start of Month 10am to 12th of that month', () => {
Expand All @@ -53,8 +53,13 @@ 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 = new Date('2024-08-28T10:00:00+01:00');
const until = new Date('2024-08-31T23:59:59+01:00');
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 onCallUser = new OnCallUser(
'1',
'John Doe',
Expand All @@ -74,8 +79,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 = new Date('2024-08-28T10:00:00+01:00');
const until = new Date('2024-09-02T10:00:00+01:00');
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 onCallUser = new OnCallUser(
'1',
'John Doe',
Expand All @@ -100,33 +105,96 @@ describe('should calculate the payment for an on call user', () => {
'1PF7DNAV',
'YW Oncall',
[
new OnCallPeriod(new Date('2024-08-01T00:00:00+01:00'), new Date('2024-08-06T10:00:00+01:00')),
new OnCallPeriod(new Date('2024-08-28T10:00:00+01:00'), new Date('2024-09-01T00:00:00+01:00'))
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
)
),
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
)
)
]
),
new OnCallUser(
'PGO3DTM',
'SK Oncall',
[
new OnCallPeriod(new Date('2024-08-06T10:00:00+01:00'),
new Date('2024-08-15T10:00:00+01:00')),
new OnCallPeriod(new Date('2024-08-16T10:00:00+01:00'),
new Date('2024-08-21T10:00:00+01:00'))
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
)
),
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
)
)
]
),
new OnCallUser(
'PINI77A',
'EG Oncall',
[
new OnCallPeriod(new Date('2024-08-15T00:00:00+01:00'),
new Date('2024-08-16T10:00:00+01:00'))
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
)
)
]
),
new OnCallUser(
'PJXZDBT',
'CE Oncall',
[
new OnCallPeriod(new Date('2024-08-21T10:00:00+01:00'), new Date('2024-08-28T10:00:00+01:00'))
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
)
)
]
)
];
Expand Down

0 comments on commit ecabb55

Please sign in to comment.