@arpinum/ddd
Version:
DDD framework CQRS and ES friendly
108 lines (88 loc) • 3.36 kB
JavaScript
'use strict';
const should = require('chai').should();
const {Message} = require('../tools');
const {EntityNotFoundError, TechnicalError} = require('../errors');
const {MemoryEventStore} = require('../test');
const {RepositoryContract} = require('../types');
const Cat = require('./test/cat');
const CatRepository = require('./test/catRepository');
const Repository = require('./repository');
describe('The repository', () => {
let eventStore;
let repository;
beforeEach(() => {
eventStore = new MemoryEventStore();
repository = new CatRepository({eventStore});
});
context('after creation', () => {
it('should match RepositoryContract', () => {
let repository = new Repository({
eventStore,
AggregateRootType: Cat
});
RepositoryContract.is(repository).should.be.true;
});
});
context('while getting by id', () => {
it('should apply all aggregate root\'s events', () => {
eventStore.events.push(
new Message({id: '1', type: 'CatCreated', aggregate: {type: 'Cat', id: '42'}, payload: {age: 1}}),
new Message({id: '2', type: 'StuffOccurred', aggregate: {type: 'Dog', id: 'dog_id'}, payload: {name: 'Wulfy'}}),
new Message({id: '3', type: 'CatNamed', aggregate: {type: 'Cat', id: '42'}, payload: {name: 'Garfield'}}),
new Message({id: '4', type: 'CatNamed', aggregate: {type: 'Cat', id: 'another_id'}, payload: {name: 'Isidor'}})
);
let getById = repository.getById('42');
return getById.then(cat => {
cat.should.include({id: '42', name: 'Garfield', age: 1});
});
});
it('should fail if event cannot be applied', () => {
eventStore.events.push(
new Message({id: '1', type: 'FailingEvent', aggregate: {type: 'Cat', id: '42'}})
);
let getById = repository.getById('42');
return getById.then(
() => Promise.resolve(new Error('Should fail')),
rejection => {
rejection.should.be.instanceOf(TechnicalError);
rejection.message.should.equal('Event cannot be applied');
});
});
it('should fail if root is missing', () => {
let getById = repository.getById('missing_id');
return getById.then(
() => Promise.resolve(new Error('Should fail')),
rejection => {
rejection.should.be.instanceOf(EntityNotFoundError);
rejection.message.should.equal('No entity for {"id":"missing_id"}');
});
});
it('won\'t fail if root may be missing', () => {
let getById = repository.getById('missing_id', {maybeMissing: true});
return getById.then(result => {
should.not.exist(result);
});
});
it('should use decorators if provided before applying an event', () => {
eventStore.events.push(
new Message({
id: '1',
type: 'CatCreated',
aggregate: {type: 'Cat', id: '42'},
payload: {age: 1}
}));
eventStore.events.push(
new Message({
id: '2',
type: 'CatBirthdateDefined',
aggregate: {type: 'Cat', id: '42'},
payload: {birthDate: '2010-01-01'}
})
);
let getById = repository.getById('42');
return getById.then(cat => {
cat.birthDate.should.deep.equal(new Date('2010-01-01'));
});
});
});
});