acelga-bus
Version:
An extensible typescript message bus with support for middlewares
120 lines • 5.13 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const publisher_1 = require("@src/corebus/publisher");
describe('Publisher', () => {
let pub;
const handler = (item) => Promise.resolve();
beforeEach(() => {
pub = new publisher_1.Publisher(handler);
});
it('it launch an event with the message to publish', (done) => {
pub = new publisher_1.Publisher((event) => {
expect(event).toBe(34);
done();
return Promise.resolve();
});
pub.publish(34);
});
it('executes the middleware chaining and launch the event for publishing', (done) => {
const pubNumber = new publisher_1.Publisher((event) => {
expect(event).toBe(6);
done();
return Promise.resolve();
});
pubNumber.pushMiddleware((item) => Promise.resolve(item / 3));
pubNumber.unshiftMiddleware((item) => Promise.resolve(item - 2));
pubNumber.unshiftMiddleware((item) => Promise.resolve(item / 2));
pubNumber.pushMiddleware((item) => Promise.resolve(item + 1));
pubNumber.publish(34);
});
it('should not execute the handler if a middleware returns void', (done) => {
testAvoidHandlerCall(undefined, done);
});
it('should not execute the handler if a middleware returns \'\'', (done) => {
testAvoidHandlerCall('', done);
});
it('should not execute the handler if a middleware returns 0', (done) => {
testAvoidHandlerCall(0, done);
});
it('should not execute the handler if a middleware returns NaN', (done) => {
testAvoidHandlerCall(NaN, done);
});
it('it does not launch is the middleware chain don\'t finish', (done) => {
const spyHandler = jest.fn((event) => Promise.resolve());
const pubNumber = new publisher_1.Publisher(spyHandler);
pubNumber.pushMiddleware((item) => Promise.resolve(item / 3));
pubNumber.unshiftMiddleware((item) => item - 2);
pubNumber.unshiftMiddleware((item) => Promise.resolve(undefined));
pubNumber.pushMiddleware((item) => Promise.resolve(item + 1));
pubNumber.publish(34);
setTimeout(() => {
expect(spyHandler.mock.calls.length).toBe(0);
done();
}, 0);
});
it('should be cloned with the same handler and chain', (done) => {
const spyHandler = jest.fn((event) => Promise.resolve());
const pub1 = new publisher_1.Publisher(spyHandler);
pub1.pushMiddleware((item) => Promise.resolve(item / 3));
const pub2 = pub1.clone();
pub2.pushMiddleware((item) => Promise.resolve(item + 1));
pub1.publish(9);
pub2.publish(81);
setTimeout(() => {
expect(spyHandler.mock.calls.length).toBe(2);
expect(spyHandler.mock.calls.filter(a => a[0] === 3).length).toBe(1);
expect(spyHandler.mock.calls.filter(a => a[0] === 28).length).toBe(1);
done();
}, 0);
});
it('should be cloned with the same handler and chain', (done) => {
const spyHandler = jest.fn((event) => Promise.resolve());
const publisher = new publisher_1.Publisher(spyHandler);
publisher.pushMiddleware((item) => Promise.resolve(item / 3));
publisher.publish(9);
publisher.cleanMiddlewares();
publisher.publish(9);
setTimeout(() => {
expect(spyHandler.mock.calls.length).toBe(2);
expect(spyHandler.mock.calls.filter(a => a[0] === 3).length).toBe(1);
expect(spyHandler.mock.calls.filter(a => a[0] === 9).length).toBe(1);
done();
}, 0);
});
it('should keep firstAndKeep & lastAndKeep middlewares if provided', (done) => {
const spyHandler = (event) => {
expect(event).toBe(4);
done();
return Promise.resolve();
};
const publisher = new publisher_1.Publisher(spyHandler);
publisher.pushMiddlewareAndKeepLast((item) => Promise.resolve(item / 3));
publisher.pushMiddleware((item) => Promise.resolve(item + 2));
publisher.publish(10);
});
it('should keep firstAndKeep & lastAndKeep middlewares if provided', (done) => {
const spyHandler = (event) => {
expect(event).toBe(5);
done();
return Promise.resolve();
};
const publisher = new publisher_1.Publisher(spyHandler);
publisher.unshiftMiddlewareAndKeepFirst((item) => Promise.resolve(item / 3));
publisher.unshiftMiddleware((item) => Promise.resolve(item + 2));
publisher.publish(9);
});
});
function testAvoidHandlerCall(value, done) {
const spyHandler = jest.fn((event) => {
done.fail();
return Promise.resolve();
});
const publisher = new publisher_1.Publisher(spyHandler);
publisher.pushMiddleware((item) => Promise.resolve());
publisher.publish(value);
setTimeout(() => {
expect(spyHandler.mock.calls.length).toBe(0);
done();
}, 5);
}
//# sourceMappingURL=publisher.spec.js.map