mirror of
https://github.com/facebook/docusaurus.git
synced 2025-08-02 00:09:48 +02:00
Fix CLI unit tests + refactor getLocaleConfig method
This commit is contained in:
parent
b640334248
commit
f6f3e6cdb4
12 changed files with 38 additions and 25 deletions
|
@ -11,7 +11,7 @@ import {
|
|||
updateTranslationFileMessages,
|
||||
getPluginI18nPath,
|
||||
localizePath,
|
||||
getCurrentLocaleConfig,
|
||||
getLocaleConfig,
|
||||
} from '../i18nUtils';
|
||||
import type {I18n, I18nLocaleConfig} from '@docusaurus/types';
|
||||
|
||||
|
@ -182,7 +182,7 @@ describe('localizePath', () => {
|
|||
});
|
||||
});
|
||||
|
||||
describe('getCurrentLocaleConfig', () => {
|
||||
describe('getLocaleConfig', () => {
|
||||
const localeConfigEn: I18nLocaleConfig = {
|
||||
path: 'path',
|
||||
direction: 'rtl',
|
||||
|
@ -213,7 +213,7 @@ describe('getCurrentLocaleConfig', () => {
|
|||
|
||||
it('returns single locale config', () => {
|
||||
expect(
|
||||
getCurrentLocaleConfig(
|
||||
getLocaleConfig(
|
||||
i18n({currentLocale: 'en', localeConfigs: {en: localeConfigEn}}),
|
||||
),
|
||||
).toEqual(localeConfigEn);
|
||||
|
@ -221,7 +221,7 @@ describe('getCurrentLocaleConfig', () => {
|
|||
|
||||
it('returns correct locale config among 2', () => {
|
||||
expect(
|
||||
getCurrentLocaleConfig(
|
||||
getLocaleConfig(
|
||||
i18n({
|
||||
currentLocale: 'fr',
|
||||
localeConfigs: {en: localeConfigEn, fr: localeConfigFr},
|
||||
|
@ -230,9 +230,21 @@ describe('getCurrentLocaleConfig', () => {
|
|||
).toEqual(localeConfigFr);
|
||||
});
|
||||
|
||||
it('accepts locale to look for as param', () => {
|
||||
expect(
|
||||
getLocaleConfig(
|
||||
i18n({
|
||||
currentLocale: 'fr',
|
||||
localeConfigs: {en: localeConfigEn, fr: localeConfigFr},
|
||||
}),
|
||||
'en',
|
||||
),
|
||||
).toEqual(localeConfigEn);
|
||||
});
|
||||
|
||||
it('throws for locale config that does not exist', () => {
|
||||
expect(() =>
|
||||
getCurrentLocaleConfig(
|
||||
getLocaleConfig(
|
||||
i18n({
|
||||
currentLocale: 'fr',
|
||||
localeConfigs: {en: localeConfigEn},
|
||||
|
|
|
@ -118,11 +118,12 @@ export function localizePath({
|
|||
// TODO we may extract this to a separate package
|
||||
// we want to use it on the frontend too
|
||||
// but "docusaurus-utils-common" (agnostic utils) is not an ideal place since
|
||||
export function getCurrentLocaleConfig(i18n: I18n): I18nLocaleConfig {
|
||||
const localeConfig = i18n.localeConfigs[i18n.currentLocale];
|
||||
export function getLocaleConfig(i18n: I18n, locale?: string): I18nLocaleConfig {
|
||||
const localeToLookFor = locale ?? i18n.currentLocale;
|
||||
const localeConfig = i18n.localeConfigs[localeToLookFor];
|
||||
if (!localeConfig) {
|
||||
throw new Error(
|
||||
`Can't find locale config for locale ${logger.code(i18n.currentLocale)}`,
|
||||
`Can't find locale config for locale ${logger.code(localeToLookFor)}`,
|
||||
);
|
||||
}
|
||||
return localeConfig;
|
||||
|
|
|
@ -34,7 +34,7 @@ export {
|
|||
updateTranslationFileMessages,
|
||||
getPluginI18nPath,
|
||||
localizePath,
|
||||
getCurrentLocaleConfig,
|
||||
getLocaleConfig,
|
||||
} from './i18nUtils';
|
||||
export {mapAsyncSequential, findAsyncSequential} from './jsUtils';
|
||||
export {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue