Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 8 - incoming transform :: document updates are not live replicated #9

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -709,4 +709,106 @@ function tests(dbName, dbType) {
});
});
});

describe(dbType + ': live replication tests', function () {

var db;
var remote;

// 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');
});

afterEach(function () {
return Pouch.destroy(dbName).then(function () {
return Pouch.destroy(dbName + '_other');
});
});

it('test replication transforms incoming, and subsequent UPDATE works', function () {

// Ongoing live replication
var replicateHandle = Pouch.replicate(remote, db, {
live: true
});

// 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;
// Our transformation function
db.transform({
incoming: function (doc) {
console.log("INCOMING!");
doc.foo = 'baz';
// Resolve anything that's waiting for the incoming handler to finish
setTimeout(function () {
d.resolve();
}, 100);
return doc;
}
});

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');
});


result.then(function () {
replicateHandle.cancel();
}, function () {
replicateHandle.cancel();
});


return result;

});

});
}