Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
28 changed files
with
1,390 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,207 @@ | ||
const Email = require('email-templates'); | ||
const path = require('path'); | ||
const cheerio = require('cheerio'); | ||
const _ = require('lodash'); | ||
|
||
const engine = require('../index'); | ||
|
||
const email = new Email({ | ||
views: { | ||
root: path.join(__dirname, 'fixtures/effe'), | ||
options: { | ||
extension: 'hbs', | ||
engineSource: engine({ | ||
layouts: path.join(__dirname, 'fixtures/handlebars/layouts'), | ||
partials: path.join(__dirname, 'fixtures/handlebars/partials'), | ||
helpers: path.join(__dirname, 'fixtures/handlebars/helpers') | ||
}) | ||
} | ||
} | ||
}); | ||
|
||
describe('Effe', () => { | ||
it('should be render a single file', () => { | ||
expect( | ||
email.render('hbs', { name: 'John' }).then(html => { | ||
const $ = cheerio.load(html, { xmlMode: true }); | ||
return $('html').length; | ||
}) | ||
).resolves.toEqual(0); | ||
|
||
return expect( | ||
email.render('hbs', { name: 'John' }).then(html => { | ||
return _.trim(html); | ||
}) | ||
).resolves.toEqual('Hello John!'); | ||
}); | ||
|
||
it('should be render all', () => { | ||
const _renderAll = email.renderAll('all', { name: 'John' }); | ||
|
||
expect(_renderAll.then(o => _.isObject(o))).resolves.toBe(true); | ||
expect(_renderAll.then(o => Object.keys(o))).resolves.toEqual( | ||
expect.arrayContaining(['subject', 'html', 'text']) | ||
); | ||
expect(_renderAll.then(o => _.trim(o.subject))).resolves.toBe('Hello John!'); | ||
expect( | ||
_renderAll.then(o => { | ||
const $ = cheerio.load(o.html, { xmlMode: true }); | ||
return $('html').length; | ||
}) | ||
).resolves.toBe(1); | ||
expect( | ||
_renderAll.then(o => { | ||
const $ = cheerio.load(o.html, { xmlMode: true }); | ||
return $('body p').html(); | ||
}) | ||
).resolves.toBe('Hello John!'); | ||
return expect(_renderAll.then(o => _.trim(o.text))).resolves.toBe('Hello John!'); | ||
}); | ||
|
||
it('should be render with Inky html tag', () => { | ||
const _renderAll = email.renderAll('inky', { name: 'John' }); | ||
|
||
expect(_renderAll.then(o => _.isObject(o))).resolves.toBe(true); | ||
expect(_renderAll.then(o => Object.keys(o))).resolves.toEqual( | ||
expect.arrayContaining(['subject', 'html', 'text']) | ||
); | ||
expect(_renderAll.then(o => _.trim(o.subject))).resolves.toBe('Hello John!'); | ||
expect( | ||
_renderAll.then(o => { | ||
const $ = cheerio.load(o.html, { xmlMode: true }); | ||
return $('body table.wrapper').length; | ||
}) | ||
).resolves.toBe(1); | ||
expect( | ||
_renderAll.then(o => { | ||
const $ = cheerio.load(o.html, { xmlMode: true }); | ||
return $('table.button').text(); | ||
}) | ||
).resolves.toBe('Click Me!'); | ||
return expect(_renderAll.then(o => _.trim(o.text))).resolves.toBe('Hello John!'); | ||
}); | ||
|
||
it('should be render with header data options', () => { | ||
const _renderAll = email.renderAll('withHeaderData', { name: 'John' }); | ||
|
||
expect(_renderAll.then(o => _.isObject(o))).resolves.toBe(true); | ||
expect(_renderAll.then(o => Object.keys(o))).resolves.toEqual( | ||
expect.arrayContaining(['subject', 'html', 'text']) | ||
); | ||
expect(_renderAll.then(o => _.trim(o.subject))).resolves.toBe('Hello John!'); | ||
expect( | ||
_renderAll.then(o => { | ||
const $ = cheerio.load(o.html, { xmlMode: true }); | ||
return $('body table.wrapper').length; | ||
}) | ||
).resolves.toBe(1); | ||
expect( | ||
_renderAll.then(o => { | ||
const $ = cheerio.load(o.html, { xmlMode: true }); | ||
return $('body table.wrapper p').text(); | ||
}) | ||
).resolves.toBe('Hello Tim!'); | ||
return expect(_renderAll.then(o => _.trim(o.text))).resolves.toBe('Hello John!'); | ||
}); | ||
|
||
it('should be render with helper', () => { | ||
const _renderAll = email.renderAll('withHelpers', { name: 'John' }); | ||
|
||
expect(_renderAll.then(o => _.isObject(o))).resolves.toBe(true); | ||
expect(_renderAll.then(o => Object.keys(o))).resolves.toEqual( | ||
expect.arrayContaining(['subject', 'html', 'text']) | ||
); | ||
expect(_renderAll.then(o => _.trim(o.subject))).resolves.toBe('Hello John!'); | ||
expect( | ||
_renderAll.then(o => { | ||
const $ = cheerio.load(o.html, { xmlMode: true }); | ||
return $('body').length; | ||
}) | ||
).resolves.toBe(1); | ||
expect( | ||
_renderAll.then(o => { | ||
const $ = cheerio.load(o.html, { xmlMode: true }); | ||
return $('body p').html(); | ||
}) | ||
).resolves.toBe('Hello JOHN!'); | ||
return expect(_renderAll.then(o => _.trim(o.text))).resolves.toBe('Hello John!'); | ||
}); | ||
|
||
it('should be render with other layout', () => { | ||
const _renderAll = email.renderAll('withOtherLayout', { | ||
layout: 'test', | ||
name: 'John' | ||
}); | ||
|
||
expect(_renderAll.then(o => _.isObject(o))).resolves.toBe(true); | ||
expect(_renderAll.then(o => Object.keys(o))).resolves.toEqual( | ||
expect.arrayContaining(['subject', 'html', 'text']) | ||
); | ||
expect(_renderAll.then(o => _.trim(o.subject))).resolves.toBe('Hello John!'); | ||
expect( | ||
_renderAll.then(o => { | ||
const $ = cheerio.load(o.html, { xmlMode: true }); | ||
return $('body').length; | ||
}) | ||
).resolves.toBe(0); | ||
expect( | ||
_renderAll.then(o => { | ||
const $ = cheerio.load(o.html, { xmlMode: true }); | ||
return $('div h1').html(); | ||
}) | ||
).resolves.toBe('This is a other layout'); | ||
expect( | ||
_renderAll.then(o => { | ||
const $ = cheerio.load(o.html, { xmlMode: true }); | ||
return $('div p').html(); | ||
}) | ||
).resolves.toBe('Hello John!'); | ||
return expect(_renderAll.then(o => _.trim(o.text))).resolves.toBe('Hello John!'); | ||
}); | ||
|
||
it('should be render with other partial', () => { | ||
const _renderAll = email.renderAll('withPartials', { name: 'John' }); | ||
|
||
expect(_renderAll.then(o => _.isObject(o))).resolves.toBe(true); | ||
expect(_renderAll.then(o => Object.keys(o))).resolves.toEqual( | ||
expect.arrayContaining(['subject', 'html', 'text']) | ||
); | ||
expect(_renderAll.then(o => _.trim(o.subject))).resolves.toBe('Hello John!'); | ||
expect( | ||
_renderAll.then(o => { | ||
const $ = cheerio.load(o.html, { xmlMode: true }); | ||
return $('body').length; | ||
}) | ||
).resolves.toBe(1); | ||
expect( | ||
_renderAll.then(o => { | ||
const $ = cheerio.load(o.html, { xmlMode: true }); | ||
return $('p').html(); | ||
}) | ||
).resolves.toBe('This is a hbs partial'); | ||
return expect(_renderAll.then(o => _.trim(o.text))).resolves.toBe('Hello John!'); | ||
}); | ||
|
||
it('should be have no engine options', function() { | ||
const email = new Email({ | ||
views: { | ||
root: path.join(__dirname, 'fixtures/effe'), | ||
options: { | ||
extension: 'hbs', | ||
engineSource: engine() | ||
} | ||
} | ||
}); | ||
|
||
expect( | ||
email.render('hbs', { name: 'John' }).then(html => { | ||
const $ = cheerio.load(html, { xmlMode: true }); | ||
return $('html').length; | ||
}) | ||
).resolves.toEqual(0); | ||
|
||
return expect( | ||
email.render('hbs', { name: 'John' }).then(html => _.trim(html)) | ||
).resolves.toEqual('Hello John!'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<p>Hello {{name}}!</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Hello {{name}}! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Hello {{name}}! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Hello {{name}}! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<wrapper> | ||
<p>Hello {{name}}!</p> | ||
<button href="#">Click Me!</button> | ||
</wrapper> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Hello {{name}}! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Hello {{name}}! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
name: Tim | ||
--- | ||
|
||
<wrapper> | ||
<p>Hello {{name}}!</p> | ||
</wrapper> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Hello {{name}}! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Hello {{name}}! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<p>Hello {{upcase name}}!</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Hello {{name}}! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Hello {{name}}! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<p>Hello {{name}}!</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Hello {{name}}! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Hello {{name}}! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<wrapper> | ||
{{> test}} | ||
</wrapper> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Hello {{name}}! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Hello {{name}}! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
const _ = require('lodash'); | ||
const helpers = module.exports; | ||
|
||
helpers.uppercase = function(str) { | ||
if (_.isObject(str) && str.fn) { | ||
return str.fn(this).toUpperCase(); | ||
} | ||
if (!_.isString(str)) return ''; | ||
return str.toUpperCase(); | ||
}; | ||
|
||
helpers.upcase = function() { | ||
return helpers.uppercase.apply(this, arguments); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<div> | ||
<h1>This is a other layout</h1> | ||
|
||
{{> body}} | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<p>This is a hbs partial</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{{!-- This is the base layout for your project, and will be used on every page. --}} | ||
|
||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" | ||
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> | ||
|
||
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> | ||
<head> | ||
<link rel="stylesheet" type="text/css" href="{{root}}css/app.css"> | ||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> | ||
<meta name="viewport" content="width=device-width"> | ||
<title>{{subject}}</title> | ||
<!-- <style> --> | ||
</head> | ||
<body> | ||
<span class="preheader">{{description}}</span> | ||
<table class="body"> | ||
<tr> | ||
<td class="center" align="center" valign="top"> | ||
<center> | ||
{{!-- Pages you create in the src/pages/ folder are inserted here when the flattened emails are created. --}} | ||
{{> body}} | ||
</center> | ||
</td> | ||
</tr> | ||
</table> | ||
<!-- prevent Gmail on iOS font size manipulation --> | ||
<div style="display:none; white-space:nowrap; font:15px courier; line-height:0;"> </div> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,54 @@ | ||
'use strict'; | ||
|
||
module.exports = {}; | ||
const path = require('path'); | ||
const _ = require('lodash'); | ||
const fs = require('fs'); | ||
|
||
/** | ||
* Initializes an instance of Effe. | ||
* @constructor | ||
* @param {object} options - Configuration options to use. | ||
*/ | ||
function Effe(options = {}) { | ||
this.options = options; | ||
this.Handlebars = require('handlebars'); | ||
this.layouts = {}; | ||
|
||
if (this.options.layouts) { | ||
this._loadLayouts([path.join(__dirname, 'hbs/layouts'), this.options.layouts]); | ||
} else { | ||
this._loadLayouts(path.join(__dirname, 'hbs/layouts')); | ||
} | ||
if (this.options.partials) this._loadPartials(this.options.partials); | ||
if (this.options.helpers) this._loadHelpers(this.options.helpers); | ||
} | ||
|
||
Effe.prototype._loadLayouts = require('./loadLayouts'); | ||
Effe.prototype._loadPartials = require('./loadPartials'); | ||
Effe.prototype._loadHelpers = require('./loadHelpers'); | ||
Effe.prototype._render = require('./render').render; | ||
|
||
/** | ||
* Make the object with the engines | ||
* @param {Object} effe | ||
* @return {Function} | ||
*/ | ||
function makeRenderer(effe) { | ||
return function(path, options, cb) { | ||
options.filename = path; | ||
exports.handlebars = {}; | ||
|
||
fs.readFile(path, function() { | ||
exports.handlebars.render = effe | ||
._render(_.toString(arguments[1]), options) | ||
.then(html => cb(null, html)); | ||
}); | ||
}; | ||
} | ||
|
||
module.exports = function(options) { | ||
const effe = new Effe(options); | ||
const handlebars = makeRenderer(effe); | ||
|
||
return { handlebars }; | ||
}; |
Oops, something went wrong.