-
-
Notifications
You must be signed in to change notification settings - Fork 18.1k
/
Copy pathapp.render.js
468 lines (378 loc) · 13.2 KB
/
app.render.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
'use strict'
var assert = require('node:assert')
var express = require('..');
var path = require('node:path')
var tmpl = require('./support/tmpl');
const View = require('../lib/view.js');
describe('app', function(){
describe('.render(name, fn)', function(){
it('should support absolute paths', function(done){
var app = createApp();
app.locals.user = { name: 'tobi' };
app.render(path.join(__dirname, 'fixtures', 'user.tmpl'), function (err, str) {
if (err) return done(err);
assert.strictEqual(str, '<p>tobi</p>')
done();
})
})
it('should support absolute paths with "view engine"', function(done){
var app = createApp();
app.set('view engine', 'tmpl');
app.locals.user = { name: 'tobi' };
app.render(path.join(__dirname, 'fixtures', 'user'), function (err, str) {
if (err) return done(err);
assert.strictEqual(str, '<p>tobi</p>')
done();
})
})
it('should expose app.locals', function(done){
var app = createApp();
app.set('views', path.join(__dirname, 'fixtures'))
app.locals.user = { name: 'tobi' };
app.render('user.tmpl', function (err, str) {
if (err) return done(err);
assert.strictEqual(str, '<p>tobi</p>')
done();
})
})
it('should support index.<engine>', function(done){
var app = createApp();
app.set('views', path.join(__dirname, 'fixtures'))
app.set('view engine', 'tmpl');
app.render('blog/post', function (err, str) {
if (err) return done(err);
assert.strictEqual(str, '<h1>blog post</h1>')
done();
})
})
it('should handle render error throws', function(done){
var app = express();
function View(name, options){
this.name = name;
this.path = 'fale';
}
View.prototype.render = function(options, fn){
throw new Error('err!');
};
app.set('view', View);
app.render('something', function(err, str){
assert.ok(err)
assert.strictEqual(err.message, 'err!')
done();
})
})
describe('when the file does not exist', function(){
it('should provide a helpful error', function(done){
var app = createApp();
app.set('views', path.join(__dirname, 'fixtures'))
app.render('rawr.tmpl', function (err) {
assert.ok(err)
assert.equal(err.message, 'Failed to lookup view "rawr.tmpl" in views directory "' + path.join(__dirname, 'fixtures') + '"')
done();
});
})
})
describe('when an error occurs', function(){
it('should invoke the callback', function(done){
var app = createApp();
app.set('views', path.join(__dirname, 'fixtures'))
app.render('user.tmpl', function (err) {
assert.ok(err)
assert.equal(err.name, 'RenderError')
done()
})
})
})
describe('when an extension is given', function(){
it('should render the template', function(done){
var app = createApp();
app.set('views', path.join(__dirname, 'fixtures'))
app.render('email.tmpl', function (err, str) {
if (err) return done(err);
assert.strictEqual(str, '<p>This is an email</p>')
done();
})
})
})
describe('when "view engine" is given', function(){
it('should render the template', function(done){
var app = createApp();
app.set('view engine', 'tmpl');
app.set('views', path.join(__dirname, 'fixtures'))
app.render('email', function(err, str){
if (err) return done(err);
assert.strictEqual(str, '<p>This is an email</p>')
done();
})
})
})
describe('when "views" is given', function(){
it('should lookup the file in the path', function(done){
var app = createApp();
app.set('views', path.join(__dirname, 'fixtures', 'default_layout'))
app.locals.user = { name: 'tobi' };
app.render('user.tmpl', function (err, str) {
if (err) return done(err);
assert.strictEqual(str, '<p>tobi</p>')
done();
})
})
describe('when array of paths', function(){
it('should lookup the file in the path', function(done){
var app = createApp();
var views = [
path.join(__dirname, 'fixtures', 'local_layout'),
path.join(__dirname, 'fixtures', 'default_layout')
]
app.set('views', views);
app.locals.user = { name: 'tobi' };
app.render('user.tmpl', function (err, str) {
if (err) return done(err);
assert.strictEqual(str, '<span>tobi</span>')
done();
})
})
it('should lookup in later paths until found', function(done){
var app = createApp();
var views = [
path.join(__dirname, 'fixtures', 'local_layout'),
path.join(__dirname, 'fixtures', 'default_layout')
]
app.set('views', views);
app.locals.name = 'tobi';
app.render('name.tmpl', function (err, str) {
if (err) return done(err);
assert.strictEqual(str, '<p>tobi</p>')
done();
})
})
it('should error if file does not exist', function(done){
var app = createApp();
var views = [
path.join(__dirname, 'fixtures', 'local_layout'),
path.join(__dirname, 'fixtures', 'default_layout')
]
app.set('views', views);
app.locals.name = 'tobi';
app.render('pet.tmpl', function (err, str) {
assert.ok(err)
assert.equal(err.message, 'Failed to lookup view "pet.tmpl" in views directories "' + views[0] + '" or "' + views[1] + '"')
done();
})
})
})
})
describe('when a "view" constructor is given', function(){
it('should create an instance of it', function(done){
var app = express();
function View(name, options){
this.name = name;
this.path = 'path is required by application.js as a signal of success even though it is not used there.';
}
View.prototype.render = function(options, fn){
fn(null, 'abstract engine');
};
app.set('view', View);
app.render('something', function(err, str){
if (err) return done(err);
assert.strictEqual(str, 'abstract engine')
done();
})
})
})
describe('caching', function(){
it('should always lookup view without cache', function(done){
var app = express();
var count = 0;
function View(name, options){
this.name = name;
this.path = 'fake';
count++;
}
View.prototype.render = function(options, fn){
fn(null, 'abstract engine');
};
app.set('view cache', false);
app.set('view', View);
app.render('something', function(err, str){
if (err) return done(err);
assert.strictEqual(count, 1)
assert.strictEqual(str, 'abstract engine')
app.render('something', function(err, str){
if (err) return done(err);
assert.strictEqual(count, 2)
assert.strictEqual(str, 'abstract engine')
done();
})
})
})
it('should cache with "view cache" setting', function(done){
var app = express();
var count = 0;
function View(name, options){
this.name = name;
this.path = 'fake';
count++;
}
View.prototype.render = function(options, fn){
fn(null, 'abstract engine');
};
app.set('view cache', true);
app.set('view', View);
app.render('something', function(err, str){
if (err) return done(err);
assert.strictEqual(count, 1)
assert.strictEqual(str, 'abstract engine')
app.render('something', function(err, str){
if (err) return done(err);
assert.strictEqual(count, 1)
assert.strictEqual(str, 'abstract engine')
done();
})
})
})
})
})
describe('.render(name, options, fn)', function(){
it('should render the template', function(done){
var app = createApp();
app.set('views', path.join(__dirname, 'fixtures'))
var user = { name: 'tobi' };
app.render('user.tmpl', { user: user }, function (err, str) {
if (err) return done(err);
assert.strictEqual(str, '<p>tobi</p>')
done();
})
})
it('should expose app.locals', function(done){
var app = createApp();
app.set('views', path.join(__dirname, 'fixtures'))
app.locals.user = { name: 'tobi' };
app.render('user.tmpl', {}, function (err, str) {
if (err) return done(err);
assert.strictEqual(str, '<p>tobi</p>')
done();
})
})
it('should give precedence to app.render() locals', function(done){
var app = createApp();
app.set('views', path.join(__dirname, 'fixtures'))
app.locals.user = { name: 'tobi' };
var jane = { name: 'jane' };
app.render('user.tmpl', { user: jane }, function (err, str) {
if (err) return done(err);
assert.strictEqual(str, '<p>jane</p>')
done();
})
})
describe('caching', function(){
it('should cache with cache option', function(done){
var app = express();
var count = 0;
function View(name, options){
this.name = name;
this.path = 'fake';
count++;
}
View.prototype.render = function(options, fn){
fn(null, 'abstract engine');
};
app.set('view cache', false);
app.set('view', View);
app.render('something', {cache: true}, function(err, str){
if (err) return done(err);
assert.strictEqual(count, 1)
assert.strictEqual(str, 'abstract engine')
app.render('something', {cache: true}, function(err, str){
if (err) return done(err);
assert.strictEqual(count, 1)
assert.strictEqual(str, 'abstract engine')
done();
})
})
})
})
})
})
describe('View.prototype.render', function () {
it('should force callback to be async and pass correct arguments', function (done) {
var mockEngine = function (filePath, options, callback) {
callback(null, 'rendered content');
};
var view = new View('test', {
root: path.join(__dirname, 'fixtures'),
engines: { '.tmpl': mockEngine },
defaultEngine: '.tmpl'
});
var isAsync = false;
var originalCallback = function (err, html) {
assert(isAsync, 'Callback should be async');
assert.strictEqual(err, null, 'Error should be null');
assert.strictEqual(html, 'rendered content', 'Rendered content should match');
done();
};
view.render({}, function (err, html) {
isAsync = true;
originalCallback(err, html);
});
});
it('should handle errors correctly', function (done) {
var mockEngine = function (filePath, options, callback) {
callback(new Error('render error'));
};
var view = new View('test', {
root: path.join(__dirname, 'fixtures'),
engines: { '.tmpl': mockEngine },
defaultEngine: '.tmpl'
});
view.render({}, function (err, html) {
assert(err instanceof Error, 'Error should be an instance of Error');
assert.strictEqual(err.message, 'render error', 'Error message should match');
assert.strictEqual(html, undefined, 'HTML should be undefined when there is an error');
done();
});
});
it('should handle synchronous callbacks correctly', function (done) {
var mockEngine = function (filePath, options, callback) {
callback(null, 'sync rendered content');
};
var view = new View('test', {
root: path.join(__dirname, 'fixtures'),
engines: { '.tmpl': mockEngine },
defaultEngine: '.tmpl'
});
var isAsync = false;
var originalCallback = function (err, html) {
assert(isAsync, 'Callback should be async');
assert.strictEqual(err, null, 'Error should be null');
assert.strictEqual(html, 'sync rendered content', 'Rendered content should match');
done();
};
view.render({}, function (err, html) {
isAsync = true;
originalCallback(err, html);
});
});
it('should pass correct arguments to the engine', function (done) {
var mockEngine = function (filePath, options, callback) {
assert.strictEqual(filePath, view.path, 'File path should match');
assert.deepStrictEqual(options, { key: 'value' }, 'Options should match');
callback(null, 'rendered content');
};
var view = new View('test', {
root: path.join(__dirname, 'fixtures'),
engines: { '.tmpl': mockEngine },
defaultEngine: '.tmpl'
});
view.render({ key: 'value' }, function (err, html) {
assert.strictEqual(err, null, 'Error should be null');
assert.strictEqual(html, 'rendered content', 'Rendered content should match');
done();
});
});
});
function createApp() {
var app = express();
app.engine('.tmpl', tmpl);
return app;
}