Skip to content

Commit

Permalink
Moved _getTemplateUrl into the Revoice object and added tests for it
Browse files Browse the repository at this point in the history
  • Loading branch information
d4nyll committed Aug 25, 2017
1 parent 2fb80ea commit f315884
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
26 changes: 15 additions & 11 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,6 @@ import ItemSchema from './schema/item.json';

import * as errorStrings from './errors.js';

const _getTemplateUrl = function _getTemplateUrl(template) {
// If the template is a alphanumeric string,
// we assume it's using a pre-defined template
// and we return the path to it
// Otherwise, we treat it as a user-provided path,
// and simply return that
return /^[a-zA-Z0-9]+$/.test(template) ? `./templates/${template}.html` : template;
}

const Revoice = {};

Revoice.DEFAULT_TEMPLATE = 'default';
Expand All @@ -29,11 +20,24 @@ Revoice.DEFAULT_OPTIONS = {
margin: '1cm',
}

/**
* Get the URL of the template HTML
*/
Revoice.getTemplateUrl = function getTemplateUrl(template) {
if (!template) return `./templates/${Revoice.DEFAULT_TEMPLATE}.html`;
// If the template is a alphanumeric string,
// we assume it's using a pre-defined template
// and we return the path to it
// Otherwise, we treat it as a user-provided path,
// and simply return that
return /^[a-zA-Z0-9]+$/.test(template) ? `./templates/${template}.html` : template;
}

Revoice.getTemplate = function (template = Revoice.DEFAULT_TEMPLATE) {
return new Promise(function (resolve, reject) {
fs.exists(_getTemplateUrl(template), function (fileExists) {
fs.exists(Revoice.getTemplateUrl(template), function (fileExists) {
if (fileExists) {
fs.readFile(_getTemplateUrl(template), 'utf8', function (err, data) {
fs.readFile(Revoice.getTemplateUrl(template), 'utf8', function (err, data) {
resolve(data);
})
} else {
Expand Down
21 changes: 21 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,27 @@ import ValidInvoice from './sample/invoice/valid.json';
import ZeroItemsInvoice from './sample/invoice/no-items.json';

describe('Revoice', function() {
describe('#getTemplateUrl()', function() {
it('should exists', function() {
expect(typeof Revoice.getTemplateUrl).to.be.equal('function');
});
it('should return a string', function() {
expect(Revoice.getTemplateUrl()).to.be.a('string');
});
it('should return an the URL of the default template if no argument is supplied', function() {
return expect(Revoice.getTemplateUrl()).to.include(Revoice.DEFAULT_TEMPLATE);
});
it('should return a path to the predefined template directory when an alphanumeric string is supplied', function() {
const testString = randomstring.generate({
charset: 'alphanumeric'
});
return expect(Revoice.getTemplateUrl(testString)).to.equal(`./templates/${testString}.html`);
});
it('should return the value passed in if it is not alphanumeric', function() {
const testString = './test/sample/templates/test.html'
return expect(Revoice.getTemplateUrl(testString)).to.equal(testString);
});
});
describe('#getTemplate()', function() {
it('should exists', function() {
expect(typeof Revoice.getTemplate).to.be.equal('function');
Expand Down

0 comments on commit f315884

Please sign in to comment.