Skip to content

Commit

Permalink
[upload-docs] add tests
Browse files Browse the repository at this point in the history
Closes #27
  • Loading branch information
alxndrsn committed Oct 31, 2017
1 parent 8a2b8ed commit 69f88f3
Show file tree
Hide file tree
Showing 8 changed files with 3,341 additions and 55 deletions.
3,286 changes: 3,234 additions & 52 deletions package-lock.json

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "index.js",
"scripts": {
"clean": "rm -rf ./build/",
"jshint": "jshint src/**/*.js bin/*.js test/**/*.js",
"jshint": "jshint src/**/*.js bin/*.js test/*.js test/**/*.js",
"test": "npm run jshint && npm run clean && mocha test/**/*.spec.js"
},
"bin": {
Expand Down Expand Up @@ -37,9 +37,12 @@
},
"devDependencies": {
"chai": "^4.0.0",
"express-pouchdb": "^2.3.7",
"jshint": "^2.9.5",
"lodash": "^4.17.4",
"memdown": "^1.4.1",
"mocha": "^3.4.2",
"ncp": "^2.0.0"
"ncp": "^2.0.0",
"pouchdb": "^6.3.4"
}
}
9 changes: 8 additions & 1 deletion test/.jshintrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
{
"extends": "../.jshintrc",
"predef": [ "before", "beforeEach", "describe", "it" ]
"predef": [
"after",
"afterEach",
"before",
"beforeEach",
"describe",
"it"
]
}
39 changes: 39 additions & 0 deletions test/api-stub.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const memdown = require('memdown');
const memPouch = require('pouchdb').defaults({ db:memdown });
const express = require('express');
const expressPouch = require('express-pouchdb');

const PORT = 5988;

const opts = {
inMemoryConfig: true,
logPath: 'build/test/express-pouchdb.log',
mode: 'fullCouchDB',
};
const app = express();
app.use('/', stripAuth, expressPouch(memPouch, opts));

let server;

module.exports = {
couchUrl: `http://admin:pass@localhost:${PORT}/medic`,
db: new memPouch('medic'),
start: () => {
if(server) throw new Error('Server already started.');
server = app.listen(PORT);
},
stop: () => {
server.close();
server = null;
},
};

/**
* Strip basic auth header because right now
* 1. we don't need to test it; and
* 2. I don't know how to configure it in pouchdb-server/pouchdb-express
*/
function stripAuth(req, res, next) {
delete req.headers.authorization;
next();
}
3 changes: 3 additions & 0 deletions test/data/upload-docs/json_docs/one.doc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"_id": "one"
}
3 changes: 3 additions & 0 deletions test/data/upload-docs/json_docs/three.doc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"_id": "three"
}
3 changes: 3 additions & 0 deletions test/data/upload-docs/json_docs/two.doc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"_id": "two"
}
46 changes: 46 additions & 0 deletions test/fn/upload-docs.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const api = require('../api-stub');
const assert = require('chai').assert;
const ncp = require('ncp');
const uploadDocs = require('../../src/fn/upload-docs');

describe('upload-docs', function() {
beforeEach(api.start);
afterEach(api.stop);

it('should upload docs to pouch', function(done) {

// given
const srcDir = `test/data/upload-docs`;
const testDir = `build/test/upload-docs`;

ncp(srcDir, testDir, err => {
if(err) done(err);

// and
// TODO there are no docs in the db

// when
uploadDocs(testDir, api.couchUrl)

.then(() => {

api.db.allDocs()
.then(res => {

// then
assert.deepEqual(res.rows.map(doc => doc.id), [
'one', 'three', 'two'
]);

done();

})
.catch(done);

});

});

});

});

0 comments on commit 69f88f3

Please sign in to comment.