node-eventstore-client/test/subscribeToAll_test.js

44 lines
1.7 KiB
JavaScript
Raw Normal View History

2016-10-15 05:53:23 +00:00
const uuid = require('uuid');
const client = require('../src/client');
const allCredentials = new client.UserCredentials("admin", "changeit");
module.exports = {
'Test Subscribe To All Happy Path': function(test) {
const resolveLinkTos = false;
const numberOfPublishedEvents = 5;
var publishedEvents = [];
for(var i=0;i<numberOfPublishedEvents;i++)
publishedEvents.push(client.createJsonEventData(uuid.v4(), {a: Math.random(), b: uuid.v4()}, null, 'anEvent'));
function testAllPublishedEventsAppeared() {
test.areEqual("receivedEvents.length", receivedEvents.length, numberOfPublishedEvents);
}
function testEventsAppearedInCorrectOrder() {
for (var j = 0; j < numberOfPublishedEvents; j++)
test.ok(receivedEvents[j].originalEvent.eventId === publishedEvents[j].eventId,
"receivedEvents[" + j + "] != publishedEvents[" + j + "]");
}
var receivedEvents = [];
function eventAppeared(subscription, event) {
receivedEvents.push(event);
if (receivedEvents.length === numberOfPublishedEvents) subscription.close();
}
function subscriptionDropped(subscription, reason, error) {
if (error) return test.done(error);
testAllPublishedEventsAppeared();
testEventsAppearedInCorrectOrder();
test.done();
}
var self = this;
this.conn.subscribeToAll(resolveLinkTos, eventAppeared, subscriptionDropped, allCredentials)
.then(function(subscription) {
test.areEqual("subscription.isSubscribedToAll", subscription.isSubscribedToAll, true);
return self.conn.appendToStream(self.testStreamName, client.expectedVersion.emptyStream, publishedEvents);
})
.catch(test.done)
}
};
require('./common/base_test').init(module.exports);