Browse Source

Improve tests

pull/7491/head
Thomas Kaul 3 hours ago
parent
commit
e92af2d64b
  1. 28
      libs/common/src/lib/helper.ahead-of-utc.time-zone.spec.ts
  2. 28
      libs/common/src/lib/helper.behind-utc.time-zone.spec.ts
  3. 33
      libs/common/src/lib/helper.spec.ts

28
libs/common/src/lib/helper.ahead-of-utc.time-zone.spec.ts

@ -0,0 +1,28 @@
/**
* @jest-environment <rootDir>/../../apps/api/jest-environment-tz.js
* @jest-environment-options {"timeZone": "Pacific/Auckland"}
*/
import {
DATE_FORMAT,
getLocalDateFromUtcDate
} from '@ghostfolio/common/helper';
import { format } from 'date-fns';
describe('Helper in a time zone ahead of UTC', () => {
describe('Get local date from UTC date', () => {
it.each([
['in summer time', '2026-01-01', '2025-12-31T11:00:00.000Z'],
['at the end of summer time', '2026-04-05', '2026-04-04T11:00:00.000Z'],
['in standard time', '2026-07-01', '2026-06-30T12:00:00.000Z'],
['at the start of summer time', '2026-09-27', '2026-09-26T12:00:00.000Z']
])('Preserve the calendar date %s', (_, dateString, localMidnight) => {
const localDate = getLocalDateFromUtcDate(
new Date(`${dateString}T00:00:00.000Z`)
);
expect(format(localDate, DATE_FORMAT)).toEqual(dateString);
expect(localDate).toEqual(new Date(localMidnight));
});
});
});

28
libs/common/src/lib/helper.behind-utc.time-zone.spec.ts

@ -0,0 +1,28 @@
/**
* @jest-environment <rootDir>/../../apps/api/jest-environment-tz.js
* @jest-environment-options {"timeZone": "America/New_York"}
*/
import {
DATE_FORMAT,
getLocalDateFromUtcDate
} from '@ghostfolio/common/helper';
import { format } from 'date-fns';
describe('Helper in a time zone behind UTC', () => {
describe('Get local date from UTC date', () => {
it.each([
['in standard time', '2026-01-01', '2026-01-01T05:00:00.000Z'],
['at the start of summer time', '2026-03-08', '2026-03-08T05:00:00.000Z'],
['in summer time', '2026-07-01', '2026-07-01T04:00:00.000Z'],
['at the end of summer time', '2026-11-01', '2026-11-01T04:00:00.000Z']
])('Preserve the calendar date %s', (_, dateString, localMidnight) => {
const localDate = getLocalDateFromUtcDate(
new Date(`${dateString}T00:00:00.000Z`)
);
expect(format(localDate, DATE_FORMAT)).toEqual(dateString);
expect(localDate).toEqual(new Date(localMidnight));
});
});
});

33
libs/common/src/lib/helper.spec.ts

@ -9,7 +9,6 @@ import {
getCountryCodeFromCurrency, getCountryCodeFromCurrency,
getEmojiFlag, getEmojiFlag,
getHoldingName, getHoldingName,
getLocalDateFromUtcDate,
getNumberFormatGroup, getNumberFormatGroup,
getStringOrNull, getStringOrNull,
getStringOrUndefined, getStringOrUndefined,
@ -238,38 +237,6 @@ describe('Helper', () => {
}); });
}); });
describe('Get local date from UTC date', () => {
const originalTz = process.env.TZ;
afterEach(() => {
process.env.TZ = originalTz;
});
it('Preserve the calendar date in a timezone behind UTC', () => {
process.env.TZ = 'America/New_York';
const localDate = getLocalDateFromUtcDate(
new Date('2026-01-05T00:00:00.000Z')
);
expect(localDate.getFullYear()).toEqual(2026);
expect(localDate.getMonth()).toEqual(0);
expect(localDate.getDate()).toEqual(5);
});
it('Preserve the calendar date in a timezone ahead of UTC', () => {
process.env.TZ = 'Asia/Tokyo';
const localDate = getLocalDateFromUtcDate(
new Date('2026-01-05T00:00:00.000Z')
);
expect(localDate.getFullYear()).toEqual(2026);
expect(localDate.getMonth()).toEqual(0);
expect(localDate.getDate()).toEqual(5);
});
});
describe('Get number format group', () => { describe('Get number format group', () => {
let languageGetter: jest.SpyInstance<string, [], any>; let languageGetter: jest.SpyInstance<string, [], any>;

Loading…
Cancel
Save