app.render.js
This file
Path
test/app.render.js
374 lines · 10.3 KiB · JavaScript
Last changed by
tests: use strict modeDouglas Christopher Wilsonbefore adoptiona659137
History of this fileTake it
The walk stopped after 200 commits: lines older than 76bf96e point at it.
tests: use strict modeDouglas Christopher Wilsonbefore adoptiona659137
line 1why, one step back1'use strict'deps: finalhandler@1.1.1Douglas Christopher Wilsonbefore adoption76bf96e
lines 2–16why, one step back23var assert = require('assert')4var express = require('..');5var path = require('path')6var tmpl = require('./support/tmpl');78describe('app', function(){9 describe('.render(name, fn)', function(){10 it('should support absolute paths', function(done){11 var app = createApp();1213 app.locals.user = { name: 'tobi' };1415 app.render(path.join(__dirname, 'fixtures', 'user.tmpl'), function (err, str) {16 if (err) return done(err);tests: remove global dependency on shouldDouglas Christopher Wilsonbefore adoptionbd4fdfe
line 17why, one step back17 assert.strictEqual(str, '<p>tobi</p>')deps: finalhandler@1.1.1Douglas Christopher Wilsonbefore adoption76bf96e
lines 18–29why, one step back18 done();19 })20 })2122 it('should support absolute paths with "view engine"', function(done){23 var app = createApp();2425 app.set('view engine', 'tmpl');26 app.locals.user = { name: 'tobi' };2728 app.render(path.join(__dirname, 'fixtures', 'user'), function (err, str) {29 if (err) return done(err);tests: remove global dependency on shouldDouglas Christopher Wilsonbefore adoptionbd4fdfe
line 30why, one step back30 assert.strictEqual(str, '<p>tobi</p>')deps: finalhandler@1.1.1Douglas Christopher Wilsonbefore adoption76bf96e
lines 31–42why, one step back31 done();32 })33 })3435 it('should expose app.locals', function(done){36 var app = createApp();3738 app.set('views', path.join(__dirname, 'fixtures'))39 app.locals.user = { name: 'tobi' };4041 app.render('user.tmpl', function (err, str) {42 if (err) return done(err);tests: remove global dependency on shouldDouglas Christopher Wilsonbefore adoptionbd4fdfe
line 43why, one step back43 assert.strictEqual(str, '<p>tobi</p>')deps: finalhandler@1.1.1Douglas Christopher Wilsonbefore adoption76bf96e
lines 44–55why, one step back44 done();45 })46 })4748 it('should support index.<engine>', function(done){49 var app = createApp();5051 app.set('views', path.join(__dirname, 'fixtures'))52 app.set('view engine', 'tmpl');5354 app.render('blog/post', function (err, str) {55 if (err) return done(err);tests: remove global dependency on shouldDouglas Christopher Wilsonbefore adoptionbd4fdfe
line 56why, one step back56 assert.strictEqual(str, '<h1>blog post</h1>')deps: finalhandler@1.1.1Douglas Christopher Wilsonbefore adoption76bf96e
lines 57–75why, one step back57 done();58 })59 })6061 it('should handle render error throws', function(done){62 var app = express();6364 function View(name, options){65 this.name = name;66 this.path = 'fale';67 }6869 View.prototype.render = function(options, fn){70 throw new Error('err!');71 };7273 app.set('view', View);7475 app.render('something', function(err, str){tests: remove global dependency on shouldDouglas Christopher Wilsonbefore adoptionbd4fdfe
lines 76–77why, one step back76 assert.ok(err)77 assert.strictEqual(err.message, 'err!')deps: finalhandler@1.1.1Douglas Christopher Wilsonbefore adoption76bf96e
lines 78–116why, one step back78 done();79 })80 })8182 describe('when the file does not exist', function(){83 it('should provide a helpful error', function(done){84 var app = createApp();8586 app.set('views', path.join(__dirname, 'fixtures'))87 app.render('rawr.tmpl', function (err) {88 assert.ok(err)89 assert.equal(err.message, 'Failed to lookup view "rawr.tmpl" in views directory "' + path.join(__dirname, 'fixtures') + '"')90 done();91 });92 })93 })9495 describe('when an error occurs', function(){96 it('should invoke the callback', function(done){97 var app = createApp();9899 app.set('views', path.join(__dirname, 'fixtures'))100101 app.render('user.tmpl', function (err) {102 assert.ok(err)103 assert.equal(err.name, 'RenderError')104 done()105 })106 })107 })108109 describe('when an extension is given', function(){110 it('should render the template', function(done){111 var app = createApp();112113 app.set('views', path.join(__dirname, 'fixtures'))114115 app.render('email.tmpl', function (err, str) {116 if (err) return done(err);tests: remove global dependency on shouldDouglas Christopher Wilsonbefore adoptionbd4fdfe
line 117why, one step back117 assert.strictEqual(str, '<p>This is an email</p>')deps: finalhandler@1.1.1Douglas Christopher Wilsonbefore adoption76bf96e
lines 118–131why, one step back118 done();119 })120 })121 })122123 describe('when "view engine" is given', function(){124 it('should render the template', function(done){125 var app = createApp();126127 app.set('view engine', 'tmpl');128 app.set('views', path.join(__dirname, 'fixtures'))129130 app.render('email', function(err, str){131 if (err) return done(err);tests: remove global dependency on shouldDouglas Christopher Wilsonbefore adoptionbd4fdfe
line 132why, one step back132 assert.strictEqual(str, '<p>This is an email</p>')deps: finalhandler@1.1.1Douglas Christopher Wilsonbefore adoption76bf96e
lines 133–146why, one step back133 done();134 })135 })136 })137138 describe('when "views" is given', function(){139 it('should lookup the file in the path', function(done){140 var app = createApp();141142 app.set('views', path.join(__dirname, 'fixtures', 'default_layout'))143 app.locals.user = { name: 'tobi' };144145 app.render('user.tmpl', function (err, str) {146 if (err) return done(err);tests: remove global dependency on shouldDouglas Christopher Wilsonbefore adoptionbd4fdfe
line 147why, one step back147 assert.strictEqual(str, '<p>tobi</p>')deps: finalhandler@1.1.1Douglas Christopher Wilsonbefore adoption76bf96e
lines 148–164why, one step back148 done();149 })150 })151152 describe('when array of paths', function(){153 it('should lookup the file in the path', function(done){154 var app = createApp();155 var views = [156 path.join(__dirname, 'fixtures', 'local_layout'),157 path.join(__dirname, 'fixtures', 'default_layout')158 ]159160 app.set('views', views);161 app.locals.user = { name: 'tobi' };162163 app.render('user.tmpl', function (err, str) {164 if (err) return done(err);tests: remove global dependency on shouldDouglas Christopher Wilsonbefore adoptionbd4fdfe
line 165why, one step back165 assert.strictEqual(str, '<span>tobi</span>')deps: finalhandler@1.1.1Douglas Christopher Wilsonbefore adoption76bf96e
lines 166–181why, one step back166 done();167 })168 })169170 it('should lookup in later paths until found', function(done){171 var app = createApp();172 var views = [173 path.join(__dirname, 'fixtures', 'local_layout'),174 path.join(__dirname, 'fixtures', 'default_layout')175 ]176177 app.set('views', views);178 app.locals.name = 'tobi';179180 app.render('name.tmpl', function (err, str) {181 if (err) return done(err);tests: remove global dependency on shouldDouglas Christopher Wilsonbefore adoptionbd4fdfe
line 182why, one step back182 assert.strictEqual(str, '<p>tobi</p>')deps: finalhandler@1.1.1Douglas Christopher Wilsonbefore adoption76bf96e
lines 183–222why, one step back183 done();184 })185 })186187 it('should error if file does not exist', function(done){188 var app = createApp();189 var views = [190 path.join(__dirname, 'fixtures', 'local_layout'),191 path.join(__dirname, 'fixtures', 'default_layout')192 ]193194 app.set('views', views);195 app.locals.name = 'tobi';196197 app.render('pet.tmpl', function (err, str) {198 assert.ok(err)199 assert.equal(err.message, 'Failed to lookup view "pet.tmpl" in views directories "' + views[0] + '" or "' + views[1] + '"')200 done();201 })202 })203 })204 })205206 describe('when a "view" constructor is given', function(){207 it('should create an instance of it', function(done){208 var app = express();209210 function View(name, options){211 this.name = name;212 this.path = 'path is required by application.js as a signal of success even though it is not used there.';213 }214215 View.prototype.render = function(options, fn){216 fn(null, 'abstract engine');217 };218219 app.set('view', View);220221 app.render('something', function(err, str){222 if (err) return done(err);tests: remove global dependency on shouldDouglas Christopher Wilsonbefore adoptionbd4fdfe
line 223why, one step back223 assert.strictEqual(str, 'abstract engine')deps: finalhandler@1.1.1Douglas Christopher Wilsonbefore adoption76bf96e
lines 224–248why, one step back224 done();225 })226 })227 })228229 describe('caching', function(){230 it('should always lookup view without cache', function(done){231 var app = express();232 var count = 0;233234 function View(name, options){235 this.name = name;236 this.path = 'fake';237 count++;238 }239240 View.prototype.render = function(options, fn){241 fn(null, 'abstract engine');242 };243244 app.set('view cache', false);245 app.set('view', View);246247 app.render('something', function(err, str){248 if (err) return done(err);tests: remove global dependency on shouldDouglas Christopher Wilsonbefore adoptionbd4fdfe
lines 249–250why, one step back249 assert.strictEqual(count, 1)250 assert.strictEqual(str, 'abstract engine')deps: finalhandler@1.1.1Douglas Christopher Wilsonbefore adoption76bf96e
lines 251–252why, one step back251 app.render('something', function(err, str){252 if (err) return done(err);tests: remove global dependency on shouldDouglas Christopher Wilsonbefore adoptionbd4fdfe
lines 253–254why, one step back253 assert.strictEqual(count, 2)254 assert.strictEqual(str, 'abstract engine')deps: finalhandler@1.1.1Douglas Christopher Wilsonbefore adoption76bf96e
lines 255–278why, one step back255 done();256 })257 })258 })259260 it('should cache with "view cache" setting', function(done){261 var app = express();262 var count = 0;263264 function View(name, options){265 this.name = name;266 this.path = 'fake';267 count++;268 }269270 View.prototype.render = function(options, fn){271 fn(null, 'abstract engine');272 };273274 app.set('view cache', true);275 app.set('view', View);276277 app.render('something', function(err, str){278 if (err) return done(err);tests: remove global dependency on shouldDouglas Christopher Wilsonbefore adoptionbd4fdfe
lines 279–280why, one step back279 assert.strictEqual(count, 1)280 assert.strictEqual(str, 'abstract engine')deps: finalhandler@1.1.1Douglas Christopher Wilsonbefore adoption76bf96e
lines 281–282why, one step back281 app.render('something', function(err, str){282 if (err) return done(err);tests: remove global dependency on shouldDouglas Christopher Wilsonbefore adoptionbd4fdfe
lines 283–284why, one step back283 assert.strictEqual(count, 1)284 assert.strictEqual(str, 'abstract engine')deps: finalhandler@1.1.1Douglas Christopher Wilsonbefore adoption76bf96e
lines 285–301why, one step back285 done();286 })287 })288 })289 })290 })291292 describe('.render(name, options, fn)', function(){293 it('should render the template', function(done){294 var app = createApp();295296 app.set('views', path.join(__dirname, 'fixtures'))297298 var user = { name: 'tobi' };299300 app.render('user.tmpl', { user: user }, function (err, str) {301 if (err) return done(err);tests: remove global dependency on shouldDouglas Christopher Wilsonbefore adoptionbd4fdfe
line 302why, one step back302 assert.strictEqual(str, '<p>tobi</p>')deps: finalhandler@1.1.1Douglas Christopher Wilsonbefore adoption76bf96e
lines 303–314why, one step back303 done();304 })305 })306307 it('should expose app.locals', function(done){308 var app = createApp();309310 app.set('views', path.join(__dirname, 'fixtures'))311 app.locals.user = { name: 'tobi' };312313 app.render('user.tmpl', {}, function (err, str) {314 if (err) return done(err);tests: remove global dependency on shouldDouglas Christopher Wilsonbefore adoptionbd4fdfe
line 315why, one step back315 assert.strictEqual(str, '<p>tobi</p>')deps: finalhandler@1.1.1Douglas Christopher Wilsonbefore adoption76bf96e
lines 316–328why, one step back316 done();317 })318 })319320 it('should give precedence to app.render() locals', function(done){321 var app = createApp();322323 app.set('views', path.join(__dirname, 'fixtures'))324 app.locals.user = { name: 'tobi' };325 var jane = { name: 'jane' };326327 app.render('user.tmpl', { user: jane }, function (err, str) {328 if (err) return done(err);tests: remove global dependency on shouldDouglas Christopher Wilsonbefore adoptionbd4fdfe
line 329why, one step back329 assert.strictEqual(str, '<p>jane</p>')deps: finalhandler@1.1.1Douglas Christopher Wilsonbefore adoption76bf96e
lines 330–353why, one step back330 done();331 })332 })333334 describe('caching', function(){335 it('should cache with cache option', function(done){336 var app = express();337 var count = 0;338339 function View(name, options){340 this.name = name;341 this.path = 'fake';342 count++;343 }344345 View.prototype.render = function(options, fn){346 fn(null, 'abstract engine');347 };348349 app.set('view cache', false);350 app.set('view', View);351352 app.render('something', {cache: true}, function(err, str){353 if (err) return done(err);tests: remove global dependency on shouldDouglas Christopher Wilsonbefore adoptionbd4fdfe
lines 354–355why, one step back354 assert.strictEqual(count, 1)355 assert.strictEqual(str, 'abstract engine')deps: finalhandler@1.1.1Douglas Christopher Wilsonbefore adoption76bf96e
lines 356–357why, one step back356 app.render('something', {cache: true}, function(err, str){357 if (err) return done(err);tests: remove global dependency on shouldDouglas Christopher Wilsonbefore adoptionbd4fdfe
lines 358–359why, one step back358 assert.strictEqual(count, 1)359 assert.strictEqual(str, 'abstract engine')deps: finalhandler@1.1.1Douglas Christopher Wilsonbefore adoption76bf96e
lines 360–374why, one step back360 done();361 })362 })363 })364 })365 })366})367368function createApp() {369 var app = express();370371 app.engine('.tmpl', tmpl);372373 return app;374}