Skip to content

Commit

Permalink
test(koa): cover Koa middleware with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gergelyke authored and Peter Marton committed Jul 27, 2015
1 parent e824db5 commit f0dce2e
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 2 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "",
"main": "index.js",
"scripts": {
"test": "NODE_ENV=test mocha $(find src -name \"*.spec.js\")",
"test": "NODE_ENV=test mocha --require co-mocha $(find src -name \"*.spec.js\")",
"eslint": "eslint src"
},
"repository": {
Expand All @@ -21,6 +21,7 @@
"babel-eslint": "^4.0.0",
"chai": "^3.2.0",
"chai-subset": "^1.0.1",
"co-mocha": "^1.1.2",
"eslint": "1.0.0-rc-3",
"express": "^4.13.1",
"hapi": "^8.8.0",
Expand Down
2 changes: 1 addition & 1 deletion src/koa/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function create(options) {

if (isGet(this) && isPrefixed(this, prefix)) {
this.body = yield graphql(schema, query);
return;
return this.body;
}

yield next;
Expand Down
120 changes: 120 additions & 0 deletions src/koa/index.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
var expect = require('chai').expect;

describe('graffiti koa', function() {

var koa = require('./');

describe('checks for required options', function() {

it('throws an error if not all met', function() {

var mwFactory = koa.create({
graphql: {}
});

try {
mwFactory({
prefix: '/graphql'
});
} catch (ex) {
expect(ex).to.be.ok;
return;
}

throw new Error('Error should have been thrown');

});

});

it('creates the schema', function() {
var mwFactory = koa.create({
graphql: {}
});
var getSchemaSpy = this.sandbox.spy();
var models = [{
name: 'User'
}];

mwFactory({
prefix: '/graphql',
models: models,
adapter: {
getSchema: getSchemaSpy
}
});

expect(getSchemaSpy).to.be.calledWith(models);
});

describe('requested url starts with prefix', function() {

it('returns with proper results', function *() {

var result = {
data: 1
};

var mwFactory = koa.create({
graphql: function() {
return Promise.resolve(result);
}
});

var mw = mwFactory({
prefix: '/graphql',
models: [],
adapter: {
getSchema: function() {}
}
});

var request = {
method: 'GET',
path: '/graphql',
query: {
q: '{__type}'
}
};

var res = yield mw.call(request);

expect(res).to.eql(result);
});

});

describe('requested url does not start with prefix', function() {

it('calls the next middleware', function *(done) {

var mwFactory = koa.create({
graphql: {}
});

var mw = mwFactory({
prefix: '/graphql',
models: [],
adapter: {
getSchema: function() {}
}
});

var request = {
method: 'GET',
path: '/not-good',
query: {
q: '{__type}'
}
};

yield mw.call(request, function *() {
yield done();
});

throw new Error('next should have been called');
});

});

});

0 comments on commit f0dce2e

Please sign in to comment.