Skip to content
Myr

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 file

Take 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 back
1

Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c

line 2why, one step back
2var assert = require('assert')

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

line 3why, one step back
3var express = require('..');

Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c

line 4why, one step back
4var path = require('path')

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

line 5why, one step back
5var tmpl = require('./support/tmpl');

Merge tag '3.14.0'Douglas Christopher Wilsonbefore adoption4ea6f21

lines 6–9why, one step back
6
7describe('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 back
10 var app = createApp();

Merge tag '3.14.0'Douglas Christopher Wilsonbefore adoption4ea6f21

lines 11–13why, one step back
11
12 app.locals.user = { name: 'tobi' };13

Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c

line 14why, one step back
14 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 back
15 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 back
22 var app = createApp();

Merge tag '3.14.0'Douglas Christopher Wilsonbefore adoption4ea6f21

line 23why, one step back
23

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

line 24why, one step back
24 app.set('view engine', 'tmpl');

Merge tag '3.14.0'Douglas Christopher Wilsonbefore adoption4ea6f21

lines 25–26why, one step back
25 app.locals.user = { name: 'tobi' };
26

Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c

line 27why, one step back
27 app.render(path.join(__dirname, 'fixtures', 'user'), function (err, str) {

Merge tag '3.14.0'Douglas Christopher Wilsonbefore adoption4ea6f21

lines 28–34why, one step back
28 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 back
35 var app = createApp();

Merge tag '3.14.0'Douglas Christopher Wilsonbefore adoption4ea6f21

line 36why, one step back
36

Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c

line 37why, one step back
37 app.set('views', path.join(__dirname, 'fixtures'))

Merge tag '3.14.0'Douglas Christopher Wilsonbefore adoption4ea6f21

lines 38–39why, one step back
38 app.locals.user = { name: 'tobi' };
39

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

line 40why, one step back
40 app.render('user.tmpl', function (err, str) {

Merge tag '3.14.0'Douglas Christopher Wilsonbefore adoption4ea6f21

lines 41–47why, one step back
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){

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

line 48why, one step back
48 var app = createApp();

Merge tag '3.14.0'Douglas Christopher Wilsonbefore adoption4ea6f21

line 49why, one step back
49

Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c

line 50why, one step back
50 app.set('views', path.join(__dirname, 'fixtures'))

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

line 51why, one step back
51 app.set('view engine', 'tmpl');

Merge tag '3.14.0'Douglas Christopher Wilsonbefore adoption4ea6f21

line 52why, one step back
52

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

line 53why, one step back
53 app.render('blog/post', function (err, str) {

Merge tag '3.14.0'Douglas Christopher Wilsonbefore adoption4ea6f21

lines 54–74why, one step back
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){

Merge tag '4.15.4'Douglas Christopher Wilsonbefore adoptionc319fe2

line 75why, one step back
75 err.should.be.ok()

Merge tag '3.14.0'Douglas Christopher Wilsonbefore adoption4ea6f21

lines 76–82why, one step back
76 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 back
83 var app = createApp();
84

Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c

line 85why, one step back
85 app.set('views', path.join(__dirname, 'fixtures'))

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

line 86why, one step back
86 app.render('rawr.tmpl', function (err) {

Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c

lines 87–88why, one step back
87 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 back
89 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 back
96 var app = createApp();

Merge tag '3.14.0'Douglas Christopher Wilsonbefore adoption4ea6f21

line 97why, one step back
97

Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c

line 98why, one step back
98 app.set('views', path.join(__dirname, 'fixtures'))

Merge tag '3.14.0'Douglas Christopher Wilsonbefore adoption4ea6f21

line 99why, one step back
99

Merge tag '4.15.5'Douglas Christopher Wilsonbefore adoption19c8d64

lines 100–103why, one step back
100 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 back
104 })
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 back
110 var app = createApp();

Merge tag '3.14.0'Douglas Christopher Wilsonbefore adoption4ea6f21

line 111why, one step back
111

Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c

line 112why, one step back
112 app.set('views', path.join(__dirname, 'fixtures'))

Merge tag '3.14.0'Douglas Christopher Wilsonbefore adoption4ea6f21

line 113why, one step back
113

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

line 114why, one step back
114 app.render('email.tmpl', function (err, str) {

Merge tag '3.14.0'Douglas Christopher Wilsonbefore adoption4ea6f21

lines 115–123why, one step back
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){

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

line 124why, one step back
124 var app = createApp();

Merge tag '3.14.0'Douglas Christopher Wilsonbefore adoption4ea6f21

line 125why, one step back
125

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

line 126why, one step back
126 app.set('view engine', 'tmpl');

Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c

line 127why, one step back
127 app.set('views', path.join(__dirname, 'fixtures'))

Merge tag '3.14.0'Douglas Christopher Wilsonbefore adoption4ea6f21

lines 128–136why, one step back
128
129 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 })136

Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352

lines 137–138why, one step back
137 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 back
139 var app = createApp();

Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352

line 140why, one step back
140

Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c

line 141why, one step back
141 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 back
142 app.locals.user = { name: 'tobi' };
143

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

line 144why, one step back
144 app.render('user.tmpl', function (err, str) {

Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352

lines 145–152why, one step back
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){

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

line 153why, one step back
153 var app = createApp();

Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c

lines 154–157why, one step back
154 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 back
158
159 app.set('views', views);160 app.locals.user = { name: 'tobi' };161

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

line 162why, one step back
162 app.render('user.tmpl', function (err, str) {

Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352

lines 163–169why, one step back
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){

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

line 170why, one step back
170 var app = createApp();

Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c

lines 171–174why, one step back
171 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 back
175
176 app.set('views', views);177 app.locals.name = 'tobi';178

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

line 179why, one step back
179 app.render('name.tmpl', function (err, str) {

Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352

lines 180–186why, one step back
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){

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

line 187why, one step back
187 var app = createApp();

Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c

lines 188–191why, one step back
188 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 back
192
193 app.set('views', views);194 app.locals.name = 'tobi';195

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

line 196why, one step back
196 app.render('pet.tmpl', function (err, str) {

Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c

lines 197–198why, one step back
197 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 back
199 done();
200 })201 })202 })203 })204

Merge tag '3.14.0'Douglas Christopher Wilsonbefore adoption4ea6f21

lines 205–292why, one step back
205 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 back
293 var app = createApp();

Merge tag '3.14.0'Douglas Christopher Wilsonbefore adoption4ea6f21

line 294why, one step back
294

Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c

line 295why, one step back
295 app.set('views', path.join(__dirname, 'fixtures'))

Merge tag '3.14.0'Douglas Christopher Wilsonbefore adoption4ea6f21

lines 296–298why, one step back
296
297 var user = { name: 'tobi' };298

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

line 299why, one step back
299 app.render('user.tmpl', { user: user }, function (err, str) {

Merge tag '3.14.0'Douglas Christopher Wilsonbefore adoption4ea6f21

lines 300–306why, one step back
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){

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

line 307why, one step back
307 var app = createApp();

Merge tag '3.14.0'Douglas Christopher Wilsonbefore adoption4ea6f21

line 308why, one step back
308

Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c

line 309why, one step back
309 app.set('views', path.join(__dirname, 'fixtures'))

Merge tag '3.14.0'Douglas Christopher Wilsonbefore adoption4ea6f21

lines 310–311why, one step back
310 app.locals.user = { name: 'tobi' };
311

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

line 312why, one step back
312 app.render('user.tmpl', {}, function (err, str) {

Merge tag '3.14.0'Douglas Christopher Wilsonbefore adoption4ea6f21

lines 313–319why, one step back
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){

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

line 320why, one step back
320 var app = createApp();

Merge tag '3.14.0'Douglas Christopher Wilsonbefore adoption4ea6f21

line 321why, one step back
321

Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c

line 322why, one step back
322 app.set('views', path.join(__dirname, 'fixtures'))

Merge tag '3.14.0'Douglas Christopher Wilsonbefore adoption4ea6f21

lines 323–325why, one step back
323 app.locals.user = { name: 'tobi' };
324 var jane = { name: 'jane' };325

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

line 326why, one step back
326 app.render('user.tmpl', { user: jane }, function (err, str) {

Merge tag '3.14.0'Douglas Christopher Wilsonbefore adoption4ea6f21

lines 327–365why, one step back
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})

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

lines 366–373why, one step back
366
367function createApp() {368 var app = express();369370 app.engine('.tmpl', tmpl);371372 return app;373}