eventric
Version:
Build JavaScript applications with Behaviour-driven Domain Design. Based on DDD, BDD, CQRS and EventSourcing.
163 lines (157 loc) • 5.87 kB
JavaScript
describe('Context', function() {
var Context, RepositoryMock, eventBusStub;
Context = null;
RepositoryMock = (function() {
function RepositoryMock() {}
return RepositoryMock;
})();
eventBusStub = null;
beforeEach(function() {
eventBusStub = {
subscribeToDomainEvent: sandbox.stub(),
subscribeToDomainEventWithAggregateId: sandbox.stub(),
publishDomainEvent: sandbox.stub()
};
mockery.registerMock('eventric/src/event_bus', sandbox.stub().returns(eventBusStub));
mockery.registerMock('eventric/src/context/repository', RepositoryMock);
return Context = require('eventric/src/context');
});
describe('#initialize', function() {
it('should instantiate all registered projections', function() {
var ProjectionStub, context;
context = new Context('exampleContext');
ProjectionStub = (function() {
function ProjectionStub() {}
ProjectionStub.prototype.stores = ['inmemory'];
return ProjectionStub;
})();
context.addProjection('SomeProjection', ProjectionStub);
return context.initialize().then(function() {
return context.getProjectionStore('inmemory', 'SomeProjection', function(err, projectionStore) {
return expect(projectionStore).to.deep.equal({});
});
});
});
return it('should instantiate and initialize all registered adapters', function(done) {
var AdapterFactory, context;
context = new Context('exampleContext');
AdapterFactory = sandbox.stub();
context.addAdapter('Adapter', AdapterFactory);
return context.initialize((function(_this) {
return function() {
expect(AdapterFactory).to.have.been.calledWithNew;
return done();
};
})(this));
});
});
describe('#command', function() {
describe('given the context was not initialized yet', function() {
return it('should callback with an error', function(done) {
var someContext;
someContext = new Context('exampleContext');
return someContext.command('getSomething')["catch"](function(error) {
expect(error).to.be.an.instanceOf(Error);
return done();
});
});
});
describe('given the command has no registered handler', function() {
return it('should call the callback with a command not found error', function(done) {
var someContext;
someContext = new Context('exampleContext');
return someContext.initialize((function(_this) {
return function() {
var callback;
callback = sinon.spy();
return someContext.command('doSomething', {
id: 42,
foo: 'bar'
})["catch"](function(error) {
expect(error).to.be.an["instanceof"](Error);
return done();
});
};
})(this));
});
});
return describe('has a registered handler', function() {
return it('should execute the command handler', function(done) {
var commandStub, someContext;
commandStub = sandbox.stub();
someContext = new Context('exampleContext');
return someContext.initialize((function(_this) {
return function() {
var params;
someContext.addCommandHandler('doSomething', commandStub);
params = {
foo: 'bar'
};
someContext.command('doSomething', params);
expect(commandStub.calledWith(params, sinon.match.func)).to.be["true"];
return done();
};
})(this));
});
});
});
describe('#query', function() {
var someContext;
someContext = null;
beforeEach(function() {
return someContext = new Context('exampleContext');
});
describe('given the context was not initialized yet', function() {
return it('should callback with an error', function(done) {
return someContext.query('getSomething')["catch"](function(error) {
expect(error).to.be.an.instanceOf(Error);
return done();
});
});
});
describe('given the query has no matching queryhandler', function() {
return it('should callback with an error', function(done) {
return someContext.initialize((function(_this) {
return function() {
return someContext.query('getSomething')["catch"](function(error) {
expect(error).to.be.an.instanceOf(Error);
return done();
});
};
})(this));
});
});
return describe('given the query has a matching queryhandler', function() {
return it('should call the queryhandler function', function(done) {
var queryStub;
queryStub = sandbox.stub().yields(null, 'result');
someContext.addQueryHandler('getSomething', queryStub);
return someContext.initialize((function(_this) {
return function() {
return someContext.query('getSomething').then(function(result) {
expect(result).to.equal('result');
expect(queryStub).to.have.been.calledWith;
return done();
});
};
})(this));
});
});
});
return describe('#emitDomainEvent', function() {
beforeEach(function(done) {
var someContext;
someContext = new Context('ExampleContext');
someContext.defineDomainEvent('WhatSoEver', function() {});
return someContext.initialize((function(_this) {
return function() {
someContext.emitDomainEvent('WhatSoEver');
return done();
};
})(this));
});
return it('should publish the DomainEvent on the EventBus', function() {
return expect(eventBusStub.publishDomainEvent).to.have.been.called;
});
});
});