Splitting locale matching into separate file to facilitate testing

This commit is contained in:
Jeff Avallone 2019-01-06 16:54:24 -05:00
parent ba8461c281
commit 2a77792165
3 changed files with 34 additions and 14 deletions

View File

@ -5,22 +5,9 @@ import ExpandIcon from 'react-feather/dist/icons/chevrons-down';
import i18n, { locales } from 'i18n';
import localeToAvailable from './locale-to-available';
import style from './style.module.css';
const localeToAvailable = (locale, available, defaultLocale) => {
if (available.includes(locale)) {
return locale;
}
const parts = locale.split('-');
if (parts.length > 0 && available.includes(parts[0])) {
return parts[0];
}
return defaultLocale;
};
export class LocaleSwitcher extends React.PureComponent {
state = {
current: localeToAvailable(

View File

@ -0,0 +1,15 @@
const localeToAvailable = (locale, available, defaultLocale) => {
if (available.includes(locale)) {
return locale;
}
const parts = locale.split('-');
if (parts.length > 0 && available.includes(parts[0])) {
return parts[0];
}
return defaultLocale;
};
export default localeToAvailable;

View File

@ -0,0 +1,18 @@
import localeToAvailable from './locale-to-available';
describe('localeToAvailable', () => {
test('when requested language and region are available', () => {
expect(localeToAvailable('en-US', ['en', 'en-US', 'other'], 'other'))
.toEqual('en-US');
});
test('when only requested language is available', () => {
expect(localeToAvailable('en-US', ['en', 'en-GB', 'other'], 'other'))
.toEqual('en');
});
test('when language is unavailable', () => {
expect(localeToAvailable('en-US', ['tlh', 'other'], 'other'))
.toEqual('other');
});
});