From f0059a305a9e46217fdc67e3144762cff16110bb Mon Sep 17 00:00:00 2001 From: Jonathan Budzenski Date: Fri, 20 Mar 2020 10:58:50 -0500 Subject: [PATCH] Only set timezone when user setting is a valid timezone (#57850) * Only set timezone when user setting is a valid timezone Currently we set the global browser timezone based on the user's advanced settings. This setting includes a list of timezones and a non-standard 'Browser' option which can be translated as set the timezone to my current. In order to avoid warnings and possible future errors we only set timezone if it exists in moments list of installed timezones Closes #38515 * feedback * only set timezone if defined * Update src/core/public/integrations/moment/moment_service.ts Co-Authored-By: Mikhail Shustov * Update src/core/public/integrations/moment/moment_service.ts Co-Authored-By: Mikhail Shustov * Update src/core/public/integrations/moment/moment_service.test.ts Co-Authored-By: Mikhail Shustov * Update src/core/public/integrations/moment/moment_service.test.ts Co-Authored-By: Mikhail Shustov * update test name Co-authored-by: Mikhail Shustov Co-authored-by: Elastic Machine --- .../moment/moment_service.test.mocks.ts | 3 +++ .../moment/moment_service.test.ts | 20 +++++++++++++++++++ .../integrations/moment/moment_service.ts | 5 ++++- 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/core/public/integrations/moment/moment_service.test.mocks.ts b/src/core/public/integrations/moment/moment_service.test.mocks.ts index 2fa013a6bf132..bb13232157b78 100644 --- a/src/core/public/integrations/moment/moment_service.test.mocks.ts +++ b/src/core/public/integrations/moment/moment_service.test.mocks.ts @@ -21,6 +21,9 @@ export const momentMock = { locale: jest.fn(() => 'default-locale'), tz: { setDefault: jest.fn(), + zone: jest.fn( + z => [{ name: 'tz1' }, { name: 'tz2' }, { name: 'tz3' }].find(f => z === f.name) || null + ), }, weekdays: jest.fn(() => ['dow1', 'dow2', 'dow3']), updateLocale: jest.fn(), diff --git a/src/core/public/integrations/moment/moment_service.test.ts b/src/core/public/integrations/moment/moment_service.test.ts index c9b4479f2f163..bc48ba2a85f63 100644 --- a/src/core/public/integrations/moment/moment_service.test.ts +++ b/src/core/public/integrations/moment/moment_service.test.ts @@ -47,6 +47,26 @@ describe('MomentService', () => { expect(momentMock.updateLocale).toHaveBeenCalledWith('default-locale', { week: { dow: 0 } }); }); + it('does not set unknkown zone', async () => { + const tz$ = new BehaviorSubject('timezone/undefined'); + const uiSettings = uiSettingsServiceMock.createSetupContract(); + uiSettings.get$.mockReturnValueOnce(tz$); + + service.start({ uiSettings }); + await flushPromises(); + expect(momentMock.tz.setDefault).not.toHaveBeenCalled(); + }); + + it('sets timezone when a zone is defined', async () => { + const tz$ = new BehaviorSubject('tz3'); + const uiSettings = uiSettingsServiceMock.createSetupContract(); + uiSettings.get$.mockReturnValueOnce(tz$); + + service.start({ uiSettings }); + await flushPromises(); + expect(momentMock.tz.setDefault).toHaveBeenCalledWith('tz3'); + }); + test('updates moment config', async () => { const tz$ = new BehaviorSubject('tz1'); const dow$ = new BehaviorSubject('dow1'); diff --git a/src/core/public/integrations/moment/moment_service.ts b/src/core/public/integrations/moment/moment_service.ts index 65f2bdea02933..69cc29231d6e4 100644 --- a/src/core/public/integrations/moment/moment_service.ts +++ b/src/core/public/integrations/moment/moment_service.ts @@ -35,7 +35,10 @@ export class MomentService implements CoreService { public async setup() {} public async start({ uiSettings }: StartDeps) { - const setDefaultTimezone = (tz: string) => moment.tz.setDefault(tz); + const setDefaultTimezone = (tz: string) => { + const zone = moment.tz.zone(tz); + if (zone) moment.tz.setDefault(zone.name); + }; const setStartDayOfWeek = (day: string) => { const dow = moment.weekdays().indexOf(day); moment.updateLocale(moment.locale(), { week: { dow } } as any);