app.render.js
This file
Path
test/app.render.js
373 lines · 10.1 KiB · JavaScript
Last changed by
Merge tag '4.15.5'Douglas Christopher Wilsonbefore adoption19c8d64
History of this fileTake it
The walk stopped after 200 commits: lines older than 4ea6f21 point at it.
Merge tag '3.14.0'Douglas Christopher Wilsonbefore adoption4ea6f21
line 1why, one step back1Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c
line 2why, one step back2var assert = require('assert')Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 3why, one step back3var express = require('..');Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c
line 4why, one step back4var path = require('path')Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 5why, one step back5var tmpl = require('./support/tmpl');Merge tag '3.14.0'Douglas Christopher Wilsonbefore adoption4ea6f21
lines 6–9why, one step back67describe('app', function(){8 describe('.render(name, fn)', function(){9 it('should support absolute paths', function(done){Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 10why, one step back10 var app = createApp();Merge tag '3.14.0'Douglas Christopher Wilsonbefore adoption4ea6f21
lines 11–13why, one step back1112 app.locals.user = { name: 'tobi' };13Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c
line 14why, one step back14 app.render(path.join(__dirname, 'fixtures', 'user.tmpl'), function (err, str) {Merge tag '3.14.0'Douglas Christopher Wilsonbefore adoption4ea6f21
lines 15–21why, 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){Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 22why, one step back22 var app = createApp();Merge tag '3.14.0'Douglas Christopher Wilsonbefore adoption4ea6f21
line 23why, one step back23Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 24why, one step back24 app.set('view engine', 'tmpl');Merge tag '3.14.0'Douglas Christopher Wilsonbefore adoption4ea6f21
lines 25–26why, one step back25 app.locals.user = { name: 'tobi' };26Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c
line 27why, one step back27 app.render(path.join(__dirname, 'fixtures', 'user'), function (err, str) {Merge tag '3.14.0'Douglas Christopher Wilsonbefore adoption4ea6f21
lines 28–34why, 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){Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 35why, one step back35 var app = createApp();Merge tag '3.14.0'Douglas Christopher Wilsonbefore adoption4ea6f21
line 36why, one step back36Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c
line 37why, one step back37 app.set('views', path.join(__dirname, 'fixtures'))Merge tag '3.14.0'Douglas Christopher Wilsonbefore adoption4ea6f21
lines 38–39why, one step back38 app.locals.user = { name: 'tobi' };39Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 40why, one step back40 app.render('user.tmpl', function (err, str) {Merge tag '3.14.0'Douglas Christopher Wilsonbefore adoption4ea6f21
lines 41–47why, one step back41 if (err) return done(err);42 str.should.equal('<p>tobi</p>');43 done();44 })45 })4647 it('should support index.<engine>', function(done){Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 48why, one step back48 var app = createApp();Merge tag '3.14.0'Douglas Christopher Wilsonbefore adoption4ea6f21
line 49why, one step back49Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c
line 50why, one step back50 app.set('views', path.join(__dirname, 'fixtures'))Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 51why, one step back51 app.set('view engine', 'tmpl');Merge tag '3.14.0'Douglas Christopher Wilsonbefore adoption4ea6f21
line 52why, one step back52Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 53why, one step back53 app.render('blog/post', function (err, str) {Merge tag '3.14.0'Douglas Christopher Wilsonbefore adoption4ea6f21
lines 54–74why, one step back54 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){Merge tag '4.15.4'Douglas Christopher Wilsonbefore adoptionc319fe2
line 75why, one step back75 err.should.be.ok()Merge tag '3.14.0'Douglas Christopher Wilsonbefore adoption4ea6f21
lines 76–82why, one step back76 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){Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
lines 83–84why, one step back83 var app = createApp();84Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c
line 85why, one step back85 app.set('views', path.join(__dirname, 'fixtures'))Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 86why, one step back86 app.render('rawr.tmpl', function (err) {Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c
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') + '"')Merge tag '3.14.0'Douglas Christopher Wilsonbefore adoption4ea6f21
lines 89–95why, one step back89 done();90 });91 })92 })9394 describe('when an error occurs', function(){95 it('should invoke the callback', function(done){Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 96why, one step back96 var app = createApp();Merge tag '3.14.0'Douglas Christopher Wilsonbefore adoption4ea6f21
line 97why, one step back97Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c
line 98why, one step back98 app.set('views', path.join(__dirname, 'fixtures'))Merge tag '3.14.0'Douglas Christopher Wilsonbefore adoption4ea6f21
line 99why, one step back99Merge tag '4.15.5'Douglas Christopher Wilsonbefore adoption19c8d64
lines 100–103why, one step back100 app.render('user.tmpl', function (err) {101 assert.ok(err)102 assert.equal(err.name, 'RenderError')103 done()Merge tag '3.14.0'Douglas Christopher Wilsonbefore adoption4ea6f21
lines 104–109why, one step back104 })105 })106 })107108 describe('when an extension is given', function(){109 it('should render the template', function(done){Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 110why, one step back110 var app = createApp();Merge tag '3.14.0'Douglas Christopher Wilsonbefore adoption4ea6f21
line 111why, one step back111Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c
line 112why, one step back112 app.set('views', path.join(__dirname, 'fixtures'))Merge tag '3.14.0'Douglas Christopher Wilsonbefore adoption4ea6f21
line 113why, one step back113Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 114why, one step back114 app.render('email.tmpl', function (err, str) {Merge tag '3.14.0'Douglas Christopher Wilsonbefore adoption4ea6f21
lines 115–123why, one step back115 if (err) return done(err);116 str.should.equal('<p>This is an email</p>');117 done();118 })119 })120 })121122 describe('when "view engine" is given', function(){123 it('should render the template', function(done){Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 124why, one step back124 var app = createApp();Merge tag '3.14.0'Douglas Christopher Wilsonbefore adoption4ea6f21
line 125why, one step back125Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 126why, one step back126 app.set('view engine', 'tmpl');Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c
line 127why, one step back127 app.set('views', path.join(__dirname, 'fixtures'))Merge tag '3.14.0'Douglas Christopher Wilsonbefore adoption4ea6f21
lines 128–136why, one step back128129 app.render('email', function(err, str){130 if (err) return done(err);131 str.should.equal('<p>This is an email</p>');132 done();133 })134 })135 })136Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352
lines 137–138why, one step back137 describe('when "views" is given', function(){138 it('should lookup the file in the path', function(done){Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 139why, one step back139 var app = createApp();Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352
line 140why, one step back140Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c
line 141why, one step back141 app.set('views', path.join(__dirname, 'fixtures', 'default_layout'))Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352
lines 142–143why, one step back142 app.locals.user = { name: 'tobi' };143Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 144why, one step back144 app.render('user.tmpl', function (err, str) {Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352
lines 145–152why, one step back145 if (err) return done(err);146 str.should.equal('<p>tobi</p>');147 done();148 })149 })150151 describe('when array of paths', function(){152 it('should lookup the file in the path', function(done){Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 153why, one step back153 var app = createApp();Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c
lines 154–157why, one step back154 var views = [155 path.join(__dirname, 'fixtures', 'local_layout'),156 path.join(__dirname, 'fixtures', 'default_layout')157 ]Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352
lines 158–161why, one step back158159 app.set('views', views);160 app.locals.user = { name: 'tobi' };161Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 162why, one step back162 app.render('user.tmpl', function (err, str) {Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352
lines 163–169why, one step back163 if (err) return done(err);164 str.should.equal('<span>tobi</span>');165 done();166 })167 })168169 it('should lookup in later paths until found', function(done){Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 170why, one step back170 var app = createApp();Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c
lines 171–174why, one step back171 var views = [172 path.join(__dirname, 'fixtures', 'local_layout'),173 path.join(__dirname, 'fixtures', 'default_layout')174 ]Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352
lines 175–178why, one step back175176 app.set('views', views);177 app.locals.name = 'tobi';178Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 179why, one step back179 app.render('name.tmpl', function (err, str) {Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352
lines 180–186why, one step back180 if (err) return done(err);181 str.should.equal('<p>tobi</p>');182 done();183 })184 })185186 it('should error if file does not exist', function(done){Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 187why, one step back187 var app = createApp();Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c
lines 188–191why, one step back188 var views = [189 path.join(__dirname, 'fixtures', 'local_layout'),190 path.join(__dirname, 'fixtures', 'default_layout')191 ]Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352
lines 192–195why, one step back192193 app.set('views', views);194 app.locals.name = 'tobi';195Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 196why, one step back196 app.render('pet.tmpl', function (err, str) {Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c
lines 197–198why, one step back197 assert.ok(err)198 assert.equal(err.message, 'Failed to lookup view "pet.tmpl" in views directories "' + views[0] + '" or "' + views[1] + '"')Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352
lines 199–204why, one step back199 done();200 })201 })202 })203 })204Merge tag '3.14.0'Douglas Christopher Wilsonbefore adoption4ea6f21
lines 205–292why, one step back205 describe('when a "view" constructor is given', function(){206 it('should create an instance of it', function(done){207 var app = express();208209 function View(name, options){210 this.name = name;211 this.path = 'path is required by application.js as a signal of success even though it is not used there.';212 }213214 View.prototype.render = function(options, fn){215 fn(null, 'abstract engine');216 };217218 app.set('view', View);219220 app.render('something', function(err, str){221 if (err) return done(err);222 str.should.equal('abstract engine');223 done();224 })225 })226 })227228 describe('caching', function(){229 it('should always lookup view without cache', function(done){230 var app = express();231 var count = 0;232233 function View(name, options){234 this.name = name;235 this.path = 'fake';236 count++;237 }238239 View.prototype.render = function(options, fn){240 fn(null, 'abstract engine');241 };242243 app.set('view cache', false);244 app.set('view', View);245246 app.render('something', function(err, str){247 if (err) return done(err);248 count.should.equal(1);249 str.should.equal('abstract engine');250 app.render('something', function(err, str){251 if (err) return done(err);252 count.should.equal(2);253 str.should.equal('abstract engine');254 done();255 })256 })257 })258259 it('should cache with "view cache" setting', function(done){260 var app = express();261 var count = 0;262263 function View(name, options){264 this.name = name;265 this.path = 'fake';266 count++;267 }268269 View.prototype.render = function(options, fn){270 fn(null, 'abstract engine');271 };272273 app.set('view cache', true);274 app.set('view', View);275276 app.render('something', function(err, str){277 if (err) return done(err);278 count.should.equal(1);279 str.should.equal('abstract engine');280 app.render('something', function(err, str){281 if (err) return done(err);282 count.should.equal(1);283 str.should.equal('abstract engine');284 done();285 })286 })287 })288 })289 })290291 describe('.render(name, options, fn)', function(){292 it('should render the template', function(done){Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 293why, one step back293 var app = createApp();Merge tag '3.14.0'Douglas Christopher Wilsonbefore adoption4ea6f21
line 294why, one step back294Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c
line 295why, one step back295 app.set('views', path.join(__dirname, 'fixtures'))Merge tag '3.14.0'Douglas Christopher Wilsonbefore adoption4ea6f21
lines 296–298why, one step back296297 var user = { name: 'tobi' };298Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 299why, one step back299 app.render('user.tmpl', { user: user }, function (err, str) {Merge tag '3.14.0'Douglas Christopher Wilsonbefore adoption4ea6f21
lines 300–306why, one step back300 if (err) return done(err);301 str.should.equal('<p>tobi</p>');302 done();303 })304 })305306 it('should expose app.locals', function(done){Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 307why, one step back307 var app = createApp();Merge tag '3.14.0'Douglas Christopher Wilsonbefore adoption4ea6f21
line 308why, one step back308Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c
line 309why, one step back309 app.set('views', path.join(__dirname, 'fixtures'))Merge tag '3.14.0'Douglas Christopher Wilsonbefore adoption4ea6f21
lines 310–311why, one step back310 app.locals.user = { name: 'tobi' };311Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 312why, one step back312 app.render('user.tmpl', {}, function (err, str) {Merge tag '3.14.0'Douglas Christopher Wilsonbefore adoption4ea6f21
lines 313–319why, one step back313 if (err) return done(err);314 str.should.equal('<p>tobi</p>');315 done();316 })317 })318319 it('should give precedence to app.render() locals', function(done){Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 320why, one step back320 var app = createApp();Merge tag '3.14.0'Douglas Christopher Wilsonbefore adoption4ea6f21
line 321why, one step back321Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c
line 322why, one step back322 app.set('views', path.join(__dirname, 'fixtures'))Merge tag '3.14.0'Douglas Christopher Wilsonbefore adoption4ea6f21
lines 323–325why, one step back323 app.locals.user = { name: 'tobi' };324 var jane = { name: 'jane' };325Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 326why, one step back326 app.render('user.tmpl', { user: jane }, function (err, str) {Merge tag '3.14.0'Douglas Christopher Wilsonbefore adoption4ea6f21
lines 327–365why, one step back327 if (err) return done(err);328 str.should.equal('<p>jane</p>');329 done();330 })331 })332333 describe('caching', function(){334 it('should cache with cache option', function(done){335 var app = express();336 var count = 0;337338 function View(name, options){339 this.name = name;340 this.path = 'fake';341 count++;342 }343344 View.prototype.render = function(options, fn){345 fn(null, 'abstract engine');346 };347348 app.set('view cache', false);349 app.set('view', View);350351 app.render('something', {cache: true}, function(err, str){352 if (err) return done(err);353 count.should.equal(1);354 str.should.equal('abstract engine');355 app.render('something', {cache: true}, function(err, str){356 if (err) return done(err);357 count.should.equal(1);358 str.should.equal('abstract engine');359 done();360 })361 })362 })363 })364 })365})Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
lines 366–373why, one step back366367function createApp() {368 var app = express();369370 app.engine('.tmpl', tmpl);371372 return app;373}