Geronte
Geront takes a JSON schema and uses Dorante and Pretender to create a client-side-only stub of an API. Currently, it's only tested against the Heroku JSON API schema.
Install
bower install geronte --save
Usage
Create a new instance of Geronte and set up the default request handlers based on the links in your API schema:
var geronte = new Geronte(apiSchema);
geronte.setupRequestHandlers();
Add some custom factories and request handlers:
geronte.dorante.defineFactory('foo', { bar: 'baz' });
geronte.createStub('GET', '/foos', [geronte.dorante.factory('foo')]);
Reset the custom request handlers (but leave custom factories alone):
geronte.reset();
Shut down the Geronte server and return XMLHTTPRequest
to normal:
geronte.shutdown();
Expectations
Geronte can also define request expectations:
geronte.expect('POST', '/foo');
// do stuff
geronte.done();
// throws an error if POST /foo didn't happen
Dynamic segments are supported for paths:
geronte.expect('POST', '/foo/:id');
// do stuff
geronte.done();
// throws an error if POST /foo/anything didn't happen
Expectations can be made against request headers, body and form data:
geronte.expect('POST', '/foo').with({
headers: { Accept: 'application/json' },
data: { foo: 'bar' }
});
geronte.expect('PUT', '/bar').with({
headers: { 'Content-Type': 'application/json' },
body: '{"bar":"baz"}'
});
// do stuff
geronte.done();
// throws an error if POST /foo or PUT /baz didn't happen with the specified
// headers and body
A callback can also be set against an expectation, either instead of or in addition to body and header expectations. This is a good place for setting more fine grained expectations. This callback is only executed once other expectations have been satisfied:
geronte.expect('PUT', '/bar').with(function(jqXHR) {
expect(JSON.parse(jqXHR.requestBody).foo).toEqual('bar');
});
// OR
geronte.expect('PUT', '/bar').with({
headers: { 'Content-Type': 'application/json' },
body: '{"bar":"baz"}'
}, function(jqXHR) {
expect(JSON.parse(jqXHR.requestBody).foo).toEqual('bar');
});