mirror of
https://github.com/facebook/docusaurus.git
synced 2025-04-30 02:37:59 +02:00
* feat(content-blog): support json feed * feat(content-blog): support json feed * feat(content-blog): add json type to default feed options * Refactors, docs, validation * Fix test * Ammend docs * Add API doc Co-authored-by: Joshua Chen <sidachen2003@gmail.com>
150 lines
4.5 KiB
TypeScript
150 lines
4.5 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 {PluginOptionSchema, DEFAULT_OPTIONS} from '../pluginOptionSchema';
|
|
|
|
// the type of remark/rehype plugins can be either function, object or array
|
|
const markdownPluginsFunctionStub = () => {};
|
|
const markdownPluginsObjectStub = {};
|
|
|
|
test('should normalize options', () => {
|
|
const {value, error} = PluginOptionSchema.validate({});
|
|
expect(value).toEqual(DEFAULT_OPTIONS);
|
|
expect(error).toBe(undefined);
|
|
});
|
|
|
|
test('should accept correctly defined user options', () => {
|
|
const userOptions = {
|
|
...DEFAULT_OPTIONS,
|
|
feedOptions: {type: 'rss', title: 'myTitle'},
|
|
path: 'not_blog',
|
|
routeBasePath: 'myBlog',
|
|
postsPerPage: 5,
|
|
include: ['api/*', 'docs/*'],
|
|
};
|
|
const {value, error} = PluginOptionSchema.validate(userOptions);
|
|
expect(value).toEqual({
|
|
...userOptions,
|
|
feedOptions: {type: ['rss'], title: 'myTitle', copyright: ''},
|
|
});
|
|
expect(error).toBe(undefined);
|
|
});
|
|
|
|
test('should accept valid user options', async () => {
|
|
const userOptions = {
|
|
...DEFAULT_OPTIONS,
|
|
routeBasePath: 'myBlog',
|
|
beforeDefaultRemarkPlugins: [],
|
|
beforeDefaultRehypePlugins: [markdownPluginsFunctionStub],
|
|
remarkPlugins: [[markdownPluginsFunctionStub, {option1: '42'}]],
|
|
rehypePlugins: [
|
|
markdownPluginsObjectStub,
|
|
[markdownPluginsFunctionStub, {option1: '42'}],
|
|
],
|
|
};
|
|
const {value, error} = await PluginOptionSchema.validate(userOptions);
|
|
expect(value).toEqual(userOptions);
|
|
expect(error).toBe(undefined);
|
|
});
|
|
|
|
test('should throw Error in case of invalid options', () => {
|
|
const {error} = PluginOptionSchema.validate({
|
|
path: 'not_blog',
|
|
postsPerPage: -1,
|
|
include: ['api/*', 'docs/*'],
|
|
routeBasePath: 'not_blog',
|
|
});
|
|
|
|
expect(error).toMatchSnapshot();
|
|
});
|
|
|
|
test('should throw Error in case of invalid feedtype', () => {
|
|
const {error} = PluginOptionSchema.validate({
|
|
feedOptions: {
|
|
type: 'none',
|
|
},
|
|
});
|
|
|
|
expect(error).toMatchSnapshot();
|
|
});
|
|
|
|
test('should convert all feed type to array with other feed type', () => {
|
|
const {value} = PluginOptionSchema.validate({
|
|
feedOptions: {type: 'all'},
|
|
});
|
|
expect(value).toEqual({
|
|
...DEFAULT_OPTIONS,
|
|
feedOptions: {type: ['rss', 'atom', 'json'], copyright: ''},
|
|
});
|
|
});
|
|
|
|
test('should accept null type and return same', () => {
|
|
const {value, error} = PluginOptionSchema.validate({
|
|
feedOptions: {type: null},
|
|
});
|
|
expect(value).toEqual({
|
|
...DEFAULT_OPTIONS,
|
|
feedOptions: {type: null},
|
|
});
|
|
expect(error).toBe(undefined);
|
|
});
|
|
|
|
test('should contain array with rss + atom for missing feed type', () => {
|
|
const {value} = PluginOptionSchema.validate({
|
|
feedOptions: {},
|
|
});
|
|
expect(value).toEqual(DEFAULT_OPTIONS);
|
|
});
|
|
|
|
test('should have array with rss + atom, title for missing feed type', () => {
|
|
const {value} = PluginOptionSchema.validate({
|
|
feedOptions: {title: 'title'},
|
|
});
|
|
expect(value).toEqual({
|
|
...DEFAULT_OPTIONS,
|
|
feedOptions: {type: ['rss', 'atom'], title: 'title', copyright: ''},
|
|
});
|
|
});
|
|
|
|
describe('blog sidebar', () => {
|
|
test('should accept 0 sidebar count', () => {
|
|
const userOptions = {blogSidebarCount: 0};
|
|
const {value, error} = PluginOptionSchema.validate(userOptions);
|
|
expect(value).toEqual({...DEFAULT_OPTIONS, ...userOptions});
|
|
expect(error).toBe(undefined);
|
|
});
|
|
|
|
test('should accept "ALL" sidebar count', () => {
|
|
const userOptions = {blogSidebarCount: 'ALL'};
|
|
const {value, error} = PluginOptionSchema.validate(userOptions);
|
|
expect(value).toEqual({...DEFAULT_OPTIONS, ...userOptions});
|
|
expect(error).toBe(undefined);
|
|
});
|
|
|
|
test('should reject "abcdef" sidebar count', () => {
|
|
const userOptions = {blogSidebarCount: 'abcdef'};
|
|
const {error} = PluginOptionSchema.validate(userOptions);
|
|
expect(error).toMatchInlineSnapshot(
|
|
`[ValidationError: "blogSidebarCount" must be one of [ALL, number]]`,
|
|
);
|
|
});
|
|
|
|
test('should accept "all posts" sidebar title', () => {
|
|
const userOptions = {blogSidebarTitle: 'all posts'};
|
|
const {value, error} = PluginOptionSchema.validate(userOptions);
|
|
expect(value).toEqual({...DEFAULT_OPTIONS, ...userOptions});
|
|
expect(error).toBe(undefined);
|
|
});
|
|
|
|
test('should reject 42 sidebar title', () => {
|
|
const userOptions = {blogSidebarTitle: 42};
|
|
const {error} = PluginOptionSchema.validate(userOptions);
|
|
expect(error).toMatchInlineSnapshot(
|
|
`[ValidationError: "blogSidebarTitle" must be a string]`,
|
|
);
|
|
});
|
|
});
|