-
Notifications
You must be signed in to change notification settings - Fork 10
/
render.test.js
226 lines (203 loc) · 6.4 KB
/
render.test.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
/*!
* connect-render - test/render.test.js
* Copyright(c) 2012 fengmk2 <fengmk2@gmail.com>
* MIT Licensed
*/
/**
* Module dependencies.
*/
var path = require('path');
var rewire = require('rewire');
var fs = require('fs');
var connect = require('connect');
var request = require('supertest');
var render = rewire('../lib/render');
var should = require('should');
var options = {
root: path.join(__dirname, 'views'),
layout: 'layout.html',
cache: true, // `false` for debug
helpers: {
sitename: 'connect-render demo site',
starttime: new Date().getTime(),
requestURL: function (req, res) {
return req.url;
},
overwrite: 'default'
},
filters: {
query: function (url) {
return require('url').parse(url);
}
}
};
var app = connect(render(options));
app.use(function (req, res) {
if (req.url === '/viewerror') {
return res.render('noexists.html', { name: 'fengmk2' });
}
if (req.url === '/filters') {
return res.render('filters.html', { layout: false });
}
if (req.url === '/xml') {
res.setHeader('Content-Type', 'text/xml');
return res.render('xml.html', { layout: false });
}
if (req.url === '/nolayout') {
return res.render('nolayout.html', { layout: false });
}
if (req.url === '/partial_in_partial') {
return res.render('./partial_in_partial.html', { layout: false });
}
if (req.url === '/partial_not_exists') {
return res.render('partial_not_exists.html', { layout: false });
}
if (req.url === '/error') {
return res.render('error.html');
}
if (req.url === '/options.scope') {
return res.render('options.scope.html', {
layout: false,
scope: { name: 'scope test' },
othername: 'out of scope name'
});
}
if (req.url === '/layout_error') {
return res.render('index.html', {
layout: 'error.html',
name: 'fengmk2'
});
}
if (req.url === '/overwrite') {
return res.render('overwrite.html', {
layout: false,
overwrite: 'success'
});
}
if (req.url === '/nooverwrite') {
return res.render('overwrite.html', {
layout: false
});
}
res.render('index.html', { name: 'fengmk2' });
});
app.use(function (err, req, res, next) {
// console.log(err.stack);
next(err);
});
var success = fs.readFileSync(__dirname + '/success.html', 'utf8')
.replace('$starttime$', options.helpers.starttime);
describe('render.test.js', function () {
before(function (done) {
app = app.listen(0, done);
});
describe('render()', function () {
it('should work and return Content-Type', function (done) {
var cache = render.__get__('cache');
cache.should.not.have.property('index.html');
cache.should.not.have.property('layout.html');
request(app).get('/')
.expect('Content-Type', 'text/html; charset=utf-8')
.expect(200, success)
.end(function (err, res) {
cache.should.have.property('index.html');
// cache['index.html'].should.be.a('function');
cache.should.have.property('layout.html');
// cache['layout.html'].should.be.a('function');
done(err);
});
});
it('should return text/xml', function (done) {
request(app)
.get('/xml')
.expect('Content-Type', 'text/xml; charset=utf-8')
.expect(200, done);
});
it('should work with cache', function (done) {
var cache = render.__get__('cache');
cache.should.have.property('index.html');//.with.be.a('function');
cache.should.have.property('layout.html');//.with.be.a('function');
request(app).get('/')
.expect(200)
.expect(success)
.end(function (err, res) {
cache.should.have.property('index.html');//.with.be.a('function');
cache.should.have.property('layout.html');//.with.be.a('function');
done(err);
});
});
it('should error when view not exists', function (done) {
request(app).get('/viewerror')
.expect(500)
.expect(/ENOENT/, done);
});
it('should return no layout', function (done) {
request(app).get('/nolayout')
.expect(200)
.expect('nolayout', done);
});
it('should return 500 when template error', function (done) {
request(app).get('/error')
.expect(500, function (err, res) {
should.not.exists(err);
res.text.should.include('ReferenceError: ');
res.text.should.include('error_var is not defined');
done();
});
});
it('should return 500 when layout error', function (done) {
request(app).get('/layout_error')
.expect(500)
.expect(/error_var is not defined/, done);
});
// options.scope not support on ejs@0.8.4+
// https://github.com/visionmedia/ejs/commit/5786c8e5af92f4a52a214850b6b1220682ed9d79
it.skip('should support options.scope', function (done) {
request(app).get('/options.scope')
.expect(200, function (err, res) {
should.not.exists(err);
res.text.should.equal('scope test:out of scope name');
done();
});
});
it('should render with filters', function (done) {
request(app).get('/filters')
.expect(200)
.expect('你… | 12,345.12 | 2012-11-11 00:00:00 | /filters', done);
});
it('should overwrite:`default` to overwrite:`success`', function (done) {
request(app).get('/overwrite')
.expect(200)
.expect('success', done);
});
it('should overwrite:`default` to overwrite:`default`', function (done) {
request(app).get('/nooverwrite')
.expect(200)
.expect('default', done);
});
});
describe('partial()', function () {
it('should fixed partial in partial', function (done) {
request(app).get('/partial_in_partial')
.expect(200)
.expect('partialpartial', done);
});
it('should console.error when partial view not exists', function (done) {
var _error = console.error;
var errormsg = '';
console.error = function (msg) {
var util = require('util');
errormsg = util.format.apply(util, Array.prototype.slice.call(arguments));
};
request(app).get('/partial_not_exists')
.expect(200)
.expect('partial_not_exists')
.end(function (err, res) {
console.error = _error;
errormsg.should.include('[connect-render] Error: cannot load view partial');
errormsg.should.include('Error: ENOENT, no such file or directory');
done(err);
});
});
});
});