mirror of
https://github.com/facebook/docusaurus.git
synced 2025-07-23 11:38:48 +02:00
refactor: unify how validateOptions is handled (#6961)
* refactor: unify how validateOptions is handled * fix types * fix again
This commit is contained in:
parent
44107fb879
commit
6e2eb44964
43 changed files with 542 additions and 540 deletions
|
@ -9,7 +9,8 @@ import path from 'path';
|
|||
import {loadContext} from '@docusaurus/core/lib/server';
|
||||
|
||||
import pluginContentPages from '../index';
|
||||
import {PluginOptionSchema} from '../pluginOptionSchema';
|
||||
import {validateOptions} from '../options';
|
||||
import {normalizePluginOptions} from '@docusaurus/utils-validation';
|
||||
|
||||
describe('docusaurus-plugin-content-pages', () => {
|
||||
it('loads simple pages', async () => {
|
||||
|
@ -17,9 +18,12 @@ describe('docusaurus-plugin-content-pages', () => {
|
|||
const context = await loadContext(siteDir);
|
||||
const plugin = await pluginContentPages(
|
||||
context,
|
||||
PluginOptionSchema.validate({
|
||||
path: 'src/pages',
|
||||
}).value,
|
||||
validateOptions({
|
||||
validate: normalizePluginOptions,
|
||||
options: {
|
||||
path: 'src/pages',
|
||||
},
|
||||
}),
|
||||
);
|
||||
const pagesMetadata = await plugin.loadContent!();
|
||||
|
||||
|
@ -37,9 +41,12 @@ describe('docusaurus-plugin-content-pages', () => {
|
|||
currentLocale: 'fr',
|
||||
},
|
||||
},
|
||||
PluginOptionSchema.validate({
|
||||
path: 'src/pages',
|
||||
}).value,
|
||||
validateOptions({
|
||||
validate: normalizePluginOptions,
|
||||
options: {
|
||||
path: 'src/pages',
|
||||
},
|
||||
}),
|
||||
);
|
||||
const pagesMetadata = await plugin.loadContent!();
|
||||
|
||||
|
|
|
@ -5,31 +5,29 @@
|
|||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
import {PluginOptionSchema, DEFAULT_OPTIONS} from '../pluginOptionSchema';
|
||||
import type {PluginOptions} from '@docusaurus/plugin-content-pages';
|
||||
import {validateOptions, DEFAULT_OPTIONS} from '../options';
|
||||
import {normalizePluginOptions} from '@docusaurus/utils-validation';
|
||||
import type {Options} from '@docusaurus/plugin-content-pages';
|
||||
|
||||
function normalizePluginOptions(
|
||||
options: Partial<PluginOptions>,
|
||||
): PluginOptions {
|
||||
const {value, error} = PluginOptionSchema.validate(options, {
|
||||
convert: false,
|
||||
});
|
||||
if (error) {
|
||||
throw error;
|
||||
} else {
|
||||
return value;
|
||||
}
|
||||
function testValidate(options: Options) {
|
||||
return validateOptions({validate: normalizePluginOptions, options});
|
||||
}
|
||||
|
||||
const defaultOptions = {
|
||||
...DEFAULT_OPTIONS,
|
||||
id: 'default',
|
||||
};
|
||||
|
||||
describe('normalizePagesPluginOptions', () => {
|
||||
it('returns default options for undefined user options', () => {
|
||||
const value = normalizePluginOptions({});
|
||||
expect(value).toEqual(DEFAULT_OPTIONS);
|
||||
expect(testValidate({})).toEqual(defaultOptions);
|
||||
});
|
||||
|
||||
it('fills in default options for partially defined user options', () => {
|
||||
const value = normalizePluginOptions({path: 'src/pages'});
|
||||
expect(value).toEqual(DEFAULT_OPTIONS);
|
||||
expect(testValidate({path: 'src/foo'})).toEqual({
|
||||
...defaultOptions,
|
||||
path: 'src/foo',
|
||||
});
|
||||
});
|
||||
|
||||
it('accepts correctly defined user options', () => {
|
||||
|
@ -39,13 +37,15 @@ describe('normalizePagesPluginOptions', () => {
|
|||
include: ['**/*.{js,jsx,ts,tsx}'],
|
||||
exclude: ['**/$*/'],
|
||||
};
|
||||
const value = normalizePluginOptions(userOptions);
|
||||
expect(value).toEqual({...DEFAULT_OPTIONS, ...userOptions});
|
||||
expect(testValidate(userOptions)).toEqual({
|
||||
...defaultOptions,
|
||||
...userOptions,
|
||||
});
|
||||
});
|
||||
|
||||
it('rejects bad path inputs', () => {
|
||||
expect(() => {
|
||||
normalizePluginOptions({
|
||||
testValidate({
|
||||
// @ts-expect-error: bad attribute
|
||||
path: 42,
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue