Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add basic integration tests

  • Loading branch information...
commit 1f5de3ac0b84f55dd10ea5a3f37d90119d8b6dfc 1 parent 9a41ad0
@jasonsims jasonsims authored
Showing with 123 additions and 0 deletions.
  1. +123 −0 test/integration.js
View
123 test/integration.js
@@ -0,0 +1,123 @@
+var emailTemplates = require('../lib/main')
+ , expect = require('chai').expect
+ , fs = require('fs')
+ , path = require('path')
+ , mkdirp = require('mkdirp')
+ , rimraf = require('rimraf')
+ , templateDir = path.join(__dirname, '..', '.tmproj')
+ , templateName = 'test-template'
+ , templatePath = path.join(templateDir, templateName)
+
+describe('Email templates', function() {
+ /////////////////////////////////////////////////////////////////////////////
+ // Setup test environment.
+ beforeEach(function(done) {
+ // Setup the template directory structure.
+ mkdirp(templatePath, done)
+ })
+ afterEach(function(done) {
+ // Destroy the template directory structure.
+ rimraf(templatePath, done)
+ })
+
+ /////////////////////////////////////////////////////////////////////////////
+ // Test base functionality
+ describe('should render', function() {
+ it('html file', function(done) {
+ var html = '<h4><%= item%></h4>'
+ fs.writeFileSync(path.join(templateDir, templateName, 'html.ejs'), html)
+ emailTemplates(templateDir, function(err, template) {
+ template(templateName, {item: 'test'}, function(err, html, text) {
+ expect(err).to.be.null
+ expect(text).to.be.false
+ expect(html).to.equal('<h4>test</h4>')
+ done()
+ })
+ })
+ })
+
+ it('html and text files', function(done) {
+ var html = '<h4><%= item%></h4>'
+ , text = '<%= item%>'
+ fs.writeFileSync(path.join(templateDir, templateName, 'html.ejs'), html)
+ fs.writeFileSync(path.join(templateDir, templateName, 'text.ejs'), text)
+
+ emailTemplates(templateDir, function(err, template) {
+ template(templateName, {item: 'test'}, function(err, html, text) {
+ expect(err).to.be.null
+ expect(html).to.equal('<h4>test</h4>')
+ expect(text).to.equal('test')
+ done()
+ })
+ })
+ })
+
+ it('html with inline CSS and text file', function(done) {
+ var html = '<h4><%= item%></h4>'
+ , text = '<%= item%>'
+ , css = 'h4 { color: #ccc }'
+ fs.writeFileSync(path.join(templateDir, templateName, 'html.ejs'), html)
+ fs.writeFileSync(path.join(templateDir, templateName, 'text.ejs'), text)
+ fs.writeFileSync(path.join(templateDir, templateName, 'style.ejs'), css)
+
+ emailTemplates(templateDir, function(err, template) {
+ template(templateName, {item: 'test'}, function(err, html, text) {
+ expect(err).to.be.null
+ expect(text).to.equal('test')
+ expect(html).to.equal(
+ '<html><body><h4 style=\"color: #ccc;\">test</h4></body></html>')
+ done()
+ })
+ })
+ })
+ })
+
+ /////////////////////////////////////////////////////////////////////////////
+ // Test that error handling is working as expected.
+ describe('should error', function() {
+ it('if template directory was not defined', function(done) {
+ var badVar;
+ emailTemplates(badVar, function(err, template) {
+ expect(err.message).to.equal('templateDirectory is undefined')
+ expect(template).to.be.undefined
+ done()
+ })
+ })
+
+ it('if template name was not defined', function(done) {
+ emailTemplates(templateDir, function(err, template) {
+ var badVar
+ template(badVar, {item: 'test'}, function(err, html, text) {
+ expect(html).to.be.undefined
+ expect(text).to.be.undefined
+ expect(err).to.contain('templateName was not defined')
+ done()
+ })
+ })
+ })
+
+ it('on missing html file', function(done) {
+ emailTemplates(templateDir, function(err, template) {
+ template(templateName, {item: 'test'}, function(err, html, text) {
+ expect(html).to.be.undefined
+ expect(text).to.be.undefined
+ expect(err.code).to.equal('ENOENT')
+
+ done()
+ })
+ })
+ })
+
+ it('on empty html file', function(done) {
+ fs.writeFileSync(path.join(templateDir, templateName, 'html.ejs'), '')
+ emailTemplates(templateDir, function(err, template) {
+ template(templateName, {item: 'test'}, function(err, html, text) {
+ expect(html).to.be.undefined
+ expect(text).to.be.undefined
+ expect(err).to.contain('was an empty file')
+ done()
+ })
+ })
+ })
+ })
+})
Please sign in to comment.
Something went wrong with that request. Please try again.