Added API tests

This commit is contained in:
Patrick Steele-Idem 2014-05-05 14:38:56 -06:00
parent 5dd4e08163
commit 5c6ff73a35
5 changed files with 277 additions and 80 deletions

View File

@ -99,13 +99,13 @@ Hello ${data.name}!
The template can then be rendered as shown in the following sample code:
```javascript
var raptorTemplates = require('raptor-templates');
var templatePath = require.resolve('./hello.rhtml');
raptorTemplates.render(templatePath, {
var template = require('raptor-templates').load(templatePath);
template.render({
name: 'World',
colors: ["red", "green", "blue"]
}, function(err, output) {
},
function(err, output) {
console.log(output);
});
```
@ -205,8 +205,8 @@ npm install raptor-templates --global
### Callback API
```javascript
var raptorTemplates = require('raptor-templates');
raptorTemplates.render('template.rhtml', {
var template = require('raptor-templates').load('template.rhtml');
template.render({
name: 'Frank',
count: 30
},
@ -222,12 +222,11 @@ raptorTemplates.render('template.rhtml', {
### Streaming API
```javascript
var raptorTemplates = require('raptor-templates');
var template = require('raptor-templates').load('template.rhtml');
var out = require('fs').createWriteStream('index.html', 'utf8');
// Render the template to 'index.html'
raptorTemplates
.stream('template.rhtml', {
template.stream({
name: 'Frank',
count: 30
})
@ -239,6 +238,7 @@ raptorTemplates
```javascript
var raptorTemplates = require('raptor-templates');
var template = raptorTemplates.load('template.rhtml');
var out = require('fs').createWriteStream('index.html', 'utf8');
var context = raptorTemplates.createContext(out);
@ -251,10 +251,8 @@ setTimeout(function() {
}, 1000);
// Render the template to the existing render context:
raptorTemplates
.render(
'template.rhtml',
{
template
.render({
name: 'World'
},
context);
@ -280,9 +278,14 @@ Given the following module code that will be used to render a template on the cl
_run.js_:
```javascript
var raptorTemplates = require('raptor-templates');
var templatePath = require.resolve('./hello.rhtml');
raptorTemplates.render(templatePath, {name: 'John'}, function(err, output) {
var template = require('raptor-templates').load(templatePath);
templatePath.render({
name: 'John'
},
function(err, output) {
document.body.innerHTML = output;
});
```
@ -1166,7 +1169,8 @@ The complete code for this example is shown below:
_components/tabs/renderer.js:_
```javascript
var raptorTemplates = require('raptor-templates');
var templatePath = require.resolve('./template.rhtml');
var template = require('raptor-templates').load(templatePath);
module.exports = function render(input, context) {
var nestedTabs = [];
@ -1180,10 +1184,9 @@ module.exports = function render(input, context) {
});
// Now render the markup for the tabs:
raptorTemplates.render(require.resolve('./template.rhtml'), {
template.render({
tabs: nestedTabs
}, context);
};
```

View File

@ -45,7 +45,8 @@
"chai": "~1.8.1",
"jshint": "^2.5.0",
"raptor-cache": "^0.2.7-beta",
"dustjs-linkedin": "^2.3.4"
"dustjs-linkedin": "^2.3.4",
"through": "^2.3.4"
},
"license": "Apache License v2.0",
"bin": {

View File

@ -83,7 +83,7 @@ Template.prototype = {
};
}
return new Readable(this._, data);
return new Readable(this, data);
}
};

192
test/api-tests.js Normal file
View File

@ -0,0 +1,192 @@
'use strict';
var chai = require('chai');
chai.Assertion.includeStack = true;
require('chai').should();
var expect = require('chai').expect;
var nodePath = require('path');
var raptorTemplates = require('../');
var through = require('through');
describe('raptor-templates/rhtml' , function() {
beforeEach(function(done) {
done();
});
it('should allow a template to be loaded and rendered using a callback', function(done) {
raptorTemplates.render(
nodePath.join(__dirname, 'test-project/hello.rhtml'),
{
name: 'John'
},
function(err, output) {
if (err) {
return done(err);
}
expect(output).to.equal('Hello John!');
done();
});
});
it('should allow a template to be loaded and rendered to a context wrapping a string builder', function(done) {
var context = raptorTemplates.createContext();
context
.on('end', function() {
expect(context.getOutput()).to.equal('Hello John!');
done();
})
.on('error', function(e) {
done(e);
});
raptorTemplates.render(
nodePath.join(__dirname, 'test-project/hello.rhtml'),
{
name: 'John'
},
context);
context.end();
});
it('should allow a template to be loaded and rendered to a context wrapping a stream', function(done) {
var output = '';
var stream = through(function write(data) {
output += data;
});
var context = raptorTemplates.createContext(stream);
context
.on('end', function() {
expect(output).to.equal('Hello John!');
done();
})
.on('error', function(e) {
done(e);
});
raptorTemplates.render(
nodePath.join(__dirname, 'test-project/hello.rhtml'),
{
name: 'John'
},
context);
context.end();
});
it('should allow a template to be loaded and rendered to a stream', function(done) {
var output = '';
var outStream = through(function write(data) {
output += data;
});
raptorTemplates.stream(
nodePath.join(__dirname, 'test-project/hello.rhtml'),
{
name: 'John'
})
.pipe(outStream)
.on('end', function() {
expect(output).to.equal('Hello John!');
done();
})
.on('error', function(e) {
done(e);
});
});
/// TEMPLATE LOADING:
it('should allow a template to be loaded and rendered using a callback', function(done) {
var template = raptorTemplates.load(nodePath.join(__dirname, 'test-project/hello.rhtml'));
template.render({
name: 'John'
},
function(err, output) {
if (err) {
return done(err);
}
expect(output).to.equal('Hello John!');
done();
});
});
it('should allow a template to be loaded and rendered to a context wrapping a string builder', function(done) {
var context = raptorTemplates.createContext();
context
.on('end', function() {
expect(context.getOutput()).to.equal('Hello John!');
done();
})
.on('error', function(e) {
done(e);
});
var template = raptorTemplates.load(nodePath.join(__dirname, 'test-project/hello.rhtml'));
template.render({
name: 'John'
},
context);
context.end();
});
it('should allow a template to be loaded and rendered to a context wrapping a stream', function(done) {
var output = '';
var stream = through(function write(data) {
output += data;
});
var context = raptorTemplates.createContext(stream);
context
.on('end', function() {
expect(output).to.equal('Hello John!');
done();
})
.on('error', function(e) {
done(e);
});
var template = raptorTemplates.load(nodePath.join(__dirname, 'test-project/hello.rhtml'));
template.render({
name: 'John'
},
context);
context.end();
});
it('should allow a template to be loaded and rendered to a stream', function(done) {
var template = raptorTemplates.load(nodePath.join(__dirname, 'test-project/hello.rhtml'));
var output = '';
var outStream = through(function write(data) {
output += data;
});
template.stream({
name: 'John'
})
.pipe(outStream)
.on('end', function() {
expect(output).to.equal('Hello John!');
done();
})
.on('error', function(e) {
done(e);
});
});
});

View File

@ -0,0 +1 @@
Hello $data.name!