Skip to content

Commit

Permalink
better docs test & move out metadata to fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
endiliey committed Jul 15, 2018
1 parent b63f585 commit b19e1d7
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 157 deletions.
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',
},
};
191 changes: 34 additions & 157 deletions lib/server/__tests__/docs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ const path = require('path');
const fs = require('fs-extra');
const docs = require('../docs');
const metadataUtils = require('../metadataUtils');
const readMetadata = require('../readMetadata.js');

jest.mock('../env', () => ({
translation: {
Expand All @@ -38,122 +37,7 @@ jest.mock('../env', () => ({
},
}));

const Metadata = {
'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',
},
};
const Metadata = require(path.join(__dirname, '__fixtures__', 'metadata.js'));

const doc1 = fs.readFileSync(
path.join(__dirname, '__fixtures__', 'doc1.md'),
Expand All @@ -176,6 +60,7 @@ describe('mdToHtmlify', () => {
mdToHtml,
Metadata['en-doc1']
);
expect(content1).not.toContain('/docs/en/next/');
expect(content1).toMatchSnapshot();
expect(content1).toEqual(rawContent1);
});
Expand All @@ -186,69 +71,61 @@ describe('mdToHtmlify', () => {
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'];
const spy = jest.spyOn(fs, 'existsSync');

docs.getFile(metadata);
expect(spy).toHaveBeenLastCalledWith(
path.join(
process.cwd(),
'translated_docs',
metadata.language,
metadata.source
)
expect(docs.getFile(metadata)).toEqual(
fakeContent['website/translated_docs/ko/doc1.md']
);
spy.mockRestore();
});

test('versioned docs', () => {
const metadata = Metadata['en-version-1.0.0-doc2'];
const spy = jest.spyOn(fs, 'existsSync');

docs.getFile(metadata);
expect(spy).toHaveBeenLastCalledWith(
path.join(process.cwd(), 'versioned_docs', metadata.source)
expect(docs.getFile(metadata)).toEqual(
fakeContent['website/versioned_docs/version-1.0.0/doc2.md']
);
spy.mockRestore();
});

test('translated & versioned docs', () => {
const metadata = Metadata['ko-version-1.0.0-doc1'];
const spy = jest.spyOn(fs, 'existsSync');

docs.getFile(metadata);
expect(spy).toHaveBeenLastCalledWith(
path.join(
process.cwd(),
'translated_docs',
metadata.language,
metadata.source
)
expect(docs.getFile(metadata)).toEqual(
fakeContent['website/translated_docs/ko/version-1.0.0/doc1.md']
);
spy.mockRestore();
});

test('normal docs', () => {
const metadata = Metadata['en-doc1'];
const spy = jest.spyOn(fs, 'existsSync');

docs.getFile(metadata);
expect(spy).toHaveBeenLastCalledWith(
path.join(
process.cwd(),
'..',
readMetadata.getDocsPath(),
metadata.source
)
);
spy.mockRestore();
expect(docs.getFile(metadata)).toEqual(fakeContent['docs/doc1.md']);
});
});

Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
]
}
},
"jest": {
"testPathIgnorePatterns": ["/node_modules/", "__fixtures__"]
},
"bin": {
"docusaurus-start": "./lib/start-server.js",
"docusaurus-build": "./lib/build-files.js",
Expand Down

0 comments on commit b19e1d7

Please sign in to comment.