1 | 'use strict';
|
2 |
|
3 | const expect = require('chai').expect;
|
4 | const Controller = require('../lib/Controller');
|
5 |
|
6 | describe('class Controller', function() {
|
7 |
|
8 | let Ctrl;
|
9 | let ctrl;
|
10 |
|
11 | beforeEach(function() {
|
12 | Ctrl = class TestCtrl extends Controller {
|
13 | constructor(name, mountPath) {
|
14 | super(name, mountPath);
|
15 | }
|
16 | };
|
17 |
|
18 | ctrl = new Ctrl;
|
19 | });
|
20 |
|
21 | describe('constructor(name, mountPath)', function() {
|
22 | it('should extract name and mountPath from controller name', function() {
|
23 | expect(ctrl).to.have.property('name', 'test');
|
24 | expect(ctrl).to.have.property('mountPath', 'test');
|
25 |
|
26 | let ctrl1 = new (class TestController extends Controller {
|
27 | constructor() {
|
28 | super();
|
29 | }
|
30 | });
|
31 |
|
32 | expect(ctrl1).to.have.property('name', 'test');
|
33 | expect(ctrl1).to.have.property('mountPath', 'test');
|
34 | });
|
35 |
|
36 | it('should set name and mountPath by constructor', function() {
|
37 | let ctrl = new Ctrl('my', '/my');
|
38 | expect(ctrl).to.have.property('name', 'my');
|
39 | expect(ctrl).to.have.property('mountPath', '/my');
|
40 | });
|
41 |
|
42 | [
|
43 | ['basePath', '/'],
|
44 | ['parent', null],
|
45 | ['__hooksConfig', {}],
|
46 | ['__configs', {}]
|
47 | ].forEach(function(prop) {
|
48 | it(`should have initial properties ${prop[0]} with specific value`, function() {
|
49 | expect(ctrl).to.have.property(prop[0]).to.deep.eq(prop[1]);
|
50 | });
|
51 | });
|
52 |
|
53 | Object.getOwnPropertyNames(Controller.prototype).forEach(function(name) {
|
54 | it(`should raise an error if internal method '${name}' is overwritten`, function() {
|
55 | if (name === 'constructor') return;
|
56 | Ctrl.prototype[name] = function() {};
|
57 | expect(function() {
|
58 | new Ctrl();
|
59 | }).to.throw(Error).and.have.property('message', `Method: \`${name}\` is reserved by baiji.Controller, please rename it`);
|
60 | });
|
61 | });
|
62 | });
|
63 |
|
64 | describe('setName(name)', function() {
|
65 | it('should set correct name', function() {
|
66 | expect(ctrl).to.have.property('name', 'test');
|
67 | ctrl.setName('abc');
|
68 | expect(ctrl).to.have.property('name', 'abc');
|
69 | });
|
70 | });
|
71 |
|
72 | describe('getName()', function() {
|
73 | it('should get correct name', function() {
|
74 | expect(ctrl.getName()).to.eq('test');
|
75 | });
|
76 | });
|
77 |
|
78 | describe('setBasePath(basePath)', function() {
|
79 | it('should set correct basePath', function() {
|
80 | expect(ctrl).to.have.property('basePath', '/');
|
81 | ctrl.setBasePath('/abc');
|
82 | expect(ctrl).to.have.property('basePath', '/abc');
|
83 | });
|
84 | });
|
85 |
|
86 | describe('getBasePath()', function() {
|
87 | it('should get correct basePath', function() {
|
88 | expect(ctrl.getBasePath()).to.eq('/');
|
89 | });
|
90 | });
|
91 |
|
92 | describe('setMountPath(basePath)', function() {
|
93 | it('should set correct mountPath', function() {
|
94 | expect(ctrl).to.have.property('mountPath', 'test');
|
95 | ctrl.setMountPath('/abc');
|
96 | expect(ctrl).to.have.property('mountPath', '/abc');
|
97 | });
|
98 | });
|
99 |
|
100 | describe('getMountPath()', function() {
|
101 | it('should get correct mountPath', function() {
|
102 | expect(ctrl.getMountPath()).to.eq('/test');
|
103 | });
|
104 | });
|
105 |
|
106 | describe('configure(nameOrConfigs, methodConfig)', function() {
|
107 | it('should be able to config via object', function() {
|
108 | ctrl.configure({ abc: 1 });
|
109 | expect(ctrl.__configs).to.have.property('abc', 1);
|
110 | });
|
111 |
|
112 | it('should be able to config via name and object', function() {
|
113 | ctrl.configure('name', { abc: 1 });
|
114 | expect(ctrl.__configs).to.have.property('name').deep.eq({ abc: 1 });
|
115 | });
|
116 | });
|
117 |
|
118 |
|
119 | [
|
120 | 'before',
|
121 | 'after',
|
122 | 'afterError'
|
123 | ].forEach(function(hookName) {
|
124 | describe(`${hookName}Action(nameOrFn, options)`, function() {
|
125 | it('should raise an error if there is no action method can be found', function() {
|
126 | expect(function() {
|
127 | ctrl[`${hookName}Action`]('non_existed_method');
|
128 | }).to.throw(Error).to.have.property('message', 'No method named \'non_existed_method\' defined');
|
129 | });
|
130 |
|
131 | it(`should set ${hookName} actions`, function() {
|
132 | ctrl.filterUser = function() {};
|
133 | ctrl[`${hookName}Action`]('filterUser');
|
134 | expect(ctrl.__hooksConfig).to.have.deep.property(`${hookName}.filterUser`).deep.eq({
|
135 | fn: ctrl.filterUser,
|
136 | options: {
|
137 | except: [],
|
138 | only: ['*']
|
139 | }
|
140 | });
|
141 | });
|
142 |
|
143 | it('should add proper filters by `except` option', function() {
|
144 | ctrl.filterUser = function() {};
|
145 | ctrl[`${hookName}Action`]('filterUser', { except: 'index' });
|
146 |
|
147 | expect(ctrl.__hooksConfig).to.have.deep.property(`${hookName}.filterUser`).deep.eq({
|
148 | fn: ctrl.filterUser,
|
149 | options: {
|
150 | except: ['index'],
|
151 | only: ['*']
|
152 | }
|
153 | });
|
154 |
|
155 | ctrl.__hooksConfig = {};
|
156 |
|
157 | ctrl[`${hookName}Action`]('filterUser', { except: '*' });
|
158 |
|
159 | expect(ctrl.__hooksConfig).to.have.deep.property(`${hookName}.filterUser`).deep.eq({
|
160 | fn: ctrl.filterUser,
|
161 | options: {
|
162 | except: ['*'],
|
163 | only: []
|
164 | }
|
165 | });
|
166 |
|
167 | ctrl.__hooksConfig = {};
|
168 |
|
169 | ctrl[`${hookName}Action`]('filterUser', { except: ['index', 'show'] });
|
170 |
|
171 | expect(ctrl.__hooksConfig).to.have.deep.property(`${hookName}.filterUser`).deep.eq({
|
172 | fn: ctrl.filterUser,
|
173 | options: {
|
174 | except: ['index', 'show'],
|
175 | only: ['*']
|
176 | }
|
177 | });
|
178 | });
|
179 |
|
180 | it('should add proper filters by `only` option', function() {
|
181 | ctrl.filterUser = function() {};
|
182 | ctrl[`${hookName}Action`]('filterUser', { only: 'index' });
|
183 |
|
184 | expect(ctrl.__hooksConfig).to.have.deep.property(`${hookName}.filterUser`).deep.eq({
|
185 | fn: ctrl.filterUser,
|
186 | options: {
|
187 | except: [],
|
188 | only: ['index']
|
189 | }
|
190 | });
|
191 |
|
192 | ctrl.__hooksConfig = {};
|
193 |
|
194 | ctrl[`${hookName}Action`]('filterUser', { only: '*' });
|
195 |
|
196 | expect(ctrl.__hooksConfig).to.have.deep.property(`${hookName}.filterUser`).deep.eq({
|
197 | fn: ctrl.filterUser,
|
198 | options: {
|
199 | except: [],
|
200 | only: ['*']
|
201 | }
|
202 | });
|
203 |
|
204 | ctrl.__hooksConfig = {};
|
205 |
|
206 | ctrl[`${hookName}Action`]('filterUser', { only: ['index', 'show'] });
|
207 |
|
208 | expect(ctrl.__hooksConfig).to.have.deep.property(`${hookName}.filterUser`).deep.eq({
|
209 | fn: ctrl.filterUser,
|
210 | options: {
|
211 | except: [],
|
212 | only: ['index', 'show']
|
213 | }
|
214 | });
|
215 | });
|
216 | });
|
217 |
|
218 | });
|
219 |
|
220 |
|
221 | [
|
222 | 'before',
|
223 | 'after',
|
224 | 'afterError'
|
225 | ].forEach(function(hookName) {
|
226 | let HookName = hookName.replace(/^[a-z]/, function(r) { return r.toUpperCase(); });
|
227 |
|
228 | describe(`skip${HookName}Action(nameOrFn, options)`, function() {
|
229 | beforeEach(function() {
|
230 | ctrl.filterUser = function() {};
|
231 | ctrl[`${hookName}Action`]('filterUser');
|
232 | });
|
233 |
|
234 | it('should raise an error if there is no action method can be found', function() {
|
235 | expect(function() {
|
236 | ctrl[`skip${HookName}Action`]('non_existed_method');
|
237 | }).to.throw(Error).to.have.property('message', 'No method named \'non_existed_method\' defined');
|
238 | });
|
239 |
|
240 | it(`should skip ${hookName} actions`, function() {
|
241 | ctrl[`skip${HookName}Action`]('filterUser');
|
242 | expect(ctrl.__hooksConfig).to.have.deep.property(`${hookName}.filterUser.options`).deep.eq({
|
243 | except: ['*'],
|
244 | only: []
|
245 | });
|
246 | });
|
247 |
|
248 | it('should skip filters by `except` option', function() {
|
249 | ctrl[`skip${HookName}Action`]('filterUser', { except: 'index' });
|
250 |
|
251 | expect(ctrl.__hooksConfig).to.have.deep.property(`${hookName}.filterUser.options`).deep.eq({
|
252 | only: ['index'],
|
253 | except: []
|
254 | });
|
255 |
|
256 | ctrl.__hooksConfig = {};
|
257 | ctrl[`${hookName}Action`]('filterUser');
|
258 | ctrl[`skip${HookName}Action`]('filterUser', { except: '*' });
|
259 | expect(ctrl.__hooksConfig).to.have.deep.property(`${hookName}.filterUser.options`).deep.eq({
|
260 | only: ['*'],
|
261 | except: []
|
262 | });
|
263 |
|
264 | ctrl.__hooksConfig = {};
|
265 | ctrl[`${hookName}Action`]('filterUser');
|
266 | ctrl[`skip${HookName}Action`]('filterUser', { except: ['index', 'show'] });
|
267 | expect(ctrl.__hooksConfig).to.have.deep.property(`${hookName}.filterUser.options`).deep.eq({
|
268 | only: ['index', 'show'],
|
269 | except: []
|
270 | });
|
271 |
|
272 | ctrl.__hooksConfig = {};
|
273 | ctrl[`${hookName}Action`]('filterUser', { only: 'index' });
|
274 | ctrl[`skip${HookName}Action`]('filterUser', { except: ['show'] });
|
275 | expect(ctrl.__hooksConfig).to.have.deep.property(`${hookName}.filterUser.options`).deep.eq({
|
276 | only: ['show'],
|
277 | except: []
|
278 | });
|
279 |
|
280 | ctrl.__hooksConfig = {};
|
281 | ctrl[`${hookName}Action`]('filterUser', { only: 'index' });
|
282 | ctrl[`skip${HookName}Action`]('filterUser', { except: ['index'] });
|
283 | expect(ctrl.__hooksConfig).to.have.deep.property(`${hookName}.filterUser.options`).deep.eq({
|
284 | only: ['index'],
|
285 | except: []
|
286 | });
|
287 | });
|
288 |
|
289 | it('should skip filters by `only` option', function() {
|
290 | ctrl.filterUser = function() {};
|
291 | ctrl[`skip${HookName}Action`]('filterUser', { only: 'index' });
|
292 |
|
293 | expect(ctrl.__hooksConfig).to.have.deep.property(`${hookName}.filterUser.options`).deep.eq({
|
294 | only: ['*'],
|
295 | except: ['index']
|
296 | });
|
297 |
|
298 | ctrl.__hooksConfig = {};
|
299 | ctrl[`${hookName}Action`]('filterUser');
|
300 | ctrl[`skip${HookName}Action`]('filterUser', { only: '*' });
|
301 | expect(ctrl.__hooksConfig).to.have.deep.property(`${hookName}.filterUser.options`).deep.eq({
|
302 | only: [],
|
303 | except: ['*']
|
304 | });
|
305 |
|
306 | ctrl.__hooksConfig = {};
|
307 | ctrl[`${hookName}Action`]('filterUser');
|
308 | ctrl[`skip${HookName}Action`]('filterUser', { only: ['index', 'show'] });
|
309 | expect(ctrl.__hooksConfig).to.have.deep.property(`${hookName}.filterUser.options`).deep.eq({
|
310 | only: ['*'],
|
311 | except: ['index', 'show']
|
312 | });
|
313 |
|
314 | ctrl.__hooksConfig = {};
|
315 | ctrl[`${hookName}Action`]('filterUser', { only: 'index' });
|
316 | ctrl[`skip${HookName}Action`]('filterUser', { only: ['show'] });
|
317 | expect(ctrl.__hooksConfig).to.have.deep.property(`${hookName}.filterUser.options`).deep.eq({
|
318 | only: ['index'],
|
319 | except: ['show']
|
320 | });
|
321 |
|
322 | ctrl.__hooksConfig = {};
|
323 | ctrl[`${hookName}Action`]('filterUser', { only: 'index' });
|
324 | ctrl[`skip${HookName}Action`]('filterUser', { only: ['index'] });
|
325 | expect(ctrl.__hooksConfig).to.have.deep.property(`${hookName}.filterUser.options`).deep.eq({
|
326 | only: [],
|
327 | except: ['index']
|
328 | });
|
329 |
|
330 | ctrl.__hooksConfig = {};
|
331 | ctrl[`${hookName}Action`]('filterUser', { only: 'index', except: 'show' });
|
332 | ctrl[`skip${HookName}Action`]('filterUser', { only: ['index'] });
|
333 | expect(ctrl.__hooksConfig).to.have.deep.property(`${hookName}.filterUser.options`).deep.eq({
|
334 | only: [],
|
335 | except: ['show', 'index']
|
336 | });
|
337 | });
|
338 | });
|
339 | });
|
340 | });
|