Skip to content

Commit

Permalink
browser-side momentjs: use guess for unknown timezones (#188899)
Browse files Browse the repository at this point in the history
## Summary

Fix #123579
  • Loading branch information
pgayvallet authored Jul 24, 2024
1 parent db6886f commit 69268b8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const momentMock = {
locale: jest.fn(() => 'default-locale'),
tz: {
setDefault: jest.fn(),
guess: jest.fn(),
zone: jest.fn(
(z) => [{ name: 'tz1' }, { name: 'tz2' }, { name: 'tz3' }].find((f) => z === f.name) || null
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,22 @@
*/

import { momentMock } from './moment_service.test.mocks';

import { BehaviorSubject } from 'rxjs';
import { MomentService } from './moment_service';
import { uiSettingsServiceMock } from '@kbn/core-ui-settings-browser-mocks';
import { BehaviorSubject } from 'rxjs';

describe('MomentService', () => {
let service: MomentService;

beforeEach(() => {
momentMock.tz.setDefault.mockClear();
momentMock.tz.guess.mockClear();
momentMock.weekdays.mockClear();
momentMock.updateLocale.mockClear();
service = new MomentService();
});

afterEach(() => service.stop());

const flushPromises = () => new Promise((resolve) => setTimeout(resolve, 100));
Expand All @@ -36,24 +40,30 @@ describe('MomentService', () => {
expect(momentMock.updateLocale).toHaveBeenCalledWith('default-locale', { week: { dow: 0 } });
});

it('does not set unknkown zone', async () => {
const tz$ = new BehaviorSubject('timezone/undefined');
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).not.toHaveBeenCalled();
expect(momentMock.tz.setDefault).toHaveBeenCalledWith('tz3');
});

it('sets timezone when a zone is defined', async () => {
const tz$ = new BehaviorSubject('tz3');
it('set timezone with result of moment.tz.guess for unknown zone', async () => {
const tz$ = new BehaviorSubject('timezone/undefined');
const uiSettings = uiSettingsServiceMock.createSetupContract();
uiSettings.get$.mockReturnValueOnce(tz$);

const guessedTz = { guessed: true };
momentMock.tz.guess.mockReturnValue(guessedTz);

service.start({ uiSettings });
await flushPromises();
expect(momentMock.tz.setDefault).toHaveBeenCalledWith('tz3');

expect(momentMock.tz.guess).toHaveBeenCalledTimes(1);
expect(momentMock.tz.setDefault).toHaveBeenCalledTimes(1);
expect(momentMock.tz.setDefault).toHaveBeenCalledWith(guessedTz);
});

test('updates moment config', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ export class MomentService implements CoreService {
public async start({ uiSettings }: StartDeps) {
const setDefaultTimezone = (tz: string) => {
const zone = moment.tz.zone(tz);
if (zone) moment.tz.setDefault(zone.name);
if (zone) {
moment.tz.setDefault(zone.name);
} else {
moment.tz.setDefault(moment.tz.guess());
}
};
const setStartDayOfWeek = (day: string) => {
const dow = moment.weekdays().indexOf(day);
Expand Down

0 comments on commit 69268b8

Please sign in to comment.