routing/tests/index-spec.ts

86 lines
2.8 KiB
TypeScript
Raw Normal View History

import chai from 'chai';
import sinon from 'ts-sinon';
import sinonChai from 'sinon-chai';
2021-05-19 12:55:10 +02:00
chai.use(sinonChai);
2021-05-19 12:55:10 +02:00
const expect = chai.expect;
import { ImplementableApi, Router } from '../src/index';
// const well_build_routing_file: Router.GlobalConfig = require('./rsrc/wellBuildedRoutingFile.json');
const events: Router.EventsType[] = [
{
name: 'newEntries',
type: 'emit',
},
{
name: 'addListener',
type: 'received',
},
];
type AllowedEvents = 'newEntries' | 'addListener'; // need to match with the EventsType's names
class FakeApi extends ImplementableApi<AllowedEvents> {
constructor(conf: ImplementableApi.Config) {
super(conf);
}
receivedMessage(msg: ImplementableApi.Message<AllowedEvents>) {
// don't do anything
}
}
describe.only('testing the routing part', function () {
describe('testing the injection', function () {
it('will test the dependency injection', function () {
// given
const myRouter = new Router<AllowedEvents>(events);
const myObj = new FakeApi({ name: 'myFakeApi' });
// first expect
expect(myRouter.api_array).to.be.empty;
expect(myRouter.events).to.be.empty;
// when
myRouter.injectDependency(myObj);
// expect
expect(myRouter.api_array).to.have.keys(['myFakeApi']);
expect(myRouter.api_array['myFakeApi']).to.be.instanceOf(FakeApi);
expect(myRouter.events).to.be.empty;
});
});
describe('testing the data transmission', function () {
it('it will emit a upload request message', function () {});
it('it will emit a new listener request', function () {});
});
describe('testing the data reception', function () {
it.only('it will received a new entries notification', function () {
//given
const myMessageWhoWouldBeSend: ImplementableApi.Message<AllowedEvents> =
{
rawContent: 'My content',
type: 'logging',
idListener: 5,
};
const myStub = sinon.stub(FakeApi.prototype, 'receivedMessage');
const myRouter = new Router([]);
const myObj = new FakeApi({ name: 'myFakeApi' });
myRouter.injectDependency(myObj);
// when
myRouter.receivedMessage(myMessageWhoWouldBeSend);
// expect
expect(myStub.firstCall).to.have.been.calledWith(
myMessageWhoWouldBeSend
);
expect(myStub.secondCall).to.have.been.calledWith({
rawContent:
'The dependency `myFakeApi` was well injected into the router',
type: 'logging',
});
});
});
});