Splitting locale matching into separate file to facilitate testing
This commit is contained in:
parent
ba8461c281
commit
2a77792165
@ -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(
|
||||
|
15
src/components/LocaleSwitcher/locale-to-available.js
Normal file
15
src/components/LocaleSwitcher/locale-to-available.js
Normal 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;
|
18
src/components/LocaleSwitcher/locale-to-available.test.js
Normal file
18
src/components/LocaleSwitcher/locale-to-available.test.js
Normal 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');
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue
Block a user