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: The template can then be rendered as shown in the following sample code:
```javascript ```javascript
var raptorTemplates = require('raptor-templates');
var templatePath = require.resolve('./hello.rhtml'); var templatePath = require.resolve('./hello.rhtml');
var template = require('raptor-templates').load(templatePath);
raptorTemplates.render(templatePath, { template.render({
name: 'World', name: 'World',
colors: ["red", "green", "blue"] colors: ["red", "green", "blue"]
}, function(err, output) { },
function(err, output) {
console.log(output); console.log(output);
}); });
``` ```
@ -205,8 +205,8 @@ npm install raptor-templates --global
### Callback API ### Callback API
```javascript ```javascript
var raptorTemplates = require('raptor-templates'); var template = require('raptor-templates').load('template.rhtml');
raptorTemplates.render('template.rhtml', { template.render({
name: 'Frank', name: 'Frank',
count: 30 count: 30
}, },
@ -222,12 +222,11 @@ raptorTemplates.render('template.rhtml', {
### Streaming API ### Streaming API
```javascript ```javascript
var raptorTemplates = require('raptor-templates'); var template = require('raptor-templates').load('template.rhtml');
var out = require('fs').createWriteStream('index.html', 'utf8'); var out = require('fs').createWriteStream('index.html', 'utf8');
// Render the template to 'index.html' // Render the template to 'index.html'
raptorTemplates template.stream({
.stream('template.rhtml', {
name: 'Frank', name: 'Frank',
count: 30 count: 30
}) })
@ -239,6 +238,7 @@ raptorTemplates
```javascript ```javascript
var raptorTemplates = require('raptor-templates'); var raptorTemplates = require('raptor-templates');
var template = raptorTemplates.load('template.rhtml');
var out = require('fs').createWriteStream('index.html', 'utf8'); var out = require('fs').createWriteStream('index.html', 'utf8');
var context = raptorTemplates.createContext(out); var context = raptorTemplates.createContext(out);
@ -251,10 +251,8 @@ setTimeout(function() {
}, 1000); }, 1000);
// Render the template to the existing render context: // Render the template to the existing render context:
raptorTemplates template
.render( .render({
'template.rhtml',
{
name: 'World' name: 'World'
}, },
context); context);
@ -280,11 +278,16 @@ Given the following module code that will be used to render a template on the cl
_run.js_: _run.js_:
```javascript ```javascript
var raptorTemplates = require('raptor-templates');
var templatePath = require.resolve('./hello.rhtml'); var templatePath = require.resolve('./hello.rhtml');
raptorTemplates.render(templatePath, {name: 'John'}, function(err, output) { var template = require('raptor-templates').load(templatePath);
document.body.innerHTML = output;
}); templatePath.render({
name: 'John'
},
function(err, output) {
document.body.innerHTML = output;
});
``` ```
You can then bundle up the above program for running in the browser using either [raptor-optimizer](https://github.com/raptorjs3/raptor-optimizer) (recommended) or [browserify](https://github.com/substack/node-browserify). You can then bundle up the above program for running in the browser using either [raptor-optimizer](https://github.com/raptorjs3/raptor-optimizer) (recommended) or [browserify](https://github.com/substack/node-browserify).
@ -1166,7 +1169,8 @@ The complete code for this example is shown below:
_components/tabs/renderer.js:_ _components/tabs/renderer.js:_
```javascript ```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) { module.exports = function render(input, context) {
var nestedTabs = []; var nestedTabs = [];
@ -1180,10 +1184,9 @@ module.exports = function render(input, context) {
}); });
// Now render the markup for the tabs: // Now render the markup for the tabs:
raptorTemplates.render(require.resolve('./template.rhtml'), { template.render({
tabs: nestedTabs tabs: nestedTabs
}, context); }, context);
}; };
``` ```

View File

