mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-11 16:17:25 +02:00
test: improve test coverage (#7181)
This commit is contained in:
parent
44966e19e9
commit
f219a2ac90
7 changed files with 48 additions and 14 deletions
|
@ -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)
|
||||||
|
|
|
@ -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', () => {
|
||||||
|
|
|
@ -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"`,
|
||||||
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -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/",
|
||||||
|
},
|
||||||
|
],
|
||||||
}
|
}
|
||||||
`;
|
`;
|
||||||
|
|
|
@ -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});
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
|
|
|
@ -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'),
|
||||||
],
|
],
|
||||||
|
|
|
@ -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),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue