Skip to content

Commit

Permalink
test: Fixing tests after rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
capellini committed Jul 14, 2017
1 parent f245028 commit c381cf8
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 35 deletions.
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ var EventEmitter = require('events').EventEmitter

var assign = require('lodash/assign')

var handleChanges = require('./lib/helpers/handle-changes')
var internals = Store.internals = {}
internals.handleChanges = require('./lib/helpers/handle-changes')

function Store (dbName, options) {
if (!(this instanceof Store)) return new Store(dbName, options)
Expand Down Expand Up @@ -64,7 +65,7 @@ function Store (dbName, options) {

state.api = api

handleChanges(state)
internals.handleChanges(state)

return api
}
Expand Down
7 changes: 4 additions & 3 deletions lib/helpers/add-one.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ var PouchDBErrors = require('pouchdb-errors')
var Promise = require('lie')
var uuid = require('pouchdb-utils').uuid

var addTimestamps = require('../utils/add-timestamps')
var put = require('./db-put')
var internals = addOne.internals = {}
internals.addTimestamps = require('../utils/add-timestamps')
internals.put = require('./db-put')

function addOne (state, doc, prefix) {
if (typeof doc !== 'object') {
Expand All @@ -25,7 +26,7 @@ function addOne (state, doc, prefix) {

delete doc.hoodie

return put(state, addTimestamps(doc))
return internals.put(state, internals.addTimestamps(doc))

.catch(function (error) {
if (error.status === 409) {
Expand Down
6 changes: 4 additions & 2 deletions lib/helpers/db-bulk-docs.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module.exports = bulkDocs

var internals = bulkDocs.internals = {}

// we wrap PouchDB’s .bulkDocs method in order to guarantee that the events get
// emmited before the Promise gets resolved. This is how the rest of Hoodie’s
// APIs behave. Also it avoids confusion that listening to events after adding
Expand Down Expand Up @@ -36,7 +38,7 @@ function bulkDocs (state, docs) {
}

scope.result = result
.map(toNormalisedError.bind(null, scope))
.map(internals.toNormalisedError.bind(null, scope))
.map(function (result, i) {
if (!result.rev) {
return result
Expand All @@ -57,7 +59,7 @@ function toRev (result) {
return result.rev
}

function toNormalisedError (scope, result, i) {
internals.toNormalisedError = function toNormalisedError (scope, result, i) {
if (result instanceof Error) {
if (result.status === 409) {
var conflict = new Error('Document with id "' + scope.docs[i]._id + '" already exists')
Expand Down
2 changes: 1 addition & 1 deletion tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ require('./integration/update-or-add')
require('./integration/update')
require('./integration/with-id-prefix')

require('./unit/add-many-test')
require('./unit/add-one-test')
require('./unit/bulkdocs-test')
require('./unit/connect-test')
require('./unit/disconnect-test')
6 changes: 6 additions & 0 deletions tests/integration/constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,16 @@ test('Store.defaults with defaults', function (t) {
remote: 'test-db-remote',
foo: 'baz'
}
simple.mock(Store.internals, 'handleChanges').callFn(function () {})

var defaults = Store.defaults(options)

defaults('test-db')

t.is(pouchStub.callCount, 1, 'use default value when no value is provided')

simple.restore()

t.end()
})

Expand All @@ -157,6 +160,7 @@ test('Store.defaults overriden', function (t) {
remote: 'test-db-remote',
foo: 'baz'
}
simple.mock(Store.internals, 'handleChanges').callFn(function () {})

var defaults = Store.defaults(options)

Expand All @@ -165,5 +169,7 @@ test('Store.defaults overriden', function (t) {
t.is(pouchDefaultStub.callCount, 0, 'default value is overridden')
t.is(pouchOverrideStub.callCount, 1, 'overridden value is used')

simple.restore()

t.end()
})
21 changes: 0 additions & 21 deletions tests/unit/add-many-test.js

This file was deleted.

13 changes: 7 additions & 6 deletions tests/unit/add-one-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,21 @@ test('add-one non-409 error', function (t) {
t.plan(1)

var non409Error = new Error('not a 409 error')
var state = {
db: {
put: simple.stub().rejectWith(non409Error)
}
}

simple.mock(addOne.internals, 'put').rejectWith(non409Error)
simple.mock(addOne.internals, 'addTimestamps')

addOne(state, {})
var state = {}
var doc = {}
addOne(state, doc)

.then(function () {
t.fail('should throw an error')
})

.catch(function (error) {
t.is(error, non409Error)

simple.restore()
})
})
14 changes: 14 additions & 0 deletions tests/unit/bulkdocs-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
var test = require('tape').test

var bulkDocs = require('../../lib/helpers/db-bulk-docs')

test('toNormalisedError non-409 error', function (t) {
var scope = {} // scope is not checked in this test, so it can be empty
var non409Error = new Error('not a 409 error')

var error = bulkDocs.internals.toNormalisedError(scope, non409Error, 0)

t.is(error, non409Error, 'a non-409 error is returned untouched')

t.end()
})

0 comments on commit c381cf8

Please sign in to comment.