mirror of
https://github.com/facebook/docusaurus.git
synced 2025-07-27 05:28:43 +02:00
add tests
This commit is contained in:
parent
8ab6ddac54
commit
45af11fd13
5 changed files with 113 additions and 34 deletions
|
@ -17,3 +17,21 @@ exports[`docusaurus-plugin-content-showcase loads simple showcase 1`] = `
|
||||||
],
|
],
|
||||||
}
|
}
|
||||||
`;
|
`;
|
||||||
|
|
||||||
|
exports[`docusaurus-plugin-content-showcase loads simple showcase with tags in options 1`] = `
|
||||||
|
{
|
||||||
|
"items": [
|
||||||
|
{
|
||||||
|
"description": "World",
|
||||||
|
"preview": "github.com/ozakione.png",
|
||||||
|
"source": "https://github.com/facebook/docusaurus",
|
||||||
|
"tags": [
|
||||||
|
"opensource",
|
||||||
|
"meta",
|
||||||
|
],
|
||||||
|
"title": "Hello",
|
||||||
|
"website": "https://docusaurus.io/",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}
|
||||||
|
`;
|
||||||
|
|
|
@ -45,7 +45,7 @@ function testField(params: {
|
||||||
try {
|
try {
|
||||||
validateShowcaseFrontMatter(frontMatter);
|
validateShowcaseFrontMatter(frontMatter);
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`Doc front matter is expected to be rejected, but was accepted successfully:\n ${JSON.stringify(
|
`Showcase front matter is expected to be rejected, but was accepted successfully:\n ${JSON.stringify(
|
||||||
frontMatter,
|
frontMatter,
|
||||||
null,
|
null,
|
||||||
2,
|
2,
|
||||||
|
@ -61,7 +61,7 @@ function testField(params: {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
describe('doc front matter schema', () => {
|
describe('showcase front matter schema', () => {
|
||||||
it('accepts valid frontmatter', () => {
|
it('accepts valid frontmatter', () => {
|
||||||
const frontMatter: ShowcaseFrontMatter = {
|
const frontMatter: ShowcaseFrontMatter = {
|
||||||
title: 'title',
|
title: 'title',
|
||||||
|
@ -97,5 +97,11 @@ describe('validateShowcaseFrontMatter full', () => {
|
||||||
website: 'website',
|
website: 'website',
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
|
invalidFrontMatters: [
|
||||||
|
[
|
||||||
|
{},
|
||||||
|
'"title" is required. "description" is required. "preview" is required. "website" is required. "source" is required. "tags" is required',
|
||||||
|
],
|
||||||
|
],
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -12,9 +12,6 @@ import {normalizePluginOptions} from '@docusaurus/utils-validation';
|
||||||
import pluginContentPages from '../index';
|
import pluginContentPages from '../index';
|
||||||
import {validateOptions} from '../options';
|
import {validateOptions} from '../options';
|
||||||
|
|
||||||
// todo add test with tags in config
|
|
||||||
// todo add test with tags in yaml
|
|
||||||
|
|
||||||
describe('docusaurus-plugin-content-showcase', () => {
|
describe('docusaurus-plugin-content-showcase', () => {
|
||||||
it('loads simple showcase', async () => {
|
it('loads simple showcase', async () => {
|
||||||
const siteDir = path.join(__dirname, '__fixtures__', 'website');
|
const siteDir = path.join(__dirname, '__fixtures__', 'website');
|
||||||
|
@ -24,8 +21,8 @@ describe('docusaurus-plugin-content-showcase', () => {
|
||||||
validateOptions({
|
validateOptions({
|
||||||
validate: normalizePluginOptions,
|
validate: normalizePluginOptions,
|
||||||
options: {
|
options: {
|
||||||
// todo broken because we use aliasedPaths
|
|
||||||
path: 'src/showcase',
|
path: 'src/showcase',
|
||||||
|
tags: 'tags.yaml',
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
@ -33,4 +30,61 @@ describe('docusaurus-plugin-content-showcase', () => {
|
||||||
|
|
||||||
expect(showcaseMetadata).toMatchSnapshot();
|
expect(showcaseMetadata).toMatchSnapshot();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('loads simple showcase with tags in options', async () => {
|
||||||
|
const siteDir = path.join(__dirname, '__fixtures__', 'website');
|
||||||
|
const context = await loadContext({siteDir});
|
||||||
|
const plugin = pluginContentPages(
|
||||||
|
context,
|
||||||
|
validateOptions({
|
||||||
|
validate: normalizePluginOptions,
|
||||||
|
options: {
|
||||||
|
path: 'src/showcase',
|
||||||
|
tags: {
|
||||||
|
opensource: {
|
||||||
|
label: 'Open-Source',
|
||||||
|
description: {
|
||||||
|
message:
|
||||||
|
'Open-Source Docusaurus sites can be useful for inspiration!',
|
||||||
|
id: 'showcase.tag.opensource.description',
|
||||||
|
},
|
||||||
|
color: '#39ca30',
|
||||||
|
},
|
||||||
|
meta: {
|
||||||
|
label: 'Meta',
|
||||||
|
description: {
|
||||||
|
message:
|
||||||
|
'Docusaurus sites of Meta (formerly Facebook) projects',
|
||||||
|
id: 'showcase.tag.meta.description',
|
||||||
|
},
|
||||||
|
color: '#4267b2',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
const showcaseMetadata = await plugin.loadContent!();
|
||||||
|
|
||||||
|
expect(showcaseMetadata).toMatchSnapshot();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('throw loading inexistant tags file', async () => {
|
||||||
|
const siteDir = path.join(__dirname, '__fixtures__', 'website');
|
||||||
|
const context = await loadContext({siteDir});
|
||||||
|
const plugin = pluginContentPages(
|
||||||
|
context,
|
||||||
|
validateOptions({
|
||||||
|
validate: normalizePluginOptions,
|
||||||
|
options: {
|
||||||
|
path: 'src/showcase',
|
||||||
|
tags: 'wrong.yaml',
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
await expect(
|
||||||
|
plugin.loadContent!(),
|
||||||
|
).rejects.toThrowErrorMatchingInlineSnapshot(
|
||||||
|
`"Failed to read tags file for showcase"`,
|
||||||
|
);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -8,7 +8,6 @@
|
||||||
import fs from 'fs-extra';
|
import fs from 'fs-extra';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import {
|
import {
|
||||||
aliasedSitePathToRelativePath,
|
|
||||||
getFolderContainingFile,
|
getFolderContainingFile,
|
||||||
getPluginI18nPath,
|
getPluginI18nPath,
|
||||||
Globby,
|
Globby,
|
||||||
|
@ -42,31 +41,6 @@ function createTagSchema(tags: string[]): Joi.Schema {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getTagsList(filePath: string | TagOption[]): Promise<string[]> {
|
|
||||||
if (typeof filePath === 'object') {
|
|
||||||
return Object.keys(filePath);
|
|
||||||
}
|
|
||||||
|
|
||||||
const rawYaml = await fs.readFile(
|
|
||||||
// todo should we use aliasedPath ?
|
|
||||||
// because it breaks tests showcase/index.test.ts#L27-L28
|
|
||||||
aliasedSitePathToRelativePath(filePath),
|
|
||||||
'utf-8',
|
|
||||||
);
|
|
||||||
const unsafeYaml = Yaml.load(rawYaml);
|
|
||||||
const safeYaml = tagSchema.validate(unsafeYaml);
|
|
||||||
|
|
||||||
if (safeYaml.error) {
|
|
||||||
throw new Error(
|
|
||||||
`There was an error extracting tags: ${safeYaml.error.message}`,
|
|
||||||
{cause: safeYaml.error},
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const tagLabels = Object.keys(safeYaml.value);
|
|
||||||
return tagLabels;
|
|
||||||
}
|
|
||||||
|
|
||||||
export default function pluginContentShowcase(
|
export default function pluginContentShowcase(
|
||||||
context: LoadContext,
|
context: LoadContext,
|
||||||
options: PluginOptions,
|
options: PluginOptions,
|
||||||
|
@ -82,6 +56,34 @@ export default function pluginContentShowcase(
|
||||||
}),
|
}),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
async function getTagsList(
|
||||||
|
configTags: string | TagOption[],
|
||||||
|
): Promise<string[]> {
|
||||||
|
if (typeof configTags === 'object') {
|
||||||
|
return Object.keys(configTags);
|
||||||
|
}
|
||||||
|
|
||||||
|
const tagsPath = path.resolve(contentPaths.contentPath, configTags);
|
||||||
|
|
||||||
|
try {
|
||||||
|
const rawYaml = await fs.readFile(tagsPath, 'utf-8');
|
||||||
|
const unsafeYaml = Yaml.load(rawYaml);
|
||||||
|
const safeYaml = tagSchema.validate(unsafeYaml);
|
||||||
|
|
||||||
|
if (safeYaml.error) {
|
||||||
|
throw new Error(
|
||||||
|
`There was an error extracting tags: ${safeYaml.error.message}`,
|
||||||
|
{cause: safeYaml.error},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const tagLabels = Object.keys(safeYaml.value);
|
||||||
|
return tagLabels;
|
||||||
|
} catch (error) {
|
||||||
|
throw new Error(`Failed to read tags file for showcase`, {cause: error});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
name: 'docusaurus-plugin-content-showcase',
|
name: 'docusaurus-plugin-content-showcase',
|
||||||
|
|
||||||
|
@ -97,7 +99,6 @@ export default function pluginContentShowcase(
|
||||||
if (!(await fs.pathExists(contentPaths.contentPath))) {
|
if (!(await fs.pathExists(contentPaths.contentPath))) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
console.log('contentPaths:', contentPaths);
|
|
||||||
|
|
||||||
const {include} = options;
|
const {include} = options;
|
||||||
|
|
||||||
|
|
|
@ -16,7 +16,7 @@ export const DEFAULT_OPTIONS: PluginOptions = {
|
||||||
routeBasePath: '/', // URL Route.
|
routeBasePath: '/', // URL Route.
|
||||||
include: ['**/*.{yml,yaml}'],
|
include: ['**/*.{yml,yaml}'],
|
||||||
exclude: [...GlobExcludeDefault, 'tags.*'],
|
exclude: [...GlobExcludeDefault, 'tags.*'],
|
||||||
tags: '@site/showcase/tags.yaml',
|
tags: 'tags.yaml',
|
||||||
};
|
};
|
||||||
|
|
||||||
export const tagSchema = Joi.object().pattern(
|
export const tagSchema = Joi.object().pattern(
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue