Skip to content

Commit

Permalink
Flesh out useful API tests
Browse files Browse the repository at this point in the history
  • Loading branch information
takkaria committed Dec 9, 2015
1 parent b2fd62b commit 8a3fe45
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 13 deletions.
12 changes: 8 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,14 @@ var EmailTemplates = function (options) {
context = context || {};
options.juice.webResources.relativeTo = options.juice.webResources.relativeTo || options.root;

var html = self.useTemplate(templatePath, context);
var $ = cheerio.load(html);
if (options.rewriteUrl)
self.rewriteUrls($, options.rewriteUrl);
try {
var html = self.useTemplate(templatePath, context);
var $ = cheerio.load(html);
if (options.rewriteUrl)
self.rewriteUrls($, options.rewriteUrl);
} catch (err) {
return cb(err);
}

// Inline resources
juice.juiceResources($.html(), options.juice, function(err, inlinedHTML) {
Expand Down
43 changes: 34 additions & 9 deletions test/api.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,48 @@
var EmailTemplates = require('../');
var assert = require('assert');
var path = require('path');
var cheerio = require('cheerio');

describe('EmailTemplates', function() {
describe('constructor', function() {
it('should return an object');

it('should accept no arguments');

it('should return an object', function() {
var templates = new EmailTemplates();
assert.equal(Object.prototype.toString(templates), "[object Object]");
});
});

describe('render', function() {
it("should feed errors through callback (nonexistent templates)");
it("should feed errors through callback (nonexistent templates)", function(done) {
var templates = new EmailTemplates();
templates.render('nonexistent', null, function(err, html, text) {
assert.equal(err.errno, -2);
assert.equal(err.code, 'ENOENT');
done();
});
});

it("should respect the 'text' option", function(done) {
var templates = new EmailTemplates({
root: "test/templates/",
text: false
});

templates.render('text_file_alternative.html', null, function(err, html, text) {
assert.equal(text, null);
done(err);
});
});
});

it("should feed errors through callback (malformed context)");
describe('rewriteUrls', function() {

it("should respect the 'text' attribute'");
it('should always pass strings to rewriteUrl function', function() {
var $ = cheerio.load("<a>testing</a> <a href=''>testing-2</a>");
var templates = new EmailTemplates();

it("should look for templates in the specified location");
templates.rewriteUrls($, function(url) {
assert.equal(typeof url, "string");
});
});

});
})

0 comments on commit 8a3fe45

Please sign in to comment.