fix(v2): modify validation schema and tests for rehype/remark + remove duplicate dependency (#3247)

* chore(v2): remove duplicate dependency

* fix(v2): make changes to validation for rehype and remark plugins

* style(v2): run prettier
This commit is contained in:
Teik Jun 2020-08-11 21:17:23 +08:00 committed by GitHub
parent 9399ad61c9
commit fda1590b0d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 63 additions and 42 deletions

View file

@ -9,12 +9,14 @@ import {PluginOptionSchema, DEFAULT_OPTIONS} from '../pluginOptionSchema';
import {normalizePluginOptions} from '@docusaurus/utils-validation';
// the type of remark/rehype plugins is function
const remarkRehypePluginStub = () => {};
const markdownPluginsFunctionStub = () => {};
const markdownPluginsObjectStub = {};
describe('normalizeDocsPluginOptions', () => {
test('should return default options for undefined user options', async () => {
const {value} = await PluginOptionSchema.validate({});
const {value, error} = await PluginOptionSchema.validate({});
expect(value).toEqual(DEFAULT_OPTIONS);
expect(error).toBe(undefined);
});
test('should accept correctly defined user options', async () => {
@ -26,29 +28,57 @@ describe('normalizeDocsPluginOptions', () => {
sidebarPath: 'my-sidebar', // Path to sidebar configuration for showing a list of markdown pages.
docLayoutComponent: '@theme/DocPage',
docItemComponent: '@theme/DocItem',
remarkPlugins: [],
rehypePlugins: [remarkRehypePluginStub],
remarkPlugins: [markdownPluginsObjectStub],
rehypePlugins: [markdownPluginsFunctionStub],
showLastUpdateTime: true,
showLastUpdateAuthor: true,
admonitions: {},
excludeNextVersionDocs: true,
disableVersioning: true,
};
const {value} = await PluginOptionSchema.validate(userOptions);
const {value, error} = await PluginOptionSchema.validate(userOptions);
expect(value).toEqual(userOptions);
expect(error).toBe(undefined);
});
test('should accept correctly defined remark and rehype plugin options', async () => {
const userOptions = {
...DEFAULT_OPTIONS,
remarkPlugins: [remarkRehypePluginStub, {option1: '42'}],
remarkPlugins: [[markdownPluginsFunctionStub, {option1: '42'}]],
rehypePlugins: [
remarkRehypePluginStub,
[remarkRehypePluginStub, {option1: '42'}],
markdownPluginsObjectStub,
[markdownPluginsFunctionStub, {option1: '42'}],
],
};
const {value} = await PluginOptionSchema.validate(userOptions);
const {value, error} = await PluginOptionSchema.validate(userOptions);
expect(value).toEqual(userOptions);
expect(error).toBe(undefined);
});
test('should reject invalid remark plugin options', () => {
expect(() => {
normalizePluginOptions(PluginOptionSchema, {
remarkPlugins: [[{option1: '42'}, markdownPluginsFunctionStub]],
});
}).toThrowErrorMatchingInlineSnapshot(
`"\\"remarkPlugins[0]\\" does not match any of the allowed types"`,
);
});
test('should reject invalid rehype plugin options', () => {
expect(() => {
normalizePluginOptions(PluginOptionSchema, {
rehypePlugins: [
[
markdownPluginsFunctionStub,
{option1: '42'},
markdownPluginsFunctionStub,
],
],
});
}).toThrowErrorMatchingInlineSnapshot(
`"\\"rehypePlugins[0]\\" does not match any of the allowed types"`,
);
});
test('should reject bad path inputs', () => {

View file

@ -36,7 +36,7 @@ export const PluginOptionSchema = Joi.object({
routeBasePath: Joi.string().allow('').default(DEFAULT_OPTIONS.routeBasePath),
homePageId: Joi.string().optional(),
include: Joi.array().items(Joi.string()).default(DEFAULT_OPTIONS.include),
sidebarPath: Joi.string().default(DEFAULT_OPTIONS.sidebarPath),
sidebarPath: Joi.string().allow('').default(DEFAULT_OPTIONS.sidebarPath),
docLayoutComponent: Joi.string().default(DEFAULT_OPTIONS.docLayoutComponent),
docItemComponent: Joi.string().default(DEFAULT_OPTIONS.docItemComponent),
remarkPlugins: RemarkPluginsSchema.default(DEFAULT_OPTIONS.remarkPlugins),