Skip to content

Commit

Permalink
test(express): finish up test cases for the Express middleware
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 348aac9 commit b0b3dd0
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 33 deletions.
42 changes: 23 additions & 19 deletions src/express/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
var config = require('../config');
var graphql = require('graphql').graphql;
var checkDep = require('../util').checkDep;

function isGet(request) {
Expand All @@ -10,30 +9,35 @@ function isPrefixed(request, prefix) {
return request.path.indexOf(prefix) === 0;
}

function express(options) {
function create(options) {

var models = checkDep(options, 'models');
var adapter = checkDep(options, 'adapter');
var prefix = checkDep(options, 'prefix');
var graphql = checkDep(options, 'graphql');

var schema = adapter.getSchema(models);
return function(options) {

return function(request, respone, next) {
var models = checkDep(options, 'models');
var adapter = checkDep(options, 'adapter');
var prefix = checkDep(options, 'prefix');

var query = request.query.q;
var schema = adapter.getSchema(models);

if (isGet(request) && isPrefixed(request, prefix)) {
return graphql(schema, query)
.then(function(result) {
respone.json(result);
})
.catch(function(err) {
next(err);
});
}
return function(request, respone, next) {

return next();
var query = request.query.q;

if (isGet(request) && isPrefixed(request, prefix)) {
return graphql(schema, query)
.then(function(result) {
respone.json(result);
})
.catch(function(err) {
next(err);
});
}

return next();
};
};
}

module.exports = express;
module.exports.create = create;
86 changes: 79 additions & 7 deletions src/express/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ describe('graffiti express', function() {

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

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

try {
express({
mwFactory({
prefix: '/graphql'
});
} catch (ex) {
Expand All @@ -23,7 +27,11 @@ describe('graffiti express', function() {

it('doesn\'t throw if all is passed', function() {

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

var mw = mwFactory({
prefix: '/graphql',
models: [],
adapter: {
Expand All @@ -36,13 +44,15 @@ describe('graffiti express', function() {
});

it('creates the schema', function() {

var mwFactory = express.create({
graphql: {}
});
var getSchemaSpy = this.sandbox.spy();
var models = [{
name: 'User'
}];

var mw = express({
mwFactory({
prefix: '/graphql',
models: models,
adapter: {
Expand All @@ -55,15 +65,77 @@ describe('graffiti express', function() {

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

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

var result = {
data: 1
};

var mwFactory = express.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 response = {
json: function(d) {
expect(d).to.eql(result);
}
};

it('returns with an error');
mw(request, response);
});

});

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

it('calls the next middleware');
it('calls the next middleware', function() {

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

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

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

var response = {};

var nextSpy = this.sandbox.spy();

mw(request, response, nextSpy);

expect(nextSpy.called).to.be.ok;
});

});

Expand Down
7 changes: 5 additions & 2 deletions src/hapi/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
function hapi() {
function create() {

return function hapi() {

};
}

module.exports = hapi;
module.exports.create = create;
14 changes: 11 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
var graphql = require('graphql');

var koa = require('./koa');
var hapi = require('./hapi');
var express = require('./express');

module.exports.koa = koa;
module.exports.hapi = hapi;
module.exports.express = express;
module.exports.koa = koa.create({
graphql: graphql.graphql
});
module.exports.hapi = hapi.create({
graphql: graphql.graphql
});
module.exports.express = express.create({
graphql: graphql.graphql
});
7 changes: 5 additions & 2 deletions src/koa/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
function koa() {
function create() {

return function koa() {

};
}

module.exports = koa;
module.exports.create = create;

0 comments on commit b0b3dd0

Please sign in to comment.