From 46cb91665f6b24ebd8618800db4bd346de5b94e7 Mon Sep 17 00:00:00 2001 From: Joshua Chen Date: Sat, 16 Apr 2022 16:20:35 +0800 Subject: [PATCH] test: improve test coverage --- packages/docusaurus-logger/src/index.ts | 16 ++++++++-------- .../src/__tests__/globUtils.test.ts | 11 +++++++++-- .../exports/__tests__/Interpolate.test.tsx | 5 +++++ .../__tests__/__snapshots__/index.test.ts.snap | 16 +++++++++++++++- .../src/server/plugins/__tests__/index.test.ts | 6 ++++++ .../src/server/plugins/__tests__/presets.test.ts | 3 +++ packages/docusaurus/src/server/routes.ts | 5 ++--- 7 files changed, 48 insertions(+), 14 deletions(-) diff --git a/packages/docusaurus-logger/src/index.ts b/packages/docusaurus-logger/src/index.ts index 08e985e77ed4..7d5b577dff26 100644 --- a/packages/docusaurus-logger/src/index.ts +++ b/packages/docusaurus-logger/src/index.ts @@ -5,16 +5,16 @@ * LICENSE file in the root directory of this source tree. */ -import chalk, {type Chalk} from 'chalk'; +import chalk from 'chalk'; type InterpolatableValue = string | number | (string | number)[]; -const path = (msg: unknown): string => chalk.cyan(chalk.underline(`"${msg}"`)); -const url = (msg: unknown): string => chalk.cyan(chalk.underline(msg)); -const name = (msg: unknown): string => chalk.blue(chalk.bold(msg)); +const path = (msg: unknown): string => chalk.cyan.underline(`"${msg}"`); +const url = (msg: unknown): string => chalk.cyan.underline(msg); +const name = (msg: unknown): string => chalk.blue.bold(msg); const code = (msg: unknown): string => chalk.cyan(`\`${msg}\``); -const subdue: Chalk = chalk.gray; -const num: Chalk = chalk.yellow; +const subdue = (msg: unknown): string => chalk.gray(msg); +const num = (msg: unknown): string => chalk.yellow(msg); function interpolate( msgs: TemplateStringsArray, @@ -69,7 +69,7 @@ function info( ): void; function info(msg: unknown, ...values: InterpolatableValue[]): void { console.info( - `${chalk.cyan(chalk.bold('[INFO]'))} ${ + `${chalk.cyan.bold('[INFO]')} ${ values.length === 0 ? stringify(msg) : interpolate(msg as TemplateStringsArray, ...values) @@ -115,7 +115,7 @@ function success( ): void; function success(msg: unknown, ...values: InterpolatableValue[]): void { console.log( - `${chalk.green(chalk.bold('[SUCCESS]'))} ${ + `${chalk.green.bold('[SUCCESS]')} ${ values.length === 0 ? stringify(msg) : interpolate(msg as TemplateStringsArray, ...values) diff --git a/packages/docusaurus-utils/src/__tests__/globUtils.test.ts b/packages/docusaurus-utils/src/__tests__/globUtils.test.ts index e2f3d52f2c1f..c35e164920ba 100644 --- a/packages/docusaurus-utils/src/__tests__/globUtils.test.ts +++ b/packages/docusaurus-utils/src/__tests__/globUtils.test.ts @@ -12,9 +12,8 @@ import { } from '../globUtils'; describe('createMatcher', () => { - const matcher = createMatcher(GlobExcludeDefault); - it('match default exclude MD/MDX partials correctly', () => { + const matcher = createMatcher(GlobExcludeDefault); expect(matcher('doc.md')).toBe(false); expect(matcher('category/doc.md')).toBe(false); expect(matcher('category/subcategory/doc.md')).toBe(false); @@ -32,6 +31,7 @@ describe('createMatcher', () => { }); it('match default exclude tests correctly', () => { + const matcher = createMatcher(GlobExcludeDefault); expect(matcher('xyz.js')).toBe(false); expect(matcher('xyz.ts')).toBe(false); expect(matcher('xyz.jsx')).toBe(false); @@ -63,6 +63,13 @@ describe('createMatcher', () => { expect(matcher('folder/__tests__/xyz.jsx')).toBe(true); expect(matcher('folder/__tests__/xyz.tsx')).toBe(true); }); + + it('matches nothing given nothing', () => { + const matcher = createMatcher([]); + expect(matcher('foo')).toBe(false); + expect(matcher('')).toBe(false); + expect(matcher('we/are/the/champions')).toBe(false); + }); }); describe('createAbsoluteFilePathMatcher', () => { diff --git a/packages/docusaurus/src/client/exports/__tests__/Interpolate.test.tsx b/packages/docusaurus/src/client/exports/__tests__/Interpolate.test.tsx index ac9d430955c7..57f944d58b8f 100644 --- a/packages/docusaurus/src/client/exports/__tests__/Interpolate.test.tsx +++ b/packages/docusaurus/src/client/exports/__tests__/Interpolate.test.tsx @@ -132,5 +132,10 @@ describe('', () => { ).toThrowErrorMatchingInlineSnapshot( `"The Docusaurus component only accept simple string values. Received: React element"`, ); + expect(() => + renderer.create({null}), + ).toThrowErrorMatchingInlineSnapshot( + `"The Docusaurus component only accept simple string values. Received: object"`, + ); }); }); diff --git a/packages/docusaurus/src/server/plugins/__tests__/__snapshots__/index.test.ts.snap b/packages/docusaurus/src/server/plugins/__tests__/__snapshots__/index.test.ts.snap index 688eaa837af2..76e6a9bee903 100644 --- a/packages/docusaurus/src/server/plugins/__tests__/__snapshots__/index.test.ts.snap +++ b/packages/docusaurus/src/server/plugins/__tests__/__snapshots__/index.test.ts.snap @@ -63,6 +63,20 @@ exports[`loadPlugins loads plugins 1`] = ` }, }, ], - "pluginsRouteConfigs": [], + "pluginsRouteConfigs": [ + { + "component": "Comp", + "context": { + "data": { + "content": "path", + }, + "plugin": "/packages/docusaurus/src/server/plugins/__tests__/__fixtures__/site-with-plugin/.docusaurus/test1/default/plugin-route-context-module-100.json", + }, + "modules": { + "content": "path", + }, + "path": "foo/", + }, + ], } `; diff --git a/packages/docusaurus/src/server/plugins/__tests__/index.test.ts b/packages/docusaurus/src/server/plugins/__tests__/index.test.ts index d585d644c4af..7e3ae0f428e2 100644 --- a/packages/docusaurus/src/server/plugins/__tests__/index.test.ts +++ b/packages/docusaurus/src/server/plugins/__tests__/index.test.ts @@ -31,6 +31,12 @@ describe('loadPlugins', () => { return this.prop; }, async contentLoaded({content, actions}) { + actions.addRoute({ + path: 'foo', + component: 'Comp', + modules: {content: 'path'}, + context: {content: 'path'}, + }); actions.setGlobalData({content, prop: this.prop}); }, }), diff --git a/packages/docusaurus/src/server/plugins/__tests__/presets.test.ts b/packages/docusaurus/src/server/plugins/__tests__/presets.test.ts index a50117e371c6..10d50a29506a 100644 --- a/packages/docusaurus/src/server/plugins/__tests__/presets.test.ts +++ b/packages/docusaurus/src/server/plugins/__tests__/presets.test.ts @@ -129,6 +129,9 @@ describe('loadPresets', () => { path.join(__dirname, '__fixtures__/presets/preset-plugins.js'), {docs: {path: '../'}}, ], + false, + null, + undefined, path.join(__dirname, '__fixtures__/presets/preset-themes.js'), path.join(__dirname, '__fixtures__/presets/preset-mixed.js'), ], diff --git a/packages/docusaurus/src/server/routes.ts b/packages/docusaurus/src/server/routes.ts index 733bf0bc2f51..87cfedf87a82 100644 --- a/packages/docusaurus/src/server/routes.ts +++ b/packages/docusaurus/src/server/routes.ts @@ -272,9 +272,8 @@ ${JSON.stringify(routeConfig)}`, res.routesChunkNames[`${routePath}-${routeHash}`] = { // Avoid clash with a prop called "component" ...genChunkNames({__comp: component}, 'component', component, res), - ...(context - ? genChunkNames({__context: context}, 'context', routePath, res) - : {}), + ...(context && + genChunkNames({__context: context}, 'context', routePath, res)), ...genChunkNames(modules, 'module', routePath, res), };