mirror of
https://github.com/facebook/docusaurus.git
synced 2025-04-30 10:48:05 +02:00
136 lines
3.8 KiB
TypeScript
136 lines
3.8 KiB
TypeScript
/**
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*/
|
|
|
|
import {jest} from '@jest/globals';
|
|
import path from 'path';
|
|
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 {PluginOptions} from '@docusaurus/plugin-content-blog';
|
|
|
|
const DefaultI18N: I18n = {
|
|
currentLocale: 'en',
|
|
locales: ['en'],
|
|
defaultLocale: 'en',
|
|
localeConfigs: {},
|
|
};
|
|
|
|
function getBlogContentPaths(siteDir: string): BlogContentPaths {
|
|
return {
|
|
contentPath: path.resolve(siteDir, 'blog'),
|
|
contentPathLocalized: path.resolve(
|
|
siteDir,
|
|
'i18n',
|
|
'en',
|
|
'docusaurus-plugin-content-blog',
|
|
),
|
|
};
|
|
}
|
|
|
|
async function testGenerateFeeds(
|
|
context: LoadContext,
|
|
options: PluginOptions,
|
|
): Promise<void> {
|
|
const blogPosts = await generateBlogPosts(
|
|
getBlogContentPaths(context.siteDir),
|
|
context,
|
|
options,
|
|
);
|
|
|
|
await createBlogFeedFiles({
|
|
blogPosts: blogPosts.filter((post) => !post.metadata.frontMatter.draft),
|
|
options,
|
|
siteConfig: context.siteConfig,
|
|
outDir: context.outDir,
|
|
});
|
|
}
|
|
|
|
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 = {
|
|
title: 'Hello',
|
|
baseUrl: '/',
|
|
url: 'https://docusaurus.io',
|
|
favicon: 'image/favicon.ico',
|
|
};
|
|
const outDir = path.join(siteDir, 'build-snap');
|
|
|
|
await testGenerateFeeds(
|
|
{
|
|
siteDir,
|
|
siteConfig,
|
|
i18n: DefaultI18N,
|
|
outDir,
|
|
} as LoadContext,
|
|
{
|
|
path: 'invalid-blog-path',
|
|
routeBasePath: 'blog',
|
|
tagsBasePath: 'tags',
|
|
authorsMapPath: 'authors.yml',
|
|
include: ['*.md', '*.mdx'],
|
|
feedOptions: {
|
|
type: [feedType],
|
|
copyright: 'Copyright',
|
|
},
|
|
readingTime: ({content, defaultReadingTime}) =>
|
|
defaultReadingTime({content}),
|
|
} as PluginOptions,
|
|
);
|
|
|
|
expect(fsMock).toBeCalledTimes(0);
|
|
fsMock.mockClear();
|
|
});
|
|
|
|
test('shows feed item for each post', async () => {
|
|
const siteDir = path.join(__dirname, '__fixtures__', 'website');
|
|
const outDir = path.join(siteDir, 'build-snap');
|
|
const siteConfig = {
|
|
title: 'Hello',
|
|
baseUrl: '/myBaseUrl/',
|
|
url: 'https://docusaurus.io',
|
|
favicon: 'image/favicon.ico',
|
|
};
|
|
|
|
// Build is quite difficult to mock, so we built the blog beforehand and
|
|
// copied the output to the fixture...
|
|
await testGenerateFeeds(
|
|
{
|
|
siteDir,
|
|
siteConfig,
|
|
i18n: DefaultI18N,
|
|
outDir,
|
|
} as LoadContext,
|
|
{
|
|
path: 'blog',
|
|
routeBasePath: 'blog',
|
|
tagsBasePath: 'tags',
|
|
authorsMapPath: 'authors.yml',
|
|
include: DEFAULT_OPTIONS.include,
|
|
exclude: DEFAULT_OPTIONS.exclude,
|
|
feedOptions: {
|
|
type: [feedType],
|
|
copyright: 'Copyright',
|
|
},
|
|
readingTime: ({content, defaultReadingTime}) =>
|
|
defaultReadingTime({content}),
|
|
} as PluginOptions,
|
|
);
|
|
|
|
expect(fsMock.mock.calls.map((call) => call[1])).toMatchSnapshot();
|
|
fsMock.mockClear();
|
|
});
|
|
});
|
|
});
|
|
});
|