Skip to content

Instantly share code, notes, and snippets.

@zoepage
Created June 10, 2015 11:54
Show Gist options
  • Save zoepage/8f71bf9d35b2cbfed929 to your computer and use it in GitHub Desktop.
Save zoepage/8f71bf9d35b2cbfed929 to your computer and use it in GitHub Desktop.
test for error eventEmmiter
'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
}
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)
})
})
@zoepage
Copy link
Author

zoepage commented Jun 18, 2015

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