app.render.js
This file
Path
test/app.render.js
373 lines · 10.1 KiB · JavaScript
Last changed by
tests: check render error without engine-specific messageKunal Pathakbefore adoption19a2eeb
History of this fileTake it
The walk stopped after 200 commits: lines older than dd2b897 point at it.
examples: comment the usage of process.nextTickMuhammad Saqibbefore adoptiondd2b897
line 1why, one step back1tests: use path.join instead of concatenationchainhelenbefore adoptioneece385
line 2why, one step back2var assert = require('assert')examples: comment the usage of process.nextTickMuhammad Saqibbefore adoptiondd2b897
line 3why, one step back3var express = require('..');tests: use path.join instead of concatenationchainhelenbefore adoptioneece385
line 4why, one step back4var path = require('path')examples: comment the usage of process.nextTickMuhammad Saqibbefore adoptiondd2b897
lines 5–13why, one step back5var tmpl = require('./support/tmpl');67describe('app', function(){8 describe('.render(name, fn)', function(){9 it('should support absolute paths', function(done){10 var app = createApp();1112 app.locals.user = { name: 'tobi' };13tests: use path.join instead of concatenationchainhelenbefore adoptioneece385
line 14why, one step back14 app.render(path.join(__dirname, 'fixtures', 'user.tmpl'), function (err, str) {examples: comment the usage of process.nextTickMuhammad Saqibbefore adoptiondd2b897
lines 15–26why, 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){22 var app = createApp();2324 app.set('view engine', 'tmpl');25 app.locals.user = { name: 'tobi' };26tests: use path.join instead of concatenationchainhelenbefore adoptioneece385
line 27why, one step back27 app.render(path.join(__dirname, 'fixtures', 'user'), function (err, str) {examples: comment the usage of process.nextTickMuhammad Saqibbefore adoptiondd2b897
lines 28–36why, 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){35 var app = createApp();36tests: use path.join instead of concatenationchainhelenbefore adoptioneece385
line 37why, one step back37 app.set('views', path.join(__dirname, 'fixtures'))examples: comment the usage of process.nextTickMuhammad Saqibbefore adoptiondd2b897
lines 38–49why, one step back38 app.locals.user = { name: 'tobi' };3940 app.render('user.tmpl', function (err, str) {41 if (err) return done(err);42 str.should.equal('<p>tobi</p>');43 done();44 })45 })4647 it('should support index.<engine>', function(done){48 var app = createApp();49tests: use path.join instead of concatenationchainhelenbefore adoptioneece385
line 50why, one step back50 app.set('views', path.join(__dirname, 'fixtures'))examples: comment the usage of process.nextTickMuhammad Saqibbefore adoptiondd2b897
lines 51–74why, one step back51 app.set('view engine', 'tmpl');5253 app.render('blog/post', function (err, str) {54 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){tests: fix incorrect should usageDaniel Walasekbefore adoption713d2ae
line 75why, one step back75 err.should.be.ok()examples: comment the usage of process.nextTickMuhammad Saqibbefore adoptiondd2b897
lines 76–84why, 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){83 var app = createApp();84tests: use path.join instead of concatenationchainhelenbefore adoptioneece385
line 85why, one step back85 app.set('views', path.join(__dirname, 'fixtures'))examples: comment the usage of process.nextTickMuhammad Saqibbefore adoptiondd2b897
line 86why, one step back86 app.render('rawr.tmpl', function (err) {tests: use path.join instead of concatenationchainhelenbefore adoptioneece385
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') + '"')examples: comment the usage of process.nextTickMuhammad Saqibbefore adoptiondd2b897
lines 89–97why, one step back89 done();90 });91 })92 })9394 describe('when an error occurs', function(){95 it('should invoke the callback', function(done){96 var app = createApp();97tests: use path.join instead of concatenationchainhelenbefore adoptioneece385
line 98why, one step back98 app.set('views', path.join(__dirname, 'fixtures'))examples: comment the usage of process.nextTickMuhammad Saqibbefore adoptiondd2b897
line 99why, one step back99tests: check render error without engine-specific messageKunal Pathakbefore adoption19a2eeb
lines 100–103why, one step back100 app.render('user.tmpl', function (err) {101 assert.ok(err)102 assert.equal(err.name, 'RenderError')103 done()examples: comment the usage of process.nextTickMuhammad Saqibbefore adoptiondd2b897
lines 104–111why, one step back104 })105 })106 })107108 describe('when an extension is given', function(){109 it('should render the template', function(done){110 var app = createApp();111tests: use path.join instead of concatenationchainhelenbefore adoptioneece385
line 112why, one step back112 app.set('views', path.join(__dirname, 'fixtures'))examples: comment the usage of process.nextTickMuhammad Saqibbefore adoptiondd2b897
lines 113–126why, one step back113114 app.render('email.tmpl', function (err, str) {115 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){124 var app = createApp();125126 app.set('view engine', 'tmpl');tests: use path.join instead of concatenationchainhelenbefore adoptioneece385
line 127why, one step back127 app.set('views', path.join(__dirname, 'fixtures'))examples: comment the usage of process.nextTickMuhammad Saqibbefore adoptiondd2b897
lines 128–140why, 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 })136137 describe('when "views" is given', function(){138 it('should lookup the file in the path', function(done){139 var app = createApp();140tests: use path.join instead of concatenationchainhelenbefore adoptioneece385
line 141why, one step back141 app.set('views', path.join(__dirname, 'fixtures', 'default_layout'))examples: comment the usage of process.nextTickMuhammad Saqibbefore adoptiondd2b897
lines 142–153why, one step back142 app.locals.user = { name: 'tobi' };143144 app.render('user.tmpl', function (err, str) {145 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){153 var app = createApp();tests: use path.join instead of concatenationchainhelenbefore adoptioneece385
lines 154–157why, one step back154 var views = [155 path.join(__dirname, 'fixtures', 'local_layout'),156 path.join(__dirname, 'fixtures', 'default_layout')157 ]examples: comment the usage of process.nextTickMuhammad Saqibbefore adoptiondd2b897
lines 158–170why, one step back158159 app.set('views', views);160 app.locals.user = { name: 'tobi' };161162 app.render('user.tmpl', function (err, str) {163 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){170 var app = createApp();tests: use path.join instead of concatenationchainhelenbefore adoptioneece385
lines 171–174why, one step back171 var views = [172 path.join(__dirname, 'fixtures', 'local_layout'),173 path.join(__dirname, 'fixtures', 'default_layout')174 ]examples: comment the usage of process.nextTickMuhammad Saqibbefore adoptiondd2b897
lines 175–187why, one step back175176 app.set('views', views);177 app.locals.name = 'tobi';178179 app.render('name.tmpl', function (err, str) {180 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){187 var app = createApp();tests: use path.join instead of concatenationchainhelenbefore adoptioneece385
lines 188–191why, one step back188 var views = [189 path.join(__dirname, 'fixtures', 'local_layout'),190 path.join(__dirname, 'fixtures', 'default_layout')191 ]examples: comment the usage of process.nextTickMuhammad Saqibbefore adoptiondd2b897
lines 192–196why, one step back192193 app.set('views', views);194 app.locals.name = 'tobi';195196 app.render('pet.tmpl', function (err, str) {tests: use path.join instead of concatenationchainhelenbefore adoptioneece385
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] + '"')examples: comment the usage of process.nextTickMuhammad Saqibbefore adoptiondd2b897
lines 199–294why, one step back199 done();200 })201 })202 })203 })204205 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){293 var app = createApp();294tests: use path.join instead of concatenationchainhelenbefore adoptioneece385
line 295why, one step back295 app.set('views', path.join(__dirname, 'fixtures'))examples: comment the usage of process.nextTickMuhammad Saqibbefore adoptiondd2b897
lines 296–308why, one step back296297 var user = { name: 'tobi' };298299 app.render('user.tmpl', { user: user }, function (err, str) {300 if (err) return done(err);301 str.should.equal('<p>tobi</p>');302 done();303 })304 })305306 it('should expose app.locals', function(done){307 var app = createApp();308tests: use path.join instead of concatenationchainhelenbefore adoptioneece385
line 309why, one step back309 app.set('views', path.join(__dirname, 'fixtures'))examples: comment the usage of process.nextTickMuhammad Saqibbefore adoptiondd2b897
lines 310–321why, one step back310 app.locals.user = { name: 'tobi' };311312 app.render('user.tmpl', {}, function (err, str) {313 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){320 var app = createApp();321tests: use path.join instead of concatenationchainhelenbefore adoptioneece385
line 322why, one step back322 app.set('views', path.join(__dirname, 'fixtures'))examples: comment the usage of process.nextTickMuhammad Saqibbefore adoptiondd2b897
lines 323–373why, one step back323 app.locals.user = { name: 'tobi' };324 var jane = { name: 'jane' };325326 app.render('user.tmpl', { user: jane }, function (err, str) {327 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})366367function createApp() {368 var app = express();369370 app.engine('.tmpl', tmpl);371372 return app;373}