wip tests

This commit is contained in:
ozakione 2024-04-08 15:11:01 +02:00
parent 9eab6aecfe
commit 0fdb9d66da
4 changed files with 143 additions and 93 deletions

View file

@ -1,40 +0,0 @@
/**
* 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 {validateShowcaseItem} from '../validation';
import type {ShowcaseItem} from '@docusaurus/plugin-content-showcase';
// todo broken
describe('showcase front matter schema', () => {
it('accepts valid frontmatter', () => {
const item: ShowcaseItem = {
title: 'title',
description: 'description',
preview: 'preview',
source: 'source',
tags: [],
website: 'website',
};
expect(validateShowcaseItem({items: item, tagsSchema, tags})).toEqual(item);
});
it('reject invalid frontmatter', () => {
const frontMatter = {};
expect(() =>
validateShowcaseItem(frontMatter),
).toThrowErrorMatchingInlineSnapshot(
`"Cannot read properties of undefined (reading 'validate')"`,
);
});
it('reject invalid frontmatter value', () => {
const frontMatter = {title: 42};
expect(() =>
validateShowcaseItem(frontMatter),
).toThrowErrorMatchingInlineSnapshot(
`"Cannot read properties of undefined (reading 'validate')"`,
);
});
});

View file

@ -8,82 +8,80 @@
import path from 'path'; import path from 'path';
import {loadContext} from '@docusaurus/core/src/server/site'; import {loadContext} from '@docusaurus/core/src/server/site';
import {normalizePluginOptions} from '@docusaurus/utils-validation'; import {normalizePluginOptions} from '@docusaurus/utils-validation';
import {fromPartial} from '@total-typescript/shoehorn';
import pluginContentPages from '../index'; import pluginContentPages from '../index';
import {validateOptions} from '../options'; import {validateOptions} from '../options';
import type {PluginOptions} from '@docusaurus/plugin-content-showcase';
const loadPluginContent = async (siteDir: string, options: PluginOptions) => {
const context = await loadContext({siteDir});
const plugin = await pluginContentPages(
context,
validateOptions({
validate: normalizePluginOptions,
options,
}),
);
return plugin.loadContent!();
};
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');
const context = await loadContext({siteDir}); const showcaseMetadata = await loadPluginContent(
const plugin = pluginContentPages( siteDir,
context, fromPartial({
validateOptions({ path: 'src/showcase',
validate: normalizePluginOptions, tags: 'tags.yaml',
options: {
path: 'src/showcase',
tags: 'tags.yaml',
},
}), }),
); );
const showcaseMetadata = await plugin.loadContent!();
expect(showcaseMetadata).toMatchSnapshot(); expect(showcaseMetadata).toMatchSnapshot();
}); });
it('loads simple showcase with tags in options', async () => { it('loads simple showcase with tags in options', async () => {
const siteDir = path.join(__dirname, '__fixtures__', 'website'); const tags = {
const context = await loadContext({siteDir}); opensource: {
const plugin = pluginContentPages( label: 'Open-Source',
context, description: {
validateOptions({ message:
validate: normalizePluginOptions, 'Open-Source Docusaurus sites can be useful for inspiration!',
options: { id: 'showcase.tag.opensource.description',
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',
},
},
}, },
color: '#39ca30',
},
meta: {
label: 'Meta',
description: {
message: 'Docusaurus sites of Meta (formerly Facebook) projects',
id: 'showcase.tag.meta.description',
},
color: '#4267b2',
},
};
const siteDir = path.join(__dirname, '__fixtures__', 'website');
const showcaseMetadata = await loadPluginContent(
siteDir,
fromPartial({
path: 'src/showcase',
tags,
}), }),
); );
const showcaseMetadata = await plugin.loadContent!();
expect(showcaseMetadata).toMatchSnapshot(); expect(showcaseMetadata).toMatchSnapshot();
}); });
it('throw loading inexistant tags file', async () => { it('throw loading inexistant tags file', async () => {
const siteDir = path.join(__dirname, '__fixtures__', 'website'); const siteDir = path.join(__dirname, '__fixtures__', 'website');
const context = await loadContext({siteDir}); await expect(async () => {
const plugin = pluginContentPages( await loadPluginContent(
context, siteDir,
validateOptions({ fromPartial({
validate: normalizePluginOptions,
options: {
path: 'src/showcase', path: 'src/showcase',
tags: 'wrong.yaml', tags: 'wrong.yaml',
}, }),
}), );
); }).rejects.toThrowErrorMatchingInlineSnapshot(
await expect(
plugin.loadContent!(),
).rejects.toThrowErrorMatchingInlineSnapshot(
`"Failed to read tags file for showcase"`, `"Failed to read tags file for showcase"`,
); );
}); });

View file

@ -0,0 +1,89 @@
/**
* 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 {createShowcaseItemSchema, validateShowcaseItem} from '../validation';
import {getTagsList} from '../tags';
import type {ShowcaseItem} from '@docusaurus/plugin-content-showcase';
const tags = {
favorite: {
label: 'Favorite',
description: {
message:
'Our favorite Docusaurus sites that you must absolutely check out!',
id: 'showcase.tag.favorite.description',
},
color: '#e9669e',
},
opensource: {
label: 'Open-Source',
description: {
message: 'Open-Source Docusaurus sites can be useful for inspiration!',
id: 'showcase.tag.opensource.description',
},
color: '#39ca30',
},
};
async function prepareSchema() {
const tagList = await getTagsList({
configTags: tags,
configPath: '',
});
return createShowcaseItemSchema(tagList);
}
// todo broken
describe('showcase item schema', () => {
it('accepts valid item', async () => {
const item: ShowcaseItem = {
title: 'title',
description: 'description',
preview: 'preview',
source: 'source',
tags: [],
website: 'website',
};
const showcaseItemSchema = await prepareSchema();
expect(validateShowcaseItem({item, showcaseItemSchema})).toEqual(item);
});
it('reject invalid tags', async () => {
const item: ShowcaseItem = {
title: 'title',
description: 'description',
preview: 'preview',
source: 'source',
tags: ['invalid'],
website: 'website',
};
const showcaseItemSchema = await prepareSchema();
expect(() =>
validateShowcaseItem({item, showcaseItemSchema}),
).toThrowErrorMatchingInlineSnapshot(
`""tags[0]" must be one of [favorite, opensource]"`,
);
});
it('reject invalid item', async () => {
const item = {};
const showcaseItemSchema = await prepareSchema();
expect(() =>
validateShowcaseItem({item, showcaseItemSchema}),
).toThrowErrorMatchingInlineSnapshot(
`""title" is required. "description" is required. "preview" is required. "website" is required. "source" is required"`,
);
});
it('reject invalid item value', async () => {
const item = {title: 42};
const showcaseItemSchema = await prepareSchema();
expect(() =>
validateShowcaseItem({item, showcaseItemSchema}),
).toThrowErrorMatchingInlineSnapshot(
`""title" must be a string. "description" is required. "preview" is required. "website" is required. "source" is required"`,
);
});
});

View file

@ -19,7 +19,10 @@ export const tagSchema = Joi.object().pattern(
message: Joi.string().required(), message: Joi.string().required(),
id: Joi.string().required(), id: Joi.string().required(),
}).required(), }).required(),
color: Joi.string().required(), color: Joi.string()
// todo doesn't seems to work ???
.regex(/^#[\dA-Fa-f]{6}$/)
.required(),
}), }),
); );