From 3c9bf2294fce6885b239fbc623930dfb4d08416d Mon Sep 17 00:00:00 2001 From: Paul Rastoin <45004772+prastoin@users.noreply.github.com> Date: Mon, 31 Mar 2025 15:39:05 +0200 Subject: [PATCH] test(front): fix main failing test due to timezone (#11295) # Introduction Due to winter to summer timezone update, it shown that we have some unit tests flakiness due to both mocked and unmocked date.now invokation between app bootstrap and test bootstrap This PR does not refactor this behavior Just fix the currently failing test ## Note Removed a duplicated file --- packages/twenty-front/setupTests.ts | 2 +- .../findAvailableTimeZoneOption.test.ts | 5 ++-- .../AvailableTimezoneOptionsByLabel.ts | 2 +- .../accounts/utils/formatTimeZoneLabel.ts | 29 ------------------- 4 files changed, 5 insertions(+), 33 deletions(-) delete mode 100644 packages/twenty-front/src/modules/settings/accounts/utils/formatTimeZoneLabel.ts diff --git a/packages/twenty-front/setupTests.ts b/packages/twenty-front/setupTests.ts index e5aa41c46..d995aadc1 100644 --- a/packages/twenty-front/setupTests.ts +++ b/packages/twenty-front/setupTests.ts @@ -13,4 +13,4 @@ import '@testing-library/jest-dom'; */ global.structuredClone = (val) => { return JSON.parse(JSON.stringify(val)); -}; +}; \ No newline at end of file diff --git a/packages/twenty-front/src/modules/localization/utils/__tests__/findAvailableTimeZoneOption.test.ts b/packages/twenty-front/src/modules/localization/utils/__tests__/findAvailableTimeZoneOption.test.ts index 05e670b59..b1058e7ad 100644 --- a/packages/twenty-front/src/modules/localization/utils/__tests__/findAvailableTimeZoneOption.test.ts +++ b/packages/twenty-front/src/modules/localization/utils/__tests__/findAvailableTimeZoneOption.test.ts @@ -1,11 +1,12 @@ import { findAvailableTimeZoneOption } from '@/localization/utils/findAvailableTimeZoneOption'; -jest.useFakeTimers().setSystemTime(new Date('2024-01-01T00:00:00.000Z')); +// TODO: This test is flaky, datetime retrieve in its scope, if mocked will be different than the one computed to build AVAILABLE_TIME_ZONE_OPTIONS_BY_LABEL +// We should refactor our tests to start from a controlled mocked date directly within `setupTests.ts` describe('findAvailableTimeZoneOption', () => { it('should find the matching available IANA time zone select option from a given IANA time zone', () => { const ianaTimeZone = 'Europe/Paris'; const value = 'Europe/Paris'; - const label = '(GMT+01:00) Central European Standard Time - Paris'; + const label = '(GMT+02:00) Central European Summer Time - Paris'; const option = findAvailableTimeZoneOption(ianaTimeZone); diff --git a/packages/twenty-front/src/modules/settings/accounts/constants/AvailableTimezoneOptionsByLabel.ts b/packages/twenty-front/src/modules/settings/accounts/constants/AvailableTimezoneOptionsByLabel.ts index 6e8ea2b3a..eba6bb352 100644 --- a/packages/twenty-front/src/modules/settings/accounts/constants/AvailableTimezoneOptionsByLabel.ts +++ b/packages/twenty-front/src/modules/settings/accounts/constants/AvailableTimezoneOptionsByLabel.ts @@ -1,7 +1,7 @@ /* eslint-disable @typescript-eslint/naming-convention */ /* eslint-disable @nx/workspace-max-consts-per-file */ import { IANA_TIME_ZONES } from '@/localization/constants/IanaTimeZones'; -import { formatTimeZoneLabel } from '@/settings/accounts/utils/formatTimeZoneLabel'; +import { formatTimeZoneLabel } from '@/localization/utils/formatTimeZoneLabel'; import { SelectOption } from 'twenty-ui'; export const AVAILABLE_TIME_ZONE_OPTIONS_BY_LABEL = IANA_TIME_ZONES.reduce< diff --git a/packages/twenty-front/src/modules/settings/accounts/utils/formatTimeZoneLabel.ts b/packages/twenty-front/src/modules/settings/accounts/utils/formatTimeZoneLabel.ts deleted file mode 100644 index e5f1ac97e..000000000 --- a/packages/twenty-front/src/modules/settings/accounts/utils/formatTimeZoneLabel.ts +++ /dev/null @@ -1,29 +0,0 @@ -import defaultLocale from 'date-fns/locale/en-US'; -import { formatInTimeZone } from 'date-fns-tz'; - -/** - * Formats a IANA time zone to a select option label. - * @param ianaTimeZone IANA time zone - * @returns Formatted label - * @example 'Europe/Paris' => '(GMT+01:00) Central European Time - Paris' - */ -export const formatTimeZoneLabel = (ianaTimeZone: string) => { - const timeZoneWithGmtOffset = formatInTimeZone( - Date.now(), - ianaTimeZone, - `(OOOO) zzzz`, - { locale: defaultLocale }, - ); - const ianaTimeZoneParts = ianaTimeZone.split('/'); - const location = - ianaTimeZoneParts.length > 1 - ? ianaTimeZoneParts.slice(-1)[0].replaceAll('_', ' ') - : undefined; - - const timeZoneLabel = - !location || timeZoneWithGmtOffset.includes(location) - ? timeZoneWithGmtOffset - : [timeZoneWithGmtOffset, location].join(' - '); - - return timeZoneLabel; -};