-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathview_test.js
More file actions
230 lines (207 loc) · 5.27 KB
/
view_test.js
File metadata and controls
230 lines (207 loc) · 5.27 KB
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
var demand = require('must');
var request = require('supertest');
var methodOverride = require('method-override');
var bodyParser = require('body-parser');
var keystone = require('../../index.js');
var getApp = function() {
var app = keystone.express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(methodOverride());
return app;
};
describe('Keystone.View', function() {
describe('new', function() {
it('must be an instance of View', function(done) {
var app = getApp();
app.get('/', function(req, res) {
var view = new keystone.View(req, res);
view.must.be.an.instanceof(keystone.View);
res.send('OK');
});
request(app)
.get('/')
.expect('OK', done);
});
});
describe('.render(callback)', function() {
it('must call the callback function', function(done) {
var app = getApp();
app.get('/', function(req, res) {
var view = new keystone.View(req, res);
view.render(function() {
res.send('OK');
});
});
request(app)
.get('/')
.expect('OK', done);
});
});
describe('.render(callback)', function() {
it('must pass (err, req, res) to the callback', function(done) {
var app = getApp();
app.get('/', function(req, res) {
var view = new keystone.View(req, res);
view.render(function(err, req2, res2) {
demand(err).not.exist();
req2.must.equal(req);
res2.must.equal(res);
res.send('OK');
});
});
request(app)
.get('/')
.expect('OK', done);
});
});
describe('.on(event, [match,] fn)', function() {
it('must call init methods first', function(done) {
var app = getApp();
app.get('/', function(req, res) {
var view = new keystone.View(req, res);
var status = 'NOT OK';
view.on('init', function(next) {
status = 'OK';
next();
});
view.render(function() {
res.send(status);
});
});
request(app)
.get('/')
.expect('OK', done);
});
function getApp_getAndPost() {
var app = getApp();
app.all('/', function(req, res) {
var view = new keystone.View(req, res);
var status = 'OK';
view.on('get', function(next) {
status = 'OK GET';
next();
});
view.on('post', function(next) {
status = 'OK POST';
next();
});
view.render(function() {
res.send(status);
});
});
return app;
}
it('must call get actions correctly', function(done) {
request(getApp_getAndPost())
.get('/')
.expect('OK GET', done);
});
it('must call post actions correctly', function(done) {
request(getApp_getAndPost())
.post('/')
.expect('OK POST', done);
});
function getApp_conditionalGet() {
var app = getApp();
app.get('/', function(req, res) {
var view = new keystone.View(req, res);
var status = 'OK';
view.on('get', { test: 'yes' }, function(next) {
status = 'OK GET';
next();
});
view.render(function() {
res.send(status);
});
});
return app;
}
it('must invoke get actions with matching query parameters', function(done) {
request(getApp_conditionalGet())
.get('/?test=yes')
.expect('OK GET', done);
});
it('must skip get actions without matching query parameters', function(done) {
request(getApp_conditionalGet())
.get('/')
.expect('OK', done);
});
function getApp_conditionalPostValue() {
var app = getApp();
app.post('/', function(req, res) {
var view = new keystone.View(req, res);
var status = 'OK';
view.on('post', { test: 'yes' }, function(next) {
status = 'OK POST';
next();
});
view.render(function() {
res.send(status);
});
});
return app;
}
it('must invoke post actions with matching body data', function(done) {
request(getApp_conditionalPostValue())
.post('/')
.send({ test: 'yes' })
.expect('OK POST', done);
});
it('must skip post actions with non-matching body data', function(done) {
request(getApp_conditionalPostValue())
.post('/')
.send({ test: 'no' })
.expect('OK', done);
});
function getApp_conditionalPostTruthy() {
var app = getApp();
app.post('/', function(req, res) {
var view = new keystone.View(req, res);
var status = 'OK';
view.on('post', { test: true }, function(next) {
status = 'OK POST';
next();
});
view.render(function() {
res.send(status);
});
});
return app;
}
it('must invoke post actions with body data present', function(done) {
request(getApp_conditionalPostTruthy())
.post('/')
.send({ test: 'yes' })
.expect('OK POST', done);
});
it('must skip post actions without matching body data', function(done) {
request(getApp_conditionalPostTruthy())
.post('/')
.expect('OK', done);
});
function getApp_extRequest() {
var app = getApp();
app.get('/', function(req, res) {
req.ext = { prop: 'value' };
var view = new keystone.View(req, res);
var status = 'NOT OK';
view.on({ 'ext.prop': 'value' }, function(next) {
status = 'OK';
next();
});
view.render(function() {
res.send(status);
});
});
return app;
}
it('must invoke actions based on req properties', function(done) {
request(getApp_extRequest())
.get('/')
.expect('OK', done);
});
});
});