From 0fdb9d66da361597ee463237261cd19ee8bcc0a6 Mon Sep 17 00:00:00 2001 From: ozakione <29860391+OzakIOne@users.noreply.github.com> Date: Mon, 8 Apr 2024 15:11:01 +0200 Subject: [PATCH] wip tests --- .../src/__tests__/frontMatter.test.ts | 40 ------- .../src/__tests__/index.test.ts | 102 +++++++++--------- .../src/__tests__/validation.test.ts | 89 +++++++++++++++ .../src/tags.ts | 5 +- 4 files changed, 143 insertions(+), 93 deletions(-) delete mode 100644 packages/docusaurus-plugin-content-showcase/src/__tests__/frontMatter.test.ts create mode 100644 packages/docusaurus-plugin-content-showcase/src/__tests__/validation.test.ts diff --git a/packages/docusaurus-plugin-content-showcase/src/__tests__/frontMatter.test.ts b/packages/docusaurus-plugin-content-showcase/src/__tests__/frontMatter.test.ts deleted file mode 100644 index bd97ead995..0000000000 --- a/packages/docusaurus-plugin-content-showcase/src/__tests__/frontMatter.test.ts +++ /dev/null @@ -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')"`, - ); - }); -}); diff --git a/packages/docusaurus-plugin-content-showcase/src/__tests__/index.test.ts b/packages/docusaurus-plugin-content-showcase/src/__tests__/index.test.ts index 97bd15c758..66660d4679 100644 --- a/packages/docusaurus-plugin-content-showcase/src/__tests__/index.test.ts +++ b/packages/docusaurus-plugin-content-showcase/src/__tests__/index.test.ts @@ -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"`, ); }); diff --git a/packages/docusaurus-plugin-content-showcase/src/__tests__/validation.test.ts b/packages/docusaurus-plugin-content-showcase/src/__tests__/validation.test.ts new file mode 100644 index 0000000000..cf93a2c565 --- /dev/null +++ b/packages/docusaurus-plugin-content-showcase/src/__tests__/validation.test.ts @@ -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"`, + ); + }); +}); diff --git a/packages/docusaurus-plugin-content-showcase/src/tags.ts b/packages/docusaurus-plugin-content-showcase/src/tags.ts index 50f818658b..97bf80f50e 100644 --- a/packages/docusaurus-plugin-content-showcase/src/tags.ts +++ b/packages/docusaurus-plugin-content-showcase/src/tags.ts @@ -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(), }), );