-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathextensions.test.js
More file actions
233 lines (189 loc) · 7.61 KB
/
Copy pathextensions.test.js
File metadata and controls
233 lines (189 loc) · 7.61 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
231
232
233
// This test written using mocha and should. Run it using `make test` command.
var should = require('./'), app, compound, hatch;
var group;
describe('extensions /controller/', function() {
before(function(done) {
app = getApp(function() {
hatch = app.compound.hatch;
hatch.loadModules(__dirname + '/fixtures/modules');
app.compound.structure.controllers.test = TestController;
app.compound.models.Group.findOne(function(err, g) {
group = g;
done();
});
});
});
describe('moduleEnabled', function() {
it('should be defined on controller context', test(function(c) {
should.exist(c.moduleEnabled);
c.moduleEnabled.should.be.a('function');
c.next();
}));
it('should return config when module enabled', test(function(c) {
c.moduleEnabled('not exist').should.be.false;
c.locals.group = group;
c.moduleEnabled('admin').should.be.ok;
c.next();
}));
it('should return false when module not enabled', test(function(c) {
c.locals.group = group;
c.moduleEnabled('adminka').should.not.be.ok;
c.next();
}));
it('should return false when module not enabled', test(function(c) {
delete c.locals.group;
c.moduleEnabled('not exist').should.be.false;
c.next();
}));
});
describe('moduleConfigured', function() {
it('should be defined on controller context', test(function(c) {
should.exist(c.moduleConfigured);
c.moduleConfigured.should.be.a('function');
c.next();
}));
it('should return false when module is not enabled', test(function(c) {
c.moduleConfigured('adminka').should.be.false;
c.next();
}));
it('should return true when module not configurable', test(function(c) {
c.locals.group = group;
hatch.modules.admin = {info: {}};
c.moduleConfigured('admin').should.be.true;
c.next();
}));
it('should return false when module not configured or configuration is not valid', test(function(c) {
c.locals.group = group;
hatch.modules.admin = {info:{settings:{fields:{a:{required: true}}}}};
c.moduleConfigured('admin').should.be.false;
c.next();
}));
it('should return true when module configured ok', test(function(c) {
c.locals.group = group;
hatch.modules.admin = {info:{settings:{fields:{a:{required: true}}}}};
c.moduleEnabled('admin').contract = {a: 1};
c.moduleConfigured('admin').should.be.true;
c.next();
}));
});
describe('pathFor', function() {
it('should be defined on controller context', test(function(c) {
should.exist(c.pathFor);
c.next();
}));
it('should return path helpers collection for module', test(function(c) {
hatch.modules.simple.compound.map.resources('users');
should.exist(c.pathFor('simple').users);
c.pathFor('simple').users().should.equal('/do/simple/users');
c.next();
}));
it('should return empty object for not existing module', test(function(c) {
delete hatch.modules.admin;
JSON.stringify(c.pathFor('admin')).should.equal('{}');
c.next();
}));
});
describe('stripHtml', function() {
it('should be defined on controller context', test(function(c) {
should.exist(c.stripHtml);
c.next();
}));
it('should return string without html', test(function(c) {
c.stripHtml('hello <b attr="param">world</b>').should.equal('hello world');
c.next();
}));
it('should return text with specified length without trimming words', test(function(c) {
c.stripHtml('hello lorem <i>ipsum</i>', 10).should.have.lengthOf(8);
c.stripHtml('hello lorem', 10).should.have.lengthOf(8);
c.stripHtml('hellolo lolo', 10).should.have.lengthOf(10);
c.stripHtml('hellolololo', 10).should.have.lengthOf(13);
c.next();
}));
});
describe('renderContent', function() {
it('should be defined on controller context', test(function(c) {
should.exist(c.renderContent);
c.next();
}));
it('should be defined on controller context', test(function(c) {
c.renderContent('ha');
c.next();
}));
});
describe('errors', function() {
it('should raise 404 error', test(function(c) {
should.exist(c.errors.NotFound);
var err = c.errors.NotFound('message');
err.should.be.instanceOf(Error);
err.should.be.instanceOf(c.errors.NotFound);
err.code.should.equal(404);
err.message.should.equal('message');
c.next();
}));
it('should raise 403 error', test(function(c) {
should.exist(c.errors.Forbidden);
var err = c.errors.Forbidden('message');
err.should.be.instanceOf(Error);
err.should.be.instanceOf(c.errors.Forbidden);
err.code.should.equal(403);
err.message.should.equal('message');
c.next();
}));
it('should raise 500 error', test(function(c) {
should.exist(c.errors.InternalError);
var err = c.errors.InternalError('message');
err.should.be.instanceOf(Error);
err.should.be.instanceOf(c.errors.InternalError);
err.code.should.equal(500);
err.message.should.equal('message');
c.next();
}));
it('should raise 503 widget error', test(function(c) {
should.exist(c.errors.WidgetError);
var err = c.errors.WidgetError('message');
err.should.be.instanceOf(Error);
err.should.be.instanceOf(c.errors.WidgetError);
err.code.should.equal(503);
err.message.should.equal('message');
c.next();
}));
});
describe('formatNumber', function() {
it('should format number', test(function(c) {
should.exist(c.formatNumber);
c.formatNumber(1234).should.equal('1.2k');
c.formatNumber(123456).should.equal('123.5k');
c.formatNumber(1234567).should.equal('1.2m');
c.formatNumber(1234567890).should.equal('1.2b');
c.formatNumber(543).should.equal(543);
c.next();
}));
});
describe('fromNow', function() {
it('should return time from now in words', test(function(c) {
should.exist(c.fromNow);
var day = 86400000;
c.fromNow(Date.now() - day).should.equal('a day ago');
c.fromNow(Date.now() - day * 2).should.equal('2 days ago');
c.next();
}));
});
describe('getUrl', function() {
it('should return url to media', test(function(c) {
should.exist(c.getUrl);
c.getUrl('str').should.equal('str');
c.getUrl({url: 'test'}).should.equal('test');
c.next();
}));
});
});
function test(fn) {
return function (done) {
TestController.test = fn;
app.compound.controllerBridge.callControllerAction('test', 'action', {pagePath: ''}, {}, done);
};
}
function TestController(){}
TestController.prototype.action = function(c) {
TestController.test.call(this, c);
};