Browse files

complete refactoring, only replay and documentation are missing

  • Loading branch information...
1 parent 7c5ffdc commit 693908afa060631879a515c454b0030e4e292202 @adrai committed Jan 8, 2014
View
4 index.js
@@ -8,6 +8,6 @@ if (typeof module.exports !== 'undefined') {
index.VERSION = '0.0.1';
-index.contextEventDenormalizer = require('./lib/contextEventDenormalizer');
-index.eventDenormalizerBase = require('./lib/bases/eventDenormalizerBase');
+index.eventDenormalizer = require('./lib/eventDenormalizer');
+index.viewBuilderBase = require('./lib/bases/viewBuilderBase');
index.eventExtenderBase = require('./lib/bases/eventExtenderBase');
View
126 lib/bases/eventDenormalizerBase.js
@@ -1,126 +0,0 @@
-var _ = require('lodash'),
- eventEmitter = require('../eventEmitter');
-
-var EventDenormalizer = {};
-EventDenormalizer.prototype = {
-
- configure: function(fn) {
- fn.call(this);
- return this;
- },
-
- use: function(module) {
- if (!module) return;
-
- if (module.commit) {
- this.repository = module;
- }
- },
-
- setOptions: function(options) {
- this.options = options;
- },
-
- create: function(evt, aux, callback) {
- return this.defaultAction(evt, aux, 'create', callback);
- },
-
- update: function(evt, aux, callback) {
- return this.defaultAction(evt, aux, 'update', callback);
- },
-
- 'delete': function(evt, aux, callback) {
- return this.defaultAction(evt, aux, 'delete', callback);
- },
-
- defaultAction: function(evt, aux, action, callback) {
-
- var self = this;
- aux.repository.get(evt.payload.id, function(err, vm) {
- // If the view model has just been created (i.e. it has not been
- // saved yet), and it shall be deleted, simply discard it and
- // return.
- if((vm.actionOnCommit === 'create') && (action === 'delete')) {
- return callback(null);
- }
-
- if(action !== 'delete') {
- _.extend(vm, evt.payload);
- } else {
- vm.destroy();
- }
-
- aux.repository.commit(vm, callback);
- });
-
- },
-
- _getAux: function() {
- var self = this;
-
- this._aux = this._aux || {
- repository: self.repository,
- };
-
- return this._aux;
- },
-
- handle: function(evt) {
-
- var self = this;
-
- // Map events to function names:
- // - For the event handler matching the current event, its name is returned
- // - For all other event handlers, undefined is returned
- var fnNames = _.map(this.events, function(item) {
- if (_.isString(item) && item === evt.event) {
- return item;
- } else if (item[evt.event]) {
- return item[evt.event];
- }
- }
- );
-
- // Reduce function names to function name:
- // - Replace all undefineds by an empty string
- // - Keep all non-undefined values
- //
- // NOTE: This will fail if multiple event handlers match the current event,
- // but this is not allowed anyway, so it can only happen on error.
- var fnName = _.reduce(fnNames, function(memo, item) {
- return memo + (item || '');
- }, '');
-
- if(this[fnName]) {
- // Call the event handler found by map-reduce.
- this[fnName](evt, this._getAux(), function(err) {
- if (err && err.name === 'ConcurrencyError') {
- setTimeout(function() {
- self.handle(evt);
- }, 100);
- return;
- }
- if (err) {
- throw err;
- }
- eventEmitter.emit('denormalized:' + evt.event, evt);
- });
- } else {
- throw new Error('missing handle function');
- }
-
- }
-
-};
-
-module.exports = {
-
- extend: function(obj) {
- var newObj = _.extend(_.clone(EventDenormalizer.prototype), obj);
-
- newObj.options = {};
-
- return newObj;
- }
-
-};
View
135 lib/bases/viewBuilderBase.js
@@ -0,0 +1,135 @@
+var _ = require('lodash'),
+ eventEmitter = require('../eventEmitter');
+
+function dive(obj, key) {
+ var keys = key.split('.');
+ var x = 0;
+ var value = obj;
+ while (keys[x]) {
+ value = value && value[keys[x]];
+ x++;
+ }
+ return value;
+}
+
+var ViewBuilder = {};
+ViewBuilder.prototype = {
+
+ configure: function(fn) {
+ fn.call(this);
+ return this;
+ },
+
+ use: function(module) {
+ if (!module) return;
+
+ if (module.commit) {
+ this.repository = module;
+ }
+ },
+
+ loadViewModel: function(id, callback) {
+ this.repository.get(id, callback);
+ },
+
+ saveViewModel: function(vm, callback) {
+ this.repository.commit(vm, callback);
+ },
+
+ setOptions: function(options) {
+ this.options = options;
+ },
+
+ 'create': function(evt, vm) {
+ this.defaultAction(evt, vm, 'create');
+ },
+
+ 'update': function(evt, vm) {
+ this.defaultAction(evt, vm, 'update');
+ },
+
+ 'delete': function(evt, vm) {
+ this.defaultAction(evt, vm, 'delete');
+ },
+
+ defaultAction: function(evt, vm, action) {
+ if(action !== 'delete') {
+ _.extend(vm, evt.payload);
+ } else {
+ vm.destroy();
+ }
+ },
+
+ handle: function(evt) {
+
+ var self = this;
+
+ // Map events to function names:
+ // - For the event handler matching the current event, its name is returned
+ // - For all other event handlers, undefined is returned
+ var fnNames = _.map(this.events, function(item) {
+ if (_.isString(item) && item === evt.event) {
+ return item;
+ } else if (item[evt.event]) {
+ return item[evt.event];
+ }
+ }
+ );
+
+ // Reduce function names to function name:
+ // - Replace all undefineds by an empty string
+ // - Keep all non-undefined values
+ //
+ // NOTE: This will fail if multiple event handlers match the current event,
+ // but this is not allowed anyway, so it can only happen on error.
+ var fnName = _.reduce(fnNames, function(memo, item) {
+ return memo + (item || '');
+ }, '');
+
+ if(this[fnName]) {
+ var vmIdPath = 'payload.id';
+ if (this.viewModelIds && this.viewModelIds[evt.event]) {
+ vmIdPath = this.viewModelIds[evt.event];
+ }
+ var vmId = dive(evt, vmIdPath);
+
+ this.loadViewModel(vmId, function(err, vm) {
+ if (err) {
+ throw err;
+ }
+
+ // Call the event handler found by map-reduce.
+ self[fnName](evt, vm);
+
+ self.saveViewModel(vm, function(err) {
+ if (err && err.name === 'ConcurrencyError') {
+ setTimeout(function() {
+ self.handle(evt);
+ }, 100);
+ return;
+ }
+ if (err) {
+ throw err;
+ }
+ eventEmitter.emit('denormalized:' + evt.event, evt);
+ });
+ });
+ } else {
+ throw new Error('missing handle function');
+ }
+
+ }
+
+};
+
+module.exports = {
+
+ extend: function(obj) {
+ var newObj = _.extend(_.clone(ViewBuilder.prototype), obj);
+
+ newObj.options = {};
+
+ return newObj;
+ }
+
+};
View
14 lib/contextEventDenormalizer.js → lib/eventDenormalizer.js
@@ -1,4 +1,4 @@
-var eventDenormalizerLoader = require('./loaders/eventDenormalizerLoader')
+var viewBuilderLoader = require('./loaders/viewBuilderLoader')
, eventExtenderLoader = require('./loaders/eventExtenderLoader')
, eventDispatcher = require('./eventDispatcher')
, eventEmitter = require('./eventEmitter')
@@ -11,9 +11,9 @@ var eventDenormalizerLoader = require('./loaders/eventDenormalizerLoader')
, repository = require('viewmodel').write.create()
, revisionGuardStore = require('./revisionGuardStore')
, revisionGuard = require('./revisionGuard')
- , ctxEvtDen;
+ , evtDen;
-module.exports = ctxEvtDen = _.extend(new EventEmitter2({
+module.exports = evtDen = _.extend(new EventEmitter2({
wildcard: true,
delimiter: ':',
maxListeners: 1000 // default would be 10!
@@ -37,11 +37,11 @@ module.exports = ctxEvtDen = _.extend(new EventEmitter2({
_.defaults(options, defaults);
eventEmitter.on('extended:*', function(evt) {
- ctxEvtDen.emit('event', evt);
+ evtDen.emit('event', evt);
});
eventEmitter.on('eventMissing', function(id, aggregateRevision, eventRevision, evt) {
- ctxEvtDen.emit('eventMissing', id, aggregateRevision, eventRevision, evt);
+ evtDen.emit('eventMissing', id, aggregateRevision, eventRevision, evt);
});
async.series([
@@ -51,7 +51,7 @@ module.exports = ctxEvtDen = _.extend(new EventEmitter2({
},
function(callback) {
- eventDenormalizerLoader.configure(function() {
+ viewBuilderLoader.configure(function() {
this.use(repository);
});
eventExtenderLoader.configure(function() {
@@ -69,7 +69,7 @@ module.exports = ctxEvtDen = _.extend(new EventEmitter2({
},
function(callback) {
- eventDenormalizerLoader.load(options.denormalizersPath, { ignoreRevision: options.ignoreRevision }, callback);
+ viewBuilderLoader.load(options.viewBuildersPath, { ignoreRevision: options.ignoreRevision }, callback);
},
function(callback) {
View
28 lib/loaders/eventDenormalizerLoader.js → lib/loaders/viewBuilderLoader.js
@@ -3,7 +3,7 @@ var existsSync = require('fs').existsSync || require('path').existsSync
, utils = require('../utils')
, _ = require('lodash');
-var eventDenormalizerLoader = {
+var viewBuilderLoader = {
configure: function(fn) {
fn.call(this);
@@ -25,27 +25,27 @@ var eventDenormalizerLoader = {
options = { ignoreRevision: false };
}
- var eventDenormalizers = [];
+ var viewBuilders = [];
if (!existsSync(p)){
- return callback(null, eventDenormalizers);
+ return callback(null, viewBuilders);
}
utils.path.dive(p, function(err, file) {
- var eventDenormalizer = require(file);
- eventDenormalizer.setOptions(options);
- eventDenormalizers.push(eventDenormalizer);
+ var viewBuilder = require(file);
+ viewBuilder.setOptions(options);
+ viewBuilders.push(viewBuilder);
// add repository
- var repo = eventDenormalizerLoader.repository.extend({
- collectionName: eventDenormalizer.collectionName
+ var repo = viewBuilderLoader.repository.extend({
+ collectionName: viewBuilder.collectionName
});
- eventDenormalizer.configure(function() {
- eventDenormalizer.use(repo);
+ viewBuilder.configure(function() {
+ viewBuilder.use(repo);
});
// event binding
- var evtNames = _.map(eventDenormalizer.events, function(item) {
+ var evtNames = _.map(viewBuilder.events, function(item) {
if (_.isString(item)) {
return item;
} else {
@@ -61,7 +61,7 @@ var eventDenormalizerLoader = {
});
function action(evt) {
- eventDenormalizer.handle(evt);
+ viewBuilder.handle(evt);
}
// bind each denormalizer event
@@ -71,9 +71,9 @@ var eventDenormalizerLoader = {
eventEmitter.register('denormalize:' + evtName);
}
}, function() {
- callback(null, eventDenormalizers);
+ callback(null, viewBuilders);
});
}
};
-module.exports = eventDenormalizerLoader;
+module.exports = viewBuilderLoader;
View
4 package.json
@@ -15,7 +15,7 @@
"lodash": ">= 1.2.1",
"eventemitter2": ">= 0.4.13",
"node-queue": ">= 0.4.0",
- "viewmodel": ">= 0.5.1"
+ "viewmodel": ">= 0.5.2"
},
"devDependencies": {
"sinon": ">= 1.3.4",
@@ -48,6 +48,6 @@
}
],
"scripts" : {
- "test" : "mocha && mocha test/integration/contextEventDenormalizerTest.js"
+ "test" : "mocha && mocha test/integration/eventDenormalizerTest.js"
}
}
View
69 ...tegration/contextEventDenormalizerTest.js → test/integration/eventDenormalizerTest.js
@@ -1,12 +1,12 @@
var expect = require('expect.js')
, async = require('async')
- , contextEventDenormalizer = require('../../lib/contextEventDenormalizer')
+ , eventDenormalizer = require('../../lib/eventDenormalizer')
, repository = require('viewmodel').write
, dummyRepo
, eventEmitter = require('../../lib/eventEmitter')
, dummyEmitter = new (require('events').EventEmitter)();
-var dummyDenormalizer = require('./eventDenormalizers/dummyDenormalizer');
+var dummyViewBuilder = require('./viewBuilders/dummyViewBuilder');
function cleanRepo(done) {
dummyRepo.find(function(err, results) {
@@ -19,23 +19,23 @@ function cleanRepo(done) {
});
}
-describe('ContextEventDenormalizer', function() {
+describe('EventDenormalizer', function() {
before(function(done) {
repository.init(function() {
- contextEventDenormalizer.on('event', function(evt) {
+ eventDenormalizer.on('event', function(evt) {
dummyEmitter.emit('published', evt);
});
- contextEventDenormalizer.initialize({
- denormalizersPath: __dirname + '/eventDenormalizers',
+ eventDenormalizer.initialize({
+ viewBuildersPath: __dirname + '/viewBuilders',
extendersPath: __dirname + '/eventExtenders',
repository: { type: 'inMemory' }
}, function(err) {
dummyRepo = repository.extend({
- collectionName: dummyDenormalizer.collectionName
+ collectionName: dummyViewBuilder.collectionName
});
done();
});
@@ -52,7 +52,7 @@ describe('ContextEventDenormalizer', function() {
var evt = 'foobar';
- contextEventDenormalizer.denormalize(evt, function(err) {
+ eventDenormalizer.denormalize(evt, function(err) {
expect(err).not.to.be.ok();
done();
});
@@ -86,7 +86,7 @@ describe('ContextEventDenormalizer', function() {
evt.head.revision = 1;
- contextEventDenormalizer.denormalize(evt, function(err) {
+ eventDenormalizer.denormalize(evt, function(err) {
expect(err).not.to.be.ok();
done();
});
@@ -114,7 +114,7 @@ describe('ContextEventDenormalizer', function() {
evt.head.revision = 2;
- contextEventDenormalizer.denormalize(evt, function(err) {
+ eventDenormalizer.denormalize(evt, function(err) {
expect(err).not.to.be.ok();
done();
});
@@ -162,7 +162,7 @@ describe('ContextEventDenormalizer', function() {
});
});
- contextEventDenormalizer.denormalize(evt, function(err) {});
+ eventDenormalizer.denormalize(evt, function(err) {});
});
@@ -177,7 +177,7 @@ describe('ContextEventDenormalizer', function() {
});
});
- contextEventDenormalizer.denormalize(evt, function(err) {});
+ eventDenormalizer.denormalize(evt, function(err) {});
});
@@ -196,7 +196,7 @@ describe('ContextEventDenormalizer', function() {
});
});
- contextEventDenormalizer.denormalize(evt, function(err) {});
+ eventDenormalizer.denormalize(evt, function(err) {});
});
@@ -211,7 +211,7 @@ describe('ContextEventDenormalizer', function() {
});
});
- contextEventDenormalizer.denormalize(evt, function(err) {});
+ eventDenormalizer.denormalize(evt, function(err) {});
});
@@ -242,7 +242,6 @@ describe('ContextEventDenormalizer', function() {
beforeEach(function (done) {
dummyRepo.get(evt.payload.id, function(err, vm) {
- vm.revision = 2;
dummyRepo.commit(vm, done);
});
@@ -259,7 +258,7 @@ describe('ContextEventDenormalizer', function() {
});
});
- contextEventDenormalizer.denormalize(evt, function(err) {});
+ eventDenormalizer.denormalize(evt, function(err) {});
});
@@ -274,7 +273,7 @@ describe('ContextEventDenormalizer', function() {
});
});
- contextEventDenormalizer.denormalize(evt, function(err) {});
+ eventDenormalizer.denormalize(evt, function(err) {});
});
@@ -293,7 +292,7 @@ describe('ContextEventDenormalizer', function() {
});
});
- contextEventDenormalizer.denormalize(evt, function(err) {});
+ eventDenormalizer.denormalize(evt, function(err) {});
});
@@ -308,7 +307,7 @@ describe('ContextEventDenormalizer', function() {
});
});
- contextEventDenormalizer.denormalize(evt, function(err) {});
+ eventDenormalizer.denormalize(evt, function(err) {});
});
@@ -321,16 +320,15 @@ describe('ContextEventDenormalizer', function() {
beforeEach(function (done) {
- denorm = require('./eventDenormalizers/dummyDenormalizer');
- orgFunc = denorm._getAux().defaultRevisionUpdateStrategy;
+ denorm = require('./viewBuilders/dummyViewBuilder');
+ orgFunc = denorm.dummySpezi;
- denorm._getAux().defaultRevisionUpdateStrategy = function(vm, evt) {
+ denorm.dummySpezi = function(evt, vm) {
vm.commit(function() {});
- denorm._getAux().defaultRevisionUpdateStrategy = orgFunc;
+ denorm.dummySpezi = orgFunc;
};
dummyRepo.get(evt.payload.id, function(err, vm) {
- vm.revision = 2;
dummyRepo.commit(vm, done);
});
@@ -339,6 +337,7 @@ describe('ContextEventDenormalizer', function() {
it('it should retry to rehandle the event', function(done) {
evt.head.revision = 5;
+ evt.event = 'dummySpezi';
eventEmitter.once('denormalized:' + evt.event, function(data) {
dummyRepo.get(data.payload.id, function(err, vm) {
@@ -347,7 +346,7 @@ describe('ContextEventDenormalizer', function() {
});
});
- contextEventDenormalizer.denormalize(evt, function(err) {});
+ eventDenormalizer.denormalize(evt, function(err) {});
});
@@ -368,12 +367,11 @@ describe('ContextEventDenormalizer', function() {
revision: 6
},
payload: {
- id: '23'
+ id: '237654323567'
}
};
dummyRepo.get('9876', function(err, vm) {
- vm.revision = 2;
dummyRepo.commit(vm, function(err) {
dummyRepo.get('9876', function(err, vm) {
viewModel = vm;
@@ -388,7 +386,7 @@ describe('ContextEventDenormalizer', function() {
it('it should neither insert nor update a record within the view model database', function(done) {
- evt.head.revision = 6;
+ evt.head.revision = 1;
eventEmitter.once('denormalized:' + evt.event, function(data) {
dummyRepo.find(function(err, results) {
@@ -399,7 +397,7 @@ describe('ContextEventDenormalizer', function() {
});
});
- contextEventDenormalizer.denormalize(evt, function(err) {});
+ eventDenormalizer.denormalize(evt, function(err) {});
});
@@ -418,7 +416,7 @@ describe('ContextEventDenormalizer', function() {
});
});
- contextEventDenormalizer.denormalize(evt, function(err) {});
+ eventDenormalizer.denormalize(evt, function(err) {});
});
@@ -443,7 +441,6 @@ describe('ContextEventDenormalizer', function() {
};
dummyRepo.get(evt.payload.id, function(err, vm) {
- vm.revision = 2;
dummyRepo.commit(vm, done);
});
@@ -479,7 +476,7 @@ describe('ContextEventDenormalizer', function() {
}
};
- contextEventDenormalizer.denormalize(secondEvt, function(err) {});
+ eventDenormalizer.denormalize(secondEvt, function(err) {});
});
@@ -508,7 +505,7 @@ describe('ContextEventDenormalizer', function() {
check();
});
- contextEventDenormalizer.denormalize(firstEvt, function(err) {});
+ eventDenormalizer.denormalize(firstEvt, function(err) {});
});
@@ -535,7 +532,7 @@ describe('ContextEventDenormalizer', function() {
it('it should notify it to be able to make a replay', function(done) {
- contextEventDenormalizer.denormalize(evtTimeout, function(err) {});
+ eventDenormalizer.denormalize(evtTimeout, function(err) {});
eventEmitter.once('eventMissing', function(id, aggregateRevision, eventRevision, evt) {
expect(evt).to.eql(evtTimeout);
@@ -562,7 +559,7 @@ describe('ContextEventDenormalizer', function() {
expect(true).to.be(false);
});
- contextEventDenormalizer.denormalize(evt, function(err) {});
+ eventDenormalizer.denormalize(evt, function(err) {});
setTimeout(function() {
done();
@@ -598,7 +595,7 @@ describe('ContextEventDenormalizer', function() {
eventEmitter.once('denormalized:' + evt.event, function(data) {
done();
});
- contextEventDenormalizer.denormalize(evt, function(err) {});
+ eventDenormalizer.denormalize(evt, function(err) {});
});
View
14 test/integration/eventDenormalizers/dummyDenormalizer.js
@@ -1,14 +0,0 @@
-var eventDenormalizerBase = require('../../../index').eventDenormalizerBase;
-
-var dummyDenormalizer = eventDenormalizerBase.extend({
-
- events: ['dummied', {'dummyCreated': 'create'}, {'dummyChanged': 'update'}, {'dummyDeleted': 'delete'}],
- collectionName: 'dummies',
-
- dummied: function(evt, aux, callback) {
- callback(null);
- }
-
-});
-
-module.exports = dummyDenormalizer;
View
18 test/integration/viewBuilders/dummyViewBuilder.js
@@ -0,0 +1,18 @@
+var viewBuilderBase = require('../../../index').viewBuilderBase;
+
+var dummyViewBuilder = viewBuilderBase.extend({
+
+ events: ['dummied', {'dummyCreated': 'create'}, {'dummyChanged': 'update'}, {'dummyDeleted': 'delete'}, 'dummySpezi'],
+ viewModelIds: { 'dummyCreated': 'payload.id' },
+ collectionName: 'dummies',
+
+ dummied: function(evt, vm) {
+ },
+
+ dummySpezi: function(evt, vm) {
+ vm.otherValue = 'value';
+ }
+
+});
+
+module.exports = dummyViewBuilder;
View
99 test/eventDenormalizerBaseTest.js → test/viewBuilderBaseTest.js
@@ -4,16 +4,16 @@ var expect = require('expect.js')
, repository = require('viewmodel').write
, dummyRepo
, eventEmitter = require('../lib/eventEmitter')
- , eventDenormalizerBase = require('../lib/bases/eventDenormalizerBase')
+ , viewBuilderBase = require('../lib/bases/viewBuilderBase')
, Queue = require('../lib/orderQueue');
-var dummyDenormalizer = eventDenormalizerBase.extend({
+var dummyViewBuilder = viewBuilderBase.extend({
- events: ['dummied', {'dummyCreated': 'create'}, {'dummyChanged': 'update'}, {'dummyDeleted': 'delete'}],
+ events: ['dummied', {'dummyCreated': 'create'}, {'dummyChanged': 'update'}, {'dummyDeleted': 'delete'}, {'dummySpecialized': 'update'}],
+ viewModelIds: { 'dummySpecialized': 'payload.special.id' },
collectionName: 'dummies',
- dummied: function(evt, repository, callback) {
- callback(null);
+ dummied: function(evt, vm) {
}
});
@@ -29,31 +29,30 @@ function cleanRepo(done) {
});
}
-describe('EventDenormalizerBase', function() {
+describe('ViewBuilderBase', function() {
before(function(done) {
repository.init(function(err) {
dummyRepo = repository.extend({
- collectionName: dummyDenormalizer.collectionName
+ collectionName: dummyViewBuilder.collectionName
});
- dummyDenormalizer.configure(function() {
- dummyDenormalizer.use(dummyRepo);
+ dummyViewBuilder.configure(function() {
+ dummyViewBuilder.use(dummyRepo);
});
done();
});
});
- describe('used by a dummy eventDenormalizer', function() {
+ describe('used by a dummy viewBuilder', function() {
describe('calling handle with an event', function() {
afterEach(function(done) {
cleanRepo(done);
- dummyDenormalizer.queue = new Queue({ queueTimeout: 30 });
});
describe('that is mapped to', function() {
@@ -84,7 +83,7 @@ describe('EventDenormalizerBase', function() {
eventEmitter.once('denormalized:' + evt.event, function(data) {
done();
});
- dummyDenormalizer.handle(evt);
+ dummyViewBuilder.handle(evt);
});
@@ -96,28 +95,24 @@ describe('EventDenormalizerBase', function() {
dummyRepo.commit.restore();
done();
});
- dummyDenormalizer.handle(evt);
+ dummyViewBuilder.handle(evt);
});
});
- describe('with change action', function() {
+ describe('with update action', function() {
var evt;
beforeEach(function(done) {
dummyRepo.get('23', function(err, vm) {
- vm._revision = 1;
vm.foo = 'bar';
dummyRepo.commit(vm, function(err) {
evt = {
id: '82517',
event: 'dummyChanged',
- head: {
- revision: vm._revision
- },
payload: {
id: vm.id
}
@@ -133,7 +128,7 @@ describe('EventDenormalizerBase', function() {
eventEmitter.once('denormalized:' + evt.event, function(data) {
done();
});
- dummyDenormalizer.handle(evt);
+ dummyViewBuilder.handle(evt);
});
@@ -145,7 +140,7 @@ describe('EventDenormalizerBase', function() {
dummyRepo.commit.restore();
done();
});
- dummyDenormalizer.handle(evt);
+ dummyViewBuilder.handle(evt);
});
@@ -158,15 +153,11 @@ describe('EventDenormalizerBase', function() {
beforeEach(function(done) {
dummyRepo.get('23', function(err, vm) {
- vm._revision = 1;
vm.foo = 'bar';
dummyRepo.commit(vm, function(err) {
evt = {
id: '82517',
event: 'dummyDeleted',
- head: {
- revision: vm._revision
- },
payload: {
id: vm.id
}
@@ -182,7 +173,7 @@ describe('EventDenormalizerBase', function() {
eventEmitter.once('denormalized:' + evt.event, function(data) {
done();
});
- dummyDenormalizer.handle(evt);
+ dummyViewBuilder.handle(evt);
});
@@ -194,30 +185,27 @@ describe('EventDenormalizerBase', function() {
dummyRepo.commit.restore();
done();
});
- dummyDenormalizer.handle(evt);
+ dummyViewBuilder.handle(evt);
});
});
- describe('having a change action with an event revision', function() {
+ describe('having a different viewModelId', function() {
var evt;
beforeEach(function(done) {
dummyRepo.get('23', function(err, vm) {
- vm._revision = 1;
vm.foo = 'bar';
dummyRepo.commit(vm, function(err) {
evt = {
id: '82517',
- event: 'dummyChanged',
- head: {
- revision: vm._revision
- },
+ event: 'dummySpecialized',
payload: {
- id: vm.id
+ special: { id: vm.id },
+ extra: 'data'
}
};
done();
@@ -226,42 +214,15 @@ describe('EventDenormalizerBase', function() {
});
- describe('less than the expected revision', function() {
-
- it('it should raise a denormalized event', function(done) {
- evt.head.revision--;
-
- eventEmitter.once('denormalized:' + evt.event, function(data) {
- done();
- });
- dummyDenormalizer.handle(evt);
-
- });
-
- });
-
- describe('equal to the expected revision', function() {
-
- it('it should raise a denormalized event', function(done) {
-
- eventEmitter.once('denormalized:' + evt.event, function(data) {
- done();
- });
- dummyDenormalizer.handle(evt);
-
- });
-
- it('it should call the commit function on the repository', function(done) {
+ it('it should have found the right viewmodel', function(done) {
- var spy = sinon.spy(dummyRepo, 'commit');
- eventEmitter.once('denormalized:' + evt.event, function(data) {
- expect(spy.calledOnce).to.be.ok();
- dummyRepo.commit.restore();
+ eventEmitter.once('denormalized:' + evt.event, function(data) {
+ dummyRepo.get('23', function(err, vm) {
+ expect(vm.extra).to.eql('data');
done();
});
- dummyDenormalizer.handle(evt);
-
});
+ dummyViewBuilder.handle(evt);
});
@@ -290,19 +251,19 @@ describe('EventDenormalizerBase', function() {
eventEmitter.once('denormalized:' + evt.event, function(data) {
done();
});
- dummyDenormalizer.handle(evt);
+ dummyViewBuilder.handle(evt);
});
it('it should call the concrete function', function(done) {
- var spy = sinon.spy(dummyDenormalizer, 'dummied');
+ var spy = sinon.spy(dummyViewBuilder, 'dummied');
eventEmitter.once('denormalized:' + evt.event, function(data) {
expect(spy.calledOnce).to.be.ok();
- dummyDenormalizer.dummied.restore();
+ dummyViewBuilder.dummied.restore();
done();
});
- dummyDenormalizer.handle(evt);
+ dummyViewBuilder.handle(evt);
});

0 comments on commit 693908a

Please sign in to comment.