app.render.js
This file
Path
test/app.render.js
374 lines · 10.3 KiB · JavaScript
Last changed by
fix(refactor): prefix built-in node module importsSzymon Łągiewkabefore adoption4111359
History of this fileTake it
The walk stopped after 200 commits: lines older than 8a387d3 point at it.
Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b
line 1why, one step back1'use strict'deps: array-flatten@2.0.0Douglas Christopher Wilsonbefore adoption8a387d3
line 2why, one step back2fix(refactor): prefix built-in node module importsSzymon Łągiewkabefore adoption4111359
line 3why, one step back3var assert = require('node:assert')deps: array-flatten@2.0.0Douglas Christopher Wilsonbefore adoption8a387d3
line 4why, one step back4var express = require('..');fix(refactor): prefix built-in node module importsSzymon Łągiewkabefore adoption4111359
line 5why, one step back5var path = require('node:path')deps: array-flatten@2.0.0Douglas Christopher Wilsonbefore adoption8a387d3
lines 6–14why, one step back6var 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' };14Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c
line 15why, one step back15 app.render(path.join(__dirname, 'fixtures', 'user.tmpl'), function (err, str) {deps: array-flatten@2.0.0Douglas Christopher Wilsonbefore adoption8a387d3
line 16why, one step back16 if (err) return done(err);Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b
line 17why, one step back17 assert.strictEqual(str, '<p>tobi</p>')deps: array-flatten@2.0.0Douglas Christopher Wilsonbefore adoption8a387d3
lines 18–27why, 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' };27Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c
line 28why, one step back28 app.render(path.join(__dirname, 'fixtures', 'user'), function (err, str) {deps: array-flatten@2.0.0Douglas Christopher Wilsonbefore adoption8a387d3
line 29why, one step back29 if (err) return done(err);Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b
line 30why, one step back30 assert.strictEqual(str, '<p>tobi</p>')deps: array-flatten@2.0.0Douglas Christopher Wilsonbefore adoption8a387d3
lines 31–37why, one step back31 done();32 })33 })3435 it('should expose app.locals', function(done){36 var app = createApp();37Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c
line 38why, one step back38 app.set('views', path.join(__dirname, 'fixtures'))deps: array-flatten@2.0.0Douglas Christopher Wilsonbefore adoption8a387d3
lines 39–42why, one step back39 app.locals.user = { name: 'tobi' };4041 app.render('user.tmpl', function (err, str) {42 if (err) return done(err);Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b
line 43why, one step back43 assert.strictEqual(str, '<p>tobi</p>')deps: array-flatten@2.0.0Douglas Christopher Wilsonbefore adoption8a387d3
lines 44–50why, one step back44 done();45 })46 })4748 it('should support index.<engine>', function(done){49 var app = createApp();50Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c
line 51why, one step back51 app.set('views', path.join(__dirname, 'fixtures'))deps: array-flatten@2.0.0Douglas Christopher Wilsonbefore adoption8a387d3
lines 52–55why, one step back52 app.set('view engine', 'tmpl');5354 app.render('blog/post', function (err, str) {55 if (err) return done(err);Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b
line 56why, one step back56 assert.strictEqual(str, '<h1>blog post</h1>')deps: array-flatten@2.0.0Douglas Christopher Wilsonbefore adoption8a387d3
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){Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b
lines 76–77why, one step back76 assert.ok(err)77 assert.strictEqual(err.message, 'err!')deps: array-flatten@2.0.0Douglas Christopher Wilsonbefore adoption8a387d3
lines 78–85why, 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();85Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c
line 86why, one step back86 app.set('views', path.join(__dirname, 'fixtures'))deps: array-flatten@2.0.0Douglas Christopher Wilsonbefore adoption8a387d3
line 87why, one step back87 app.render('rawr.tmpl', function (err) {Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c
lines 88–89why, one step back88 assert.ok(err)89 assert.equal(err.message, 'Failed to lookup view "rawr.tmpl" in views directory "' + path.join(__dirname, 'fixtures') + '"')deps: array-flatten@2.0.0Douglas Christopher Wilsonbefore adoption8a387d3
lines 90–98why, one step back90 done();91 });92 })93 })9495 describe('when an error occurs', function(){96 it('should invoke the callback', function(done){97 var app = createApp();98Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c
line 99why, one step back99 app.set('views', path.join(__dirname, 'fixtures'))deps: array-flatten@2.0.0Douglas Christopher Wilsonbefore adoption8a387d3
line 100why, one step back100Merge tag '4.15.5'Douglas Christopher Wilsonbefore adoption19c8d64
lines 101–104why, one step back101 app.render('user.tmpl', function (err) {102 assert.ok(err)103 assert.equal(err.name, 'RenderError')104 done()deps: array-flatten@2.0.0Douglas Christopher Wilsonbefore adoption8a387d3
lines 105–112why, one step back105 })106 })107 })108109 describe('when an extension is given', function(){110 it('should render the template', function(done){111 var app = createApp();112Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c
line 113why, one step back113 app.set('views', path.join(__dirname, 'fixtures'))deps: array-flatten@2.0.0Douglas Christopher Wilsonbefore adoption8a387d3
lines 114–116why, one step back114115 app.render('email.tmpl', function (err, str) {116 if (err) return done(err);Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b
line 117why, one step back117 assert.strictEqual(str, '<p>This is an email</p>')deps: array-flatten@2.0.0Douglas Christopher Wilsonbefore adoption8a387d3
lines 118–127why, 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');Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c
line 128why, one step back128 app.set('views', path.join(__dirname, 'fixtures'))deps: array-flatten@2.0.0Douglas Christopher Wilsonbefore adoption8a387d3
lines 129–131why, one step back129130 app.render('email', function(err, str){131 if (err) return done(err);Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b
line 132why, one step back132 assert.strictEqual(str, '<p>This is an email</p>')deps: array-flatten@2.0.0Douglas Christopher Wilsonbefore adoption8a387d3
lines 133–141why, 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();141Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c
line 142why, one step back142 app.set('views', path.join(__dirname, 'fixtures', 'default_layout'))deps: array-flatten@2.0.0Douglas Christopher Wilsonbefore adoption8a387d3
lines 143–146why, one step back143 app.locals.user = { name: 'tobi' };144145 app.render('user.tmpl', function (err, str) {146 if (err) return done(err);Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b
line 147why, one step back147 assert.strictEqual(str, '<p>tobi</p>')deps: array-flatten@2.0.0Douglas Christopher Wilsonbefore adoption8a387d3
lines 148–154why, 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();Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c
lines 155–158why, one step back155 var views = [156 path.join(__dirname, 'fixtures', 'local_layout'),157 path.join(__dirname, 'fixtures', 'default_layout')158 ]deps: array-flatten@2.0.0Douglas Christopher Wilsonbefore adoption8a387d3
lines 159–164why, one step back159160 app.set('views', views);161 app.locals.user = { name: 'tobi' };162163 app.render('user.tmpl', function (err, str) {164 if (err) return done(err);Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b
line 165why, one step back165 assert.strictEqual(str, '<span>tobi</span>')deps: array-flatten@2.0.0Douglas Christopher Wilsonbefore adoption8a387d3
lines 166–171why, one step back166 done();167 })168 })169170 it('should lookup in later paths until found', function(done){171 var app = createApp();Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c
lines 172–175why, one step back172 var views = [173 path.join(__dirname, 'fixtures', 'local_layout'),174 path.join(__dirname, 'fixtures', 'default_layout')175 ]deps: array-flatten@2.0.0Douglas Christopher Wilsonbefore adoption8a387d3
lines 176–181why, one step back176177 app.set('views', views);178 app.locals.name = 'tobi';179180 app.render('name.tmpl', function (err, str) {181 if (err) return done(err);Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b
line 182why, one step back182 assert.strictEqual(str, '<p>tobi</p>')deps: array-flatten@2.0.0Douglas Christopher Wilsonbefore adoption8a387d3
lines 183–188why, one step back183 done();184 })185 })186187 it('should error if file does not exist', function(done){188 var app = createApp();Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c
lines 189–192why, one step back189 var views = [190 path.join(__dirname, 'fixtures', 'local_layout'),191 path.join(__dirname, 'fixtures', 'default_layout')192 ]deps: array-flatten@2.0.0Douglas Christopher Wilsonbefore adoption8a387d3
lines 193–197why, one step back193194 app.set('views', views);195 app.locals.name = 'tobi';196197 app.render('pet.tmpl', function (err, str) {Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c
lines 198–199why, one step back198 assert.ok(err)199 assert.equal(err.message, 'Failed to lookup view "pet.tmpl" in views directories "' + views[0] + '" or "' + views[1] + '"')deps: array-flatten@2.0.0Douglas Christopher Wilsonbefore adoption8a387d3
lines 200–222why, one step back200 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);Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b
line 223why, one step back223 assert.strictEqual(str, 'abstract engine')deps: array-flatten@2.0.0Douglas Christopher Wilsonbefore adoption8a387d3
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);Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b
lines 249–250why, one step back249 assert.strictEqual(count, 1)250 assert.strictEqual(str, 'abstract engine')deps: array-flatten@2.0.0Douglas Christopher Wilsonbefore adoption8a387d3
lines 251–252why, one step back251 app.render('something', function(err, str){252 if (err) return done(err);Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b
lines 253–254why, one step back253 assert.strictEqual(count, 2)254 assert.strictEqual(str, 'abstract engine')deps: array-flatten@2.0.0Douglas Christopher Wilsonbefore adoption8a387d3
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);Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b
lines 279–280why, one step back279 assert.strictEqual(count, 1)280 assert.strictEqual(str, 'abstract engine')deps: array-flatten@2.0.0Douglas Christopher Wilsonbefore adoption8a387d3
lines 281–282why, one step back281 app.render('something', function(err, str){282 if (err) return done(err);Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b
lines 283–284why, one step back283 assert.strictEqual(count, 1)284 assert.strictEqual(str, 'abstract engine')deps: array-flatten@2.0.0Douglas Christopher Wilsonbefore adoption8a387d3
lines 285–295why, 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();295Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c
line 296why, one step back296 app.set('views', path.join(__dirname, 'fixtures'))deps: array-flatten@2.0.0Douglas Christopher Wilsonbefore adoption8a387d3
lines 297–301why, one step back297298 var user = { name: 'tobi' };299300 app.render('user.tmpl', { user: user }, function (err, str) {301 if (err) return done(err);Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b
line 302why, one step back302 assert.strictEqual(str, '<p>tobi</p>')deps: array-flatten@2.0.0Douglas Christopher Wilsonbefore adoption8a387d3
lines 303–309why, one step back303 done();304 })305 })306307 it('should expose app.locals', function(done){308 var app = createApp();309Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c
line 310why, one step back310 app.set('views', path.join(__dirname, 'fixtures'))deps: array-flatten@2.0.0Douglas Christopher Wilsonbefore adoption8a387d3
lines 311–314why, one step back311 app.locals.user = { name: 'tobi' };312313 app.render('user.tmpl', {}, function (err, str) {314 if (err) return done(err);Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b
line 315why, one step back315 assert.strictEqual(str, '<p>tobi</p>')deps: array-flatten@2.0.0Douglas Christopher Wilsonbefore adoption8a387d3
lines 316–322why, one step back316 done();317 })318 })319320 it('should give precedence to app.render() locals', function(done){321 var app = createApp();322Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c
line 323why, one step back323 app.set('views', path.join(__dirname, 'fixtures'))deps: array-flatten@2.0.0Douglas Christopher Wilsonbefore adoption8a387d3
lines 324–328why, one step back324 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);Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b
line 329why, one step back329 assert.strictEqual(str, '<p>jane</p>')deps: array-flatten@2.0.0Douglas Christopher Wilsonbefore adoption8a387d3
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);Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b
lines 354–355why, one step back354 assert.strictEqual(count, 1)355 assert.strictEqual(str, 'abstract engine')deps: array-flatten@2.0.0Douglas Christopher Wilsonbefore adoption8a387d3
lines 356–357why, one step back356 app.render('something', {cache: true}, function(err, str){357 if (err) return done(err);Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b
lines 358–359why, one step back358 assert.strictEqual(count, 1)359 assert.strictEqual(str, 'abstract engine')deps: array-flatten@2.0.0Douglas Christopher Wilsonbefore adoption8a387d3
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}