Skip to content

Commit

Permalink
Refactor + add more tests (Part 2) (#854)
Browse files Browse the repository at this point in the history
* Refactor doc in server.js & generate.js

* Refactor finding metadata with find() & avoid param reassign

* Refactor replacing markdown link to html + add test

* Minify func to getFile & add test

* use ./ for md link as well

* nits & better test

* better mdToHtmlify

* babel-register on versions & write-translations to transpile ES6 in server/env.js

* better docs test & move out metadata to fixtures

* Update .babelrc
  • Loading branch information
endiliey authored and yangshun committed Jul 16, 2018
1 parent 03e237a commit 9f718a5
Show file tree
Hide file tree
Showing 13 changed files with 430 additions and 154 deletions.
3 changes: 2 additions & 1 deletion .babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"env": {
"test": {
"presets": [
"env"
"env",
"react"
],
"plugins": [
"transform-class-properties",
Expand Down
6 changes: 6 additions & 0 deletions lib/server/__tests__/__fixtures__/doc1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
id: doc1
title: Document 1
---

Docusaurus is the best :)
18 changes: 18 additions & 0 deletions lib/server/__tests__/__fixtures__/doc2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
id: doc2
title: Document 2
---

### Existing Docs

- [doc1](doc1.md)
- [doc2](./doc2.md)

### Non-existing Docs

- [hahaha](hahaha.md)

## Repeating Docs

- [doc1](doc1.md)
- [doc2](./doc2.md)
116 changes: 116 additions & 0 deletions lib/server/__tests__/__fixtures__/metadata.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
module.exports = {
'en-doc1': {
id: 'en-doc1',
title: 'Document 1',
source: 'doc1.md',
version: 'next',
permalink: 'docs/en/next/doc1.html',
localized_id: 'doc1',
language: 'en',
sidebar: 'docs',
category: 'Test',
next_id: 'doc2',
next: 'en-doc2',
next_title: 'Document 2',
},
'en-doc2': {
id: 'en-doc2',
title: 'Document 2',
source: 'doc2.md',
version: 'next',
permalink: 'docs/en/next/doc2.html',
localized_id: 'doc2',
language: 'en',
sidebar: 'docs',
category: 'Test',
previous_id: 'doc1',
previous: 'en-doc1',
previous_title: 'Document 1',
},
'ko-doc1': {
id: 'ko-doc1',
title: '문서 1',
source: 'doc1.md',
version: 'next',
permalink: 'docs/ko/next/doc1.html',
localized_id: 'doc1',
language: 'ko',
sidebar: 'docs',
category: 'Test',
next_id: 'doc2',
next: 'ko-doc2',
next_title: '문서 2',
},
'ko-doc2': {
id: 'ko-doc2',
title: '문서 2',
source: 'doc2.md',
version: 'next',
permalink: 'docs/ko/next/doc2.html',
localized_id: 'doc2',
language: 'ko',
sidebar: 'docs',
category: 'Test',
previous_id: 'doc1',
previous: 'ko-doc1',
previous_title: '문서 1',
},
'en-version-1.0.0-doc1': {
id: 'en-version-1.0.0-doc1',
original_id: 'doc1',
title: 'Document 1',
source: 'version-1.0.0/doc1.md',
version: '1.0.0',
permalink: 'docs/en/doc1.html',
localized_id: 'version-1.0.0-doc1',
language: 'en',
sidebar: 'version-1.0.0-docs',
category: 'Test',
next_id: 'doc2',
next: 'en-version-1.0.0-doc2',
next_title: 'Document 2',
},
'en-version-1.0.0-doc2': {
id: 'en-version-1.0.0-doc2',
original_id: 'doc2',
title: 'Document 2',
source: 'version-1.0.0/doc2.md',
version: '1.0.0',
permalink: 'docs/en/doc2.html',
localized_id: 'version-1.0.0-doc2',
language: 'en',
sidebar: 'version-1.0.0-docs',
category: 'Test',
previous_id: 'doc1',
previous: 'en-version-1.0.0-doc1',
previous_title: 'Document 1',
},
'ko-version-1.0.0-doc1': {
id: 'ko-version-1.0.0-doc1',
title: '문서 1',
source: 'version-1.0.0/doc1.md',
version: '1.0.0',
permalink: 'docs/ko/doc1.html',
localized_id: 'version-1.0.0-doc1',
language: 'ko',
sidebar: 'version-1.0.0-docs',
category: 'Test',
next_id: 'doc2',
next: 'ko-version-1.0.0-doc2',
next_title: '문서 2',
},
'ko-version-1.0.0-doc2': {
id: 'ko-version-1.0.0-doc2',
title: '문서 2',
source: 'version-1.0.0/doc2.md',
version: '1.0.0',
permalink: 'docs/ko/doc2.html',
localized_id: 'version-1.0.0-doc2',
language: 'ko',
sidebar: 'version-1.0.0-docs',
category: 'Test',
previous_id: 'doc1',
previous: 'ko-version-1.0.0-doc1',
previous_title: '문서 1',
},
};
23 changes: 23 additions & 0 deletions lib/server/__tests__/__snapshots__/docs.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`mdToHtmlify transform nothing 1`] = `
"
Docusaurus is the best :)"
`;

exports[`mdToHtmlify transform to correct link 1`] = `
"
### Existing Docs
- [doc1](/docs/en/next/doc1)
- [doc2](/docs/en/next/doc2)
### Non-existing Docs
- [hahaha](hahaha.md)
## Repeating Docs
- [doc1](/docs/en/next/doc1)
- [doc2](/docs/en/next/doc2)"
`;
134 changes: 134 additions & 0 deletions lib/server/__tests__/docs.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/**
* Copyright (c) 2017-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

// simulate cwd to website so all require (CWD+'/siteConfig.js') will work
const originalCwd = process.cwd();
if (!/website$/.test(originalCwd)) {
process.chdir(process.cwd() + '/website');
}
const path = require('path');
const fs = require('fs-extra');
const docs = require('../docs');
const metadataUtils = require('../metadataUtils');

jest.mock('../env', () => ({
translation: {
enabled: true,
enabledLanguages: () => [
{
enabled: true,
name: 'English',
tag: 'en',
},
{
enabled: true,
name: '한국어',
tag: 'ko',
},
],
},
versioning: {
enabled: true,
defaultVersion: '1.0.0',
},
}));

const Metadata = require(path.join(__dirname, '__fixtures__', 'metadata.js'));

const doc1 = fs.readFileSync(
path.join(__dirname, '__fixtures__', 'doc1.md'),
'utf8'
);

const doc2 = fs.readFileSync(
path.join(__dirname, '__fixtures__', 'doc2.md'),
'utf8'
);

describe('mdToHtmlify', () => {
const rawContent1 = metadataUtils.extractMetadata(doc1).rawContent;
const rawContent2 = metadataUtils.extractMetadata(doc2).rawContent;
const mdToHtml = metadataUtils.mdToHtml(Metadata, '/');

test('transform nothing', () => {
const content1 = docs.mdToHtmlify(
rawContent1,
mdToHtml,
Metadata['en-doc1']
);
expect(content1).not.toContain('/docs/en/next/');
expect(content1).toMatchSnapshot();
expect(content1).toEqual(rawContent1);
});

test('transform to correct link', () => {
const content2 = docs.mdToHtmlify(
rawContent2,
mdToHtml,
Metadata['en-doc2']
);
expect(content2).toContain('/docs/en/next/');
expect(content2).toMatchSnapshot();
expect(content2).not.toEqual(rawContent2);
});
});

describe('getFile', () => {
const fakeContent = {
'website/translated_docs/ko/doc1.md': '이건 가짜 야',
'website/versioned_docs/version-1.0.0/doc2.md': 'Document 2 is not good',
'website/translated_docs/ko/version-1.0.0/doc1.md':
'이것은 오래된 가짜입니다.',
'docs/doc1.md': 'Just another document',
};
fs.existsSync = jest.fn().mockReturnValue(true);
fs.readFileSync = jest.fn().mockImplementation(file => {
const fakePath = file.replace(process.cwd().replace(/website$/, ''), '');
return fakeContent[fakePath];
});

test('docs does not exist', () => {
const metadata = Metadata['en-doc1'];
fs.existsSync.mockReturnValueOnce(null);
expect(docs.getFile(metadata)).toBeNull();
});

test('null/undefined metadata', () => {
expect(docs.getFile(null)).toBeNull();
expect(docs.getFile(undefined)).toBeNull();
});

test('translated docs', () => {
const metadata = Metadata['ko-doc1'];
expect(docs.getFile(metadata)).toEqual(
fakeContent['website/translated_docs/ko/doc1.md']
);
});

test('versioned docs', () => {
const metadata = Metadata['en-version-1.0.0-doc2'];
expect(docs.getFile(metadata)).toEqual(
fakeContent['website/versioned_docs/version-1.0.0/doc2.md']
);
});

test('translated & versioned docs', () => {
const metadata = Metadata['ko-version-1.0.0-doc1'];
expect(docs.getFile(metadata)).toEqual(
fakeContent['website/translated_docs/ko/version-1.0.0/doc1.md']
);
});

test('normal docs', () => {
const metadata = Metadata['en-doc1'];
expect(docs.getFile(metadata)).toEqual(fakeContent['docs/doc1.md']);
});
});

afterAll(() => {
process.chdir(originalCwd);
});
1 change: 0 additions & 1 deletion lib/server/__tests__/routing.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

const routing = require('../routing');

describe('Blog routing', () => {
Expand Down
Loading

0 comments on commit 9f718a5

Please sign in to comment.