test: improve test coverage (#7181)

This commit is contained in:
Joshua Chen 2022-04-16 16:35:40 +08:00 committed by GitHub
parent 44966e19e9
commit f219a2ac90
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 48 additions and 14 deletions

View file

@ -5,16 +5,16 @@
* LICENSE file in the root directory of this source tree. * 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)[]; type InterpolatableValue = string | number | (string | number)[];
const path = (msg: unknown): string => chalk.cyan(chalk.underline(`"${msg}"`)); const path = (msg: unknown): string => chalk.cyan.underline(`"${msg}"`);
const url = (msg: unknown): string => chalk.cyan(chalk.underline(msg)); const url = (msg: unknown): string => chalk.cyan.underline(msg);
const name = (msg: unknown): string => chalk.blue(chalk.bold(msg)); const name = (msg: unknown): string => chalk.blue.bold(msg);
const code = (msg: unknown): string => chalk.cyan(`\`${msg}\``); const code = (msg: unknown): string => chalk.cyan(`\`${msg}\``);
const subdue: Chalk = chalk.gray; const subdue = (msg: unknown): string => chalk.gray(msg);
const num: Chalk = chalk.yellow; const num = (msg: unknown): string => chalk.yellow(msg);
function interpolate( function interpolate(
msgs: TemplateStringsArray, msgs: TemplateStringsArray,
@ -69,7 +69,7 @@ function info(
): void; ): void;
function info(msg: unknown, ...values: InterpolatableValue[]): void { function info(msg: unknown, ...values: InterpolatableValue[]): void {
console.info( console.info(
`${chalk.cyan(chalk.bold('[INFO]'))} ${ `${chalk.cyan.bold('[INFO]')} ${
values.length === 0 values.length === 0
? stringify(msg) ? stringify(msg)
: interpolate(msg as TemplateStringsArray, ...values) : interpolate(msg as TemplateStringsArray, ...values)
@ -115,7 +115,7 @@ function success(
): void; ): void;
function success(msg: unknown, ...values: InterpolatableValue[]): void { function success(msg: unknown, ...values: InterpolatableValue[]): void {
console.log( console.log(
`${chalk.green(chalk.bold('[SUCCESS]'))} ${ `${chalk.green.bold('[SUCCESS]')} ${
values.length === 0 values.length === 0
? stringify(msg) ? stringify(msg)
: interpolate(msg as TemplateStringsArray, ...values) : interpolate(msg as TemplateStringsArray, ...values)

View file

@ -12,9 +12,8 @@ import {
} from '../globUtils'; } from '../globUtils';
describe('createMatcher', () => { describe('createMatcher', () => {
const matcher = createMatcher(GlobExcludeDefault);
it('match default exclude MD/MDX partials correctly', () => { it('match default exclude MD/MDX partials correctly', () => {
const matcher = createMatcher(GlobExcludeDefault);
expect(matcher('doc.md')).toBe(false); expect(matcher('doc.md')).toBe(false);
expect(matcher('category/doc.md')).toBe(false); expect(matcher('category/doc.md')).toBe(false);
expect(matcher('category/subcategory/doc.md')).toBe(false); expect(matcher('category/subcategory/doc.md')).toBe(false);
@ -32,6 +31,7 @@ describe('createMatcher', () => {
}); });
it('match default exclude tests correctly', () => { it('match default exclude tests correctly', () => {
const matcher = createMatcher(GlobExcludeDefault);
expect(matcher('xyz.js')).toBe(false); expect(matcher('xyz.js')).toBe(false);
expect(matcher('xyz.ts')).toBe(false); expect(matcher('xyz.ts')).toBe(false);
expect(matcher('xyz.jsx')).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.jsx')).toBe(true);
expect(matcher('folder/__tests__/xyz.tsx')).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', () => { describe('createAbsoluteFilePathMatcher', () => {

View file

@ -132,5 +132,10 @@ describe('<Interpolate>', () => {
).toThrowErrorMatchingInlineSnapshot( ).toThrowErrorMatchingInlineSnapshot(
`"The Docusaurus <Interpolate> component only accept simple string values. Received: React element"`, `"The Docusaurus <Interpolate> component only accept simple string values. Received: React element"`,
); );
expect(() =>
renderer.create(<Interpolate>{null}</Interpolate>),
).toThrowErrorMatchingInlineSnapshot(
`"The Docusaurus <Interpolate> component only accept simple string values. Received: object"`,
);
}); });
}); });

View file

@ -63,6 +63,20 @@ exports[`loadPlugins loads plugins 1`] = `
}, },
}, },
], ],
"pluginsRouteConfigs": [], "pluginsRouteConfigs": [
{
"component": "Comp",
"context": {
"data": {
"content": "path",
},
"plugin": "<PROJECT_ROOT>/packages/docusaurus/src/server/plugins/__tests__/__fixtures__/site-with-plugin/.docusaurus/test1/default/plugin-route-context-module-100.json",
},
"modules": {
"content": "path",
},
"path": "foo/",
},
],
} }
`; `;

View file

@ -31,6 +31,12 @@ describe('loadPlugins', () => {
return this.prop; return this.prop;
}, },
async contentLoaded({content, actions}) { async contentLoaded({content, actions}) {
actions.addRoute({
path: 'foo',
component: 'Comp',
modules: {content: 'path'},
context: {content: 'path'},
});
actions.setGlobalData({content, prop: this.prop}); actions.setGlobalData({content, prop: this.prop});
}, },
}), }),

View file

@ -129,6 +129,9 @@ describe('loadPresets', () => {
path.join(__dirname, '__fixtures__/presets/preset-plugins.js'), path.join(__dirname, '__fixtures__/presets/preset-plugins.js'),
{docs: {path: '../'}}, {docs: {path: '../'}},
], ],
false,
null,
undefined,
path.join(__dirname, '__fixtures__/presets/preset-themes.js'), path.join(__dirname, '__fixtures__/presets/preset-themes.js'),
path.join(__dirname, '__fixtures__/presets/preset-mixed.js'), path.join(__dirname, '__fixtures__/presets/preset-mixed.js'),
], ],

View file

@ -272,9 +272,8 @@ ${JSON.stringify(routeConfig)}`,
res.routesChunkNames[`${routePath}-${routeHash}`] = { res.routesChunkNames[`${routePath}-${routeHash}`] = {
// Avoid clash with a prop called "component" // Avoid clash with a prop called "component"
...genChunkNames({__comp: component}, 'component', component, res), ...genChunkNames({__comp: component}, 'component', component, res),
...(context ...(context &&
? genChunkNames({__context: context}, 'context', routePath, res) genChunkNames({__context: context}, 'context', routePath, res)),
: {}),
...genChunkNames(modules, 'module', routePath, res), ...genChunkNames(modules, 'module', routePath, res),
}; };