UNPKG

eventric

Version:

Build JavaScript applications with Behaviour-driven Domain Design. Based on DDD, BDD, CQRS and EventSourcing.

214 lines (203 loc) 7.95 kB
describe('Remote Feature', function() { var doSomethingStub, exampleContext; exampleContext = null; doSomethingStub = null; beforeEach(function(done) { var Example; doSomethingStub = sandbox.stub(); exampleContext = eventric.context('Example'); exampleContext.defineDomainEvents({ ExampleCreated: function() {}, ExampleModified: function() {} }); exampleContext.addCommandHandlers({ 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(exampleId) { return callback(null, exampleId); }; })(this))["catch"]((function(_this) { return function(error) { return callback(error); }; })(this)); }, ModifyExample: function(params, callback) { return this.$repository('Example').findById(params.id).then((function(_this) { return function(example) { example.modify(); return _this.$repository('Example').save(params.id); }; })(this)).then((function(_this) { return function(id) { return callback(null, id); }; })(this))["catch"]((function(_this) { return function(error) { return callback(error); }; })(this)); }, DoSomething: function(params, callback) { doSomethingStub(); return callback(null); } }); Example = (function() { function Example() {} Example.prototype.create = function(callback) { this.$emitDomainEvent('ExampleCreated'); return callback(); }; Example.prototype.modify = function() { return this.$emitDomainEvent('ExampleModified'); }; return Example; })(); exampleContext.addAggregate('Example', Example); exampleContext.addQueryHandlers({ getSomething: function(params, callback) { return callback(null, 'something'); } }); return exampleContext.initialize(function() { return done(); }); }); describe('given we created and initialized some example context', function() { it('then it should be able to receive commands over a remote', function() { var exampleRemote; exampleRemote = eventric.remote('Example'); return exampleRemote.command('DoSomething').then(function() { return expect(doSomethingStub).to.have.been.calledOnce; }); }); it('then it should be able to answer queries over a remote', function() { var exampleRemote; exampleRemote = eventric.remote('Example'); return exampleRemote.query('getSomething').then(function(result) { return expect(result).to.equal('something'); }); }); it('then it should be possible to subscribe to domain events and receive them', function(done) { var exampleRemote, numberOfReceivedEvents; exampleRemote = eventric.remote('Example'); numberOfReceivedEvents = 0; exampleRemote.subscribeToDomainEvent('ExampleCreated', function() { numberOfReceivedEvents++; if (numberOfReceivedEvents === 2) { return done(); } }); exampleRemote.command('CreateExample', {}); return exampleRemote.command('CreateExample', {}); }); it('then it should be possible to subscribe to domain events for specific aggregate ids', function(done) { var exampleRemote; exampleRemote = eventric.remote('Example'); return exampleRemote.command('CreateExample').then(function(exampleId) { exampleRemote.subscribeToDomainEventWithAggregateId('ExampleModified', exampleId, function() { return done(); }); return exampleRemote.command('ModifyExample', { id: exampleId }); }); }); it('then it should be possible to unsubscribe from domain events', function(done) { var exampleRemote, firstHandler, subscriberId; exampleRemote = eventric.remote('Example'); firstHandler = sandbox.stub(); subscriberId = exampleRemote.subscribeToDomainEvent('ExampleCreated', firstHandler); exampleRemote.unsubscribeFromDomainEvent(subscriberId); exampleRemote.subscribeToDomainEvent('ExampleCreated', function() { expect(firstHandler).not.to.have.been.called; return done(); }); return exampleRemote.command('CreateExample', {}); }); it('then it should be possible to find all domain events', function() { var exampleRemote; exampleRemote = eventric.remote('Example'); return exampleRemote.command('CreateExample', {}).then(function(id) { return exampleRemote.command('ModifyExample', { id: id }); }).then(function() { return exampleRemote.findAllDomainEvents(); }).then(function(events) { return expect(events.length).to.equal(2); }); }); it('then it should be possible to find domain events by name', function() { var exampleRemote; exampleRemote = eventric.remote('Example'); return exampleRemote.command('CreateExample', {}).then(function() { return exampleRemote.findDomainEventsByName('ExampleCreated'); }).then(function(events) { return expect(events.length).to.equal(1); }); }); it('then it should be possible to find domain events by aggregate id', function() { var exampleRemote; exampleRemote = eventric.remote('Example'); return exampleRemote.command('CreateExample', {}).then(function(id) { return exampleRemote.findDomainEventsByAggregateId(id); }).then(function(events) { return expect(events.length).to.equal(1); }); }); return it('then it should be possible to find domain events by aggregate name', function() { var exampleRemote; exampleRemote = eventric.remote('Example'); return exampleRemote.command('CreateExample', {}).then(function() { return exampleRemote.findDomainEventsByAggregateName('Example'); }).then(function(events) { return expect(events.length).to.equal(1); }); }); }); return describe('given we created and initialized some example context with a custom remote endpoint', function() { var customRemoteBridge; customRemoteBridge = null; beforeEach(function() { var CustomRemoteEndpoint; CustomRemoteEndpoint = (function() { function CustomRemoteEndpoint() { customRemoteBridge = (function(_this) { return function(rpcRequest) { return _this._handleRPCRequest(rpcRequest); }; })(this); } CustomRemoteEndpoint.prototype.setRPCHandler = function(_handleRPCRequest) { this._handleRPCRequest = _handleRPCRequest; }; return CustomRemoteEndpoint; })(); return eventric.addRemoteEndpoint('custom', new CustomRemoteEndpoint); }); return it('then it should be able to receive commands over the custom remote client', function(done) { var CustomRemoteClient, exampleRemote; CustomRemoteClient = (function() { function CustomRemoteClient() {} CustomRemoteClient.prototype.rpc = function(rpcRequest, callback) { customRemoteBridge(rpcRequest); return callback(); }; return CustomRemoteClient; })(); exampleRemote = eventric.remote('Example'); exampleRemote.addClient('custom', new CustomRemoteClient); exampleRemote.set('default client', 'custom'); return exampleRemote.command('DoSomething').then(function() { expect(doSomethingStub).to.have.been.calledOnce; return done(); }); }); }); });