fix(v2): docusaurus route config generation for empty path (#1683)

* fix(v2): route config does not acceptempty string

* test snapshot

* add test

* changelog
This commit is contained in:
Endi 2019-07-21 11:14:14 +07:00 committed by GitHub
parent 2a1eb365fd
commit 71283dc7df
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 55 additions and 4 deletions

View file

@ -2,6 +2,8 @@
## Unreleased
- Fix docusaurus route config generation for certain edge case
## 2.0.0-alpha.22
- Add missing dependencies on `@docusaurus/preset-classic`

View file

@ -147,3 +147,42 @@ export default [
],
}
`;
exports[`loadRoutes route config with empty (but valid) path string 1`] = `
Object {
"registry": Object {
"component---hello-world-jse-0-f-b6c": Object {
"importStatement": "() => import(/* webpackChunkName: 'component---hello-world-jse-0-f-b6c' */ \\"hello/world.js\\")",
"modulePath": "hello/world.js",
},
},
"routesChunkNames": Object {
"": Object {
"component": "component---hello-world-jse-0-f-b6c",
},
},
"routesConfig": "
import React from 'react';
import ComponentCreator from '@docusaurus/ComponentCreator';
export default [
{
path: '',
component: ComponentCreator(''),
},
{
path: '*',
component: ComponentCreator('*')
}
];
",
"routesPaths": Array [
"404.html",
"",
],
}
`;

View file

@ -73,7 +73,7 @@ describe('loadRoutes', () => {
} as RouteConfig;
expect(loadRoutes([routeConfigWithoutPath])).rejects.toMatchInlineSnapshot(`
[Error: Invalid routeConfig (Path and component is required)
[Error: Invalid routeConfig (Path must be a string and component is required)
{"component":"hello/world.js"}]
`);
@ -83,8 +83,18 @@ describe('loadRoutes', () => {
expect(loadRoutes([routeConfigWithoutComponent])).rejects
.toMatchInlineSnapshot(`
[Error: Invalid routeConfig (Path and component is required)
[Error: Invalid routeConfig (Path must be a string and component is required)
{"path":"/hello/world"}]
`);
});
test('route config with empty (but valid) path string', async () => {
const routeConfig = {
path: '',
component: 'hello/world.js',
} as RouteConfig;
const result = await loadRoutes([routeConfig]);
expect(result).toMatchSnapshot();
});
});

View file

@ -41,9 +41,9 @@ export async function loadRoutes(pluginsRouteConfigs: RouteConfig[]) {
exact,
} = routeConfig;
if (!routePath || !component) {
if (!_.isString(routePath) || !component) {
throw new Error(
`Invalid routeConfig (Path and component is required) \n${JSON.stringify(
`Invalid routeConfig (Path must be a string and component is required) \n${JSON.stringify(
routeConfig,
)}`,
);