mirror of
https://github.com/facebook/docusaurus.git
synced 2025-07-17 08:37:57 +02:00
add unit tests for blog translate wiring
This commit is contained in:
parent
2e9ae7f28c
commit
8321df5654
1 changed files with 103 additions and 17 deletions
|
@ -6,7 +6,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import {jest} from '@jest/globals';
|
import {jest} from '@jest/globals';
|
||||||
import path from 'path';
|
import * as path from 'path';
|
||||||
import {normalizePluginOptions} from '@docusaurus/utils-validation';
|
import {normalizePluginOptions} from '@docusaurus/utils-validation';
|
||||||
import {
|
import {
|
||||||
posixPath,
|
posixPath,
|
||||||
|
@ -23,6 +23,7 @@ import type {
|
||||||
I18n,
|
I18n,
|
||||||
Validate,
|
Validate,
|
||||||
MarkdownConfig,
|
MarkdownConfig,
|
||||||
|
I18nLocaleConfig,
|
||||||
} from '@docusaurus/types';
|
} from '@docusaurus/types';
|
||||||
import type {
|
import type {
|
||||||
BlogPost,
|
BlogPost,
|
||||||
|
@ -68,7 +69,10 @@ Available blog post titles are:\n- ${blogPosts
|
||||||
return post;
|
return post;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getI18n(locale: string): I18n {
|
function getI18n(
|
||||||
|
locale: string,
|
||||||
|
localeConfigOptions?: Partial<I18nLocaleConfig>,
|
||||||
|
): I18n {
|
||||||
return {
|
return {
|
||||||
currentLocale: locale,
|
currentLocale: locale,
|
||||||
locales: [locale],
|
locales: [locale],
|
||||||
|
@ -81,6 +85,8 @@ function getI18n(locale: string): I18n {
|
||||||
htmlLang: locale,
|
htmlLang: locale,
|
||||||
direction: 'ltr',
|
direction: 'ltr',
|
||||||
path: locale,
|
path: locale,
|
||||||
|
translate: true,
|
||||||
|
...localeConfigOptions,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
@ -95,8 +101,9 @@ const BaseEditUrl = 'https://baseEditUrl.com/edit';
|
||||||
const getPlugin = async (
|
const getPlugin = async (
|
||||||
siteDir: string,
|
siteDir: string,
|
||||||
pluginOptions: Partial<PluginOptions> = {},
|
pluginOptions: Partial<PluginOptions> = {},
|
||||||
i18n: I18n = DefaultI18N,
|
i18nOptions: Partial<I18n> = {},
|
||||||
) => {
|
) => {
|
||||||
|
const i18n = {...DefaultI18N, ...i18nOptions};
|
||||||
const generatedFilesDir: string = path.resolve(siteDir, '.docusaurus');
|
const generatedFilesDir: string = path.resolve(siteDir, '.docusaurus');
|
||||||
const localizationDir = path.join(
|
const localizationDir = path.join(
|
||||||
siteDir,
|
siteDir,
|
||||||
|
@ -154,20 +161,34 @@ const getBlogTags = async (
|
||||||
};
|
};
|
||||||
|
|
||||||
describe('blog plugin', () => {
|
describe('blog plugin', () => {
|
||||||
it('getPathsToWatch returns right files', async () => {
|
describe('getPathsToWatch', () => {
|
||||||
const siteDir = path.join(__dirname, '__fixtures__', 'website');
|
async function runTest({translate}: {translate: boolean}) {
|
||||||
const plugin = await getPlugin(siteDir);
|
const siteDir = path.join(__dirname, '__fixtures__', 'website');
|
||||||
const pathsToWatch = plugin.getPathsToWatch!();
|
const plugin = await getPlugin(siteDir, {}, getI18n('en', {translate}));
|
||||||
const relativePathsToWatch = pathsToWatch.map((p) =>
|
const pathsToWatch = plugin.getPathsToWatch!();
|
||||||
posixPath(path.relative(siteDir, p)),
|
return pathsToWatch.map((p) => posixPath(path.relative(siteDir, p)));
|
||||||
);
|
}
|
||||||
expect(relativePathsToWatch).toEqual([
|
|
||||||
'i18n/en/docusaurus-plugin-content-blog/authors.yml',
|
it('getPathsToWatch returns right files', async () => {
|
||||||
'i18n/en/docusaurus-plugin-content-blog/tags.yml',
|
const relativePathsToWatch = await runTest({translate: true});
|
||||||
'blog/tags.yml',
|
expect(relativePathsToWatch).toEqual([
|
||||||
'i18n/en/docusaurus-plugin-content-blog/**/*.{md,mdx}',
|
'i18n/en/docusaurus-plugin-content-blog/authors.yml',
|
||||||
'blog/**/*.{md,mdx}',
|
'i18n/en/docusaurus-plugin-content-blog/tags.yml',
|
||||||
]);
|
// 'blog/authors.yml', // TODO weird that it's not here but tags is?
|
||||||
|
'blog/tags.yml',
|
||||||
|
'i18n/en/docusaurus-plugin-content-blog/**/*.{md,mdx}',
|
||||||
|
'blog/**/*.{md,mdx}',
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('getPathsToWatch returns right files (translate: false)', async () => {
|
||||||
|
const relativePathsToWatch = await runTest({translate: false});
|
||||||
|
expect(relativePathsToWatch).toEqual([
|
||||||
|
'blog/authors.yml',
|
||||||
|
'blog/tags.yml',
|
||||||
|
'blog/**/*.{md,mdx}',
|
||||||
|
]);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('builds a simple website', async () => {
|
it('builds a simple website', async () => {
|
||||||
|
@ -378,6 +399,54 @@ describe('blog plugin', () => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('i18n config translate is wired properly', () => {
|
||||||
|
async function runTest({translate}: {translate: boolean}) {
|
||||||
|
const siteDir = path.join(__dirname, '__fixtures__', 'website');
|
||||||
|
const blogPosts = await getBlogPosts(
|
||||||
|
siteDir,
|
||||||
|
{},
|
||||||
|
getI18n('en', {translate}),
|
||||||
|
);
|
||||||
|
|
||||||
|
// Simpler to snapshot
|
||||||
|
return blogPosts.map((post) => post.metadata.title);
|
||||||
|
}
|
||||||
|
|
||||||
|
it('works with translate: false', async () => {
|
||||||
|
await expect(runTest({translate: false})).resolves.toMatchInlineSnapshot(`
|
||||||
|
[
|
||||||
|
"test links",
|
||||||
|
"MDX Blog Sample with require calls",
|
||||||
|
"Full Blog Sample",
|
||||||
|
"Complex Slug",
|
||||||
|
"Simple Slug",
|
||||||
|
"draft",
|
||||||
|
"unlisted",
|
||||||
|
"some heading",
|
||||||
|
"date-matter",
|
||||||
|
"Happy 1st Birthday Slash!",
|
||||||
|
]
|
||||||
|
`);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('works with translate: true', async () => {
|
||||||
|
await expect(runTest({translate: true})).resolves.toMatchInlineSnapshot(`
|
||||||
|
[
|
||||||
|
"test links",
|
||||||
|
"MDX Blog Sample with require calls",
|
||||||
|
"Full Blog Sample",
|
||||||
|
"Complex Slug",
|
||||||
|
"Simple Slug",
|
||||||
|
"draft",
|
||||||
|
"unlisted",
|
||||||
|
"some heading",
|
||||||
|
"date-matter",
|
||||||
|
"Happy 1st Birthday Slash! (translated)",
|
||||||
|
]
|
||||||
|
`);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it('handles edit URL with editLocalizedBlogs: true', async () => {
|
it('handles edit URL with editLocalizedBlogs: true', async () => {
|
||||||
const siteDir = path.join(__dirname, '__fixtures__', 'website');
|
const siteDir = path.join(__dirname, '__fixtures__', 'website');
|
||||||
const blogPosts = await getBlogPosts(siteDir, {editLocalizedFiles: true});
|
const blogPosts = await getBlogPosts(siteDir, {editLocalizedFiles: true});
|
||||||
|
@ -391,6 +460,23 @@ describe('blog plugin', () => {
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('handles edit URL with editLocalizedBlogs: true and translate: false', async () => {
|
||||||
|
const siteDir = path.join(__dirname, '__fixtures__', 'website');
|
||||||
|
const blogPosts = await getBlogPosts(
|
||||||
|
siteDir,
|
||||||
|
{editLocalizedFiles: true},
|
||||||
|
getI18n('en', {translate: false}),
|
||||||
|
);
|
||||||
|
|
||||||
|
const localizedBlogPost = blogPosts.find(
|
||||||
|
(v) => v.metadata.title === 'Happy 1st Birthday Slash!',
|
||||||
|
)!;
|
||||||
|
|
||||||
|
expect(localizedBlogPost.metadata.editUrl).toBe(
|
||||||
|
`${BaseEditUrl}/blog/2018-12-14-Happy-First-Birthday-Slash.md`,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
it('handles edit URL with editUrl function', async () => {
|
it('handles edit URL with editUrl function', async () => {
|
||||||
const siteDir = path.join(__dirname, '__fixtures__', 'website');
|
const siteDir = path.join(__dirname, '__fixtures__', 'website');
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue