Skip to content
Myr

app.render.js

This file

Path

test/app.render.js

373 lines · 10.1 KiB · JavaScript

Last changed by

Not known.

History of this file

Take it

The walk stopped after 200 commits: lines older than fa272ed point at it.

docs: fix typo in jsdoc commentHrvoje Šimićbefore adoptionfa272ed

lines 1–373why, one step back
1
2var assert = require('assert')3var express = require('..');4var path = require('path')5var 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' };1314 app.render(path.join(__dirname, 'fixtures', 'user.tmpl'), function (err, str) {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){22 var app = createApp();2324 app.set('view engine', 'tmpl');25 app.locals.user = { name: 'tobi' };2627 app.render(path.join(__dirname, 'fixtures', 'user'), function (err, str) {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){35 var app = createApp();3637 app.set('views', path.join(__dirname, 'fixtures'))38 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();4950 app.set('views', path.join(__dirname, 'fixtures'))51 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){75 err.should.be.ok()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){83 var app = createApp();8485 app.set('views', path.join(__dirname, 'fixtures'))86 app.render('rawr.tmpl', function (err) {87 assert.ok(err)88 assert.equal(err.message, 'Failed to lookup view "rawr.tmpl" in views directory "' + path.join(__dirname, 'fixtures') + '"')89 done();90 });91 })92 })9394 describe('when an error occurs', function(){95 it('should invoke the callback', function(done){96 var app = createApp();9798 app.set('views', path.join(__dirname, 'fixtures'))99100 app.render('user.tmpl', function (err) {101 assert.ok(err)102 assert.equal(err.name, 'RenderError')103 done()104 })105 })106 })107108 describe('when an extension is given', function(){109 it('should render the template', function(done){110 var app = createApp();111112 app.set('views', path.join(__dirname, 'fixtures'))113114 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');127 app.set('views', path.join(__dirname, 'fixtures'))128129 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();140141 app.set('views', path.join(__dirname, 'fixtures', 'default_layout'))142 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();154 var views = [155 path.join(__dirname, 'fixtures', 'local_layout'),156 path.join(__dirname, 'fixtures', 'default_layout')157 ]158159 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();171 var views = [172 path.join(__dirname, 'fixtures', 'local_layout'),173 path.join(__dirname, 'fixtures', 'default_layout')174 ]175176 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();188 var views = [189 path.join(__dirname, 'fixtures', 'local_layout'),190 path.join(__dirname, 'fixtures', 'default_layout')191 ]192193 app.set('views', views);194 app.locals.name = 'tobi';195196 app.render('pet.tmpl', function (err, str) {197 assert.ok(err)198 assert.equal(err.message, 'Failed to lookup view "pet.tmpl" in views directories "' + views[0] + '" or "' + views[1] + '"')199 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();294295 app.set('views', path.join(__dirname, 'fixtures'))296297 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();308309 app.set('views', path.join(__dirname, 'fixtures'))310 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();321322 app.set('views', path.join(__dirname, 'fixtures'))323 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}