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

STCOM-1150: Remove unchecked calls to Moment.locale #2025

Open
wants to merge 7 commits into
base: b10.3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/Datepicker/Calendar.js
Expand Up @@ -124,7 +124,10 @@ class Calendar extends React.Component {
constructor(props) {
super(props);

moment.locale(this.props.intl.locale || this.props.locale);
const computedLocale = this.props.intl.locale || this.props.locale;
if (moment.locales().includes(computedLocale)) {
moment.locale(computedLocale);
}

const { selectedDate, dateFormat } = this.props;

Expand Down
2 changes: 2 additions & 0 deletions lib/Datepicker/tests/Datepicker-test.js
Expand Up @@ -594,6 +594,8 @@ describe('Datepicker', () => {
await datepicker.openCalendar();
});

it('does not poison moment locale cache', () => expect(moment.locales()).to.not.include('en-se'));

ncovercash marked this conversation as resolved.
Show resolved Hide resolved
it('renders Monday as the first day of the week', () => Weekday('Mon', { index: 0 }).exists());

it('renders weekday and month days in correct alignment', async () => {
Expand Down
4 changes: 3 additions & 1 deletion lib/Timepicker/TimeDropdown.js
Expand Up @@ -50,7 +50,9 @@ class TimeDropdown extends React.Component {
constructor(props) {
super(props);

moment.locale(this.props.locale);
if (moment.locales().includes(this.props.locale)) {
moment.locale(this.props.locale);
}

// handle existing value...
let initMoment;
Expand Down
21 changes: 19 additions & 2 deletions lib/Timepicker/tests/Timepicker-test.js
@@ -1,6 +1,8 @@
import { expect } from 'chai';
import React from 'react';
import { describe, beforeEach, it } from 'mocha';
import moment from 'moment-timezone';
import moment from 'moment';
import momentTz from 'moment-timezone';
import {
runAxeTest,
HTML,
Expand Down Expand Up @@ -107,6 +109,21 @@ describe('Timepicker', () => {
it('emits an event with the time formatted as displayed', () => converge(() => (timeOutput === '05:00 PM')));
});

describe('correctly handles unknown locales', () => {
beforeEach(async () => {
await mountWithContext(
<Timepicker
id="timepicker-locale-test"
locale="en-SE"
/>
);

await timepicker.clickInput();
});

it('does not poison moment locale cache', () => expect(moment.locales()).to.not.include('en-se'));
ncovercash marked this conversation as resolved.
Show resolved Hide resolved
});

describe('selecting a time with timeZone prop', () => {
let timeOutput;

Expand Down Expand Up @@ -166,7 +183,7 @@ describe('Timepicker', () => {
await timepicker.fillIn('05:00 PM');
});

it('returns an ISO 8601 time string for specific time zone', () => converge(() => (timeOutput === moment().tz(tz).isDST() ? '00:00:00.000Z' : '01:00:00.000Z')));
it('returns an ISO 8601 time string for specific time zone', () => converge(() => (timeOutput === momentTz().tz(tz).isDST() ? '00:00:00.000Z' : '01:00:00.000Z')));
});
});

Expand Down
4 changes: 2 additions & 2 deletions util/dateTimeUtils.js
Expand Up @@ -48,7 +48,7 @@ export const getLocaleDateFormat = ({ intl }) => {
format = getMomentLocalizedFormat(intl);
}

return format;
return format.replaceAll('\u202f', ' ');
ncovercash marked this conversation as resolved.
Show resolved Hide resolved
};

// getLocalizedTimeFormatInfo() -
Expand Down Expand Up @@ -128,7 +128,7 @@ export function getLocalizedTimeFormatInfo(locale) {

return {
...formatInfo,
timeFormat,
timeFormat: timeFormat.replaceAll('\u202f', ' '),
dayPeriods: [...dpOptions],
};
}