Skip to content

Commit

Permalink
Refactor & Add more tests for server utils
Browse files Browse the repository at this point in the history
  • Loading branch information
endiliey committed Jul 13, 2018
1 parent c95cf03 commit 94e856b
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 17 deletions.
8 changes: 4 additions & 4 deletions lib/core/__tests__/__snapshots__/utils.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`utils extractBlogPostBeforeTruncate 1`] = `
exports[`core utils extractBlogPostBeforeTruncate 1`] = `
"---
title: Truncation Example
---
Expand All @@ -12,7 +12,7 @@ Even this.
"
`;

exports[`utils extractBlogPostBeforeTruncate 2`] = `
exports[`core utils extractBlogPostBeforeTruncate 2`] = `
"---
title: Non-truncation Example
---
Expand All @@ -29,7 +29,7 @@ And this too.
"
`;

exports[`utils extractBlogPostSummary 1`] = `
exports[`core utils extractBlogPostSummary 1`] = `
"---
title: Truncation Example
---
Expand All @@ -48,7 +48,7 @@ Or this.
"
`;

exports[`utils extractBlogPostSummary 2`] = `
exports[`core utils extractBlogPostSummary 2`] = `
"---
title: Non-truncation Example
---
Expand Down
2 changes: 1 addition & 1 deletion lib/core/__tests__/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const blogPostWithoutTruncateContents = fs.readFileSync(
'utf8'
);

describe('utils', () => {
describe('core utils', () => {
test('blogPostHasTruncateMarker', () => {
expect(utils.blogPostHasTruncateMarker(blogPostWithTruncateContents)).toBe(
true
Expand Down
58 changes: 58 additions & 0 deletions lib/server/__tests__/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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');
});
});
15 changes: 3 additions & 12 deletions lib/server/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,14 @@ const cssnano = require('cssnano');
const path = require('path');
const escapeStringRegexp = require('escape-string-regexp');

// Return the subdirectory path from a reference directory
// Example:
// (file: 'docs/projectA/test.md', refDir: 'docs')
// returns 'projectA'
function getSubDir(file, refDir) {
let subDir = path.dirname(path.relative(refDir, file));
subDir = subDir.replace('\\', '/');
return subDir !== '.' ? subDir : null;
const subDir = path.dirname(path.relative(refDir, file)).replace('\\', '/');
return subDir !== '.' && !subDir.includes('..') ? subDir : null;
}

// Get the corresponding enabled language locale of a file.
// Example:
// (file: '/website/translated_docs/ko/projectA/test.md', refDir: 'website/translated_docs')
// returns 'ko'
function getLanguage(file, refDir) {
const regexSubFolder = new RegExp(
`/${escapeStringRegexp(path.basename(refDir))}/(.*)/.*/`
`${escapeStringRegexp(path.basename(refDir))}/(.*?)/.*`
);
const match = regexSubFolder.exec(file);

Expand Down

0 comments on commit 94e856b

Please sign in to comment.