app.render.js
This file
Path
test/app.render.js
304 lines · 7.9 KiB · JavaScript
Last changed by
tests: run render tests with internal template engineDouglas Christopher Wilsonbefore adoption9182451
History of this fileTake it
The walk stopped after 200 commits: lines older than 9f4968a point at it.
3.15.1Douglas Christopher Wilsonbefore adoption9f4968a
line 1why, one step back1tests: run render tests with internal template engineDouglas Christopher Wilsonbefore adoption9182451
lines 2–3why, one step back2var express = require('..');3var tmpl = require('./support/tmpl');3.15.1Douglas Christopher Wilsonbefore adoption9f4968a
lines 4–7why, one step back45describe('app', function(){6 describe('.render(name, fn)', function(){7 it('should support absolute paths', function(done){tests: run render tests with internal template engineDouglas Christopher Wilsonbefore adoption9182451
line 8why, one step back8 var app = createApp();3.15.1Douglas Christopher Wilsonbefore adoption9f4968a
lines 9–11why, one step back910 app.locals.user = { name: 'tobi' };11tests: run render tests with internal template engineDouglas Christopher Wilsonbefore adoption9182451
line 12why, one step back12 app.render(__dirname + '/fixtures/user.tmpl', function (err, str) {3.15.1Douglas Christopher Wilsonbefore adoption9f4968a
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){tests: run render tests with internal template engineDouglas Christopher Wilsonbefore adoption9182451
line 20why, one step back20 var app = createApp();3.15.1Douglas Christopher Wilsonbefore adoption9f4968a
line 21why, one step back21tests: run render tests with internal template engineDouglas Christopher Wilsonbefore adoption9182451
line 22why, one step back22 app.set('view engine', 'tmpl');3.15.1Douglas Christopher Wilsonbefore adoption9f4968a
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){tests: run render tests with internal template engineDouglas Christopher Wilsonbefore adoption9182451
line 33why, one step back33 var app = createApp();3.15.1Douglas Christopher Wilsonbefore adoption9f4968a
lines 34–37why, one step back3435 app.set('views', __dirname + '/fixtures');36 app.locals.user = { name: 'tobi' };37tests: run render tests with internal template engineDouglas Christopher Wilsonbefore adoption9182451
line 38why, one step back38 app.render('user.tmpl', function (err, str) {3.15.1Douglas Christopher Wilsonbefore adoption9f4968a
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){tests: run render tests with internal template engineDouglas Christopher Wilsonbefore adoption9182451
line 46why, one step back46 var app = createApp();3.15.1Douglas Christopher Wilsonbefore adoption9f4968a
lines 47–48why, one step back4748 app.set('views', __dirname + '/fixtures');tests: run render tests with internal template engineDouglas Christopher Wilsonbefore adoption9182451
line 49why, one step back49 app.set('view engine', 'tmpl');3.15.1Douglas Christopher Wilsonbefore adoption9f4968a
line 50why, one step back50tests: run render tests with internal template engineDouglas Christopher Wilsonbefore adoption9182451
line 51why, one step back51 app.render('blog/post', function (err, str) {3.15.1Douglas Christopher Wilsonbefore adoption9f4968a
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){tests: run render tests with internal template engineDouglas Christopher Wilsonbefore adoption9182451
lines 81–82why, one step back81 var app = createApp();823.15.1Douglas Christopher Wilsonbefore adoption9f4968a
line 83why, one step back83 app.set('views', __dirname + '/fixtures');tests: run render tests with internal template engineDouglas Christopher Wilsonbefore adoption9182451
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"');3.15.1Douglas Christopher Wilsonbefore adoption9f4968a
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){tests: run render tests with internal template engineDouglas Christopher Wilsonbefore adoption9182451
line 93why, one step back93 var app = createApp();3.15.1Douglas Christopher Wilsonbefore adoption9f4968a
lines 94–96why, one step back9495 app.set('views', __dirname + '/fixtures');96tests: run render tests with internal template engineDouglas Christopher Wilsonbefore adoption9182451
line 97why, one step back97 app.render('user.tmpl', function (err, str) {3.15.1Douglas Christopher Wilsonbefore adoption9f4968a
lines 98–108why, one step back98 // nextTick to prevent cyclic99 process.nextTick(function(){100 err.message.should.match(/Cannot read property 'name' of undefined/);101 done();102 });103 })104 })105 })106107 describe('when an extension is given', function(){108 it('should render the template', function(done){tests: run render tests with internal template engineDouglas Christopher Wilsonbefore adoption9182451
line 109why, one step back109 var app = createApp();3.15.1Douglas Christopher Wilsonbefore adoption9f4968a
lines 110–112why, one step back110111 app.set('views', __dirname + '/fixtures');112tests: run render tests with internal template engineDouglas Christopher Wilsonbefore adoption9182451
line 113why, one step back113 app.render('email.tmpl', function (err, str) {3.15.1Douglas Christopher Wilsonbefore adoption9f4968a
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){tests: run render tests with internal template engineDouglas Christopher Wilsonbefore adoption9182451
line 123why, one step back123 var app = createApp();3.15.1Douglas Christopher Wilsonbefore adoption9f4968a
line 124why, one step back124tests: run render tests with internal template engineDouglas Christopher Wilsonbefore adoption9182451
line 125why, one step back125 app.set('view engine', 'tmpl');3.15.1Douglas Christopher Wilsonbefore adoption9f4968a
lines 126–223why, 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 })135136 describe('when a "view" constructor is given', function(){137 it('should create an instance of it', function(done){138 var app = express();139140 function View(name, options){141 this.name = name;142 this.path = 'path is required by application.js as a signal of success even though it is not used there.';143 }144145 View.prototype.render = function(options, fn){146 fn(null, 'abstract engine');147 };148149 app.set('view', View);150151 app.render('something', function(err, str){152 if (err) return done(err);153 str.should.equal('abstract engine');154 done();155 })156 })157 })158159 describe('caching', function(){160 it('should always lookup view without cache', function(done){161 var app = express();162 var count = 0;163164 function View(name, options){165 this.name = name;166 this.path = 'fake';167 count++;168 }169170 View.prototype.render = function(options, fn){171 fn(null, 'abstract engine');172 };173174 app.set('view cache', false);175 app.set('view', View);176177 app.render('something', function(err, str){178 if (err) return done(err);179 count.should.equal(1);180 str.should.equal('abstract engine');181 app.render('something', function(err, str){182 if (err) return done(err);183 count.should.equal(2);184 str.should.equal('abstract engine');185 done();186 })187 })188 })189190 it('should cache with "view cache" setting', function(done){191 var app = express();192 var count = 0;193194 function View(name, options){195 this.name = name;196 this.path = 'fake';197 count++;198 }199200 View.prototype.render = function(options, fn){201 fn(null, 'abstract engine');202 };203204 app.set('view cache', true);205 app.set('view', View);206207 app.render('something', function(err, str){208 if (err) return done(err);209 count.should.equal(1);210 str.should.equal('abstract engine');211 app.render('something', function(err, str){212 if (err) return done(err);213 count.should.equal(1);214 str.should.equal('abstract engine');215 done();216 })217 })218 })219 })220 })221222 describe('.render(name, options, fn)', function(){223 it('should render the template', function(done){tests: run render tests with internal template engineDouglas Christopher Wilsonbefore adoption9182451
line 224why, one step back224 var app = createApp();3.15.1Douglas Christopher Wilsonbefore adoption9f4968a
lines 225–229why, one step back225226 app.set('views', __dirname + '/fixtures');227228 var user = { name: 'tobi' };229tests: run render tests with internal template engineDouglas Christopher Wilsonbefore adoption9182451
line 230why, one step back230 app.render('user.tmpl', { user: user }, function (err, str) {3.15.1Douglas Christopher Wilsonbefore adoption9f4968a
lines 231–237why, one step back231 if (err) return done(err);232 str.should.equal('<p>tobi</p>');233 done();234 })235 })236237 it('should expose app.locals', function(done){tests: run render tests with internal template engineDouglas Christopher Wilsonbefore adoption9182451
line 238why, one step back238 var app = createApp();3.15.1Douglas Christopher Wilsonbefore adoption9f4968a
lines 239–242why, one step back239240 app.set('views', __dirname + '/fixtures');241 app.locals.user = { name: 'tobi' };242tests: run render tests with internal template engineDouglas Christopher Wilsonbefore adoption9182451
line 243why, one step back243 app.render('user.tmpl', {}, function (err, str) {3.15.1Douglas Christopher Wilsonbefore adoption9f4968a
lines 244–250why, one step back244 if (err) return done(err);245 str.should.equal('<p>tobi</p>');246 done();247 })248 })249250 it('should give precedence to app.render() locals', function(done){tests: run render tests with internal template engineDouglas Christopher Wilsonbefore adoption9182451
line 251why, one step back251 var app = createApp();3.15.1Douglas Christopher Wilsonbefore adoption9f4968a
lines 252–256why, one step back252253 app.set('views', __dirname + '/fixtures');254 app.locals.user = { name: 'tobi' };255 var jane = { name: 'jane' };256tests: run render tests with internal template engineDouglas Christopher Wilsonbefore adoption9182451
line 257why, one step back257 app.render('user.tmpl', { user: jane }, function (err, str) {3.15.1Douglas Christopher Wilsonbefore adoption9f4968a
lines 258–296why, one step back258 if (err) return done(err);259 str.should.equal('<p>jane</p>');260 done();261 })262 })263264 describe('caching', function(){265 it('should cache with cache option', function(done){266 var app = express();267 var count = 0;268269 function View(name, options){270 this.name = name;271 this.path = 'fake';272 count++;273 }274275 View.prototype.render = function(options, fn){276 fn(null, 'abstract engine');277 };278279 app.set('view cache', false);280 app.set('view', View);281282 app.render('something', {cache: true}, function(err, str){283 if (err) return done(err);284 count.should.equal(1);285 str.should.equal('abstract engine');286 app.render('something', {cache: true}, function(err, str){287 if (err) return done(err);288 count.should.equal(1);289 str.should.equal('abstract engine');290 done();291 })292 })293 })294 })295 })296})tests: run render tests with internal template engineDouglas Christopher Wilsonbefore adoption9182451
lines 297–304why, one step back297298function createApp() {299 var app = express();300301 app.engine('.tmpl', tmpl);302303 return app;304}