app.render.js
This file
Path
test/app.render.js
375 lines · 10.2 KiB · JavaScript
Last changed by
tests: use path.join instead of concatenationchainhelenbefore adoptioneece385
History of this fileTake it
The walk stopped after 200 commits: lines older than 963d795 point at it.
build: support io.js 3.xDouglas Christopher Wilsonbefore adoption963d795
line 1why, one step back1tests: use path.join instead of concatenationchainhelenbefore adoptioneece385
line 2why, one step back2var assert = require('assert')build: support io.js 3.xDouglas Christopher Wilsonbefore adoption963d795
line 3why, one step back3var express = require('..');tests: use path.join instead of concatenationchainhelenbefore adoptioneece385
line 4why, one step back4var path = require('path')build: support io.js 3.xDouglas Christopher Wilsonbefore adoption963d795
lines 5–13why, one step back5var tmpl = require('./support/tmpl');67describe('app', function(){8 describe('.render(name, fn)', function(){9 it('should support absolute paths', function(done){10 var app = createApp();1112 app.locals.user = { name: 'tobi' };13tests: use path.join instead of concatenationchainhelenbefore adoptioneece385
line 14why, one step back14 app.render(path.join(__dirname, 'fixtures', 'user.tmpl'), function (err, str) {build: support io.js 3.xDouglas Christopher Wilsonbefore adoption963d795
lines 15–26why, one step back15 if (err) return done(err);16 str.should.equal('<p>tobi</p>');17 done();18 })19 })2021 it('should support absolute paths with "view engine"', function(done){22 var app = createApp();2324 app.set('view engine', 'tmpl');25 app.locals.user = { name: 'tobi' };26tests: use path.join instead of concatenationchainhelenbefore adoptioneece385
line 27why, one step back27 app.render(path.join(__dirname, 'fixtures', 'user'), function (err, str) {build: support io.js 3.xDouglas Christopher Wilsonbefore adoption963d795
lines 28–36why, one step back28 if (err) return done(err);29 str.should.equal('<p>tobi</p>');30 done();31 })32 })3334 it('should expose app.locals', function(done){35 var app = createApp();36tests: use path.join instead of concatenationchainhelenbefore adoptioneece385
line 37why, one step back37 app.set('views', path.join(__dirname, 'fixtures'))build: support io.js 3.xDouglas Christopher Wilsonbefore adoption963d795
lines 38–49why, one step back38 app.locals.user = { name: 'tobi' };3940 app.render('user.tmpl', function (err, str) {41 if (err) return done(err);42 str.should.equal('<p>tobi</p>');43 done();44 })45 })4647 it('should support index.<engine>', function(done){48 var app = createApp();49tests: use path.join instead of concatenationchainhelenbefore adoptioneece385
line 50why, one step back50 app.set('views', path.join(__dirname, 'fixtures'))build: support io.js 3.xDouglas Christopher Wilsonbefore adoption963d795
lines 51–84why, one step back51 app.set('view engine', 'tmpl');5253 app.render('blog/post', function (err, str) {54 if (err) return done(err);55 str.should.equal('<h1>blog post</h1>');56 done();57 })58 })5960 it('should handle render error throws', function(done){61 var app = express();6263 function View(name, options){64 this.name = name;65 this.path = 'fale';66 }6768 View.prototype.render = function(options, fn){69 throw new Error('err!');70 };7172 app.set('view', View);7374 app.render('something', function(err, str){75 err.should.be.ok;76 err.message.should.equal('err!');77 done();78 })79 })8081 describe('when the file does not exist', function(){82 it('should provide a helpful error', function(done){83 var app = createApp();84tests: use path.join instead of concatenationchainhelenbefore adoptioneece385
line 85why, one step back85 app.set('views', path.join(__dirname, 'fixtures'))build: support io.js 3.xDouglas Christopher Wilsonbefore adoption963d795
line 86why, one step back86 app.render('rawr.tmpl', function (err) {tests: use path.join instead of concatenationchainhelenbefore adoptioneece385
lines 87–88why, one step back87 assert.ok(err)88 assert.equal(err.message, 'Failed to lookup view "rawr.tmpl" in views directory "' + path.join(__dirname, 'fixtures') + '"')build: support io.js 3.xDouglas Christopher Wilsonbefore adoption963d795
lines 89–97why, one step back89 done();90 });91 })92 })9394 describe('when an error occurs', function(){95 it('should invoke the callback', function(done){96 var app = createApp();97tests: use path.join instead of concatenationchainhelenbefore adoptioneece385
line 98why, one step back98 app.set('views', path.join(__dirname, 'fixtures'))build: support io.js 3.xDouglas Christopher Wilsonbefore adoption963d795
lines 99–113why, one step back99100 app.render('user.tmpl', function (err, str) {101 // nextTick to prevent cyclic102 process.nextTick(function(){103 err.message.should.match(/Cannot read property '[^']+' of undefined/);104 done();105 });106 })107 })108 })109110 describe('when an extension is given', function(){111 it('should render the template', function(done){112 var app = createApp();113tests: use path.join instead of concatenationchainhelenbefore adoptioneece385
line 114why, one step back114 app.set('views', path.join(__dirname, 'fixtures'))build: support io.js 3.xDouglas Christopher Wilsonbefore adoption963d795
lines 115–128why, one step back115116 app.render('email.tmpl', function (err, str) {117 if (err) return done(err);118 str.should.equal('<p>This is an email</p>');119 done();120 })121 })122 })123124 describe('when "view engine" is given', function(){125 it('should render the template', function(done){126 var app = createApp();127128 app.set('view engine', 'tmpl');tests: use path.join instead of concatenationchainhelenbefore adoptioneece385
line 129why, one step back129 app.set('views', path.join(__dirname, 'fixtures'))build: support io.js 3.xDouglas Christopher Wilsonbefore adoption963d795
lines 130–142why, one step back130131 app.render('email', function(err, str){132 if (err) return done(err);133 str.should.equal('<p>This is an email</p>');134 done();135 })136 })137 })138139 describe('when "views" is given', function(){140 it('should lookup the file in the path', function(done){141 var app = createApp();142tests: use path.join instead of concatenationchainhelenbefore adoptioneece385
line 143why, one step back143 app.set('views', path.join(__dirname, 'fixtures', 'default_layout'))build: support io.js 3.xDouglas Christopher Wilsonbefore adoption963d795
lines 144–155why, one step back144 app.locals.user = { name: 'tobi' };145146 app.render('user.tmpl', function (err, str) {147 if (err) return done(err);148 str.should.equal('<p>tobi</p>');149 done();150 })151 })152153 describe('when array of paths', function(){154 it('should lookup the file in the path', function(done){155 var app = createApp();tests: use path.join instead of concatenationchainhelenbefore adoptioneece385
lines 156–159why, one step back156 var views = [157 path.join(__dirname, 'fixtures', 'local_layout'),158 path.join(__dirname, 'fixtures', 'default_layout')159 ]build: support io.js 3.xDouglas Christopher Wilsonbefore adoption963d795
lines 160–172why, one step back160161 app.set('views', views);162 app.locals.user = { name: 'tobi' };163164 app.render('user.tmpl', function (err, str) {165 if (err) return done(err);166 str.should.equal('<span>tobi</span>');167 done();168 })169 })170171 it('should lookup in later paths until found', function(done){172 var app = createApp();tests: use path.join instead of concatenationchainhelenbefore adoptioneece385
lines 173–176why, one step back173 var views = [174 path.join(__dirname, 'fixtures', 'local_layout'),175 path.join(__dirname, 'fixtures', 'default_layout')176 ]build: support io.js 3.xDouglas Christopher Wilsonbefore adoption963d795
lines 177–189why, one step back177178 app.set('views', views);179 app.locals.name = 'tobi';180181 app.render('name.tmpl', function (err, str) {182 if (err) return done(err);183 str.should.equal('<p>tobi</p>');184 done();185 })186 })187188 it('should error if file does not exist', function(done){189 var app = createApp();tests: use path.join instead of concatenationchainhelenbefore adoptioneece385
lines 190–193why, one step back190 var views = [191 path.join(__dirname, 'fixtures', 'local_layout'),192 path.join(__dirname, 'fixtures', 'default_layout')193 ]build: support io.js 3.xDouglas Christopher Wilsonbefore adoption963d795
lines 194–198why, one step back194195 app.set('views', views);196 app.locals.name = 'tobi';197198 app.render('pet.tmpl', function (err, str) {tests: use path.join instead of concatenationchainhelenbefore adoptioneece385
lines 199–200why, one step back199 assert.ok(err)200 assert.equal(err.message, 'Failed to lookup view "pet.tmpl" in views directories "' + views[0] + '" or "' + views[1] + '"')build: support io.js 3.xDouglas Christopher Wilsonbefore adoption963d795
lines 201–296why, one step back201 done();202 })203 })204 })205 })206207 describe('when a "view" constructor is given', function(){208 it('should create an instance of it', function(done){209 var app = express();210211 function View(name, options){212 this.name = name;213 this.path = 'path is required by application.js as a signal of success even though it is not used there.';214 }215216 View.prototype.render = function(options, fn){217 fn(null, 'abstract engine');218 };219220 app.set('view', View);221222 app.render('something', function(err, str){223 if (err) return done(err);224 str.should.equal('abstract engine');225 done();226 })227 })228 })229230 describe('caching', function(){231 it('should always lookup view without cache', function(done){232 var app = express();233 var count = 0;234235 function View(name, options){236 this.name = name;237 this.path = 'fake';238 count++;239 }240241 View.prototype.render = function(options, fn){242 fn(null, 'abstract engine');243 };244245 app.set('view cache', false);246 app.set('view', View);247248 app.render('something', function(err, str){249 if (err) return done(err);250 count.should.equal(1);251 str.should.equal('abstract engine');252 app.render('something', function(err, str){253 if (err) return done(err);254 count.should.equal(2);255 str.should.equal('abstract engine');256 done();257 })258 })259 })260261 it('should cache with "view cache" setting', function(done){262 var app = express();263 var count = 0;264265 function View(name, options){266 this.name = name;267 this.path = 'fake';268 count++;269 }270271 View.prototype.render = function(options, fn){272 fn(null, 'abstract engine');273 };274275 app.set('view cache', true);276 app.set('view', View);277278 app.render('something', function(err, str){279 if (err) return done(err);280 count.should.equal(1);281 str.should.equal('abstract engine');282 app.render('something', function(err, str){283 if (err) return done(err);284 count.should.equal(1);285 str.should.equal('abstract engine');286 done();287 })288 })289 })290 })291 })292293 describe('.render(name, options, fn)', function(){294 it('should render the template', function(done){295 var app = createApp();296tests: use path.join instead of concatenationchainhelenbefore adoptioneece385
line 297why, one step back297 app.set('views', path.join(__dirname, 'fixtures'))build: support io.js 3.xDouglas Christopher Wilsonbefore adoption963d795
lines 298–310why, one step back298299 var user = { name: 'tobi' };300301 app.render('user.tmpl', { user: user }, function (err, str) {302 if (err) return done(err);303 str.should.equal('<p>tobi</p>');304 done();305 })306 })307308 it('should expose app.locals', function(done){309 var app = createApp();310tests: use path.join instead of concatenationchainhelenbefore adoptioneece385
line 311why, one step back311 app.set('views', path.join(__dirname, 'fixtures'))build: support io.js 3.xDouglas Christopher Wilsonbefore adoption963d795
lines 312–323why, one step back312 app.locals.user = { name: 'tobi' };313314 app.render('user.tmpl', {}, function (err, str) {315 if (err) return done(err);316 str.should.equal('<p>tobi</p>');317 done();318 })319 })320321 it('should give precedence to app.render() locals', function(done){322 var app = createApp();323tests: use path.join instead of concatenationchainhelenbefore adoptioneece385
line 324why, one step back324 app.set('views', path.join(__dirname, 'fixtures'))build: support io.js 3.xDouglas Christopher Wilsonbefore adoption963d795
lines 325–375why, one step back325 app.locals.user = { name: 'tobi' };326 var jane = { name: 'jane' };327328 app.render('user.tmpl', { user: jane }, function (err, str) {329 if (err) return done(err);330 str.should.equal('<p>jane</p>');331 done();332 })333 })334335 describe('caching', function(){336 it('should cache with cache option', function(done){337 var app = express();338 var count = 0;339340 function View(name, options){341 this.name = name;342 this.path = 'fake';343 count++;344 }345346 View.prototype.render = function(options, fn){347 fn(null, 'abstract engine');348 };349350 app.set('view cache', false);351 app.set('view', View);352353 app.render('something', {cache: true}, function(err, str){354 if (err) return done(err);355 count.should.equal(1);356 str.should.equal('abstract engine');357 app.render('something', {cache: true}, function(err, str){358 if (err) return done(err);359 count.should.equal(1);360 str.should.equal('abstract engine');361 done();362 })363 })364 })365 })366 })367})368369function createApp() {370 var app = express();371372 app.engine('.tmpl', tmpl);373374 return app;375}