app.render.js
This file
Path
test/app.render.js
362 lines · 9.9 KiB · JavaScript
Last changed by
Merge tag '3.18.6'Douglas Christopher Wilsonbefore adoption4aa2801
History of this fileTake it
The walk stopped after 200 commits: lines older than 269dc53 point at it.
Invoke router.param() only when route matchesDouglas Christopher Wilsonbefore adoption269dc53
line 1why, one step back1Merge tag '3.18.6'Douglas Christopher Wilsonbefore adoption4aa2801
lines 2–3why, one step back2var express = require('..');3var tmpl = require('./support/tmpl');Invoke router.param() only when route matchesDouglas Christopher Wilsonbefore adoption269dc53
lines 4–7why, one step back45describe('app', function(){6 describe('.render(name, fn)', function(){7 it('should support absolute paths', function(done){Merge tag '3.18.6'Douglas Christopher Wilsonbefore adoption4aa2801
line 8why, one step back8 var app = createApp();Invoke router.param() only when route matchesDouglas Christopher Wilsonbefore adoption269dc53
lines 9–11why, one step back910 app.locals.user = { name: 'tobi' };11Merge tag '3.18.6'Douglas Christopher Wilsonbefore adoption4aa2801
line 12why, one step back12 app.render(__dirname + '/fixtures/user.tmpl', function (err, str) {Invoke router.param() only when route matchesDouglas Christopher Wilsonbefore adoption269dc53
lines 13–19why, one step back13 if (err) return done(err);14 str.should.equal('<p>tobi</p>');15 done();16 })17 })1819 it('should support absolute paths with "view engine"', function(done){Merge tag '3.18.6'Douglas Christopher Wilsonbefore adoption4aa2801
line 20why, one step back20 var app = createApp();Invoke router.param() only when route matchesDouglas Christopher Wilsonbefore adoption269dc53
line 21why, one step back21Merge tag '3.18.6'Douglas Christopher Wilsonbefore adoption4aa2801
line 22why, one step back22 app.set('view engine', 'tmpl');Invoke router.param() only when route matchesDouglas Christopher Wilsonbefore adoption269dc53
lines 23–32why, one step back23 app.locals.user = { name: 'tobi' };2425 app.render(__dirname + '/fixtures/user', function(err, str){26 if (err) return done(err);27 str.should.equal('<p>tobi</p>');28 done();29 })30 })3132 it('should expose app.locals', function(done){Merge tag '3.18.6'Douglas Christopher Wilsonbefore adoption4aa2801
line 33why, one step back33 var app = createApp();Invoke router.param() only when route matchesDouglas Christopher Wilsonbefore adoption269dc53
lines 34–37why, one step back3435 app.set('views', __dirname + '/fixtures');36 app.locals.user = { name: 'tobi' };37Merge tag '3.18.6'Douglas Christopher Wilsonbefore adoption4aa2801
line 38why, one step back38 app.render('user.tmpl', function (err, str) {Invoke router.param() only when route matchesDouglas Christopher Wilsonbefore adoption269dc53
lines 39–45why, one step back39 if (err) return done(err);40 str.should.equal('<p>tobi</p>');41 done();42 })43 })4445 it('should support index.<engine>', function(done){Merge tag '3.18.6'Douglas Christopher Wilsonbefore adoption4aa2801
line 46why, one step back46 var app = createApp();Invoke router.param() only when route matchesDouglas Christopher Wilsonbefore adoption269dc53
lines 47–48why, one step back4748 app.set('views', __dirname + '/fixtures');Merge tag '3.18.6'Douglas Christopher Wilsonbefore adoption4aa2801
line 49why, one step back49 app.set('view engine', 'tmpl');Invoke router.param() only when route matchesDouglas Christopher Wilsonbefore adoption269dc53
line 50why, one step back50Merge tag '3.18.6'Douglas Christopher Wilsonbefore adoption4aa2801
line 51why, one step back51 app.render('blog/post', function (err, str) {Invoke router.param() only when route matchesDouglas Christopher Wilsonbefore adoption269dc53
lines 52–80why, one step back52 if (err) return done(err);53 str.should.equal('<h1>blog post</h1>');54 done();55 })56 })5758 it('should handle render error throws', function(done){59 var app = express();6061 function View(name, options){62 this.name = name;63 this.path = 'fale';64 }6566 View.prototype.render = function(options, fn){67 throw new Error('err!');68 };6970 app.set('view', View);7172 app.render('something', function(err, str){73 err.should.be.ok;74 err.message.should.equal('err!');75 done();76 })77 })7879 describe('when the file does not exist', function(){80 it('should provide a helpful error', function(done){Merge tag '3.18.6'Douglas Christopher Wilsonbefore adoption4aa2801
lines 81–82why, one step back81 var app = createApp();82Invoke router.param() only when route matchesDouglas Christopher Wilsonbefore adoption269dc53
line 83why, one step back83 app.set('views', __dirname + '/fixtures');Merge tag '3.18.6'Douglas Christopher Wilsonbefore adoption4aa2801
lines 84–85why, one step back84 app.render('rawr.tmpl', function (err) {85 err.message.should.equal('Failed to lookup view "rawr.tmpl" in views directory "' + __dirname + '/fixtures"');Invoke router.param() only when route matchesDouglas Christopher Wilsonbefore adoption269dc53
lines 86–92why, one step back86 done();87 });88 })89 })9091 describe('when an error occurs', function(){92 it('should invoke the callback', function(done){Merge tag '3.18.6'Douglas Christopher Wilsonbefore adoption4aa2801
line 93why, one step back93 var app = createApp();Invoke router.param() only when route matchesDouglas Christopher Wilsonbefore adoption269dc53
lines 94–96why, one step back9495 app.set('views', __dirname + '/fixtures');96Merge tag '3.18.6'Douglas Christopher Wilsonbefore adoption4aa2801
line 97why, one step back97 app.render('user.tmpl', function (err, str) {Invoke router.param() only when route matchesDouglas Christopher Wilsonbefore adoption269dc53
lines 98–108why, one step back98 // nextTick to prevent cyclic99 process.nextTick(function(){100 err.message.should.match(/Cannot read property '[^']+' of undefined/);101 done();102 });103 })104 })105 })106107 describe('when an extension is given', function(){108 it('should render the template', function(done){Merge tag '3.18.6'Douglas Christopher Wilsonbefore adoption4aa2801
line 109why, one step back109 var app = createApp();Invoke router.param() only when route matchesDouglas Christopher Wilsonbefore adoption269dc53
lines 110–112why, one step back110111 app.set('views', __dirname + '/fixtures');112Merge tag '3.18.6'Douglas Christopher Wilsonbefore adoption4aa2801
line 113why, one step back113 app.render('email.tmpl', function (err, str) {Invoke router.param() only when route matchesDouglas Christopher Wilsonbefore adoption269dc53
lines 114–122why, one step back114 if (err) return done(err);115 str.should.equal('<p>This is an email</p>');116 done();117 })118 })119 })120121 describe('when "view engine" is given', function(){122 it('should render the template', function(done){Merge tag '3.18.6'Douglas Christopher Wilsonbefore adoption4aa2801
line 123why, one step back123 var app = createApp();Invoke router.param() only when route matchesDouglas Christopher Wilsonbefore adoption269dc53
line 124why, one step back124Merge tag '3.18.6'Douglas Christopher Wilsonbefore adoption4aa2801
line 125why, one step back125 app.set('view engine', 'tmpl');Invoke router.param() only when route matchesDouglas Christopher Wilsonbefore adoption269dc53
lines 126–135why, one step back126 app.set('views', __dirname + '/fixtures');127128 app.render('email', function(err, str){129 if (err) return done(err);130 str.should.equal('<p>This is an email</p>');131 done();132 })133 })134 })135Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352
lines 136–137why, one step back136 describe('when "views" is given', function(){137 it('should lookup the file in the path', function(done){Merge tag '3.18.6'Douglas Christopher Wilsonbefore adoption4aa2801
line 138why, one step back138 var app = createApp();Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352
lines 139–142why, one step back139140 app.set('views', __dirname + '/fixtures/default_layout');141 app.locals.user = { name: 'tobi' };142Merge tag '3.18.6'Douglas Christopher Wilsonbefore adoption4aa2801
line 143why, one step back143 app.render('user.tmpl', function (err, str) {Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352
lines 144–151why, one step back144 if (err) return done(err);145 str.should.equal('<p>tobi</p>');146 done();147 })148 })149150 describe('when array of paths', function(){151 it('should lookup the file in the path', function(done){Merge tag '3.18.6'Douglas Christopher Wilsonbefore adoption4aa2801
line 152why, one step back152 var app = createApp();Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352
lines 153–157why, one step back153 var views = [__dirname + '/fixtures/local_layout', __dirname + '/fixtures/default_layout'];154155 app.set('views', views);156 app.locals.user = { name: 'tobi' };157Merge tag '3.18.6'Douglas Christopher Wilsonbefore adoption4aa2801
line 158why, one step back158 app.render('user.tmpl', function (err, str) {Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352
lines 159–165why, one step back159 if (err) return done(err);160 str.should.equal('<span>tobi</span>');161 done();162 })163 })164165 it('should lookup in later paths until found', function(done){Merge tag '3.18.6'Douglas Christopher Wilsonbefore adoption4aa2801
line 166why, one step back166 var app = createApp();Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352
lines 167–171why, one step back167 var views = [__dirname + '/fixtures/local_layout', __dirname + '/fixtures/default_layout'];168169 app.set('views', views);170 app.locals.name = 'tobi';171Merge tag '3.18.6'Douglas Christopher Wilsonbefore adoption4aa2801
line 172why, one step back172 app.render('name.tmpl', function (err, str) {Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352
lines 173–179why, one step back173 if (err) return done(err);174 str.should.equal('<p>tobi</p>');175 done();176 })177 })178179 it('should error if file does not exist', function(done){Merge tag '3.18.6'Douglas Christopher Wilsonbefore adoption4aa2801
line 180why, one step back180 var app = createApp();Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352
lines 181–185why, one step back181 var views = [__dirname + '/fixtures/local_layout', __dirname + '/fixtures/default_layout'];182183 app.set('views', views);184 app.locals.name = 'tobi';185Merge tag '3.18.6'Douglas Christopher Wilsonbefore adoption4aa2801
lines 186–187why, one step back186 app.render('pet.tmpl', function (err, str) {187 err.message.should.equal('Failed to lookup view "pet.tmpl" in views directories "' + __dirname + '/fixtures/local_layout" or "' + __dirname + '/fixtures/default_layout"');Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352
lines 188–193why, one step back188 done();189 })190 })191 })192 })193Invoke router.param() only when route matchesDouglas Christopher Wilsonbefore adoption269dc53
lines 194–281why, one step back194 describe('when a "view" constructor is given', function(){195 it('should create an instance of it', function(done){196 var app = express();197198 function View(name, options){199 this.name = name;200 this.path = 'path is required by application.js as a signal of success even though it is not used there.';201 }202203 View.prototype.render = function(options, fn){204 fn(null, 'abstract engine');205 };206207 app.set('view', View);208209 app.render('something', function(err, str){210 if (err) return done(err);211 str.should.equal('abstract engine');212 done();213 })214 })215 })216217 describe('caching', function(){218 it('should always lookup view without cache', function(done){219 var app = express();220 var count = 0;221222 function View(name, options){223 this.name = name;224 this.path = 'fake';225 count++;226 }227228 View.prototype.render = function(options, fn){229 fn(null, 'abstract engine');230 };231232 app.set('view cache', false);233 app.set('view', View);234235 app.render('something', function(err, str){236 if (err) return done(err);237 count.should.equal(1);238 str.should.equal('abstract engine');239 app.render('something', function(err, str){240 if (err) return done(err);241 count.should.equal(2);242 str.should.equal('abstract engine');243 done();244 })245 })246 })247248 it('should cache with "view cache" setting', function(done){249 var app = express();250 var count = 0;251252 function View(name, options){253 this.name = name;254 this.path = 'fake';255 count++;256 }257258 View.prototype.render = function(options, fn){259 fn(null, 'abstract engine');260 };261262 app.set('view cache', true);263 app.set('view', View);264265 app.render('something', function(err, str){266 if (err) return done(err);267 count.should.equal(1);268 str.should.equal('abstract engine');269 app.render('something', function(err, str){270 if (err) return done(err);271 count.should.equal(1);272 str.should.equal('abstract engine');273 done();274 })275 })276 })277 })278 })279280 describe('.render(name, options, fn)', function(){281 it('should render the template', function(done){Merge tag '3.18.6'Douglas Christopher Wilsonbefore adoption4aa2801
line 282why, one step back282 var app = createApp();Invoke router.param() only when route matchesDouglas Christopher Wilsonbefore adoption269dc53
lines 283–287why, one step back283284 app.set('views', __dirname + '/fixtures');285286 var user = { name: 'tobi' };287Merge tag '3.18.6'Douglas Christopher Wilsonbefore adoption4aa2801
line 288why, one step back288 app.render('user.tmpl', { user: user }, function (err, str) {Invoke router.param() only when route matchesDouglas Christopher Wilsonbefore adoption269dc53
lines 289–295why, one step back289 if (err) return done(err);290 str.should.equal('<p>tobi</p>');291 done();292 })293 })294295 it('should expose app.locals', function(done){Merge tag '3.18.6'Douglas Christopher Wilsonbefore adoption4aa2801
line 296why, one step back296 var app = createApp();Invoke router.param() only when route matchesDouglas Christopher Wilsonbefore adoption269dc53
lines 297–300why, one step back297298 app.set('views', __dirname + '/fixtures');299 app.locals.user = { name: 'tobi' };300Merge tag '3.18.6'Douglas Christopher Wilsonbefore adoption4aa2801
line 301why, one step back301 app.render('user.tmpl', {}, function (err, str) {Invoke router.param() only when route matchesDouglas Christopher Wilsonbefore adoption269dc53
lines 302–308why, one step back302 if (err) return done(err);303 str.should.equal('<p>tobi</p>');304 done();305 })306 })307308 it('should give precedence to app.render() locals', function(done){Merge tag '3.18.6'Douglas Christopher Wilsonbefore adoption4aa2801
line 309why, one step back309 var app = createApp();Invoke router.param() only when route matchesDouglas Christopher Wilsonbefore adoption269dc53
lines 310–314why, one step back310311 app.set('views', __dirname + '/fixtures');312 app.locals.user = { name: 'tobi' };313 var jane = { name: 'jane' };314Merge tag '3.18.6'Douglas Christopher Wilsonbefore adoption4aa2801
line 315why, one step back315 app.render('user.tmpl', { user: jane }, function (err, str) {Invoke router.param() only when route matchesDouglas Christopher Wilsonbefore adoption269dc53
lines 316–354why, one step back316 if (err) return done(err);317 str.should.equal('<p>jane</p>');318 done();319 })320 })321322 describe('caching', function(){323 it('should cache with cache option', function(done){324 var app = express();325 var count = 0;326327 function View(name, options){328 this.name = name;329 this.path = 'fake';330 count++;331 }332333 View.prototype.render = function(options, fn){334 fn(null, 'abstract engine');335 };336337 app.set('view cache', false);338 app.set('view', View);339340 app.render('something', {cache: true}, function(err, str){341 if (err) return done(err);342 count.should.equal(1);343 str.should.equal('abstract engine');344 app.render('something', {cache: true}, function(err, str){345 if (err) return done(err);346 count.should.equal(1);347 str.should.equal('abstract engine');348 done();349 })350 })351 })352 })353 })354})Merge tag '3.18.6'Douglas Christopher Wilsonbefore adoption4aa2801
lines 355–362why, one step back355356function createApp() {357 var app = express();358359 app.engine('.tmpl', tmpl);360361 return app;362}