mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-04 20:02:54 +02:00
test: improve test coverage (#6387)
* test: improve test coverage * fix * use posixPath
This commit is contained in:
parent
a9810db1cc
commit
62223ee556
24 changed files with 463 additions and 60 deletions
|
@ -1,7 +1,8 @@
|
|||
---
|
||||
slug: /hey/my super path/héllô
|
||||
title: Complex Slug
|
||||
date: 2020-08-16
|
||||
# This date is not YAML date, but we can still use it.
|
||||
date: 2020/08/16
|
||||
---
|
||||
|
||||
complex url slug
|
||||
|
|
|
@ -1,7 +1,10 @@
|
|||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||
|
||||
exports[`blogFeed atom shows feed item for each post 1`] = `
|
||||
"<?xml version=\\"1.0\\" encoding=\\"utf-8\\"?>
|
||||
Array [
|
||||
Array [
|
||||
"build/blog/atom.xml",
|
||||
"<?xml version=\\"1.0\\" encoding=\\"utf-8\\"?>
|
||||
<feed xmlns=\\"http://www.w3.org/2005/Atom\\">
|
||||
<id>https://docusaurus.io/myBaseUrl/blog</id>
|
||||
<title>Hello Blog</title>
|
||||
|
@ -83,11 +86,16 @@ exports[`blogFeed atom shows feed item for each post 1`] = `
|
|||
<name>Sébastien Lorber (translated)</name>
|
||||
</author>
|
||||
</entry>
|
||||
</feed>"
|
||||
</feed>",
|
||||
],
|
||||
]
|
||||
`;
|
||||
|
||||
exports[`blogFeed json shows feed item for each post 1`] = `
|
||||
"{
|
||||
Array [
|
||||
Array [
|
||||
"build/blog/feed.json",
|
||||
"{
|
||||
\\"version\\": \\"https://jsonfeed.org/version/1\\",
|
||||
\\"title\\": \\"Hello Blog\\",
|
||||
\\"home_page_url\\": \\"https://docusaurus.io/myBaseUrl/blog\\",
|
||||
|
@ -163,11 +171,16 @@ exports[`blogFeed json shows feed item for each post 1`] = `
|
|||
}
|
||||
}
|
||||
]
|
||||
}"
|
||||
}",
|
||||
],
|
||||
]
|
||||
`;
|
||||
|
||||
exports[`blogFeed rss shows feed item for each post 1`] = `
|
||||
"<?xml version=\\"1.0\\" encoding=\\"utf-8\\"?>
|
||||
Array [
|
||||
Array [
|
||||
"build/blog/rss.xml",
|
||||
"<?xml version=\\"1.0\\" encoding=\\"utf-8\\"?>
|
||||
<rss version=\\"2.0\\" xmlns:dc=\\"http://purl.org/dc/elements/1.1/\\" xmlns:content=\\"http://purl.org/rss/1.0/modules/content/\\">
|
||||
<channel>
|
||||
<title>Hello Blog</title>
|
||||
|
@ -240,5 +253,7 @@ exports[`blogFeed rss shows feed item for each post 1`] = `
|
|||
<content:encoded><![CDATA[<p>Happy birthday! (translated)</p>]]></content:encoded>
|
||||
</item>
|
||||
</channel>
|
||||
</rss>"
|
||||
</rss>",
|
||||
],
|
||||
]
|
||||
`;
|
||||
|
|
|
@ -5,7 +5,24 @@
|
|||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
import {parseBlogFileName} from '../blogUtils';
|
||||
import {truncate, parseBlogFileName} from '../blogUtils';
|
||||
|
||||
describe('truncate', () => {
|
||||
test('truncates texts', () => {
|
||||
expect(
|
||||
truncate('aaa\n<!-- truncate -->\nbbb\nccc', /<!-- truncate -->/),
|
||||
).toEqual('aaa\n');
|
||||
expect(
|
||||
truncate('\n<!-- truncate -->\nbbb\nccc', /<!-- truncate -->/),
|
||||
).toEqual('\n');
|
||||
});
|
||||
test('leaves texts without markers', () => {
|
||||
expect(truncate('aaa\nbbb\nccc', /<!-- truncate -->/)).toEqual(
|
||||
'aaa\nbbb\nccc',
|
||||
);
|
||||
expect(truncate('', /<!-- truncate -->/)).toEqual('');
|
||||
});
|
||||
});
|
||||
|
||||
describe('parseBlogFileName', () => {
|
||||
test('parse file', () => {
|
||||
|
|
|
@ -6,12 +6,12 @@
|
|||
*/
|
||||
|
||||
import path from 'path';
|
||||
import {generateBlogFeed} from '../feed';
|
||||
import fs from 'fs-extra';
|
||||
import {createBlogFeedFiles} from '../feed';
|
||||
import type {LoadContext, I18n} from '@docusaurus/types';
|
||||
import type {BlogContentPaths} from '../types';
|
||||
import {DEFAULT_OPTIONS} from '../pluginOptionSchema';
|
||||
import {generateBlogPosts} from '../blogUtils';
|
||||
import type {Feed} from 'feed';
|
||||
import type {PluginOptions} from '@docusaurus/plugin-content-blog';
|
||||
|
||||
const DefaultI18N: I18n = {
|
||||
|
@ -36,23 +36,26 @@ function getBlogContentPaths(siteDir: string): BlogContentPaths {
|
|||
async function testGenerateFeeds(
|
||||
context: LoadContext,
|
||||
options: PluginOptions,
|
||||
): Promise<Feed | null> {
|
||||
): Promise<void> {
|
||||
const blogPosts = await generateBlogPosts(
|
||||
getBlogContentPaths(context.siteDir),
|
||||
context,
|
||||
options,
|
||||
);
|
||||
|
||||
return generateBlogFeed({
|
||||
await createBlogFeedFiles({
|
||||
blogPosts,
|
||||
options,
|
||||
siteConfig: context.siteConfig,
|
||||
outDir: 'build',
|
||||
});
|
||||
}
|
||||
|
||||
describe('blogFeed', () => {
|
||||
(['atom', 'rss', 'json'] as const).forEach((feedType) => {
|
||||
describe(`${feedType}`, () => {
|
||||
const fsMock = jest.spyOn(fs, 'outputFile').mockImplementation(() => {});
|
||||
|
||||
test('should not show feed without posts', async () => {
|
||||
const siteDir = __dirname;
|
||||
const siteConfig = {
|
||||
|
@ -62,7 +65,7 @@ describe('blogFeed', () => {
|
|||
favicon: 'image/favicon.ico',
|
||||
};
|
||||
|
||||
const feed = await testGenerateFeeds(
|
||||
await testGenerateFeeds(
|
||||
{
|
||||
siteDir,
|
||||
siteConfig,
|
||||
|
@ -83,7 +86,8 @@ describe('blogFeed', () => {
|
|||
} as PluginOptions,
|
||||
);
|
||||
|
||||
expect(feed).toEqual(null);
|
||||
expect(fsMock).toBeCalledTimes(0);
|
||||
fsMock.mockClear();
|
||||
});
|
||||
|
||||
test('shows feed item for each post', async () => {
|
||||
|
@ -96,7 +100,7 @@ describe('blogFeed', () => {
|
|||
favicon: 'image/favicon.ico',
|
||||
};
|
||||
|
||||
const feed = await testGenerateFeeds(
|
||||
await testGenerateFeeds(
|
||||
{
|
||||
siteDir,
|
||||
siteConfig,
|
||||
|
@ -119,22 +123,8 @@ describe('blogFeed', () => {
|
|||
} as PluginOptions,
|
||||
);
|
||||
|
||||
let feedContent = '';
|
||||
switch (feedType) {
|
||||
case 'rss':
|
||||
feedContent = feed.rss2();
|
||||
break;
|
||||
case 'json':
|
||||
feedContent = feed.json1();
|
||||
break;
|
||||
case 'atom':
|
||||
feedContent = feed.atom1();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
expect(feedContent).toMatchSnapshot();
|
||||
expect(fsMock.mock.calls).toMatchSnapshot();
|
||||
fsMock.mockClear();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
@ -204,7 +204,7 @@ describe('loadBlog', () => {
|
|||
date: new Date('2020-08-16'),
|
||||
formattedDate: 'August 16, 2020',
|
||||
frontMatter: {
|
||||
date: new Date('2020-08-16'),
|
||||
date: '2020/08/16',
|
||||
slug: '/hey/my super path/héllô',
|
||||
title: 'Complex Slug',
|
||||
},
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
|
||||
import {Feed, type Author as FeedAuthor, type Item as FeedItem} from 'feed';
|
||||
import type {BlogPost} from './types';
|
||||
import {normalizeUrl, mdxToHtml} from '@docusaurus/utils';
|
||||
import {normalizeUrl, mdxToHtml, posixPath} from '@docusaurus/utils';
|
||||
import type {DocusaurusConfig} from '@docusaurus/types';
|
||||
import path from 'path';
|
||||
import fs from 'fs-extra';
|
||||
|
@ -30,7 +30,7 @@ function mdxToFeedContent(mdxContent: string): string | undefined {
|
|||
}
|
||||
}
|
||||
|
||||
export async function generateBlogFeed({
|
||||
async function generateBlogFeed({
|
||||
blogPosts,
|
||||
options,
|
||||
siteConfig,
|
||||
|
@ -47,9 +47,7 @@ export async function generateBlogFeed({
|
|||
const {url: siteUrl, baseUrl, title, favicon} = siteConfig;
|
||||
const blogBaseUrl = normalizeUrl([siteUrl, baseUrl, routeBasePath]);
|
||||
|
||||
const updated =
|
||||
(blogPosts[0] && blogPosts[0].metadata.date) ||
|
||||
new Date('2015-10-25T16:29:00.000-07:00'); // weird legacy magic date
|
||||
const updated = blogPosts[0] && blogPosts[0].metadata.date;
|
||||
|
||||
const feed = new Feed({
|
||||
id: blogBaseUrl,
|
||||
|
@ -118,7 +116,10 @@ async function createBlogFeedFile({
|
|||
}
|
||||
})();
|
||||
try {
|
||||
await fs.outputFile(path.join(generatePath, feedPath), feedContent);
|
||||
await fs.outputFile(
|
||||
posixPath(path.join(generatePath, feedPath)),
|
||||
feedContent,
|
||||
);
|
||||
} catch (err) {
|
||||
throw new Error(`Generating ${feedType} feed failed: ${err}.`);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue