Skip to content

Commit

Permalink
Refactor test-utils to a single file
Browse files Browse the repository at this point in the history
  • Loading branch information
corycaywood committed Mar 24, 2017
1 parent 578edb1 commit 6f7a849
Show file tree
Hide file tree
Showing 18 changed files with 550 additions and 435 deletions.
8 changes: 7 additions & 1 deletion lib/config/scheme.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,16 @@ module.exports = root(
validate: shouldBeString
}),

renderer: section({
rendering: section({
highlight: option({
parseEnv: parseValue_,
parseCli: parseValue_,
validate: shouldBeBoolean,
defaultValue: true
})
})
}), {envPrefix: ENV_PREFIX}); // docpub-specific env variables prefix

function parseValue_(value) {
return JSON.parse(value);
}
12 changes: 6 additions & 6 deletions lib/md-renderer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ module.exports = class MarkdownRenderer {
*/
constructor(document, config) {
this._config = config;
const highlight = this._config.renderer.highlight
? highlighter
: null;

this._renderer = new MarkdownIt({
highlight: highlight
});
const options = {};
if (this._config.rendering.highlight) {
options.highlight = highlighter;
}

this._renderer = new MarkdownIt(options);

this._renderer.use(linkReplacer, document);
this._renderer.use(imageReplacer, document);
Expand Down
1 change: 0 additions & 1 deletion lib/metadata/validation-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ function shouldBeBoolean(value) {
checkType(value, 'boolean');
}


function checkType(value, type) {
const valueType = typeof value;
if (valueType !== type) {
Expand Down
2 changes: 1 addition & 1 deletion test/functional/read_directory.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const Category = require('../../lib/category');
const MarkdownRenderer = require('../../lib/md-renderer');
const path = require('path');
const createDummyConfig = require('../unit/zendesk-uploader/test-utils').createDummyConfig;
const createDummyConfig = require('../unit/test-utils').createDummyConfig;

describe('Documentation directory reading', () => {
const sandbox = sinon.sandbox.create();
Expand Down
77 changes: 34 additions & 43 deletions test/unit/article.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const _ = require('lodash');
const mockFs = require('mock-fs');
const Section = require('../../lib/section');
const Article = require('../../lib/article');
Expand All @@ -9,6 +8,7 @@ const MarkdownRenderer = require('../../lib/md-renderer');
const metadata = require('../../lib/metadata');
const Metadata = require('../../lib/metadata/metadata');
const logger = require('../../lib/logger');
const createArticle = require('./test-utils').createArticle;
const createDummyConfig = require('./test-utils').createDummyConfig;

describe('Article', () => {
Expand Down Expand Up @@ -41,34 +41,34 @@ describe('Article', () => {
});

it('should set article path as passed path', () => {
const article = createArticle_({path: 'foo'});
const article = createArticle({path: 'foo'});

expect(article.path).to.be.eql('foo');
});

it('should set parent section as passed section', () => {
const section = sinon.createStubInstance(Section);
const article = createArticle_({section: section});
const article = createArticle({section: section});

expect(article.section).to.be.equal(section);
});

it('should set article type as `article`', () => {
const article = createArticle_();
const article = createArticle();

expect(article.type).to.be.eql('article');
});

it('should initialise metadata', () => {
const article = createArticle_();
const article = createArticle();

expect(article.meta).to.be.instanceOf(Metadata);
});

it('should initialise metadata for specified article', () => {
sandbox.spy(metadata, 'buildForArticle');

createArticle_({path: 'some_path'});
createArticle({path: 'some_path'});

expect(metadata.buildForArticle).to.be.calledWith('some_path');
});
Expand All @@ -77,7 +77,7 @@ describe('Article', () => {
// Sinon < 2.0.0 does not support stubbing getters, so we have to redefine properties in order to mock behavior
describe('isChanged', () => {
it('should return true if article hash differes from article userMetaHash', () => {
const article = createArticle_();
const article = createArticle();

Object.defineProperty(article, 'hash', {value: 'abcdef'});
article.meta = {hash: `0a1b2c`};
Expand All @@ -86,7 +86,7 @@ describe('Article', () => {
});

it('should return false if article has same hash and userMetaHash and article has no child resources', () => {
const article = createArticle_();
const article = createArticle();

Object.defineProperty(article, 'hash', {value: 'abcdef'});
article.meta = {hash: `abcdef`};
Expand All @@ -97,7 +97,7 @@ describe('Article', () => {
});

it('should return true if article has same hash and currentHash but one of child resources changed', () => {
const article = createArticle_();
const article = createArticle();
const resource = new Resource('path', createDummyConfig(), article);

article.meta = {hash: `abcdef`};
Expand All @@ -111,7 +111,7 @@ describe('Article', () => {
});

it('should return false if document has same hash and currentHash and no child resources changed', () => {
const article = createArticle_();
const article = createArticle();
const resource = new Resource('path', createDummyConfig(), article);

article.meta = {hash: `abcdef`};
Expand Down Expand Up @@ -157,7 +157,7 @@ describe('Article', () => {
'content.md': `content_goes_here`
});

const article = createArticle_();
const article = createArticle();

return article.read()
.then(() => {
Expand All @@ -170,7 +170,7 @@ describe('Article', () => {
'content.md': `content_goes_here`
});

const article = createArticle_();
const article = createArticle();

return article.read()
.then(() => {
Expand All @@ -184,7 +184,7 @@ describe('Article', () => {
});
sandbox.spy(fsu, 'findFilesOfTypes').named('findFilesOfTypes');

const article = createArticle_();
const article = createArticle();

return article.read()
.then(() => {
Expand All @@ -195,7 +195,7 @@ describe('Article', () => {
it('should reject if unable to find any .md files', () => {
mockFs({});

const article = createArticle_();
const article = createArticle();

return expect(article.read())
.to.be.rejectedWith(/No markdown files/);
Expand All @@ -207,7 +207,7 @@ describe('Article', () => {
'more_content.md': `content_goes_here`
});

const article = createArticle_();
const article = createArticle();

return expect(article.read())
.to.be.rejectedWith(/more than 1 markdown/);
Expand All @@ -221,7 +221,7 @@ describe('Article', () => {
})
});

const article = createArticle_('.');
const article = createArticle('.');

return expect(article.read())
.to.be.rejectedWith(/EACCES/);
Expand All @@ -233,7 +233,7 @@ describe('Article', () => {
'image.jpg': 'image_bytes_here'
});

const article = createArticle_();
const article = createArticle();

return article.read()
.then(() => {
Expand All @@ -247,7 +247,7 @@ describe('Article', () => {
'image.jpg': 'image_bytes_here'
});

const article = createArticle_();
const article = createArticle();

return article.read()
.then(() => {
Expand All @@ -263,7 +263,7 @@ describe('Article', () => {
}
});

const article = createArticle_({path: 'path/to/article'});
const article = createArticle({path: 'path/to/article'});

return article.read()
.then(() => {
Expand All @@ -278,7 +278,7 @@ describe('Article', () => {
'image.jpg': 'image_bytes_here'
});

const article = createArticle_();
const article = createArticle();

return article.read()
.then(() => {
Expand All @@ -294,7 +294,7 @@ describe('Article', () => {
'image.jpeg': 'image_bytes_here'
});

const article = createArticle_();
const article = createArticle();

return article.read()
.then(() => {
Expand All @@ -310,7 +310,7 @@ describe('Article', () => {
'image.png': 'image_bytes_here'
});

const article = createArticle_();
const article = createArticle();

return article.read()
.then(() => {
Expand All @@ -326,7 +326,7 @@ describe('Article', () => {
'image.gif': 'image_bytes_here'
});

const article = createArticle_();
const article = createArticle();

return article.read()
.then(() => {
Expand All @@ -342,7 +342,7 @@ describe('Article', () => {
'image.svg': 'image_bytes_here'
});

const article = createArticle_();
const article = createArticle();

return article.read()
.then(() => {
Expand All @@ -358,7 +358,7 @@ describe('Article', () => {
'image.pdf': 'image_bytes_here'
});

const article = createArticle_();
const article = createArticle();

return article.read()
.then(() => {
Expand All @@ -379,7 +379,7 @@ describe('Article', () => {
'image.pdf': 'image_bytes_here'
});

const article = createArticle_();
const article = createArticle();

return article.read()
.then(() => {
Expand All @@ -404,7 +404,7 @@ describe('Article', () => {

sandbox.stub(Resource.prototype, 'read').resolves();

const article = createArticle_();
const article = createArticle();

return article.read()
.then(() => {
Expand All @@ -420,7 +420,7 @@ describe('Article', () => {
});

it('should update hash in meta with own hash', () => {
const article = createArticle_();
const article = createArticle();
Object.defineProperty(article, 'hash', {value: 'abcdef'});

return article.updateHash()
Expand All @@ -431,7 +431,7 @@ describe('Article', () => {
});

it('should update hashes of all its resources', () => {
const article = createArticle_();
const article = createArticle();
Object.defineProperty(article, 'hash', {value: 'abcdef'});

const resource = new Resource('res_name', createDummyConfig(), article);
Expand All @@ -454,7 +454,7 @@ describe('Article', () => {
});

it('should write updated meta to the disc', () => {
const article = createArticle_();
const article = createArticle();
Object.defineProperty(article, 'hash', {value: 'abcdef'});

return article.updateHash()
Expand All @@ -465,7 +465,7 @@ describe('Article', () => {
});

it('should reject promise if write failed', () => {
const article = createArticle_();
const article = createArticle();
const error = new Error('error');

Object.defineProperty(article, 'hash', {value: 'abcdef'});
Expand All @@ -478,7 +478,7 @@ describe('Article', () => {

describe('convertMarkdown', () => {
it('should reject if article contents were not yet read', () => {
const article = createArticle_();
const article = createArticle();

return expect(article.convertMarkdown())
.to.be.rejectedWith(/No path to markdown/);
Expand Down Expand Up @@ -515,7 +515,7 @@ describe('Article', () => {
});
sandbox.spy(MarkdownRenderer.prototype, 'render');

const article = createArticle_();
const article = createArticle();

return article.read()
.then(() => article.convertMarkdown())
Expand All @@ -531,19 +531,10 @@ describe('Article', () => {
'content.md': markdown || '# Header'
});

const article = createArticle_();
const article = createArticle();

return article.read()
.then(() => article.convertMarkdown());
}
});
});

function createArticle_(opts, config) {
opts = _.defaults(opts || {}, {
path: '.',
section: sinon.createStubInstance(Section)
});

return new Article(opts.path, createDummyConfig(config), opts.section);
}

0 comments on commit 6f7a849

Please sign in to comment.