Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only set timezone when user setting is a valid timezone #57850

Merged
merged 15 commits into from
Mar 20, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
24 changes: 24 additions & 0 deletions src/core/public/integrations/moment/moment_service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,30 @@ describe('MomentService', () => {
expect(momentMock.updateLocale).toHaveBeenCalledWith('default-locale', { week: { dow: 0 } });
});

it('uses the default timezone when a zone is not defined', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would you mind to update the test suit name? does not set unknown zone

const tz$ = new BehaviorSubject('timezone/undefined');
const dow$ = new BehaviorSubject('dow1');

const uiSettings = uiSettingsServiceMock.createSetupContract();
uiSettings.get$.mockReturnValueOnce(tz$).mockReturnValueOnce(dow$);
jbudz marked this conversation as resolved.
Show resolved Hide resolved

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

it('sets timezone when a zone is defined', async () => {
const tz$ = new BehaviorSubject('tz3');
const dow$ = new BehaviorSubject('dow1');

const uiSettings = uiSettingsServiceMock.createSetupContract();
uiSettings.get$.mockReturnValueOnce(tz$).mockReturnValueOnce(dow$);
jbudz marked this conversation as resolved.
Show resolved Hide resolved

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');
Expand Down
6 changes: 5 additions & 1 deletion src/core/public/integrations/moment/moment_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import moment from 'moment-timezone';
import { merge, Subscription } from 'rxjs';
import { get } from 'lodash';
jbudz marked this conversation as resolved.
Show resolved Hide resolved

import { tap } from 'rxjs/operators';
import { IUiSettingsClient } from '../../ui_settings';
Expand All @@ -35,7 +36,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: string | undefined = get(moment.tz.zone(tz), 'name');
moment.tz.setDefault(zone);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could add a test that moment.tz.setDefault(undefined); doesn't remove previously set timezone?

};
const setStartDayOfWeek = (day: string) => {
const dow = moment.weekdays().indexOf(day);
moment.updateLocale(moment.locale(), { week: { dow } } as any);
Expand Down