add tests

This commit is contained in:
ozakione 2024-04-03 14:59:26 +02:00
parent 8ab6ddac54
commit 45af11fd13
5 changed files with 113 additions and 34 deletions

View file

@ -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/",
},
],
}
`;

View file

@ -45,7 +45,7 @@ function testField(params: {
try {
validateShowcaseFrontMatter(frontMatter);
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,
null,
2,
@ -61,7 +61,7 @@ function testField(params: {
});
}
describe('doc front matter schema', () => {
describe('showcase front matter schema', () => {
it('accepts valid frontmatter', () => {
const frontMatter: ShowcaseFrontMatter = {
title: 'title',
@ -97,5 +97,11 @@ describe('validateShowcaseFrontMatter full', () => {
website: 'website',
},
],
invalidFrontMatters: [
[
{},
'"title" is required. "description" is required. "preview" is required. "website" is required. "source" is required. "tags" is required',
],
],
});
});

View file

@ -12,9 +12,6 @@ import {normalizePluginOptions} from '@docusaurus/utils-validation';
import pluginContentPages from '../index';
import {validateOptions} from '../options';
// todo add test with tags in config
// todo add test with tags in yaml
describe('docusaurus-plugin-content-showcase', () => {
it('loads simple showcase', async () => {
const siteDir = path.join(__dirname, '__fixtures__', 'website');
@ -24,8 +21,8 @@ describe('docusaurus-plugin-content-showcase', () => {
validateOptions({
validate: normalizePluginOptions,
options: {
// todo broken because we use aliasedPaths
path: 'src/showcase',
tags: 'tags.yaml',
},
}),
);
@ -33,4 +30,61 @@ describe('docusaurus-plugin-content-showcase', () => {
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"`,
);
});
});

View file

@ -8,7 +8,6 @@
import fs from 'fs-extra';
import path from 'path';
import {
aliasedSitePathToRelativePath,
getFolderContainingFile,
getPluginI18nPath,
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(
context: LoadContext,
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 {
name: 'docusaurus-plugin-content-showcase',
@ -97,7 +99,6 @@ export default function pluginContentShowcase(
if (!(await fs.pathExists(contentPaths.contentPath))) {
return null;
}
console.log('contentPaths:', contentPaths);
const {include} = options;

View file

@ -16,7 +16,7 @@ export const DEFAULT_OPTIONS: PluginOptions = {
routeBasePath: '/', // URL Route.
include: ['**/*.{yml,yaml}'],
exclude: [...GlobExcludeDefault, 'tags.*'],
tags: '@site/showcase/tags.yaml',
tags: 'tags.yaml',
};
export const tagSchema = Joi.object().pattern(