forked from balderdashy/sails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.viewRendering.test.js
187 lines (144 loc) · 4.95 KB
/
router.viewRendering.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
/**
* Test dependencies
*/
var assert = require('assert');
var httpHelper = require('./helpers/httpHelper.js');
var appHelper = require('./helpers/appHelper');
var _ = require('lodash');
var fs = require('fs');
describe('router :: ', function() {
describe('View routes', function() {
var appName = 'testApp';
before(function(done) {
appHelper.build(function() {
fs.writeFileSync('config/extraroutes.js', 'module.exports.routes = ' + JSON.stringify({
'/testView': {
view: 'viewtest/index'
},
'/app': {
view: 'app'
},
'/user': {
view: 'app/user/homepage'
}
}));
return done();
});
});
beforeEach(function(done) {
appHelper.lift({
verbose: false
}, function(err, sails) {
if (err) {
throw new Error(err);
}
sailsprocess = sails;
setTimeout(done, 100);
});
});
afterEach(function(done) {
sailsprocess.lower(function() {
setTimeout(done, 100);
});
});
after(function() {
process.chdir('../');
appHelper.teardown();
});
describe('with default routing', function() {
it('should respond to a get request to localhost:1342 with welcome page', function(done) {
httpHelper.testRoute('get', '', function(err, response) {
if (err) {
return done(new Error(err));
}
assert(response.body.indexOf('not found') < 0);
assert(response.body.indexOf('<!-- Default home page -->') > -1);
done();
});
});
it('should wrap the view in the default layout', function(done) {
httpHelper.testRoute('get', '', function(err, response) {
if (err) {
return done(new Error(err));
}
assert(response.body.indexOf('<html>') > -1);
done();
});
});
});
describe('with specified routing using the "view:" syntax', function() {
it('route with config {view: "app"} should respond to a get request with the "app/index.ejs" view if "app.ejs" does not exist', function(done) {
httpHelper.testRoute('get', 'app', function(err, response) {
if (err) {
return done(new Error(err));
}
assert(response.body.indexOf('not found') < 0);
assert(response.body.indexOf('App index file') > -1);
done();
});
});
it('route with config {view: "viewtest/index"} should respond to a get request with "viewtest/index.ejs"', function(done) {
httpHelper.testRoute('get', 'testView', function(err, response) {
if (err) {
return done(new Error(err));
}
assert(response.body.indexOf('not found') < 0);
assert(response.body.indexOf('indexView') > -1);
done();
});
});
it('route with config {view: "app/user/homepage"} should respond to a get request with "app/user/homepage.ejs"', function(done) {
httpHelper.testRoute('get', 'user', function(err, response) {
if (err) {
return done(new Error(err));
}
assert(response.body.indexOf('not found') < 0);
assert(response.body.indexOf('I\'m deeply nested!') > -1);
done();
});
});
});
describe('with no specified routing', function() {
before(function() {
httpHelper.writeRoutes({});
});
it('should respond to get request to :controller with the template at views/:controller/index.ejs', function(done) {
// Empty router file
httpHelper.testRoute('get', 'viewTest', function(err, response) {
if (err) {
return done(new Error(err));
}
assert(response.body.indexOf('indexView') !== -1, response.body);
done();
});
});
it('should respond to get request to :controller/:action with the template at views/:controller/:action.ejs', function(done) {
httpHelper.testRoute('get', 'viewTest/create', function(err, response) {
if (err) {
return done(new Error(err));
}
assert(response.body.indexOf('createView') !== -1);
done();
});
});
it('should merge config.views.locals into the view locals', function(done) {
httpHelper.testRoute('get', 'viewTest/viewOptions', function(err, response) {
if (err) {
return done(new Error(err));
}
assert(response.body.indexOf('!bar!') !== -1);
done();
});
});
it('should allow config.views.locals to be overridden', function(done) {
httpHelper.testRoute('get', 'viewTest/viewOptionsOverride', function(err, response) {
if (err) {
return done(new Error(err));
}
assert(response.body.indexOf('!baz!') !== -1);
done();
});
});
});
});
});