Skip to content
This repository was archived by the owner on Dec 27, 2024. It is now read-only.

Commit 8d6930f

Browse files
committed
1 parent 3603533 commit 8d6930f

File tree

6 files changed

+249
-51
lines changed

6 files changed

+249
-51
lines changed

core/@model/errors.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ export class SapiDbForModelNotFound extends Error {
1313

1414
super(`getDb for model '${modelName}' failed because database name '${dbName}'`
1515
+ ` was not defined. Make sure your @Model({dbConfig.db}) is set to a valid database name from your`
16-
+ ` environment config's dbConnections:[{name:'...'}] setting.`);
16+
+ ` environment config's dbConnections:[{name:'...'}] setting. Also, make sure you've started SakuraApi, or`
17+
+ ` that you have manually started db connections (e.g. with SakuraMongoDbConnection.connectAll())`);
1718
}
1819
}
1920

core/sakura-api.spec.ts

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
// tslint:disable:no-shadowed-variable
2+
3+
import {NextFunction, Request, Response} from 'express';
24
import {MongoClient} from 'mongodb';
35
import * as request from 'supertest';
46
import {SakuraApiConfig} from '../boot/sakura-api-config';
57
import {testMongoDbUrl, testSapi, testUrl} from '../spec/helpers/sakuraapi';
8+
import {Json} from './@model/json';
9+
import {Model} from './@model/model';
10+
import {SakuraApiModel} from './@model/sakura-api-model';
611
import {Routable, Route} from './@routable/';
12+
import {SakuraApi, SakuraApiPluginResult} from './sakura-api';
713
import Spy = jasmine.Spy;
814

915
describe('core/SakuraApi', () => {
@@ -317,5 +323,137 @@ describe('core/SakuraApi', () => {
317323
.catch(done.fail);
318324
});
319325
});
326+
327+
describe('plugins', () => {
328+
329+
function testPluginA(sapi: SakuraApi, options: any): SakuraApiPluginResult {
330+
331+
function testHandler(req: Request, res: Response, next: NextFunction) {
332+
if (!res.locals.handlerTrace) {
333+
res.locals.handlerTrace = '';
334+
}
335+
res.locals.handlerTrace += res.locals.handlerTrace = options.value;
336+
next();
337+
}
338+
339+
@Model()
340+
class TestModelPlugin extends SakuraApiModel {
341+
@Json()
342+
modelValue = 'found';
343+
}
344+
345+
@Routable()
346+
class TestRoutablePlugin {
347+
@Route({
348+
method: 'get',
349+
path: 'TestRoutablePlugin'
350+
})
351+
getHandler(req: Request, res: Response, next: NextFunction) {
352+
const result = new TestModelPlugin();
353+
354+
res
355+
.status(200)
356+
.json(result.toJson());
357+
}
358+
}
359+
360+
return {
361+
middlewareHandlers: [testHandler],
362+
models: [TestModelPlugin],
363+
routables: [TestRoutablePlugin]
364+
};
365+
}
366+
367+
function testPluginB(sapi: SakuraApi, options: any): SakuraApiPluginResult {
368+
function testHandler(req: Request, res: Response, next: NextFunction) {
369+
if (!res.locals.handlerTrace) {
370+
res.locals.handlerTrace = '';
371+
}
372+
res.locals.handlerTrace += res.locals.handlerTrace = options.value;
373+
next();
374+
}
375+
376+
return {
377+
middlewareHandlers: [testHandler]
378+
};
379+
}
380+
381+
@Routable()
382+
class RoutableTestStub {
383+
response = 'testRouterGet worked';
384+
385+
@Route({
386+
method: 'get',
387+
path: 'plugins_test'
388+
})
389+
testRouterGet(req, res) {
390+
res
391+
.status(200)
392+
.json({
393+
testHandlerResult: res.locals.handlerTrace
394+
});
395+
}
396+
}
397+
398+
const sapi = testSapi({
399+
plugins: [
400+
{
401+
options: {
402+
value: 'A'
403+
},
404+
order: 1,
405+
plugin: testPluginA
406+
},
407+
{
408+
options: {
409+
value: 'B'
410+
},
411+
order: 0,
412+
plugin: testPluginB
413+
}
414+
],
415+
routables: [
416+
RoutableTestStub
417+
]
418+
});
419+
420+
beforeEach((done) => {
421+
sapi
422+
.listen({bootMessage: ''})
423+
.then(done)
424+
.catch(done.fail);
425+
});
426+
427+
afterEach((done) => {
428+
sapi
429+
.close()
430+
.then(done)
431+
.catch(done.fail);
432+
});
433+
434+
it('adds plugin handlers in the proper order', (done) => {
435+
request(sapi.app)
436+
.get(testUrl('plugins_test'))
437+
.expect(200)
438+
.then((result) => {
439+
const body = result.body;
440+
expect(body.testHandlerResult).toBe('BA');
441+
})
442+
.then(done)
443+
.catch(done.fail);
444+
});
445+
446+
it('adds plugin models and routables', (done) => {
447+
request(sapi.app)
448+
.get(testUrl('TestRoutablePlugin'))
449+
.expect(200)
450+
.then((result) => {
451+
const body = result.body;
452+
expect(body.modelValue).toBe('found');
453+
})
454+
.then(done)
455+
.catch(done.fail);
456+
});
457+
});
320458
});
321459
// tslint:enable:no-shadowed-variable

0 commit comments

Comments
 (0)