test: genRoutesConfig

This commit is contained in:
endiliey 2018-08-11 01:34:35 +08:00
parent 084063eabe
commit 8a5807418d
3 changed files with 209 additions and 1 deletions

View file

@ -1,6 +1,6 @@
const {fileToComponentName} = require('./utils');
async function genRoutesConfig({docsData, pagesData}) {
async function genRoutesConfig({docsData = [], pagesData = []}) {
function genDocsRoute({path: docsPath, source}) {
const componentName = fileToComponentName(source);
return `

View file

@ -0,0 +1,169 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`genRoutesConfig website with no docs/pages 1`] = `
"import React from 'react';
import Docs from '@theme/Docs';
import NotFound from '@theme/NotFound';
const routes = [,,
{
path: '*',
component: NotFound
}
];
export default routes;
"
`;
exports[`genRoutesConfig website with only docs 1`] = `
"import React from 'react';
import Docs from '@theme/Docs';
import NotFound from '@theme/NotFound';
import MDHello from '@docs/hello.md';
import MDFooBaz from '@docs/foo/baz.md';
import MDFooBar from '@docs/foo/bar.md';
const routes = [
{
path: \\"/hello\\",
exact: true,
component: () => (
<Docs>
<MDHello />
</Docs>
)
},
{
path: \\"/foo/baz\\",
exact: true,
component: () => (
<Docs>
<MDFooBaz />
</Docs>
)
},
{
path: \\"/foo/bar\\",
exact: true,
component: () => (
<Docs>
<MDFooBar />
</Docs>
)
},,
{
path: '*',
component: NotFound
}
];
export default routes;
"
`;
exports[`genRoutesConfig website with only pages 1`] = `
"import React from 'react';
import Docs from '@theme/Docs';
import NotFound from '@theme/NotFound';
import JSIndex from '@pages/index.js';
import JSFoo from '@pages/foo.js';
import JSBarBaz from '@pages/bar/baz.js';
import JSFooIndex from '@pages/foo/index.js';
const routes = [,
{
path: \\"/\\",
exact: true,
component: JSIndex
},
{
path: \\"/foo\\",
exact: true,
component: JSFoo
},
{
path: \\"/bar/baz\\",
exact: true,
component: JSBarBaz
},
{
path: \\"/foo/\\",
exact: true,
component: JSFooIndex
},
{
path: '*',
component: NotFound
}
];
export default routes;
"
`;
exports[`genRoutesConfig website with pages and docs 1`] = `
"import React from 'react';
import Docs from '@theme/Docs';
import NotFound from '@theme/NotFound';
import JSFoo from '@pages/foo.js';
import JSIndex from '@pages/index.js';
import JSBarBaz from '@pages/bar/baz.js';
import JSFooIndex from '@pages/foo/index.js';
import MDHello from '@docs/hello.md';
import MDFooBar from '@docs/foo/bar.md';
import MDFooBaz from '@docs/foo/baz.md';
const routes = [
{
path: \\"/hello\\",
exact: true,
component: () => (
<Docs>
<MDHello />
</Docs>
)
},
{
path: \\"/foo/bar\\",
exact: true,
component: () => (
<Docs>
<MDFooBar />
</Docs>
)
},
{
path: \\"/foo/baz\\",
exact: true,
component: () => (
<Docs>
<MDFooBaz />
</Docs>
)
},
{
path: \\"/foo\\",
exact: true,
component: JSFoo
},
{
path: \\"/\\",
exact: true,
component: JSIndex
},
{
path: \\"/bar/baz\\",
exact: true,
component: JSBarBaz
},
{
path: \\"/foo/\\",
exact: true,
component: JSFooIndex
},
{
path: '*',
component: NotFound
}
];
export default routes;
"
`;

39
test/load/routes.test.js Normal file
View file

@ -0,0 +1,39 @@
import genRoutesConfig from '@lib/load/routes';
import loadDocs from '@lib/load/docs';
import loadPages from '@lib/load/pages';
import path from 'path';
describe('genRoutesConfig', () => {
const pagesDir = path.join(__dirname, '__fixtures__', 'simple-pages');
const docsDir = path.join(__dirname, '__fixtures__', 'simple-docs');
test('website with pages and docs', async () => {
const props = {
docsData: await loadDocs(docsDir),
pagesData: await loadPages(pagesDir)
};
const routes = await genRoutesConfig(props);
expect(routes).toMatchSnapshot();
});
test('website with only pages', async () => {
const props = {
pagesData: await loadPages(pagesDir)
};
const routes = await genRoutesConfig(props);
expect(routes).toMatchSnapshot();
});
test('website with only docs', async () => {
const props = {
docsData: await loadDocs(docsDir)
};
const routes = await genRoutesConfig(props);
expect(routes).toMatchSnapshot();
});
test('website with no docs/pages', async () => {
const routes = await genRoutesConfig({});
expect(routes).toMatchSnapshot();
});
});