Skip to content

Commit

Permalink
wip: add live replication test
Browse files Browse the repository at this point in the history
  • Loading branch information
hulkoba committed Jul 6, 2021
1 parent d368a9f commit 991d04f
Showing 1 changed file with 85 additions and 1 deletion.
86 changes: 85 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -966,8 +966,21 @@ function tests(dbName, dbType) {
var db;
var remote;

beforeEach(function () {
// Utility function - complex test incoming
var defer = function () {
var resolve, reject;
var promise = new Promise(function () {
resolve = arguments[0];
reject = arguments[1];
});
return {
resolve: resolve,
reject: reject,
promise: promise
};
};

beforeEach(function () {
db = new Pouch(dbName);
remote = new Pouch(dbName + '_other');
});
Expand Down Expand Up @@ -997,6 +1010,77 @@ function tests(dbName, dbType) {
});
});

it('test live replication transforms incoming', function () {
// We need to wait until the "incoming" change has happened.
// We'll use a re-assignable deferred object so we can wait multiple times
var d;

db.transform({
incoming: function (doc) {
doc.foo = 'baz';
// Resolve anything that's waiting for the incoming handler to finish
setTimeout(function () {
d.resolve();
}, 100);
return doc;
},
outgoing: function (doc) {
doc.boo = 'lalala';
return doc;
}
});

// Ongoing live replication
var syncHandler = remote.sync(db, {
live: true
});

var setupPromise = new Promise(function (resolve) {
// Wait for a second to give replication a chance
setTimeout(resolve, 500);
});


// The actual test
var result = setupPromise.then(function () {
// Reset the incoming listener
d = defer();
return remote.put({_id: 'doc'});
}).then(function () {
// Wait for the incoming listener - everything's updated
return d.promise;
}).then(function () {
return db.get('doc');
}).then(function (doc) {
doc.foo.should.equal('baz');
// Get the remote document so we can update it
return remote.get('doc');
}).then(function (doc) {
// Reset the incoming listener
d = defer();
// Add a new property to the object
return remote.put({_id: 'doc', _rev: doc._rev, moo: 'bar' });
}).then(function () {
// Wait for the incoming listener - everything's updated
return d.promise;
}).then(function () {
return db.get('doc');
}).then(function (doc) {
// And here - we fail
doc.should.have.property('moo');
doc.moo.should.equal('bar');
});

syncHandler.on('change', function (change) {
change.change.docs[0].foo.should.equal('baz');
});

result.then(function () {
syncHandler.cancel();
});
return result;
});

it('test replication transforms outgoing', function () {
db.transform({
outgoing: function (doc) {
Expand Down

0 comments on commit 991d04f

Please sign in to comment.