From 45af11fd134226efad9b5c9599a7cf4a274e05a2 Mon Sep 17 00:00:00 2001 From: ozakione <29860391+OzakIOne@users.noreply.github.com> Date: Wed, 3 Apr 2024 14:59:26 +0200 Subject: [PATCH] add tests --- .../__snapshots__/index.test.ts.snap | 18 ++++++ .../src/__tests__/frontMatter.test.ts | 10 ++- .../src/__tests__/index.test.ts | 62 +++++++++++++++++-- .../src/index.ts | 55 ++++++++-------- .../src/options.ts | 2 +- 5 files changed, 113 insertions(+), 34 deletions(-) diff --git a/packages/docusaurus-plugin-content-showcase/src/__tests__/__snapshots__/index.test.ts.snap b/packages/docusaurus-plugin-content-showcase/src/__tests__/__snapshots__/index.test.ts.snap index 097bb675fe..512898564a 100644 --- a/packages/docusaurus-plugin-content-showcase/src/__tests__/__snapshots__/index.test.ts.snap +++ b/packages/docusaurus-plugin-content-showcase/src/__tests__/__snapshots__/index.test.ts.snap @@ -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/", + }, + ], +} +`; diff --git a/packages/docusaurus-plugin-content-showcase/src/__tests__/frontMatter.test.ts b/packages/docusaurus-plugin-content-showcase/src/__tests__/frontMatter.test.ts index 6de639727f..ab394f129b 100644 --- a/packages/docusaurus-plugin-content-showcase/src/__tests__/frontMatter.test.ts +++ b/packages/docusaurus-plugin-content-showcase/src/__tests__/frontMatter.test.ts @@ -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', + ], + ], }); }); 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 f5b22f535f..97bd15c758 100644 --- a/packages/docusaurus-plugin-content-showcase/src/__tests__/index.test.ts +++ b/packages/docusaurus-plugin-content-showcase/src/__tests__/index.test.ts @@ -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"`, + ); + }); }); diff --git a/packages/docusaurus-plugin-content-showcase/src/index.ts b/packages/docusaurus-plugin-content-showcase/src/index.ts index 371774fe2d..7b372479df 100644 --- a/packages/docusaurus-plugin-content-showcase/src/index.ts +++ b/packages/docusaurus-plugin-content-showcase/src/index.ts @@ -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 { - 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 { + 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; diff --git a/packages/docusaurus-plugin-content-showcase/src/options.ts b/packages/docusaurus-plugin-content-showcase/src/options.ts index 3c97232420..b4b197f0ea 100644 --- a/packages/docusaurus-plugin-content-showcase/src/options.ts +++ b/packages/docusaurus-plugin-content-showcase/src/options.ts @@ -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(