From e92af2d64b3a15315954e81eb427daf5a084c8aa Mon Sep 17 00:00:00 2001 From: Thomas Kaul <4159106+dtslvr@users.noreply.github.com> Date: Sun, 29 Nov 2026 08:50:19 +0100 Subject: [PATCH] Improve tests --- .../lib/helper.ahead-of-utc.time-zone.spec.ts | 28 ++++++++++++++++ .../lib/helper.behind-utc.time-zone.spec.ts | 28 ++++++++++++++++ libs/common/src/lib/helper.spec.ts | 33 ------------------- 3 files changed, 56 insertions(+), 33 deletions(-) create mode 100644 libs/common/src/lib/helper.ahead-of-utc.time-zone.spec.ts create mode 100644 libs/common/src/lib/helper.behind-utc.time-zone.spec.ts diff --git a/libs/common/src/lib/helper.ahead-of-utc.time-zone.spec.ts b/libs/common/src/lib/helper.ahead-of-utc.time-zone.spec.ts new file mode 100644 index 0000000000..974e45d049 --- /dev/null +++ b/libs/common/src/lib/helper.ahead-of-utc.time-zone.spec.ts @@ -0,0 +1,28 @@ +/** + * @jest-environment /../../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)); + }); + }); +}); diff --git a/libs/common/src/lib/helper.behind-utc.time-zone.spec.ts b/libs/common/src/lib/helper.behind-utc.time-zone.spec.ts new file mode 100644 index 0000000000..0645952ff8 --- /dev/null +++ b/libs/common/src/lib/helper.behind-utc.time-zone.spec.ts @@ -0,0 +1,28 @@ +/** + * @jest-environment /../../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)); + }); + }); +}); diff --git a/libs/common/src/lib/helper.spec.ts b/libs/common/src/lib/helper.spec.ts index 9887c8f140..7ba92aee9a 100644 --- a/libs/common/src/lib/helper.spec.ts +++ b/libs/common/src/lib/helper.spec.ts @@ -9,7 +9,6 @@ import { getCountryCodeFromCurrency, getEmojiFlag, getHoldingName, - getLocalDateFromUtcDate, getNumberFormatGroup, getStringOrNull, 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', () => { let languageGetter: jest.SpyInstance;