-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(koa): cover Koa middleware with tests
- Loading branch information
Showing
3 changed files
with
123 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
|
||
}); | ||
|
||
}); |