@ -1,60 +1,61 @@
{ {
"name": "raptor-templates", "name": "raptor-templates",
"description": "Raptor Templates", "description": "Raptor Templates",
"keywords": [ "keywords": [
"templating", "templating",
"template", "template",
"async", "async",
"streaming" "streaming"
], ],
"repository": { "repository": {
"type": "git", "type": "git",
"url": "https://github.com/raptorjs3/raptor-templates.git" "url": "https://github.com/raptorjs3/raptor-templates.git"
}, },
"scripts": { "scripts": {
"test": "node_modules/.bin/mocha --ui bdd --reporter spec ./test && node_modules/.bin/jshint compiler/ runtime/ taglibs/ dust/" "test": "node_modules/.bin/mocha --ui bdd --reporter spec ./test && node_modules/.bin/jshint compiler/ runtime/ taglibs/ dust/"
}, },
"author": "Patrick Steele-Idem <pnidem@gmail.com>", "author": "Patrick Steele-Idem <pnidem@gmail.com>",
"maintainers": [ "maintainers": [
"Patrick Steele-Idem <pnidem@gmail.com>" "Patrick Steele-Idem <pnidem@gmail.com>"
], ],
"dependencies": { "dependencies": {
"raptor-detect": "^0.2.0-beta", "raptor-detect": "^0.2.0-beta",
"raptor-logging": "^0.2.0-beta", "raptor-logging": "^0.2.0-beta",
"raptor-strings": "^0.2.0-beta", "raptor-strings": "^0.2.0-beta",
"raptor-regexp": "^0.2.0-beta", "raptor-regexp": "^0.2.0-beta",
"raptor-util": "^0.2.0-beta", "raptor-util": "^0.2.0-beta",
"raptor-arrays": "^0.2.0-beta", "raptor-arrays": "^0.2.0-beta",
"raptor-json": "^0.2.0-beta", "raptor-json": "^0.2.0-beta",
"raptor-modules": "^0.2.0-beta", "raptor-modules": "^0.2.0-beta",
"raptor-render-context": "^0.2.0-beta", "raptor-render-context": "^0.2.0-beta",
"raptor-data-providers": "^0.2.0-beta", "raptor-data-providers": "^0.2.0-beta",
"raptor-xml": "^0.2.0-beta", "raptor-xml": "^0.2.0-beta",
"raptor-objects": "^0.2.0-beta", "raptor-objects": "^0.2.0-beta",
"raptor-ecma": "^0.2.0-beta", "raptor-ecma": "^0.2.0-beta",
"htmlparser2": "~3.5.1", "htmlparser2": "~3.5.1",
"char-props": "~0.1.5", "char-props": "~0.1.5",
"raptor-promises": "^0.2.0-beta", "raptor-promises": "^0.2.0-beta",
"raptor-args": "^0.1.9-beta", "raptor-args": "^0.1.9-beta",
"minimatch": "^0.2.14", "minimatch": "^0.2.14",
"property-handlers": "^0.2.1-beta", "property-handlers": "^0.2.1-beta",
"raptor-dust": "^0.3.6-beta" "raptor-dust": "^0.3.6-beta"
}, },
"devDependencies": { "devDependencies": {
"mocha": "~1.15.1", "mocha": "~1.15.1",
"chai": "~1.8.1", "chai": "~1.8.1",
"jshint": "^2.5.0", "jshint": "^2.5.0",
"raptor-cache": "^0.2.7-beta", "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": { "license": "Apache License v2.0",
"rhtmlc": "bin/rhtmlc" "bin": {
}, "rhtmlc": "bin/rhtmlc"
"main": "runtime/raptor-templates-runtime.js", },
"publishConfig": { "main": "runtime/raptor-templates-runtime.js",
"registry": "https://registry.npmjs.org/" "publishConfig": {
}, "registry": "https://registry.npmjs.org/"
"ebay": {}, },
"version": "0.2.36-beta" "ebay": {},
} "version": "0.2.36-beta"
}

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!