-
-
Notifications
You must be signed in to change notification settings - Fork 796
Expand file tree
/
Copy pathapplication.test.ts
More file actions
547 lines (442 loc) · 13 KB
/
application.test.ts
File metadata and controls
547 lines (442 loc) · 13 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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
/* eslint-disable @typescript-eslint/ban-ts-comment, @typescript-eslint/no-empty-function */
import assert from 'assert'
import { feathers, Feathers, getServiceOptions, Id, version } from '../src'
describe('Feathers application', () => {
it('initializes', () => {
const app = feathers()
assert.ok(app instanceof Feathers)
})
it('sets the version on main and app instance', () => {
const app = feathers()
assert.ok(version > '5.0.0')
assert.ok(app.version > '5.0.0')
})
it('is an event emitter', (done) => {
const app = feathers()
const original = { hello: 'world' }
app.on('test', (data: any) => {
assert.deepStrictEqual(original, data)
done()
})
app.emit('test', original)
})
it('uses .defaultService if available', async () => {
const app = feathers()
assert.throws(() => app.service('/todos/'), {
message: "Can not find service 'todos'"
})
app.defaultService = function (location: string) {
assert.strictEqual(location, 'todos')
return {
async get(id: string) {
return {
id,
description: `You have to do ${id}!`
}
}
}
}
const data = await app.service('/todos/').get('dishes')
assert.deepStrictEqual(data, {
id: 'dishes',
description: 'You have to do dishes!'
})
})
it('additionally passes `app` as .configure parameter (#558)', (done) => {
feathers().configure(function (app) {
assert.strictEqual(this, app)
done()
})
})
describe('Services', () => {
it('calling .use with invalid path throws', () => {
const app = feathers()
assert.throws(() => app.use(null, {}), {
message: "'null' is not a valid service path."
})
// @ts-ignore
assert.throws(() => app.use({}, {}), {
message: "'[object Object]' is not a valid service path."
})
})
it('calling .use with a non service object throws', () => {
const app = feathers()
// @ts-ignore
assert.throws(() => app.use('/bla', function () {}), {
message: 'Invalid service object passed for path `bla`'
})
})
it('registers and wraps a new service and can unregister (#2035)', async () => {
const dummyService = {
async setup(this: any, _app: any, path: string) {
this.path = path
},
async teardown(this: any, _app: any, path: string) {
this.path = path
},
async create(data: any) {
return data
}
}
const app = feathers<{ dummy: typeof dummyService }>().use('dummy', dummyService)
const wrappedService = app.service('dummy')
assert.strictEqual(
Object.getPrototypeOf(wrappedService),
dummyService,
'Object points to original service prototype'
)
const data = await wrappedService.create({
message: 'Test message'
})
assert.strictEqual(data.message, 'Test message')
await app.unuse('dummy')
assert.strictEqual(Object.keys(app.services).length, 0)
assert.throws(() => app.service('dummy'), {
message: "Can not find service 'dummy'"
})
})
it('can not register custom methods on a protected methods', async () => {
const dummyService = {
async create(data: any) {
return data
},
async removeListener(data: any) {
return data
},
async setup() {},
async teardown() {}
}
assert.throws(
() =>
feathers().use('/dummy', dummyService, {
methods: ['create', 'removeListener']
}),
{
message: "'removeListener' on service 'dummy' is not allowed as a custom method name"
}
)
assert.throws(
() =>
feathers().use('/dummy', dummyService, {
methods: ['create', 'setup']
}),
{
message: "'setup' on service 'dummy' is not allowed as a custom method name"
}
)
assert.throws(
() =>
feathers().use('/dummy', dummyService, {
methods: ['create', 'teardown']
}),
{
message: "'teardown' on service 'dummy' is not allowed as a custom method name"
}
)
})
it('can register service with no external methods', async () => {
const dummyService = {
async create(data: any) {
return data
}
}
feathers().use('dummy', dummyService, {
methods: []
})
})
it('can use a root level service', async () => {
const app = feathers().use('/', {
async get(id: string) {
return { id }
}
})
const result = await app.service('/').get('test')
assert.deepStrictEqual(result, { id: 'test' })
})
it('services can be re-used (#566)', (done) => {
const app1 = feathers()
const app2 = feathers()
app2.use('/dummy', {
async create(data: any) {
return data
}
})
const dummy = app2.service('dummy')
dummy.hooks({
before: {
create: [
(hook) => {
hook.data.fromHook = true
}
]
}
})
dummy.on('created', (data: any) => {
assert.deepStrictEqual(data, {
message: 'Hi',
fromHook: true
})
done()
})
app1.use('/testing', app2.service('dummy'))
app1.service('testing').create({ message: 'Hi' })
})
it('async hooks run before regular hooks', async () => {
const app = feathers()
app.use('/dummy', {
async create(data: any) {
return data
}
})
const dummy = app.service('dummy')
dummy.hooks({
before: {
create(ctx) {
ctx.data.order.push('before')
}
}
})
dummy.hooks([
async (ctx: any, next: any) => {
ctx.data.order = ['async']
await next()
}
])
const result = await dummy.create({
message: 'hi'
})
assert.deepStrictEqual(result, {
message: 'hi',
order: ['async', 'before']
})
})
it('services conserve Symbols', () => {
const TEST = Symbol('test')
const dummyService = {
[TEST]: true,
async setup(this: any, _app: any, path: string) {
this.path = path
},
async create(data: any) {
return data
}
}
const app = feathers().use('/dummy', dummyService)
const wrappedService = app.service('dummy')
assert.ok((wrappedService as any)[TEST])
})
it('methods conserve Symbols', () => {
const TEST = Symbol('test')
const dummyService = {
async setup(this: any, _app: any, path: string) {
this.path = path
},
async create(data: any) {
return data
}
}
;(dummyService.create as any)[TEST] = true
const app = feathers().use('/dummy', dummyService)
const wrappedService = app.service('dummy')
assert.ok((wrappedService.create as any)[TEST])
})
})
describe('Express app options compatibility', function () {
describe('.set()', () => {
it('should set a value', () => {
const app = feathers()
app.set('foo', 'bar')
assert.strictEqual(app.get('foo'), 'bar')
})
it('should return the app', () => {
const app = feathers()
assert.strictEqual(app.set('foo', 'bar'), app)
})
it('should return the app when undefined', () => {
const app = feathers()
assert.strictEqual(app.set('foo', undefined), app)
})
})
describe('.get()', () => {
it('should return undefined when unset', () => {
const app = feathers()
assert.strictEqual(app.get('foo'), undefined)
})
it('should otherwise return the value', () => {
const app = feathers()
app.set('foo', 'bar')
assert.strictEqual(app.get('foo'), 'bar')
})
})
})
describe('.setup and .teardown', () => {
it('app.setup and app.teardown calls .setup and .teardown on all services', async () => {
const app = feathers()
let setupCount = 0
let teardownCount = 0
app.use('/dummy', {
async setup(appRef: any, path: any) {
setupCount++
assert.strictEqual(appRef, app)
assert.strictEqual(path, 'dummy')
},
async teardown(appRef: any, path: any) {
teardownCount++
assert.strictEqual(appRef, app)
assert.strictEqual(path, 'dummy')
}
})
app.use('/simple', {
get(id: string) {
return Promise.resolve({ id })
}
})
app.use('/dummy2', {
async setup(appRef: any, path: any) {
setupCount++
assert.strictEqual(appRef, app)
assert.strictEqual(path, 'dummy2')
},
async teardown(appRef: any, path: any) {
teardownCount++
assert.strictEqual(appRef, app)
assert.strictEqual(path, 'dummy2')
}
})
await app.setup()
assert.ok((app as any)._isSetup)
assert.strictEqual(setupCount, 2)
await app.teardown()
assert.ok(!(app as any)._isSetup)
assert.strictEqual(teardownCount, 2)
})
it('registering app.setup but while still pending will be set up', (done) => {
const app = feathers()
app.setup()
app.use('/dummy', {
async setup(appRef: any, path: any) {
assert.ok((app as any)._isSetup)
assert.strictEqual(appRef, app)
assert.strictEqual(path, 'dummy')
done()
}
})
})
})
describe('.teardown', () => {
it('app.teardown calls .teardown on all services', async () => {
const app = feathers()
let teardownCount = 0
app.use('/dummy', {
async setup() {},
async teardown(appRef: any, path: any) {
teardownCount++
assert.strictEqual(appRef, app)
assert.strictEqual(path, 'dummy')
}
})
app.use('/simple', {
get(id: string) {
return Promise.resolve({ id })
}
})
app.use('/dummy2', {
async setup() {},
async teardown(appRef: any, path: any) {
teardownCount++
assert.strictEqual(appRef, app)
assert.strictEqual(path, 'dummy2')
}
})
await app.setup()
await app.teardown()
assert.equal((app as any)._isSetup, false)
assert.strictEqual(teardownCount, 2)
})
})
describe('mixins', () => {
class Dummy {
dummy = true
async get(id: Id) {
return { id }
}
}
it('are getting called with a service and default options', () => {
const app = feathers()
let mixinRan = false
app.mixins.push(function (service: any, location: any, options: any) {
assert.ok(service.dummy)
assert.strictEqual(location, 'dummy')
assert.deepStrictEqual(options, getServiceOptions(service))
mixinRan = true
})
app.use('/dummy', new Dummy())
assert.ok(mixinRan)
app.setup()
})
it('are getting called with a service and service options', () => {
const app = feathers()
const opts = { events: ['bla'] }
let mixinRan = false
app.mixins.push(function (service: any, location: any, options: any) {
assert.ok(service.dummy)
assert.strictEqual(location, 'dummy')
assert.deepStrictEqual(options, getServiceOptions(service))
mixinRan = true
})
app.use('/dummy', new Dummy(), opts)
assert.ok(mixinRan)
app.setup()
})
})
describe('sub apps', () => {
it('re-registers sub-app services with prefix', (done) => {
const app = feathers()
const subApp = feathers()
subApp
.use('/service1', {
async get(id: string) {
return {
id,
name: 'service1'
}
}
})
.use('/service2', {
async get(id: string) {
return {
id,
name: 'service2'
}
},
async create(data: any) {
return data
}
})
app.use('/api/', subApp)
app.service('/api/service2').once('created', (data: any) => {
assert.deepStrictEqual(data, {
message: 'This is a test'
})
subApp.service('service2').once('created', (data: any) => {
assert.deepStrictEqual(data, {
message: 'This is another test'
})
done()
})
app.service('api/service2').create({
message: 'This is another test'
})
})
;(async () => {
let data = await app.service('/api/service1').get(10)
assert.strictEqual(data.name, 'service1')
data = await app.service('/api/service2').get(1)
assert.strictEqual(data.name, 'service2')
await subApp.service('service2').create({
message: 'This is a test'
})
})()
})
})
})