This repository was archived by the owner on Apr 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathbefore.test.js
More file actions
482 lines (398 loc) · 11.6 KB
/
before.test.js
File metadata and controls
482 lines (398 loc) · 11.6 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/* eslint-disable handle-callback-err */
import assert from 'assert';
import feathers from 'feathers';
import hooks from '../src/hooks';
describe('.before hooks', () => {
describe('function([hook])', () => {
it('returning a non-hook object throws error', () => {
const app = feathers().configure(hooks()).use('/dummy', {
get (id) {
return Promise.resolve({ id });
},
before: {
get () {
return {};
}
}
});
const service = app.service('dummy');
return service.get(10).catch(e => {
assert.equal(e.message, 'before hook for \'get\' method returned invalid hook object');
});
});
it('hooks in chain can be replaced', () => {
const app = feathers().configure(hooks()).use('/dummy', {
get (id) {
return Promise.resolve({
id, description: `You have to do ${id}`
});
}
});
const service = app.service('dummy').before({
get: [
function (hook) {
return Object.assign({}, hook, {
modified: true
});
},
function (hook) {
assert.ok(hook.modified);
}
]
});
return service.get('laundry');
});
it('.before hooks can return a promise', () => {
const app = feathers().configure(hooks()).use('/dummy', {
get (id, params) {
assert.ok(params.ran, 'Ran through promise hook');
return Promise.resolve({
id: id,
description: `You have to do ${id}`
});
},
remove () {
assert.ok(false, 'Should never get here');
}
});
const service = app.service('dummy').before({
get: function (hook) {
return new Promise(resolve => {
hook.params.ran = true;
resolve();
});
},
remove () {
return new Promise((resolve, reject) => {
reject(new Error('This did not work'));
});
}
});
return service.get('dishes').then(() => service.remove(10))
.catch(error => {
assert.equal(error.message, 'This did not work');
});
});
it('.before hooks do not need to return anything', () => {
const app = feathers().configure(hooks()).use('/dummy', {
get (id, params) {
assert.ok(params.ran, 'Ran through promise hook');
return Promise.resolve({
id: id,
description: `You have to do ${id}`
});
},
remove () {
assert.ok(false, 'Should never get here');
}
});
const service = app.service('dummy').before({
get: function (hook) {
hook.params.ran = true;
},
remove () {
throw new Error('This did not work');
}
});
return service.get('dishes').then(() => service.remove(10))
.catch(error => {
assert.equal(error.message, 'This did not work');
});
});
it('.before hooks can set hook.result which will skip service method', () => {
const app = feathers().configure(hooks()).use('/dummy', {
get (id) {
assert.ok(false, 'This should never run');
return Promise.resolve({ id });
}
});
const service = app.service('dummy');
service.before({
get (hook) {
hook.result = {
id: hook.id,
message: 'Set from hook'
};
}
});
return service.get(10, {}).then(data => {
assert.deepEqual(data, {
id: 10,
message: 'Set from hook'
});
});
});
});
describe('function(hook, next)', () => {
it('gets mixed into a service and modifies data', done => {
const dummyService = {
before: {
create (hook, next) {
assert.equal(hook.type, 'before');
hook.data.modified = 'data';
Object.assign(hook.params, {
modified: 'params'
});
next(null, hook);
}
},
create (data, params, callback) {
assert.deepEqual(data, {
some: 'thing',
modified: 'data'
}, 'Data modified');
assert.deepEqual(params, {
modified: 'params'
}, 'Params modified');
callback(null, data);
}
};
const app = feathers().configure(hooks()).use('/dummy', dummyService);
const service = app.service('dummy');
service.create({ some: 'thing' }, {}, (error, data) => {
assert.ok(!error, 'No error');
assert.deepEqual(data, {
some: 'thing',
modified: 'data'
}, 'Data got modified');
done();
});
});
it('contains the app object at hook.app', done => {
const someServiceConfig = {
before: {
create (hook, next) {
hook.data.appPresent = typeof hook.app === 'function';
assert.equal(hook.data.appPresent, true);
next(null, hook);
}
},
create (data, params, callback) {
callback(null, data);
}
};
const app = feathers().configure(hooks()).use('/some-service', someServiceConfig);
const someService = app.service('some-service');
someService.create({ some: 'thing' }, {}, (error, data) => {
assert.deepEqual(data, {
some: 'thing',
appPresent: true
}, 'App object was present');
done();
});
});
it('passes errors', done => {
const dummyService = {
before: {
update (hook, next) {
next(new Error('You are not allowed to update'));
}
},
update () {
assert.ok(false, 'Never should be called');
}
};
const app = feathers().configure(hooks()).use('/dummy', dummyService);
const service = app.service('dummy');
service.update(1, {}, {}, error => {
assert.ok(error, 'Got an error');
assert.equal(error.message, 'You are not allowed to update', 'Got error message');
done();
});
});
it('calling back with no arguments uses the old ones', done => {
const dummyService = {
before: {
remove (hook, next) {
next();
}
},
remove (id, params, callback) {
assert.equal(id, 1, 'Got id');
assert.deepEqual(params, { my: 'param' });
callback();
}
};
const app = feathers().configure(hooks()).use('/dummy', dummyService);
const service = app.service('dummy');
service.remove(1, { my: 'param' }, done);
});
it('adds .before() and chains multiple hooks for the same method', done => {
const dummyService = {
create (data, params, callback) {
assert.deepEqual(data, {
some: 'thing',
modified: 'second data'
}, 'Data modified');
assert.deepEqual(params, {
modified: 'params'
}, 'Params modified');
callback(null, data);
}
};
const app = feathers().configure(hooks()).use('/dummy', dummyService);
const service = app.service('dummy');
service.before({
create (hook, next) {
hook.params.modified = 'params';
next();
}
});
service.before({
create (hook, next) {
hook.data.modified = 'second data';
next();
}
});
service.create({ some: 'thing' }, {}, error => {
assert.ok(!error, 'No error');
done();
});
});
it('chains multiple before hooks using array syntax', done => {
const dummyService = {
create (data, params, callback) {
assert.deepEqual(data, {
some: 'thing',
modified: 'second data'
}, 'Data modified');
assert.deepEqual(params, {
modified: 'params'
}, 'Params modified');
callback(null, data);
}
};
const app = feathers().configure(hooks()).use('/dummy', dummyService);
const service = app.service('dummy');
service.before({
create: [
function (hook, next) {
hook.params.modified = 'params';
next();
},
function (hook, next) {
hook.data.modified = 'second data';
next();
}
]
});
service.create({ some: 'thing' }, {}, error => {
assert.ok(!error, 'No error');
done();
});
});
it('.before hooks run in the correct order (#13)', done => {
const app = feathers().configure(hooks()).use('/dummy', {
get (id, params, callback) {
assert.deepEqual(params.items, ['first', 'second', 'third']);
callback(null, {
id: id,
items: []
});
}
});
const service = app.service('dummy');
service.before({
get (hook, next) {
hook.params.items = ['first'];
next();
}
});
service.before({
get: [
function (hook, next) {
hook.params.items.push('second');
next();
},
function (hook, next) {
hook.params.items.push('third');
next();
}
]
});
service.get(10, {}, done);
});
it('before all hooks (#11)', done => {
const app = feathers().configure(hooks()).use('/dummy', {
before: {
all (hook, next) {
hook.params.beforeAllObject = true;
next();
}
},
get (id, params, callback) {
assert.ok(params.beforeAllObject);
assert.ok(params.beforeAllMethodArray);
callback(null, {
id: id,
items: []
});
},
find (params, callback) {
assert.ok(params.beforeAllObject);
assert.ok(params.beforeAllMethodArray);
callback(null, []);
}
});
const service = app.service('dummy');
service.before([
function (hook, next) {
hook.params.beforeAllMethodArray = true;
next();
}
]);
service.find({}, () => {
service.get(1, {}, done);
});
});
it('before hooks have service as context and keep it in service method (#17)', done => {
const app = feathers().configure(hooks()).use('/dummy', {
number: 42,
get (id, params, callback) {
callback(null, {
id: id,
number: this.number,
test: params.test
});
}
});
const service = app.service('dummy');
service.before({
get (hook, next) {
hook.params.test = this.number + 2;
next();
}
});
service.get(10, {}, (error, data) => {
assert.deepEqual(data, {
id: 10,
number: 42,
test: 44
});
done();
});
});
it('calling next() multiple times does not do anything', () => {
const app = feathers().configure(hooks()).use('/dummy', {
get (id, params, callback) {
callback(null, { id });
}
});
const service = app.service('dummy');
service.before({
get: [
function (hook, next) {
next();
},
function (hook, next) {
next();
next();
}
]
});
return service.get(10).then(data => {
assert.deepEqual(data, { id: 10 });
});
});
});
});