Created
June 10, 2015 11:54
-
-
Save zoepage/8f71bf9d35b2cbfed929 to your computer and use it in GitHub Desktop.
test for error eventEmmiter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
var toId = require('./utils/to-id') | |
module.exports = push | |
/** | |
* pushes one or multiple objects from local to remote database | |
* | |
* @param {String,Object} docsOrIds object, ID of object or array of objects/ids (all optional) | |
* @return {Promise} | |
*/ | |
function push (state, options, docsOrIds) { | |
var pushedObjects = [] | |
var Promise = this.constructor.utils.Promise | |
var errors = this.constructor.Errors | |
var defer = {} | |
defer.promise = new Promise(function (resolveCallback, rejectCallback) { | |
defer.resolve = function resolve () { | |
resolveCallback.apply(null, arguments) | |
} | |
defer.reject = function reject () { | |
rejectCallback.apply(null, arguments) | |
} | |
}) | |
if (Array.isArray(docsOrIds)) { | |
docsOrIds = docsOrIds.map(toId) | |
} else { | |
docsOrIds = docsOrIds && [toId(docsOrIds)] | |
} | |
if (docsOrIds && docsOrIds.filter(Boolean).length !== docsOrIds.length) { | |
return Promise.reject(errors.NOT_AN_OBJECT) | |
} | |
var replication = this.replicate.to(options.remote, { | |
create_target: true, | |
doc_ids: docsOrIds, | |
include_docs: true, | |
retry: false | |
}) | |
replication.on('complete', function () { | |
defer.resolve(pushedObjects) | |
}) | |
replication.on('error', defer.reject) | |
replication.on('change', function (change) { | |
pushedObjects = pushedObjects.concat(change.docs) | |
for (var i = 0; i < change.docs.length; i++) { | |
state.emitter.emit('push', change.docs[i]) | |
} | |
}) | |
return defer.promise | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
test.only('api.push({}) error event', function (t) { | |
t.plan(1) | |
var db = dbFactory('hoodieDB8') | |
var PouchDB = db.constructor | |
var remoteName = PouchDB.utils.uuid(10) | |
var api = db.hoodieSync({remote: remoteName}) | |
var obj1 = {_id: 'test1', foo1: 'bar1'} | |
var obj2 = {_id: 'test2', foo1: 'bar2'} | |
db.bulkDocs([obj1, obj2]) | |
db.destroy() | |
.then(function () { | |
return api.push() | |
}) | |
.catch(function (error) { | |
t.pass(error) | |
}) | |
}) |
Wait, so what you're saying is that you've seen bulkDocs()
throw an error in production somewhere, and now you're trying to reproduce it? Or are you trying to capture an error event from the replicate()
?
As for merge conflicts: that wouldn't throw an error, but yes, if the remote DB dies (or the user goes offline), that would cause an error to be thrown, at least during non-live
, non-retry
replication.
Here's how we simulate the user going offline in one of our tests. Let me know if that helps.
Or are you trying to capture an error event from the replicate()?
THIS! :)
The test looks like exactly what we need. Thanks! I'll give it a try. :) 👍
Just FYI... This is my solution:
test('api.push() error', function (t) {
t.plan(1)
var db = dbFactory('hoodieDB8')
var PouchDB = db.constructor
var remoteName = PouchDB.utils.uuid(10)
var api = db.hoodieSync({remote: remoteName})
var obj1 = {_id: 'test1', foo1: 'bar1'}
var first = true
var data = {
get _id () {
if (first) {
first = false
return 'test1'
} else {
return {}
}
},
foo: 'bar'
}
db.bulkDocs([data, obj1])
.then(function () {
return api.push(data)
})
.then(
function (resolve) {
t.pass('The error event was not fired!')
},
function (reject) {
t.pass('The error event was fired!')
}
)
})
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think, the most common scenarios would be: