Skip to content

Commit

Permalink
test: improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh-Cena committed Apr 16, 2022
1 parent 44966e1 commit 46cb916
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 14 deletions.
16 changes: 8 additions & 8 deletions packages/docusaurus-logger/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
11 changes: 9 additions & 2 deletions packages/docusaurus-utils/src/__tests__/globUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,5 +132,10 @@ describe('<Interpolate>', () => {
).toThrowErrorMatchingInlineSnapshot(
`"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"`,
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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/",
},
],
}
`;
Original file line number Diff line number Diff line change
Expand Up @@ -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});
},
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
],
Expand Down
5 changes: 2 additions & 3 deletions packages/docusaurus/src/server/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
};

Expand Down

0 comments on commit 46cb916

Please sign in to comment.