mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-03 20:27:20 +02:00
test: utils for load
This commit is contained in:
parent
d80a2555c0
commit
cfd11fbb6d
2 changed files with 43 additions and 5 deletions
|
@ -31,13 +31,13 @@ function encodePath(userpath) {
|
|||
}
|
||||
|
||||
function fileToComponentName(file) {
|
||||
let str = file.replace(/([A-Z])/g, ' $1');
|
||||
if (str.length === 1) {
|
||||
return str.toUpperCase();
|
||||
}
|
||||
const ext = extRE.exec(file)[1];
|
||||
let str = file.replace(extRE, '');
|
||||
str = str.replace(/([A-Z])/g, ' $1');
|
||||
str = str.replace(/^[\W_]+|[\W_]+$/g, '').toLowerCase();
|
||||
str = str.charAt(0).toUpperCase() + str.slice(1);
|
||||
return str.replace(/[\W_]+(\w|$)/g, (_, ch) => ch.toUpperCase());
|
||||
str = str.replace(/[\W_]+(\w|$)/g, (_, ch) => ch.toUpperCase());
|
||||
return ext ? ext.toUpperCase() + str : str;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
|
|
38
test/load/utils.test.js
Normal file
38
test/load/utils.test.js
Normal file
|
@ -0,0 +1,38 @@
|
|||
import path from 'path';
|
||||
import {fileToPath, fileToComponentName} from '@lib/load/utils.js';
|
||||
|
||||
describe('load utils', () => {
|
||||
test('fileToComponentName', () => {
|
||||
const asserts = {
|
||||
'index.md': 'MDIndex',
|
||||
'hello/index.md': 'MDHelloIndex',
|
||||
'foo.md': 'MDFoo',
|
||||
'foo-bar.md': 'MDFooBar',
|
||||
'index.js': 'JSIndex',
|
||||
'foobar.js': 'JSFoobar',
|
||||
'docusaurus/index.js': 'JSDocusaurusIndex',
|
||||
'234.md': 'MD234',
|
||||
'2018-07-08-test.md': 'MD20180708Test',
|
||||
'%asd.md': 'MDAsd'
|
||||
};
|
||||
Object.keys(asserts).forEach(file => {
|
||||
expect(fileToComponentName(file)).toBe(asserts[file]);
|
||||
});
|
||||
});
|
||||
|
||||
test('fileToPath', () => {
|
||||
const asserts = {
|
||||
'index.md': '/',
|
||||
'hello/index.md': '/hello/',
|
||||
'foo.md': '/foo',
|
||||
'foo/bar.md': '/foo/bar',
|
||||
'index.js': '/',
|
||||
'hello/index.js': '/hello/',
|
||||
'foo.js': '/foo',
|
||||
'foo/bar.js': '/foo/bar'
|
||||
};
|
||||
Object.keys(asserts).forEach(file => {
|
||||
expect(fileToPath(file)).toBe(asserts[file]);
|
||||
});
|
||||
});
|
||||
});
|
Loading…
Add table
Reference in a new issue