mirror of
https://github.com/facebook/docusaurus.git
synced 2025-07-22 19:17:46 +02:00
wip tests
This commit is contained in:
parent
9eab6aecfe
commit
0fdb9d66da
4 changed files with 143 additions and 93 deletions
|
@ -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')"`,
|
||||
);
|
||||
});
|
||||
});
|
|
@ -8,82 +8,80 @@
|
|||
import path from 'path';
|
||||
import {loadContext} from '@docusaurus/core/src/server/site';
|
||||
import {normalizePluginOptions} from '@docusaurus/utils-validation';
|
||||
|
||||
import {fromPartial} from '@total-typescript/shoehorn';
|
||||
import pluginContentPages from '../index';
|
||||
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', () => {
|
||||
it('loads simple showcase', 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: 'tags.yaml',
|
||||
},
|
||||
const showcaseMetadata = await loadPluginContent(
|
||||
siteDir,
|
||||
fromPartial({
|
||||
path: 'src/showcase',
|
||||
tags: 'tags.yaml',
|
||||
}),
|
||||
);
|
||||
const showcaseMetadata = await plugin.loadContent!();
|
||||
|
||||
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 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 siteDir = path.join(__dirname, '__fixtures__', 'website');
|
||||
const showcaseMetadata = await loadPluginContent(
|
||||
siteDir,
|
||||
fromPartial({
|
||||
path: 'src/showcase',
|
||||
tags,
|
||||
}),
|
||||
);
|
||||
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: {
|
||||
await expect(async () => {
|
||||
await loadPluginContent(
|
||||
siteDir,
|
||||
fromPartial({
|
||||
path: 'src/showcase',
|
||||
tags: 'wrong.yaml',
|
||||
},
|
||||
}),
|
||||
);
|
||||
await expect(
|
||||
plugin.loadContent!(),
|
||||
).rejects.toThrowErrorMatchingInlineSnapshot(
|
||||
}),
|
||||
);
|
||||
}).rejects.toThrowErrorMatchingInlineSnapshot(
|
||||
`"Failed to read tags file for showcase"`,
|
||||
);
|
||||
});
|
||||
|
|
|
@ -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"`,
|
||||
);
|
||||
});
|
||||
});
|
|
@ -19,7 +19,10 @@ export const tagSchema = Joi.object().pattern(
|
|||
message: Joi.string().required(),
|
||||
id: Joi.string().required(),
|
||||
}).required(),
|
||||
color: Joi.string().required(),
|
||||
color: Joi.string()
|
||||
// todo doesn't seems to work ???
|
||||
.regex(/^#[\dA-Fa-f]{6}$/)
|
||||
.required(),
|
||||
}),
|
||||
);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue