mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-12 16:47:26 +02:00
Refactor + add more tests (Part 1) (#847)
* Refactor mdToHtml out * Refactor routing + move it to server instead of core * Refactor & Add more tests for server utils * Refactor isSeparateCss function from server & generate * Refactor insertTableOfContents from server & generate + add tests * undo small nits
This commit is contained in:
parent
a7a214fb3a
commit
defcbcc8ee
14 changed files with 322 additions and 235 deletions
|
@ -8,6 +8,24 @@ const path = require('path');
|
|||
const fs = require('fs');
|
||||
const utils = require('../utils');
|
||||
|
||||
jest.mock('../env', () => ({
|
||||
translation: {
|
||||
enabled: true,
|
||||
enabledLanguages: () => [
|
||||
{
|
||||
enabled: true,
|
||||
name: 'English',
|
||||
tag: 'en',
|
||||
},
|
||||
{
|
||||
enabled: true,
|
||||
name: '日本語',
|
||||
tag: 'ja',
|
||||
},
|
||||
],
|
||||
},
|
||||
}));
|
||||
|
||||
describe('server utils', () => {
|
||||
test('minify css', () => {
|
||||
const testCss = fs.readFileSync(
|
||||
|
@ -21,4 +39,44 @@ describe('server utils', () => {
|
|||
utils.minifyCss(testCss).then(css => expect(css).toMatchSnapshot());
|
||||
utils.minifyCss(notCss).catch(e => expect(e).toMatchSnapshot());
|
||||
});
|
||||
|
||||
test('getLanguage', () => {
|
||||
const testDocEnglish = path.join('translated_docs', 'en', 'test.md');
|
||||
const testDocJapanese = path.join('translated_docs', 'ja', 'test.md');
|
||||
const testDocJapaneseInSubfolder = path.join(
|
||||
'translated_docs',
|
||||
'ja',
|
||||
'en',
|
||||
'test.md'
|
||||
);
|
||||
const testDocInSubfolder = path.join('docs', 'ro', 'test.md');
|
||||
const testDocNoLanguage = path.join('docs', 'test.md');
|
||||
expect(utils.getLanguage(testDocEnglish, 'translated_docs')).toBe('en');
|
||||
expect(utils.getLanguage(testDocJapanese, 'translated_docs')).toBe('ja');
|
||||
expect(
|
||||
utils.getLanguage(testDocJapaneseInSubfolder, 'translated_docs')
|
||||
).toBe('ja');
|
||||
expect(utils.getLanguage(testDocInSubfolder, 'docs')).toBeNull();
|
||||
expect(utils.getLanguage(testDocNoLanguage, 'docs')).toBeNull();
|
||||
});
|
||||
|
||||
test('getSubdir', () => {
|
||||
const docA = path.join('docs', 'endiliey', 'a.md');
|
||||
const docB = path.join('docs', 'nus', 'hackers', 'b.md');
|
||||
const docC = path.join('docs', 'c.md');
|
||||
const docD = path.join('website', 'translated_docs', 'wow', 'd.md');
|
||||
const docE = path.join('website', 'translated_docs', 'lol', 'lah', 'e.md');
|
||||
const docsDir = path.join('docs');
|
||||
const translatedDir = path.join('website', 'translated_docs');
|
||||
expect(utils.getSubDir(docA, docsDir)).toEqual('endiliey');
|
||||
expect(utils.getSubDir(docA, translatedDir)).toBeNull();
|
||||
expect(utils.getSubDir(docB, docsDir)).toEqual('nus/hackers');
|
||||
expect(utils.getSubDir(docB, translatedDir)).toBeNull();
|
||||
expect(utils.getSubDir(docC, docsDir)).toBeNull();
|
||||
expect(utils.getSubDir(docC, translatedDir)).toBeNull();
|
||||
expect(utils.getSubDir(docD, docsDir)).toBeNull();
|
||||
expect(utils.getSubDir(docD, translatedDir)).toEqual('wow');
|
||||
expect(utils.getSubDir(docE, docsDir)).toBeNull();
|
||||
expect(utils.getSubDir(docE, translatedDir)).toEqual('lol/lah');
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue