Skip to content
Myr

app.render.js

This file

Path

test/app.render.js

352 lines · 9.7 KiB · JavaScript

Last changed by

Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352

History of this file

Take it

The walk stopped after 200 commits: lines older than e799c0f point at it.

Merge tag '3.10.5'Douglas Christopher Wilsonbefore adoptione799c0f

lines 1–133why, one step back
1
2var express = require('../');34describe('app', function(){5 describe('.render(name, fn)', function(){6 it('should support absolute paths', function(done){7 var app = express();89 app.locals.user = { name: 'tobi' };1011 app.render(__dirname + '/fixtures/user.jade', function(err, str){12 if (err) return done(err);13 str.should.equal('<p>tobi</p>');14 done();15 })16 })1718 it('should support absolute paths with "view engine"', function(done){19 var app = express();2021 app.set('view engine', 'jade');22 app.locals.user = { name: 'tobi' };2324 app.render(__dirname + '/fixtures/user', function(err, str){25 if (err) return done(err);26 str.should.equal('<p>tobi</p>');27 done();28 })29 })3031 it('should expose app.locals', function(done){32 var app = express();3334 app.set('views', __dirname + '/fixtures');35 app.locals.user = { name: 'tobi' };3637 app.render('user.jade', function(err, str){38 if (err) return done(err);39 str.should.equal('<p>tobi</p>');40 done();41 })42 })4344 it('should support index.<engine>', function(done){45 var app = express();4647 app.set('views', __dirname + '/fixtures');48 app.set('view engine', 'jade');4950 app.render('blog/post', function(err, str){51 if (err) return done(err);52 str.should.equal('<h1>blog post</h1>');53 done();54 })55 })5657 it('should handle render error throws', function(done){58 var app = express();5960 function View(name, options){61 this.name = name;62 this.path = 'fale';63 }6465 View.prototype.render = function(options, fn){66 throw new Error('err!');67 };6869 app.set('view', View);7071 app.render('something', function(err, str){72 err.should.be.ok;73 err.message.should.equal('err!');74 done();75 })76 })7778 describe('when the file does not exist', function(){79 it('should provide a helpful error', function(done){80 var app = express();81 app.set('views', __dirname + '/fixtures');82 app.render('rawr.jade', function(err){83 err.message.should.equal('Failed to lookup view "rawr.jade" in views directory "' + __dirname + '/fixtures"');84 done();85 });86 })87 })8889 describe('when an error occurs', function(){90 it('should invoke the callback', function(done){91 var app = express();9293 app.set('views', __dirname + '/fixtures');9495 app.render('user.jade', function(err, str){96 // nextTick to prevent cyclic97 process.nextTick(function(){98 err.message.should.match(/Cannot read property '[^']+' of undefined/);99 done();100 });101 })102 })103 })104105 describe('when an extension is given', function(){106 it('should render the template', function(done){107 var app = express();108109 app.set('views', __dirname + '/fixtures');110111 app.render('email.jade', function(err, str){112 if (err) return done(err);113 str.should.equal('<p>This is an email</p>');114 done();115 })116 })117 })118119 describe('when "view engine" is given', function(){120 it('should render the template', function(done){121 var app = express();122123 app.set('view engine', 'jade');124 app.set('views', __dirname + '/fixtures');125126 app.render('email', function(err, str){127 if (err) return done(err);128 str.should.equal('<p>This is an email</p>');129 done();130 })131 })132 })133

Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352

lines 134–191why, one step back
134 describe('when "views" is given', function(){
135 it('should lookup the file in the path', function(done){136 var app = express();137138 app.set('views', __dirname + '/fixtures/default_layout');139 app.locals.user = { name: 'tobi' };140141 app.render('user.jade', function(err, str){142 if (err) return done(err);143 str.should.equal('<p>tobi</p>');144 done();145 })146 })147148 describe('when array of paths', function(){149 it('should lookup the file in the path', function(done){150 var app = express();151 var views = [__dirname + '/fixtures/local_layout', __dirname + '/fixtures/default_layout'];152153 app.set('views', views);154 app.locals.user = { name: 'tobi' };155156 app.render('user.jade', function(err, str){157 if (err) return done(err);158 str.should.equal('<span>tobi</span>');159 done();160 })161 })162163 it('should lookup in later paths until found', function(done){164 var app = express();165 var views = [__dirname + '/fixtures/local_layout', __dirname + '/fixtures/default_layout'];166167 app.set('views', views);168 app.locals.name = 'tobi';169170 app.render('name.jade', function(err, str){171 if (err) return done(err);172 str.should.equal('<p>tobi</p>');173 done();174 })175 })176177 it('should error if file does not exist', function(done){178 var app = express();179 var views = [__dirname + '/fixtures/local_layout', __dirname + '/fixtures/default_layout'];180181 app.set('views', views);182 app.locals.name = 'tobi';183184 app.render('pet.jade', function(err, str){185 err.message.should.equal('Failed to lookup view "pet.jade" in views directories "' + __dirname + '/fixtures/local_layout" or "' + __dirname + '/fixtures/default_layout"');186 done();187 })188 })189 })190 })191

Merge tag '3.10.5'Douglas Christopher Wilsonbefore adoptione799c0f

lines 192–352why, one step back
192 describe('when a "view" constructor is given', function(){
193 it('should create an instance of it', function(done){194 var app = express();195196 function View(name, options){197 this.name = name;198 this.path = 'path is required by application.js as a signal of success even though it is not used there.';199 }200201 View.prototype.render = function(options, fn){202 fn(null, 'abstract engine');203 };204205 app.set('view', View);206207 app.render('something', function(err, str){208 if (err) return done(err);209 str.should.equal('abstract engine');210 done();211 })212 })213 })214215 describe('caching', function(){216 it('should always lookup view without cache', function(done){217 var app = express();218 var count = 0;219220 function View(name, options){221 this.name = name;222 this.path = 'fake';223 count++;224 }225226 View.prototype.render = function(options, fn){227 fn(null, 'abstract engine');228 };229230 app.set('view cache', false);231 app.set('view', View);232233 app.render('something', function(err, str){234 if (err) return done(err);235 count.should.equal(1);236 str.should.equal('abstract engine');237 app.render('something', function(err, str){238 if (err) return done(err);239 count.should.equal(2);240 str.should.equal('abstract engine');241 done();242 })243 })244 })245246 it('should cache with "view cache" setting', function(done){247 var app = express();248 var count = 0;249250 function View(name, options){251 this.name = name;252 this.path = 'fake';253 count++;254 }255256 View.prototype.render = function(options, fn){257 fn(null, 'abstract engine');258 };259260 app.set('view cache', true);261 app.set('view', View);262263 app.render('something', function(err, str){264 if (err) return done(err);265 count.should.equal(1);266 str.should.equal('abstract engine');267 app.render('something', function(err, str){268 if (err) return done(err);269 count.should.equal(1);270 str.should.equal('abstract engine');271 done();272 })273 })274 })275 })276 })277278 describe('.render(name, options, fn)', function(){279 it('should render the template', function(done){280 var app = express();281282 app.set('views', __dirname + '/fixtures');283284 var user = { name: 'tobi' };285286 app.render('user.jade', { user: user }, function(err, str){287 if (err) return done(err);288 str.should.equal('<p>tobi</p>');289 done();290 })291 })292293 it('should expose app.locals', function(done){294 var app = express();295296 app.set('views', __dirname + '/fixtures');297 app.locals.user = { name: 'tobi' };298299 app.render('user.jade', {}, function(err, str){300 if (err) return done(err);301 str.should.equal('<p>tobi</p>');302 done();303 })304 })305306 it('should give precedence to app.render() locals', function(done){307 var app = express();308309 app.set('views', __dirname + '/fixtures');310 app.locals.user = { name: 'tobi' };311 var jane = { name: 'jane' };312313 app.render('user.jade', { user: jane }, function(err, str){314 if (err) return done(err);315 str.should.equal('<p>jane</p>');316 done();317 })318 })319320 describe('caching', function(){321 it('should cache with cache option', function(done){322 var app = express();323 var count = 0;324325 function View(name, options){326 this.name = name;327 this.path = 'fake';328 count++;329 }330331 View.prototype.render = function(options, fn){332 fn(null, 'abstract engine');333 };334335 app.set('view cache', false);336 app.set('view', View);337338 app.render('something', {cache: true}, function(err, str){339 if (err) return done(err);340 count.should.equal(1);341 str.should.equal('abstract engine');342 app.render('something', {cache: true}, function(err, str){343 if (err) return done(err);344 count.should.equal(1);345 str.should.equal('abstract engine');346 done();347 })348 })349 })350 })351 })352})