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) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just FYI... This is my solution: