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