UNPKG

eventric-testing

Version:
114 lines (99 loc) 3.7 kB
var CommandQueryFactory, fakePromise, projectionFactory, stubFactory; stubFactory = require('./stub_factory'); fakePromise = require('./fake_promise'); projectionFactory = require('./projection_factory'); CommandQueryFactory = (function() { function CommandQueryFactory() {} CommandQueryFactory.prototype.wiredCommandHandler = function(commandHandler) { return this._wireHandler(commandHandler); }; CommandQueryFactory.prototype.wiredQueryHandler = function(queryHandler) { return this._wireHandler(queryHandler); }; CommandQueryFactory.prototype._wireHandler = function(handler) { var di, key; di = { $adapter: stubFactory.stub(), $aggregate: { create: stubFactory.stub(), load: stubFactory.stub() }, $domainService: stubFactory.stub(), $query: stubFactory.stub(), $projectionStore: stubFactory.stub(), $emitDomainEvent: stubFactory.stub() }; stubFactory.configureReturnValue(di.$aggregate.create, this.aggregateStub()); stubFactory.configureReturnValue(di.$aggregate.load, this.aggregateStub()); stubFactory.configureReturnValue(di.$projectionStore, projectionFactory.mongoDbStoreStub()); handler = handler.bind(di); for (key in di) { handler[key] = di[key]; } return handler; }; CommandQueryFactory.prototype.aggregateStub = function() { var saveStub; saveStub = stubFactory.stub(); stubFactory.configureReturnValue(saveStub, fakePromise.resolve()); return { $save: saveStub }; }; CommandQueryFactory.prototype.waitForQueryToReturnResult = function(context, queryName, params, timeout) { if (timeout == null) { timeout = 5000; } return this.waitForResult(function() { return context.query(queryName, params); }, timeout)["catch"](function(error) { var ref; if ((error != null ? (ref = error.message) != null ? ref.indexOf('waitForResult') : void 0 : void 0) > -1) { throw new Error("waitForQueryToReturnResult timed out for query '" + queryName + "' on context '" + context.name + "'\nwith params " + (JSON.stringify(params))); } else { throw error; } }); }; CommandQueryFactory.prototype.waitForCommandToResolve = function(context, commandName, params, timeout) { if (timeout == null) { timeout = 5000; } return this.waitForResult(function() { return context.command(commandName, params).then(function(result) { return result || true; })["catch"](function() { return void 0; }); }, timeout)["catch"](function() { throw new Error("waitForCommandToResolve timed out for command '" + commandName + "' on context '" + context.name + "'\nwith params " + (JSON.stringify(params))); }); }; CommandQueryFactory.prototype.waitForResult = function(promiseFactory, timeout) { if (timeout == null) { timeout = 5000; } return new Promise(function(resolve, reject) { var pollPromise, startTime; startTime = new Date(); pollPromise = function() { return promiseFactory().then(function(result) { var timeoutExceeded; if (result != null) { resolve(result); return; } timeoutExceeded = (new Date() - startTime) >= timeout; if (!timeoutExceeded) { setTimeout(pollPromise, 15); return; } return reject(new Error("waitForResult timed out for '" + (promiseFactory.toString()) + "'")); })["catch"](reject); }; return pollPromise(); }); }; return CommandQueryFactory; })(); module.exports = new CommandQueryFactory;