eventric
Version:
Build JavaScript applications with Behaviour-driven Domain Design. Based on DDD, BDD, CQRS and EventSourcing.
88 lines (83 loc) • 3.29 kB
JavaScript
describe('ProcessManager', function() {
return describe('given we created a context and added a ProcessManager', function() {
var exampleContext, handleDomainEventProcessManagerStub, initializeProcessManagerStub;
initializeProcessManagerStub = null;
handleDomainEventProcessManagerStub = null;
exampleContext = null;
beforeEach(function() {
var ExampleAggregateRoot;
initializeProcessManagerStub = sandbox.stub();
handleDomainEventProcessManagerStub = sandbox.stub();
eventric.addProcessManager('ExampleProcess', {
initializeWhen: {
Example: ['ExampleCreated']
},
"class": function() {
return {
initialize: function(domainEvent) {
initializeProcessManagerStub();
return exampleContext.command('ChangeExample', {
id: domainEvent.aggregate.id
});
},
fromExample_handleExampleChanged: function() {
handleDomainEventProcessManagerStub();
return this.$endProcess();
}
};
}
});
exampleContext = eventric.context('Example');
exampleContext.defineDomainEvents({
ExampleCreated: function() {},
ExampleChanged: function() {}
});
ExampleAggregateRoot = (function() {
function ExampleAggregateRoot() {}
ExampleAggregateRoot.prototype.create = function(callback) {
this.$emitDomainEvent('ExampleCreated');
return callback();
};
ExampleAggregateRoot.prototype.doSomething = function() {
return this.$emitDomainEvent('ExampleChanged');
};
return ExampleAggregateRoot;
})();
exampleContext.addAggregate('Example', ExampleAggregateRoot);
exampleContext.addCommandHandler('CreateExample', function(params, callback) {
return this.$repository('Example').create().then((function(_this) {
return function(exampleId) {
return _this.$repository('Example').save(exampleId);
};
})(this)).then((function(_this) {
return function() {
return callback();
};
})(this));
});
exampleContext.addCommandHandler('ChangeExample', function(params, callback) {
return this.$repository('Example').findById(params.id).then((function(_this) {
return function(example) {
example.doSomething();
return _this.$repository('Example').save(params.id);
};
})(this)).then((function(_this) {
return function() {
return callback();
};
})(this));
});
return exampleContext.initialize();
});
return describe('when a DomainEvent gets emitted the ProcessManager defined as initializeWhen', function() {
return it('then it should execute and end the process', function(done) {
exampleContext.subscribeToDomainEvent('ExampleChanged', function(domainEvent) {
expect(initializeProcessManagerStub).to.have.been.called;
expect(handleDomainEventProcessManagerStub).to.have.been.called;
return done();
});
return exampleContext.command('CreateExample');
});
});
});
});