fix docs version tests

This commit is contained in:
sebastien 2025-07-04 15:37:00 +02:00
parent c79a3cbb05
commit cfb354502d
2 changed files with 101 additions and 12 deletions

View file

@ -196,7 +196,9 @@ async function doProcessDocMetadata({
locale: context.i18n.currentLocale, locale: context.i18n.currentLocale,
}); });
} else if (typeof options.editUrl === 'string') { } else if (typeof options.editUrl === 'string') {
const isLocalized = contentPath === versionMetadata.contentPathLocalized; const isLocalized =
typeof versionMetadata.contentPathLocalized !== 'undefined' &&
contentPath === versionMetadata.contentPathLocalized;
const baseVersionEditUrl = const baseVersionEditUrl =
isLocalized && options.editLocalizedFiles isLocalized && options.editLocalizedFiles
? versionMetadata.editUrlLocalized ? versionMetadata.editUrlLocalized

View file

@ -6,27 +6,44 @@
*/ */
import {jest} from '@jest/globals'; import {jest} from '@jest/globals';
import path from 'path'; import * as path from 'path';
import {DEFAULT_PLUGIN_ID} from '@docusaurus/utils'; import {DEFAULT_PLUGIN_ID} from '@docusaurus/utils';
import {readVersionsMetadata} from '../version'; import {readVersionsMetadata} from '../version';
import {DEFAULT_OPTIONS} from '../../options'; import {DEFAULT_OPTIONS} from '../../options';
import type {I18n, LoadContext} from '@docusaurus/types'; import type {I18n, I18nLocaleConfig, LoadContext} from '@docusaurus/types';
import type { import type {
PluginOptions, PluginOptions,
VersionMetadata, VersionMetadata,
} from '@docusaurus/plugin-content-docs'; } from '@docusaurus/plugin-content-docs';
const DefaultI18N: I18n = { function getI18n(
path: 'i18n', locale: string,
currentLocale: 'en', localeConfigOptions?: Partial<I18nLocaleConfig>,
locales: ['en'], ): I18n {
defaultLocale: 'en', return {
localeConfigs: {}, path: 'i18n',
}; currentLocale: locale,
locales: ['en'],
defaultLocale: locale,
localeConfigs: {
[locale]: {
path: locale,
label: locale,
translate: true,
calendar: 'calendar',
htmlLang: locale,
direction: 'rtl',
...localeConfigOptions,
},
},
};
}
const DefaultI18N: I18n = getI18n('en');
describe('readVersionsMetadata', () => { describe('readVersionsMetadata', () => {
describe('simple site', () => { describe('simple site', () => {
async function loadSite() { async function loadSite({context}: {context?: Partial<LoadContext>} = {}) {
const simpleSiteDir = path.resolve( const simpleSiteDir = path.resolve(
path.join(__dirname, '../../__tests__/__fixtures__', 'simple-site'), path.join(__dirname, '../../__tests__/__fixtures__', 'simple-site'),
); );
@ -39,6 +56,7 @@ describe('readVersionsMetadata', () => {
baseUrl: '/', baseUrl: '/',
i18n: DefaultI18N, i18n: DefaultI18N,
localizationDir: path.join(simpleSiteDir, 'i18n/en'), localizationDir: path.join(simpleSiteDir, 'i18n/en'),
...context,
} as LoadContext; } as LoadContext;
const vCurrent: VersionMetadata = { const vCurrent: VersionMetadata = {
@ -73,6 +91,26 @@ describe('readVersionsMetadata', () => {
expect(versionsMetadata).toEqual([vCurrent]); expect(versionsMetadata).toEqual([vCurrent]);
}); });
it('works with translate: false', async () => {
const {defaultOptions, defaultContext, vCurrent} = await loadSite({
context: {
i18n: getI18n('en', {translate: false}),
},
});
const versionsMetadata = await readVersionsMetadata({
options: defaultOptions,
context: defaultContext,
});
expect(versionsMetadata).toEqual([
{
...vCurrent,
contentPathLocalized: undefined,
},
]);
});
it('works with base url', async () => { it('works with base url', async () => {
const {defaultOptions, defaultContext, vCurrent} = await loadSite(); const {defaultOptions, defaultContext, vCurrent} = await loadSite();
@ -188,7 +226,7 @@ describe('readVersionsMetadata', () => {
}); });
describe('versioned site, pluginId=default', () => { describe('versioned site, pluginId=default', () => {
async function loadSite() { async function loadSite({context}: {context?: Partial<LoadContext>} = {}) {
const versionedSiteDir = path.resolve( const versionedSiteDir = path.resolve(
path.join(__dirname, '../../__tests__/__fixtures__', 'versioned-site'), path.join(__dirname, '../../__tests__/__fixtures__', 'versioned-site'),
); );
@ -202,6 +240,7 @@ describe('readVersionsMetadata', () => {
baseUrl: '/', baseUrl: '/',
i18n: DefaultI18N, i18n: DefaultI18N,
localizationDir: path.join(versionedSiteDir, 'i18n/en'), localizationDir: path.join(versionedSiteDir, 'i18n/en'),
...context,
} as LoadContext; } as LoadContext;
const vCurrent: VersionMetadata = { const vCurrent: VersionMetadata = {
@ -436,6 +475,54 @@ describe('readVersionsMetadata', () => {
]); ]);
}); });
it('works with editUrl and translate=false', async () => {
const {defaultOptions, defaultContext, vCurrent, v101, v100, vWithSlugs} =
await loadSite({
context: {
i18n: getI18n('en', {translate: false}),
},
});
const versionsMetadata = await readVersionsMetadata({
options: {
...defaultOptions,
editUrl: 'https://github.com/facebook/docusaurus/edit/main/website/',
},
context: defaultContext,
});
expect(versionsMetadata).toEqual([
{
...vCurrent,
contentPathLocalized: undefined,
editUrl:
'https://github.com/facebook/docusaurus/edit/main/website/docs',
editUrlLocalized: undefined,
},
{
...v101,
contentPathLocalized: undefined,
editUrl:
'https://github.com/facebook/docusaurus/edit/main/website/versioned_docs/version-1.0.1',
editUrlLocalized: undefined,
},
{
...v100,
contentPathLocalized: undefined,
editUrl:
'https://github.com/facebook/docusaurus/edit/main/website/versioned_docs/version-1.0.0',
editUrlLocalized: undefined,
},
{
...vWithSlugs,
contentPathLocalized: undefined,
editUrl:
'https://github.com/facebook/docusaurus/edit/main/website/versioned_docs/version-withSlugs',
editUrlLocalized: undefined,
},
]);
});
it('works with editUrl and editCurrentVersion=true', async () => { it('works with editUrl and editCurrentVersion=true', async () => {
const {defaultOptions, defaultContext, vCurrent, v101, v100, vWithSlugs} = const {defaultOptions, defaultContext, vCurrent, v101, v100, vWithSlugs} =
await loadSite(); await loadSite();