Created
July 2, 2014 09:46
-
-
Save sidwood/4122594f55e74690b1cd to your computer and use it in GitHub Desktop.
Hapi plugin test that illustrates plugins containing route definitions with array values for the method will fail when the plugin is registered to more than one server instance.
This file contains hidden or 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
// plugins/products-api/test/products.test.js | |
'use strict'; | |
var Lab = require('lab'); | |
var Hapi = require('hapi'); | |
var productsApi = require('../index'); | |
var dataStore = require('../../store'); | |
var describe = Lab.experiment; | |
var expect = Lab.expect; | |
var it = Lab.test; | |
var server = new Hapi.Server(); | |
describe('Products API plugin registration', function() { | |
it('requires "data-store" plugin', function(done) { | |
var localServer = new Hapi.Server(); | |
expect(function() { | |
localServer.pack.register(productsApi); | |
}).to.throw(/missing dependency data-store/); | |
done(); | |
}); | |
it('successfully loads with "data-store" plugin', function(done) { | |
server.pack.register([dataStore, productsApi], function(err) { | |
expect(err).to.equal(undefined); | |
done(); | |
}); | |
}); | |
it('registers expected routes', function(done) { | |
var table = server.table(); | |
var paths = table.map(function(route) { | |
var obj = { | |
method: route.method, | |
path: route.path | |
}; | |
return obj; | |
}); | |
expect(table).to.have.length(6); | |
expect(paths).to.include({method: 'get', path: '/api/products'}); | |
expect(paths).to.include({method: 'get', path: '/api/products/{id}'}); | |
expect(paths).to.include({method: 'post', path: '/api/products'}); | |
expect(paths).to.include({method: 'put', path: '/api/products/{id}'}); | |
expect(paths).to.include({method: 'patch', path: '/api/products/{id}'}); | |
expect(paths).to.include({method: 'delete', path: '/api/products/{id}'}); | |
done(); | |
}); | |
}); |
This file contains hidden or 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
// plugins/products-api/lib/routes.js | |
'use strict'; | |
var products = require('./products'); | |
module.exports = [ | |
{ | |
method: 'GET', | |
path: '/api/products', | |
config: products.index | |
}, | |
{ | |
method: 'GET', | |
path: '/api/products/{id}', | |
config: products.show | |
}, | |
{ | |
method: 'POST', | |
path: '/api/products', | |
config: products.create | |
}, | |
// Route with method array value breaks test | |
{ | |
method: ['PUT', 'PATCH'], | |
path: '/api/products/{id}', | |
config: products.update | |
}, | |
// Defining each route explicitly works fine | |
// { | |
// method: 'PUT', | |
// path: '/api/products/{id}', | |
// config: products.update | |
// }, | |
// { | |
// method: 'PATCH', | |
// path: '/api/products/{id}', | |
// config: products.update | |
// }, | |
{ | |
method: 'DELETE', | |
path: '/api/products/{id}', | |
config: products.destroy | |
} | |
]; |
This file contains hidden or 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
..x | |
1 of 3 tests failed: | |
3) Plugin registration registers expected routes: | |
AssertionError: expected [ Array(5) ] to have a length of 6 but got 5 | |
AssertionError: expected [ Array(5) ] to include { method: 'put', path: '/api/products/{id}' } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment