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 1 commit
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,7 @@ export const momentMock = {
locale: jest.fn(() => 'default-locale'),
tz: {
setDefault: jest.fn(),
names: jest.fn(() => ['tz1', 'tz2', 'tz3']),
},
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('tz4');
Copy link
Contributor

Choose a reason for hiding this comment

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

This test might be a bit more obvious if the tz name was something like tz-does-not-exist

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).not.toHaveBeenCalledWith('tz4');
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we add an assertion that it was called with no args?

});

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
10 changes: 9 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,15 @@ 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 timezones = moment.tz.names();
const hasTimezone = timezones.includes(tz);
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like Moment has moment.tz.zone which checks for existance, returning the zone if loaded or null otherwise.

Could something like this work?

moment.tz.setDefault(moment.tz.zone(tz))

Copy link
Member Author

Choose a reason for hiding this comment

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

I gave it a try - zone ends up returning a Zone or null, and we have to access name. It ends up fairly close to this, so I could go either way. lemme know

const zone = moment.tz.zone(tz)
if (zone) moment.tz.setDefault(zone.name)

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, Zone is an object. I do think it's better to use moment.tz.zone to check for existence, though.

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